簡體   English   中英

檢查點是否在多邊形內不起作用

[英]Checking if a Point is inside a Polygon not working

使用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保留到數據庫中。

在此處輸入圖片說明

我認為您遇到環取向問題 您需要顛倒多邊形的順序:

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

@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的代碼返回時遇到異常,因此我在此處使用了答案中的代碼: 實體框架:SqlGeography vs DbGeography

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM