- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有个问题希望大家帮我解决
我有一个 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/
谁能告诉我如何从“POINT”类型的 DbGeography 对象集合中创建“Polygon”类型的 DbGeography 对象 到目前为止,我已经有了创建多边形的这个,但我错过了初始步骤。 1.
我像这样创建一个 DbGeography 点: String selectedLocation = String.Format("POINT ({0} {1})", lon, lat).Replace
我有两点描述一个矩形: 东北纬度/经度和西南纬度/经度。 生成可存储在 DbGeography 字段中的简单矩形实体的正确有效方法是什么? 请注意,稍后我想使用该字段来确定点是否在该矩形内。 我意识到
System.Data.Spatial.DbGeography.Intersects 方法对我来说似乎总是返回 true。我不确定为什么会这样。我在下面创建了一个简单的命令行片段,它产生了下面的控制台
我有一个多边形作为 Sys.Geography 类型保存在 SQL Server 2012 数据库上。如何获得多边形的所有点? 我正在考虑使用 AsText() 方法并解析字符串,但也许有更好的选择?
当我使用 Distance 方法时,它要求 2 个位置,并返回一个 double 值。 一个 double 值,指定 geographyValue 和 otherGeography (MSDN) 中最
我需要使用 Bing Map 服务、EF 5 和 SQL Server 2008 对大量地址进行地理编码。我在 SQL 中使用 geography 数据类型,它由 EF 转换为 DbGeography
我正在使用空间数据开始我的第一个项目。我正在使用 VS 2012 和 SQL 2012 我已经引用了 System.Data.Entity 并且可以在我的代码中使用 DbGeography 但是当我尝
我正在寻找组合多个多边形以减少点数的方法。这是前进的方向吗: var pol1 = DbGeography.PolygonFromText("POLYGON ((-2.91790532309701 5
我在 SQL 中有一个地理字段,我将其标记为空。 然后在我的 EF 实体上添加了一个可为 null 的 DbGeography? 属性,但我收到一条消息说这是不允许的。 那么,如何在数据库中插入一个未
我有一个代表起点的 DbGeography,我知道行进的距离和方向。鉴于这些信息,有没有办法使用 SqlSpatial 库获得代表新位置的新 DbGeography?即无需自己进行大圆计算或使用第 3
我将 DbGeography 多边形存储在数据库中。我的 Controller 从数据库中获取多边形,我需要将它们转换为 JSON。 var polygons = db.Areas .
长期听众,第一次来电(终于在这里注册了一个帐户!)... 我将 Visual Studio 2013 与 .NET 4.5.1 和 Entity Framework 6(最终版本,非 RC 或测试版)
我正在查询 SQL Server 中的表以在 Google map 上显示结果,并且我在服务器应用程序上使用 Entity Framework 和 DbGeography 数据类型。 简单地说,我从
如果我将 WKT 格式的多边形定义为这样(经度/纬度): string wkt = "POLYGON ((15.981800258159638 45.810693408924287, 15.98362
我正在尝试公开一个 API,允许用户发布多边形以保留在服务器上。我正在使用 ASP.NET MVC 5。如何正确格式化 AJAX 参数以发布对 DbGeography 的请求?这就是我正在尝试的: $
我已经从官方来源下载了一张带有瑞典地区的 map 作为 Shape 文件。然后我使用了 QGIS将数据转换为 geojson,然后将 geojson 转换为 DbGeography,如下所示: htt
我正在尝试从数据库中获取 Spartial 数据,然后将其序列化(效果很好)。当我尝试反序列化该数据时,会抛出 JsonSerializationException。 DbGeography geoP
我有两个 GPS 坐标,我想计算它们之间的距离,但 SQL 服务器上的结果与 c# 中的结果完全不同我用谷歌搜索发现这两种方法都以米为单位返回距离,但这种差异让我发疯。 SQL SERVER 查询 s
Google告诉我日本的经/纬度值为(36,138),但.NET抛出错误 24201: Latitude values must be between -90 and 90 degrees. 有什么想
我是一名优秀的程序员,十分优秀!