Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
noelex opened this issue Jan 15, 2021 · 1 comment

Comments

@noelex
Copy link

noelex commented Jan 15, 2021

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.

@bricelam
Copy link
Collaborator

bricelam commented May 3, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants