【问题标题】:Checking if a Point is inside a Polygon not working检查点是否在多边形内不起作用
【发布时间】:2014-05-18 01:07:58
【问题描述】:

使用 SQL Server 2012,以下查询不断告诉我,我正在寻找的点不在我正在使用的多边形内,但我知道它在。在 QGIS 2.2.0 中使用相同的数据,我清楚地看到该点位于多边形内部(屏幕截图)。

DECLARE @polygon GEOGRAPHY = GEOGRAPHY::STPolyFromText('POLYGON ((-111.0498046875 33.966142265597391, -110.9124755859375 33.472690192666633, -110.94268798828125 32.983324091837417, -111.7364501953125 32.680996432581921, -112.587890625 32.731840896865656, -113.0657958984375 33.307577130152978, -112.9010009765625 33.811102288647007, -112.32147216796875 34.1890858311723, -111.4453125 34.129994745824717, -111.0498046875 33.966142265597391))', 4326);
DECLARE @point GEOGRAPHY = GEOGRAPHY::STPointFromText('POINT (-112.0685317 33.4491407)', 4326);

SELECT  @point.STIsValid() AS [PointIsValid], --True
        @polygon.STIsValid() AS [PolygonIsValid], --True
        @point.STWithin(@polygon) AS [PointWithinPolygon], --False
        @point.STIntersects(@polygon) AS [PointIntersectsPolygon], --False
        @polygon.STContains(@point) AS [PolygonContainsPoint]; --False

我需要做什么才能让查询告诉我点在多边形中?我看到一些搜索结果谈论“方向”,但我不知道如何指定它。坐标是从 Google Maps JavaScript API 捕获的,并使用 Entity Framework 6.1 保存到数据库中。

【问题讨论】:

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


    【解决方案1】:

    我想你有一个Ring Orientation problem。你需要颠倒多边形的顺序:

    using Microsoft.SqlServer.Types;
    using System;
    using System.Collections.Generic;
    using System.Data.Spatial;
    using System.Data.SqlTypes;
    
    namespace SomeNamespace
    {
      public static class DbGeographyHelper
      {
        // 4326 is most common coordinate system used by GPS/Maps
        // 4326 format puts LONGITUDE first then LATITUDE
        private static int _coordinateSystem = 4326;
    
        public static DbGeography CreatePolygon(string wktString)
        {
          // create polygon is same order as wktString
          var sqlGeography = SqlGeography
            .STGeomFromText(new SqlChars(wktString), _coordinateSystem)
            .MakeValid();
    
          // create the polygon in the reverse order
          var invertedSqlGeography = sqlGeography.ReorientObject();
    
          // which ever one is big is probably not the one you want
          if (sqlGeography.STArea() > invertedSqlGeography.STArea())
          {
            sqlGeography = invertedSqlGeography;
          }
    
          return DbSpatialServices.Default.GeographyFromProviderValue(sqlGeography);
        }
      }
    }
    

    【讨论】:

      【解决方案2】:

      @Erik 的回答是正确的,因为 SQL Server 不包括我在多边形中指定的区域。反转多边形解决了这个问题。他的代码是正确的,但我必须进行修改以适应我的使用情况,所以这是我的代码,供任何关心的人使用:

      public static class DbGeographyExtensions {
          public static DbGeography PolygonFromGoogleMapsText(
              string wellKnownText,
              int coordinateSystemId) {
              SqlGeography geography = SqlGeography.STGeomFromText(new SqlChars(wellKnownText), coordinateSystemId).MakeValid();
              SqlGeography invertedGeography = geography.ReorientObject();
      
              if (geography.STArea() > invertedGeography.STArea()) {
                  geography = invertedGeography;
              }
      
              return DbGeography.FromText(geography.ToString(), coordinateSystemId);
          }
      }
      

      我在尝试从 @Erik 的代码返回时遇到异常,因此我使用了此处答案中的代码:Entity Framework: SqlGeography vs DbGeography

      【讨论】:

        猜你喜欢
        • 2014-04-26
        • 1970-01-01
        • 2011-06-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-07
        相关资源
        最近更新 更多