Closed Dona278 closed 2 years ago
Duplicate of #14595
@Dona278 You should be able to get this to work using the same workaround as shown for #14595. Specifically, call Intersects
on the mapped property rather than on the parameter:
var events = await _context.Events
.Where(event => event.Coordinate.Intersects(polygon))
.ToListAsync(cancellationToken);
Please comment here let us know if this doesn't work.
Thank you @ajcvickers for the reply and yes, the latest issue is a duplicated of #14595 and I've already tried to change the query in the suggested format but I have no results, and the coordinates are correct. I think that it's related to the latest attempt that I tried: switch the latitude with longitude.
Using the correct order of longitude and latitude during creation of the polygon I cannot go ahead to the query because of the exception mentioned above: System.ArgumentException: When writing a SQL Server geography value, the shell of a polygon must be oriented counter-clockwise. To write polygons without a shell, set SkipGeographyChecks
.
But here I saw that the parameter mentioned into the exception was removed and the suggested workaround
polygon.Normalize();
polygon.Reverse();
doesn't work... I'm wrong? What can I do?
Ok, I used the query as @ajcvickers has mentioned and I tried the easiest attempt ever: reverse the coordinates received from client request before the creation of the polygon 🤦♂️. Client application sends coordinates in this order: Top-Left -> Top-Right -> Bottom-Right -> Bottom-Left -> Top-Left and before, when I had the query written as a Sql query instead of with EF, I didn't have any problems. The two lines of code
polygon.Normalize();
polygon.Reverse();
are not served
Re-opening to check on Normalize
and Reverse
.
With regard to Normalize
and Reverse
:
Reverse
to be useful here it would need to be called on the rings inside the polygon object.Normalize
is not relevant here--it's only really used for some cases in NTS, but not to "normalize for the database in use" or anything like that.
I try to explain my issue with a dummy models because i can't copy-paste the structure of the project.
I've used
event.Coordinate.Distance(pointCoordinate)
method in some query without any problems (thanks to the work between EFCore and NTS) to perform a search around a requested point, but today I've got a problem with the query withpolygon.Intersects(event.Coordinate)
.I created a closed polygon in this way:
Exception thrown:
System.ArgumentException: When writing a SQL Server geography value, the shell of a polygon must be oriented counter-clockwise. To write polygons without a shell, set SkipGeographyChecks
After some tests with zone coordinates I tried to switch latitude with longitude and I see that the
.IsCCW
property on polygon object (that will be used as a check before throwing the exception described above) istrue
so no exception due to the shell of polygon but the generated query is invalid:which throw another exception:
System.Data.SqlClient.SqlException: Cannot call methods on varbinary
It seems that the creation of a geography polygon based on varbinary before calling
.STIntersects()
is lacking. Am I wrong? I'm using the coordinates to create polygon in a wrong way?