0

考虑以下:

  • ADbGeography(办公地址)
  • BDbGeography(客户地址在办公室服务区域外)
  • Polygon C DbGeography(办公室服务区)

使用上述点和多边形,我怎样才能找到BC边缘的最近距离?我假设首先我需要找到AB之间的线,然后找到线与C(= D)相交的位置,然后计算从DB的距离?

由于我对 SQL Server 空间功能的使用是有限的,而且我使用的是实体框架,所以我不确定如何在代码中表达它。我还假设我必须使用SqlGeography它,因为DbGeography它有点有限。我可能最终会为DbGeography.

对于如何完成上述任务的任何建议(希望代码示例),我将不胜感激。

4

1 回答 1

2

所以,在过去三个小时搞砸了这个之后,我找到了一个解决方案。这是任何关心的人的代码:

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;
    }
}
于 2014-05-18T19:34:36.567 回答