Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate a SqlGeography polygon-circle from a center and radius
    text
    copied!<p>I would like to save a circle in a sql-server 2008 geography field, using c#.</p> <p>In c# I have a latitude, a longitude and a radius but I just can't find a way to calculate the polygon that would represent the circle and create a <code>SqlGeography</code> from it.</p> <p>I have tried to following function to create the polygon:</p> <pre><code> private List&lt;Coordinate&gt; getCirclePoints(Coordinate center, int radius, int speed) //speed 1: draws 360 sides, 2 draws 180 etc... { var centerLat = (center.Latitude * Math.PI) / 180.0; //rad var centerLng = (center.Longitude * Math.PI) / 180.0; //rad var dist = (float)radius / 6371.0; //d = angular distance covered on earth's surface var circlePoints = new List&lt;Coordinate&gt;(); for (int x = 0; x &lt;= 360; x += speed) { var brng = x * Math.PI / 180.0; //rad var latitude = Math.Asin(Math.Sin(centerLat) * Math.Cos(dist) + Math.Cos(centerLat) * Math.Sin(dist) * Math.Cos(brng)); var longitude = ((centerLng + Math.Atan2(Math.Sin(brng) * Math.Sin(dist) * Math.Cos(centerLat), Math.Cos(dist) - Math.Sin(centerLat) * Math.Sin(latitude))) * 180.0) / Math.PI; circlePoints.Add(new Coordinate((latitude * 180.0) / Math.PI, longitude)); } return circlePoints; } </code></pre> <p>And then try to convert this <code>List&lt;Coordinate&gt;</code> to a parsable string:</p> <pre><code> var s = "POLYGON((" + string.Join(",", points.ConvertAll(p =&gt; p.Longitude + " " + p.Latitude).ToArray()) + "))"; var poly = SqlGeography.STPolyFromText(new System.Data.SqlTypes.SqlChars((SqlString)s), 4326); </code></pre> <p>But it always complains the polygon has to be on a single hemisphere, where I'm sure it is the case.</p> <p>Am I on the right track at all? Is there any other (simpler) way to do this?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload