[英]How do I test if Point is in Polygon/Multipolygon with geopandas in Python?
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.我有来自网站arcgis的美国的多边形数据,我还有一个带有城市坐标的 excel 文件。 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.
两者都是dtype:几何。 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?有人知道我做错了什么吗?
contains
in GeoPandas currently work on a pairwise basis 1-to-1, not 1-to-many. contains
在 GeoPandas 中目前以一对一的方式工作,而不是一对多。 For this purpose, use sjoin
.为此,请使用
sjoin
。
points_within = gp.sjoin(gdf, US, op='within')
That will return only those points within the US
.这将只返回
US
境内的那些点。 Alternatively, you can filter polygons which contain points.或者,您可以过滤包含点的多边形。
polygons_contains = gp.sjoin(US, gdf, op='contains')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.