简体   繁体   English

如何从多边形边缘开始找到多边形外部点的最短距离?

[英]How to find the shortest distance of a point outside of a polygon starting at the polygon's edge?

Consider the following: 考虑以下:

  • Point A DbGeography (office address) ADbGeography (办公地址)
  • Point B DbGeography (customer's address outside of office's service area) BDbGeography (客户在办公室服务区以外的地址)
  • Polygon C DbGeography (office's service area) Polygon C DbGeography (办公室服务区)

Using the above points and polygon, how can I find the closest distance of B to C 's edge? 使用上面的点和多边形,我怎样才能找到BC边缘的最近距离? I assume that first I need to find the line between A and B , then find where the line intersects C (= D ) and then calculate the distance from D to B ? 我假设首先我需要找到AB之间的线,然后找到线与C (= D )相交的位置然后计算从DB的距离?

Since my usage of SQL Server's spatial capabilities is limited and I'm using Entity Framework, I'm not sure how to express that in code. 由于我使用SQL Server的空间功能是有限的,我正在使用实体框架,我不知道如何在代码中表达它。 I also assume that I'd have to use SqlGeography for this since DbGeography is kind of limited. 我还假设我必须使用SqlGeography ,因为DbGeography是有限的。 I'd probably end up writing an extension to DbGeography . 我可能最终写了DbGeography的扩展。

I'd appreciate any suggestions (would love code examples) of how I can accomplish the above task. 我很感激有关如何完成上述任务的任何建议(会喜欢代码示例)。

So, after messing around with this for the past three hours, I found a solution. 所以,在过去三个小时搞砸了这个之后,我找到了解决方案。 Here's the code for anyone who cares: 以下是关心任何人的代码:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM