簡體   English   中英

geopandas 沒有在多邊形中找到點,即使它應該?

[英]geopandas doesn't find point in polygon even though it should?

我有一些緯度/經度坐標,需要確認它們是否在佐治亞州亞特蘭大市。 我正在測試它,但它似乎不起作用。

我從這里得到了一個似乎是合法的geojson:

https://gis.atlantaga.gov/?page=OPEN-DATA-HUB

import pandas as pd
import geopandas
atl = geopandas.read_file('Official_City_Boundary.geojson')
atl['geometry']   # this shows the image of Atlanta which appears correct

我插入了從谷歌地圖獲得的幾個坐標:

x = [33.75865421788594, -84.43974601192079]
y = [33.729117878816, -84.4017757998275]
z = [33.827871937500255, -84.39646813516548]

df = pd.DataFrame({'latitude': [x[0], y[0], z[0]], 'longitude': [x[1], y[1], z[1]]})
geometry = geopandas.points_from_xy(df.longitude, df.latitude)
points = geopandas.GeoDataFrame(geometry=geometry)

points

                     geometry
0  POINT (-84.43975 33.75865)
1  POINT (-84.40178 33.72912)
2  POINT (-84.39647 33.82787)

但是當我檢查點是否在邊界內時,只有一個是真的:

atl['geometry'].contains(points)

0     True
1    False
2    False

為什么它們不都是真的? 我做錯了嗎?

  • 我發現了一些類似於您所指的幾何形狀
  • 另一種方法是使用intersects()來查找包含關系。 注意使用unary_union作為我下載的亞特蘭大幾何圖形包含多個多邊形
import pandas as pd
import geopandas
from pathlib import Path
atl = geopandas.read_file(Path.home().joinpath("Downloads").joinpath('Official_City_Council_District_Boundaries.geojson'))
atl['geometry']   # this shows the image of Atlanta which appears correct

x = [33.75865421788594, -84.43974601192079]
y = [33.729117878816, -84.4017757998275]
z = [33.827871937500255, -84.39646813516548]

df = pd.DataFrame({'latitude': [x[0], y[0], z[0]], 'longitude': [x[1], y[1], z[1]]})
geometry = geopandas.points_from_xy(df.longitude, df.latitude)
points = geopandas.GeoDataFrame(geometry=geometry, crs="epsg:4326")

points.intersects(atl.unary_union)
0    True
1    True
2    True
dtype: bool

正如文檔中所說:

它不檢查一個 GeoSeries 的元素是否contains另一個 GeoSeries 的任何元素。

因此,您應該使用循環來檢查所有點。

暫無
暫無

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

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