NetTopologySuite.IO.SqlServerBytes icon indicating copy to clipboard operation
NetTopologySuite.IO.SqlServerBytes copied to clipboard

SqlServerBytesWriter complains about polygon is not CCW while it's valid in SQL Server

Open noelex opened this issue 4 years ago • 1 comments

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.

noelex avatar Jan 15 '21 16:01 noelex

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".

bricelam avatar May 03 '21 21:05 bricelam