简体   繁体   English

EF5几何相交查询不返回任何内容

[英]EF5 geometry intersect query returns nothing

I have a SQL Server 2008 R2 table which has a geometry (not geography) column, as well as some other "standard" columns. 我有一个SQL Server 2008 R2表,该表具有一个geometry (而非地理)列以及一些其他“标准”列。 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): 我目前在表中只有一行用于测试,并且该记录中的geometry列具有一个具有以下边界的多边形,并且当我插入该多边形时的SRID为0(零):

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. 现在,我想使用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. 在添加SqlSpatialFunctions.MakeValid方法之前,我遇到的第一个错误是s.AreaBounds结果不是“有效的”,而在代码中创建的点是。 Fixed that one with the help of this excellent post -> query-dbgeometry-for-specific-latlng-value 借助此出色的帖子-> 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. 现在,我可能会发疯(我一直在看这段代码一段时间),但是我总是返回一个空列表(计数= 0),并且我认为该点在多边形范围内。

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: 我不知道为什么要这样做,但我当然希望将来在将Entity Framework与几何SQL列一起使用时对其他人有所帮助:

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. 我经过一些深入的调试,然后通过Linq查询返回的DBGeometry字段,发现了这一点。 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. 我发现s.AreaBounds字段返回LINESTRING(...)值,而不是最初保存在数据库中的POLYGON(...)值。 Looking further through the returned object properties I found that .Envelope had what I wanted. 进一步查看返回的对象属性,我发现.Envelope具有我想要的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM