I have a SQL Server 2008 R2 table which has a geometry
(not geography) column, as well as some other "standard" columns. I currently have a single row in the table for testing, and the geometry column in that record has a polygon with the following bounds, and the SRID when I inserted the polygon was 0 (zero):
POLYGON ((380 220, 380 575, 585 575, 380 575, 380 220))
I now want to check to see if a point is in that polygon, using EF5. First I create the point:
DbGeometry testPoint = DbGeometry.PointFromText("POINT(400 240)", 0);
List<LocationArea> tResults = (from s in db.LocationAreas
where testPoint .Intersects(SqlSpatialFunctions.MakeValid(s.AreaBounds))
select s).ToList();
The first error I got, before adding the SqlSpatialFunctions.MakeValid
method was that the s.AreaBounds
result was not "valid", whilst the point created in code was. Fixed that one with the help of this excellent post -> query-dbgeometry-for-specific-latlng-value
Now, I could be going crazy (I've been looking at this code for a while), however I always get an empty list returned (count = 0), and I believe the point is within the polygon bounds.
So, any pointers would be appreciated, like I said though, could just be me :-)
Dominik
After trying many, many different things to get the result I wanted, the code below now finally works, and works every time, with every polygon / point scenario I have thrown at it so far.
Why this is required I do not know, but I certainly hope this helps someone else in future when using geometry SQL columns with Entity Framework:
DbGeometry testPoint = DbGeometry.PointFromText("POINT(400 240)", 0);
List<LocationArea> tResults = (from s in db.LocationAreas
where testPoint .Intersects(SqlSpatialFunctions.MakeValid(s.AreaBounds).Envelope)
select s).ToList();
I found this after some in-depth debugging and really going through the DBGeometry field returned via the Linq query. I found that the s.AreaBounds field returned a LINESTRING(...) value instead of what was saved in the DB originally, which was a POLYGON(...) value. Looking further through the returned object properties I found that .Envelope had what I wanted.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.