How do I test if Point is in Polygon/Multipolygon with geopandas in Python?

こ雲淡風輕ζ 提交于 2021-02-11 14:15:58

问题


I have the Polygon data from the States from the USA from the website arcgis and I also have an excel file with coordinates of citys. I have converted the coordinates to geometry data (Points). Now I want to test if the Points are in the USA. Both are dtype: geometry. I thought with this I can easily compare, but when I use my code I get for every Point the answer false. Even if there are Points that are in the USA.

The code is:

import geopandas as gp
import pandas as pd
import xlsxwriter
import xlrd
from shapely.geometry import Point, Polygon

df1 = pd.read_excel('PATH')
gdf = gp.GeoDataFrame(df1, geometry= gp.points_from_xy(df1.longitude, df1.latitude))

US = gp.read_file('PATH')

print(gdf['geometry'].contains(US['geometry']))

Does anybody know what I do wrong?


回答1:


contains in GeoPandas currently work on a pairwise basis 1-to-1, not 1-to-many. For this purpose, use sjoin.

points_within = gp.sjoin(gdf, US, op='within')

That will return only those points within the US. Alternatively, you can filter polygons which contain points.

polygons_contains = gp.sjoin(US, gdf, op='contains')


来源:https://stackoverflow.com/questions/62410871/how-do-i-test-if-point-is-in-polygon-multipolygon-with-geopandas-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!