[英]How to find the shortest distance of a point outside of a polygon starting at the polygon's edge?
考虑以下:
DbGeography
(办公地址) DbGeography
(客户在办公室服务区以外的地址) DbGeography
(办公室服务区) 使用上面的点和多边形,我怎样才能找到B到C边缘的最近距离? 我假设首先我需要找到A和B之间的线,然后找到线与C (= D )相交的位置然后计算从D到B的距离?
由于我使用SQL Server的空间功能是有限的,我正在使用实体框架,我不知道如何在代码中表达它。 我还假设我必须使用SqlGeography
,因为DbGeography
是有限的。 我可能最终写了DbGeography
的扩展。
我很感激有关如何完成上述任务的任何建议(会喜欢代码示例)。
所以,在过去三个小时搞砸了这个之后,我找到了解决方案。 以下是关心任何人的代码:
public static class DbGeographyExtensions {
/// <summary>
/// Returns a double? value containing the shortest distance from the edge of this polygon to the specified point
/// </summary>
public static double? ToShortestDistanceFromPoint(
this DbGeography polygon,
DbGeography point) {
if ((polygon != null)
&& (point != null)) {
/// Convert the DbGeography to SqlGeography
SqlGeography sqlPolygon = SqlGeography.STPolyFromText(new SqlChars(polygon.AsText()), polygon.CoordinateSystemId);
SqlGeography sqlPoint = SqlGeography.STPointFromText(new SqlChars(point.AsText()), point.CoordinateSystemId);
/// Get the shortest line from the edge of the polygon to the point
SqlGeography shortestPoint = sqlPoint.ShortestLineTo(sqlPolygon);
/// Return the length of the line (distance returns 0 because it excludes the area of the line)
return (double?)shortestPoint.STLength();
}
return null;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.