问题
In SQL Server 2008 I have:
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)
And as expected I get results 1
and 0
.
Now I am trying to bring the same code to C# using the library: Microsoft.SqlServer.Types.dll
Using the following code:
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));
I always get False
, False
..... and it should be True
, False
Could someone help in here?
Thank you
回答1:
SqlGeography.Point
and SqlGeography.STPointFromText
use different coordinate order. Try one of the following solutions to fix the issue:
- Swap coordinates in
SqlGeography.Point
method call; - Create points using WKT syntax (
SqlGeography.STPointFromText
).
来源:https://stackoverflow.com/questions/9448054/geo-functions-from-sql-server-2008-to-c-sharp-latitude-and-longitude-points-ins