geopandas 没有在多边形中找到点,即使它应该?

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

我有一些 lat/long 坐标,需要确认它们是否位于佐治亚州亚特兰大市。我正在测试它,但它似乎不起作用。

我从这里得到了一个似乎合法的 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

我插入了从 Google 地图获得的几个坐标:

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

为什么他们不都是真的?我做错了吗?

正如documentation中所说:

It does not check if an element of one GeoSeries contains any element of the other one.

所以你应该使用一个循环来检查所有的点。

  • 我发现了一些与您所指的相似的几何图形
  • 另一种方法是使用 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