[英]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.