NetTopologySuite.IO.SqlServerBytes
NetTopologySuite.IO.SqlServerBytes copied to clipboard
SqlServerBytesWriter complains about polygon is not CCW while it's valid in SQL Server
I have the following code trying to query geography in database:
var factory = NtsGeometryServices.Instance.CreateGeometryFactory(4326);
var reader = new WKTReader(factory);
var polygon = reader.Read("POLYGON(("+
"-45.70072144031528 70.79588950876575,"+
"-45.70072144031528 -32.671894242015554,"+
"-157.3218151903153 -32.671894242015554,"+
"-157.3218151903153 70.79588950876575,"+
"-45.70072144031528 70.79588950876575))");
var result = dbContext.Set<Test>()
.Where(x => x.Point.Intersects(polygon))
.ToArray();
If I run the above code, SqlServerBytesWriter throws an ArgumentException complaining that the polygon is not CCW. But if I create the geography in SQL query directly, it works perfectly:
DECLARE @g geography = 'POLYGON((-45.70072144031528 70.79588950876575,-45.70072144031528 -32.671894242015554, -157.3218151903153 -32.671894242015554, -157.3218151903153 70.79588950876575,-45.70072144031528 70.79588950876575))'
SELECT * FROM Test WHERE Point.STIntersects(@g)=1
So I'm guessing SqlServerBytesWriter has no idea about the globe and performs stricter check than SQL Server itself? It would be great if there is there's anyway I can bypass CCW check in SqlServerBytesWriter.
Correct. NTS cannot represent polygons based on the full globe. SQL Server interprets the specified ring as a hole/interior ring and uses FULLGLOBE as the shell/exterior "ring".