gpt4 book ai didi

python-3.x - 过滤多边形内的 GeoPandas 数据框,并从数据框中删除不存在的数据框

转载 作者:行者123 更新时间:2023-12-05 02:51:05 25 4
gpt4 key购买 nike

我有一个 .csv 文件,其中包含一些点(经度、纬度)。我使用以下代码将它转换为 DataFrame 并从 DataFrame 转换为 GeoDataFrame:

CSV 文件:

日期;用户ID;经度;纬度

2020-01-02;824664;-79.8831613;-2.1811152000000003

2020-03-01;123456;80.8831613;2.1811

2020-01-15;147835;-80.78035200000001;-1.4845725

我用来将 .csv 转换为 gdf ​​的代码:

df = pd.read_csv('datos25.csv', sep=';', low_memory=False, decimal='.')
gdf = geopandas.GeoDataFrame(
df, geometry=geopandas.points_from_xy(df.Longitud, df.Latitud))

然后,我使用这段代码来定义我的多边形,这是一个国家:

world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))
ec = world[world.name == 'Ecuador']

现在,我想做的是 gdf ​​中的每个点,验证它是否在多边形/国家中,如果不在,则从 DataFrame 中删除该行

例如,在这种情况下,几何列中的第二个值是:

点 (80.8831613 2.1811)

这个值所在的行应该从数据框中删除,因为它不在多边形/国家/地区

我该怎么做?

最佳答案

为了将来引用,您可以使用此 link 中的文档,我发现它非常有帮助!

您正在寻找的过程称为 Point in Polygon 并且,正如其他答案提到的那样,您可以使用函数 .within()

现在,用你已经拥有的我会做的:

#find point in polygon
#code below returns a series with boolean values
#if value is True it means the point in that index location is within the polygon we are evaluating

pip = gdf.within(ec.loc[0, 'geometry'])

#creating a new geoDataFrame that will have only the intersecting records

ec_gdf = gdf.loc[pip].copy()

#resetting index(optional step if you don't need to keep the original index values)
ec_gdf.reset_index(inplace=True, drop=True)

关于python-3.x - 过滤多边形内的 GeoPandas 数据框,并从数据框中删除不存在的数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63369715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com