efcore: NetTopologySuite SqlServer provider incorrectly translates Within query
I have this query:
var points = new[]
{
new Coordinate(neLng.Value, neLat.Value), // TR
new Coordinate(neLng.Value, swLat.Value), // BR
new Coordinate(swLng.Value, swLat.Value), // BL
new Coordinate(swLng.Value, neLat.Value), // TL
new Coordinate(neLng.Value, neLat.Value), // TR
};
var fact = NtsGeometryServices.Instance.CreateGeometryFactory(srid: 4326);
var poly = fact.CreatePolygon(points).Normalized().Reverse();
// This line is only added to demonstrate that local evaluation works correctly
var list = query.ToList().Where(x => x.Location != null && x.Location.Within(poly)).ToList();
// This does not filter to the correct results; it excludes values that should be included, and includes values that should be excluded
query = query.Where(x => x.Location.Within(poly));
The server-evaluated query is incorrect - it omits points within the polygon poly
, and includes ones that are outside it. The locally-evaluated version (“bug demo”) does return the expected results. They should be returning the same results since they are doing the same thing. query
is IQueryable
.
The values received for the bounds will be like this:
neLat 60.11131083608817
neLng -74.22879847513246
swLat 47.2231665957533
swLng -157.85672816263252
Further technical details
EF Core version: 2.2.3 Database Provider: Microsoft.EntityFrameworkCore.SqlServer
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 36 (9 by maintainers)
@bricelam That screenshot was just a demonstration. This is the one that was actually in use:
And in the code, the points are defined in counter-clockwise order:
I think this might be part of the issue:
Turns out SSMS has a Spatial Results visualizer:
That little black dot is the
@pt
So the point would be within the polygon if it had straight lines, but they connected by curves. Perhaps Reverse would just flip the polygon on the X plane - so it might work for the bottom points but wouldn’t work for the top ones…
Do you know if there is some alternative way to give that polygon to EF Core that would change how it interprets the coordinates?
Correct, it’s the SQL Server serialization format. You can turn it into WKT like this:
Hello, I am one of colleague whose the reporter #15668:
Some experience of mine would help :
entity.Property(e => e.Location).HasColumnType("geometry");