创建从圆心和半径一SqlGeography多边形圆 [英] Create a SqlGeography polygon-circle from a center and radius

查看:475
本文介绍了创建从圆心和半径一SqlGeography多边形圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想保存在SQL服务器2008年的地理场转了一圈,使用C#。



在C#中我有一个纬度,经度和半径,但我无法找到一个方法来计算将代表圆和 SqlGeography 创建从它的多边形。



我曾尝试下面的函数来创建多边形:

 私人列表<协调> getCirclePoints(坐标中心,半径为INT,INT速度)//速1:360绘制双方2平180等... 
{
VAR centerLat =(center.Latitude * Math.PI)/ 180.0 ; //弧度
VAR centerLng =(center.Longitude * Math.PI)/ 180.0; //弧度
VAR DIST =(浮点)半径/ 6371.0; // D =角距离覆盖在地球表面
变种circlePoints =新的List<协调>();
的for(int x = 0; X< = 360; X + =速度)
{
VAR方位角= X * Math.PI / 180.0; //弧度
VAR纬度= Math.Asin(Math.Sin(centerLat)* Math.Cos(DIST)+ Math.Cos(centerLat)* Math.Sin(DIST)* Math.Cos(方位角));
VAR经度=((centerLng + Math.Atan2(Math.Sin(方位角)* Math.Sin(DIST)* Math.Cos(centerLat),Math.Cos(DIST) - Math.Sin(centerLat)* Math.Sin(纬度)))* 180.0)/ Math.PI;
circlePoints.Add(新坐标((纬度* 180.0)/ Math.PI,经度));
}
返回circlePoints;
}



,然后尝试转换该列表<协调> 来可解析字符串:

 变种s =POLYGON((+的string.join( ,points.ConvertAll(p值=> p.Longitude ++ p.Latitude).ToArray())+)); 
无功聚= SqlGeography.STPolyFromText(新System.Data.SqlTypes.SqlChars((的SqlString)S),4326);



但它总是抱怨多边形必须是在一个半球,在那里我敢肯定,这是这样的。



我在正确的轨道可言吗?是否有任何其他(简单)的方式来做到这一点?


解决方案

OK,发现我自己的答案。关键是要营造一个点

  VAR点= SqlGeography.Point(纬度,经度,4326); 



然后创建围绕该点



<缓冲区pre> VAR聚= point.BufferWithTolerance(radiusInMeter,0.01,真正的); //0.01是简化多边形只保留了几个侧面



然后,你可以简单地创建一个的SqlCommand 并添加多边形参数:

  VAR参数=新的SqlParameter( @多边形,多); 
param.UdtTypeName =地理;
command.Add(参数);



希望这将帮助别人的未来都重要!


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 the polygon that would represent the circle and create a SqlGeography from it.

I have tried to following function to create the polygon:

    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;
    }

And then try to convert this List<Coordinate> to a parsable string:

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

But it always complains the polygon has to be on a single hemisphere, where I'm sure it is the case.

Am I on the right track at all? Is there any other (simpler) way to do this?

解决方案

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.Add(param);

Hope that will help someone else in the future!

这篇关于创建从圆心和半径一SqlGeography多边形圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆