[英]SQL Server 2014 find a point in polygon
我不知道為什么 SQL 在我的多邊形中沒有檢測到這個點
select geography::STGeomFromText('POLYGON ((51.561283 -0.199251
,51.562136 -0.045443
,51.468985 -0.045443
,51.472407 -0.197878
,51.561283 -0.199251))', 4326).STIntersects(geography::Point(51.519425,-0.127029, 4326))
我的多邊形在STIsValid()
的結果中有效,並且在 SSMS 顯示多邊形的正確方向上:
我的點也通過谷歌地圖位於多邊形的中心:
這是我用來檢測我的觀點是否正確的工具: https://www.doogal.co.uk/polylines.php
在文本輸入中使用這些數據:
51.561283,-0.199251
51.562136,-0.045443
51.468985,-0.045443
51.472407,-0.197878
51.561283,-0.199251
51.519425,-0.127029
奇怪的是,當我將 lat 和 long 更改為geography::Point
function 結果是真的。
select geography::STGeomFromText('POLYGON ((51.561283 -0.199251
,51.562136 -0.045443
,51.468985 -0.045443
,51.472407 -0.197878
,51.561283 -0.199251))', 4326).STIntersects(geography::Point(-0.127029,51.519425, 4326))
Geometry/Geography 的文本字符串表示始終使用XY
約定來指定坐標。 這意味着,當解釋為地理時,它違反了將緯度放在首位的通常慣例。
因此,您需要使用Long Lat
在字符串中指定您的POLYGON
。
您可以在 SSMS output 中看到這一點,您可以在其中看到多邊形位於赤道以南,東約 51° 的某個地方,而不是倫敦的某個地方。
您必須翻轉geography::Point
上的值,因為第一個值是經度,第二個值是緯度:
地理數據類型的點類型表示單個位置,其中 x 和 y 分別表示經度和緯度值。
來源: https://docs.microsoft.com/en-us/previous-versions/sql/sql-server-2008-r2/bb964737(v=sql.105)
請參閱以下直接使用Point
數據類型或使用geometry::STGeomFromText
STGeomFromText 獲取Point
object 之間的比較。 經度和經度的順序不同。
-- get the Point from text using geography::STGeomFromText
SELECT geography::STGeomFromText('POINT(51.519425 -0.127029)', 4326).Lat -- -0,127029
SELECT geography::STGeomFromText('POINT(51.519425 -0.127029)', 4326).Long -- 51,519425
-- get the Point directly using geography::Point
SELECT geography::Point(51.519425, -0.127029, 4326).Lat -- 51,519425
SELECT geography::Point(51.519425, -0.127029, 4326).Long -- -0,127029
所以你有兩種可能來解決這個問題:
1.翻轉Point
的值:
SELECT geography::STGeomFromText('POLYGON ((51.561283 -0.199251
,51.562136 -0.045443
,51.468985 -0.045443
,51.472407 -0.197878
,51.561283 -0.199251))', 4326
).STIntersects(geography::Point(-0.127029, 51.519425, 4326))
2.使用文本指定Point
:
SELECT geography::STGeomFromText('POLYGON ((51.561283 -0.199251
,51.562136 -0.045443
,51.468985 -0.045443
,51.472407 -0.197878
,51.561283 -0.199251))', 4326
).STIntersects(geography::STGeomFromText('POINT(51.519425 -0.127029)', 4326))
由於您使用的是地理數據,因此請確保多邊形與正確的區域匹配以獲取正確的數據。 您可以使用以下多邊形來匹配預期區域:
SELECT geography::STGeomFromText('POLYGON((
-0.199251 51.561283,
-0.197878 51.472407,
-0.045443 51.468985,
-0.045443 51.562136,
-0.199251 51.561283))', 4326
)
現在您可以使用以下解決方案來檢查點是否在多邊形內:
SELECT geography::STGeomFromText('POLYGON((
-0.199251 51.561283,
-0.197878 51.472407,
-0.045443 51.468985,
-0.045443 51.562136,
-0.199251 51.561283))', 4326
).STIntersects(geography::Point(51.519425, -0.127029, 4326))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.