我有个问题希望大家帮我解决
我有一个 DbGeometry 点(或 DbGeography,我可以同时使用两者),我想检查它是否在 DbGeometry 多边形(或者 DbGeography)内。
我现在正在这样做:
var dbZones = new List<WasteManager.Database.Zone>();
foreach(var zone in zones)
{
var res = from z in DatabaseContext.Zones
let boundary =
!z.BoundaryGeometry.IsValid
? SqlSpatialFunctions.MakeValid(z.BoundaryGeometry)
: z.BoundaryGeometry
where z.ID == zone.ID && point.Within(boundary)
select z;
if(res.FirstOrDefault() != null) dbZones.Add(res.FirstOrDefault());
}
所以我遍历区域(我的数据库的 EF 实体)并检查我的这个点是否在这个边界内。
问题是它没有返回任何结果,但我知道那个点在那个边界内,因为我手动创建了边界和那个边界内的点。
任何人都可以告诉我我的做法是否错误,是否有其他方法可以做到这一点或其他任何方法?
非常感谢。
曼纽尔
最佳答案
我想对 Nick Strupat 添加评论。
你应该小心戒指的方向。 SQL Server 使用左手方向,这意味着如果您沿着多边形的周边行走,您的左手应该在多边形的内侧,右手在外侧(逆时针或逆时针)。我得到了“环方向”错误,因为我在相反的方向(顺时针或右手)绘制了我的多边形,这意味着 SQL Server 正在将地球的整个表面作为多边形的区域来处理,除了我的多边形。
要检查点是否在多边形中,您应该始终使用 point.Intersects(polygon)
而不是 !point.Intersects(polygon)
。
有一个解决方案可以通过检查区域的大小来检查多边形是否正常, 如需更多信息,请访问:
https://blog.falafel.com/ring-orientation-sql-spatial/
这是我根据博客解释编写的代码:
private bool isInside(DbGeography polygon, double longitude, double latitude)
{
DbGeography point = DbGeography.FromText(string.Format("POINT({1} {0})", latitude.ToString().Replace(',', '.'), longitude.ToString().Replace(',','.')), DbGeography.DefaultCoordinateSystemId);
// If the polygon area is larger than an earth hemisphere (510 Trillion m2 / 2), we know it needs to be fixed
if (polygon.Area.HasValue && polygon.Area.Value > 255000000000000L)
{
// Convert our DbGeography polygon into a SqlGeography object for the ReorientObject() call
SqlGeography sqlPolygon = SqlGeography.STGeomFromWKB(new System.Data.SqlTypes.SqlBytes(polygon.AsBinary()), DbGeography.DefaultCoordinateSystemId);
// ReorientObject will flip the polygon so the outside becomes the inside
sqlPolygon = sqlPolygon.ReorientObject();
// Convert the SqlGeography object back into DbGeography
polygon = DbGeography.FromBinary(sqlPolygon.STAsBinary().Value);
}
return point.Intersects(polygon);
}
关于c# - 检查 dbgeometry dbgeometry/dbgeography 点是否在多边形内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13960878/