问题
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