Créer un polygone-cercle SqlGeography à partir d'un centre et d'un rayon

Je voudrais save un cercle dans un champ de géographie sql-server 2008, en utilisant c #.

Dans c # j'ai une latitude, une longitude et un rayon mais je ne trouve pas de moyen de calculer le polygone qui représenterait le cercle et créer un SqlGeography partir de celui-ci.

J'ai essayé de suivre la fonction pour créer le polygone:

  private List<Coordinate> 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<Coordinate>(); for (int x = 0; x <= 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; } 

Et puis essayez de convertir cette List<Coordinate> en une string analysable:

  var s = "POLYGON((" + ssortingng.Join(",", points.ConvertAll(p => p.Longitude + " " + p.Latitude).ToArray()) + "))"; var poly = SqlGeography.STPolyFromText(new System.Data.SqlTypes.SqlChars((SqlSsortingng)s), 4326); 

Mais il se plaint toujours que le polygone doit être sur un seul hémisphère, où je suis sûr que c'est le cas.

Suis-je sur la bonne voie? Y a-t-il un autre moyen (plus simple) de faire cela?

OK, trouvé la réponse par moi-même. L'astuce consiste à créer un point

 var point = SqlGeography.Point(latitude, longitude, 4326); 

Ensuite, créez un tampon autour du point

 var poly = point.BufferWithTolerance(radiusInMeter, 0.01, true); //0.01 is to simplify the polygon to keep only a few sides 

Ensuite, vous pouvez simplement créer un SqlCommand et append le polygone en tant que paramètre:

 var param = new SqlParameter(@"Polygon", poly); param.UdtTypeName = "Geography"; command.Add(param); 

J'espère que cela aidera quelqu'un d'autre dans le futur!