SQL Server 2008到C#的地理函数(多边形/地图区域内外的纬度和经度点)

本文关键字:区域 地图 纬度 经度 多边形 2008 Server 函数 SQL | 更新日期: 2023-09-27 18:25:44

在SQL Server 2008中,我有:

Declare @pointIn geometry
Declare @pointOut geometry
Declare @polygon geometry
SET @polygon = geometry::STGeomFromText('POLYGON((40 -9,40 -6,35 -6,35 -9,40 -9))', 4326)
SET @pointIn =  geometry::STGeomFromText('POINT (39 -8)', 4326)
SET @pointOut =  geometry::STGeomFromText('POINT (41 -3)', 4326)
select @polygon.STIntersects(@pointIn)
select @polygon.STIntersects(@pointOut)

正如预期的那样,我得到了结果10

现在,我正试图使用库Microsoft.SqlServer.Types.dll 将相同的代码带到C#中

使用以下代码:

var p1lat = 40;
var p1long = -9;
var p2lat = 40;
var p2ong = -6;
var p3lat = 35;
var p3ong = -6;
var p4lat = 35;
var p4ong = -9;
var pILat = 39;
var pILong = -8;
var pOLat = 41;
var pOLong = -3;
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
const string polygonFormat = "POLYGON(({0} {1},{2} {3},{4} {5},{6} {7},{0} {1}))";
var polygon = string.Format(polygonFormat, p1lat, p1long, p2lat, p2ong, p3lat, p3ong, p4lat, p4ong);
var geometryString = new SqlChars(new SqlString(polygon));
var area = SqlGeography.STPolyFromText(geometryString, 4326);
var pointIn = SqlGeography.Point(pILat, pILong, 4326);
var pointOut = SqlGeography.Point(pOLat, pOLong, 4326);
Console.WriteLine(area.STIntersects(pointIn));
Console.WriteLine(area.STIntersects(pointOut));

我总是得到FalseFalse。。。。。应该是TrueFalse

有人能在这里帮忙吗?

感谢

SQL Server 2008到C#的地理函数(多边形/地图区域内外的纬度和经度点)

SqlGeography.PointSqlGeography.STPointFromText使用不同的坐标顺序。尝试以下解决方案之一来解决此问题:

  • SqlGeography.Point方法调用中交换坐标
  • 使用WKT语法(SqlGeography.STPointFromText)创建点