【问题标题】:How to find the shortest distance of a point outside of a polygon starting at the polygon's edge?如何找到从多边形边缘开始的多边形外部点的最短距离?
【发布时间】:2014-05-18 16:25:42
【问题描述】:

考虑以下几点:

  • A DbGeography(办公地址)
  • Point B DbGeography(客户地址在办公室服务区域之外)
  • 多边形 C DbGeography(办公室服务区)

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

由于我对 SQL Server 空间功能的使用受到限制,并且我使用的是实体框架,因此我不确定如何在代码中表达这一点。我还假设我必须为此使用SqlGeography,因为DbGeography 有点有限。我可能最终会为DbGeography 写一个扩展。

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

【问题讨论】:

    标签: sql-server entity-framework sql-server-2012 sqlgeography


    【解决方案1】:

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

    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;
        }
    }
    

    【讨论】:

    • ShortestLineTo 方法需要 SQL Server 2012+
    猜你喜欢
    • 2020-05-10
    • 2010-10-28
    • 2021-12-06
    • 2016-01-23
    • 1970-01-01
    • 2012-06-14
    • 1970-01-01
    • 2010-10-03
    • 2016-08-26
    相关资源
    最近更新 更多