Using Geopandas, how do I select all points not within a polygon?

耗尽温柔 提交于 2019-12-13 14:08:02

问题


I have a DataFrame containing Chicago addresses which I've geocoded into latitude and longitude values, and then into Point objects (making the DataFrame a GeoDataFrame). A small fraction have been incorrectly geocoded with LatLong values outside of Chicago. I have a shapefile for Chicago's boundary (GeoDataFrame), I want to select all rows where the Points are outside of Chicago's boundary polygon.

It would be easy to select all points within the polygon (via geopandas sjoin function), but I haven't found a good way to select the points not within the polygon. Does one exist?


回答1:


If you convert the Chicago boundary GeoDataFrame to a single polygon, eg with:

chicago = df_chicago.geometry.unary_union

then you can use boolean filtering with the within operator to select points within and outside of Chicago:

within_chicago = df[df.geometry.within(chicago)]
outside_chicago = df[~df.geometry.within(chicago)]

using ~ to invert the boolean condition.

Alternatively, you could use the disjoint spatial predicate:

outside_chicago = df[df.geometry.disjoint(chicago)]


来源:https://stackoverflow.com/questions/52600811/using-geopandas-how-do-i-select-all-points-not-within-a-polygon

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