多边形内的所有点

多边形内的所有点

本文介绍了使用Geopandas,如何选择不在多边形内的所有点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含芝加哥地址的数据框,将其地理编码为纬度和经度值,然后编码为Point对象(使该DataFrame成为GeoDataFrame).一小部分错误地使用了芝加哥以外的LatLong值进行了地理编码.我有一个芝加哥边界(GeoDataFrame)的shapefile,我想选择点在芝加哥边界多边形之外的所有行.

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.

选择多边形内的所有点很容易(通过geopandas sjoin函数),但是我还没有找到一种选择不在多边形内的点的好方法.是否存在?

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?

推荐答案

如果将芝加哥边界GeoDataFrame转换为单个多边形,例如使用:

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

chicago = df_chicago.geometry.unary_union

然后,您可以使用within运算符使用布尔过滤来选择Chicago内外的点:

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)]

使用~反转布尔条件.

或者,您可以使用disjoint空间谓词:

Alternatively, you could use the disjoint spatial predicate:

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

这篇关于使用Geopandas,如何选择不在多边形内的所有点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 02:15