Create a SqlGeography polygon-circle from a center and radius

后端 未结 1 1562
感动是毒
感动是毒 2021-02-08 10:41

I would like to save a circle in a sql-server 2008 geography field, using c#.

In c# I have a latitude, a longitude and a radius but I just can\'t find a way to calculate

相关标签:
1条回答
  • 2021-02-08 10:57

    OK, found the answer on my own. The trick is to create a point

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

    Then create a buffer around the point

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

    Then you could simply create a SqlCommand and add the polygon as parameter:

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

    Hope that will help someone else in the future!

    0 讨论(0)
提交回复
热议问题