繁体   English   中英

Geopandas 数据框用于选择特定 ID 并在指定距离内查找几何图形

[英]Geopandas dataframe to select certain ID and find geometries within specified distance

我在 EPSG:2157 中创建了一个包含 15 个点特征的测试 shapefile,并将其导出为 geojson。 每个点都被分配了一个 ID - 例如 1, 2 ,3 等。它们看起来像这样:

{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": {
                "id": "1"
            },
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -5.905044078826904,
                    54.609987802465916
                ]
            }
        },
      {
            "type": "Feature",
            "properties": {
                "id": "11"
            },
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -5.902683734893799,
                    54.60972062159888
                ]
            }
        }
    ]
}

etc

我现在想使用 Python 来本质上:

  • 指定兴趣点的 ID
  • 添加以米为单位的搜索距离
  • 打印指定距离内的点的 ID 及其到兴趣点的总距离

到目前为止,我已经尝试过geopandas来让我按照https://gis.stackexchange.com/questions/349637/given-list-of-points-lat-long-how-to-find-all-points-within-给予半径

import geopandas as gpd
import pandas as pd

input_file = 'C:/test/points.geojson'
df = gpd.read_file(input_file)
df['lon'] = df['geometry'].x
df['lat'] = df['geometry'].y


gdf = gpd.GeoDataFrame(
    df,
    geometry=gpd.points_from_xy(
        df["lon"],
        df["lat"],
    ),
    crs={"init":"EPSG:2157"},
)

print(gdf)

gdf_proj = gdf.to_crs({"init": "EPSG:3857"})


x = gdf_proj.buffer(10)

neighbours = gdf_proj["geometry"].intersection(x)

# print all the nearby points
print(gdf_proj[~neighbours.is_empty])

但这只是用所有 15 个 ID 和经度/纬度打印我的原始 geopandas 数据框,

我需要一种从数据帧中指定我想要的 ID 的方法,在其上设置 10 米缓冲区并从该打印剩余 14 个点 ID 中的任何一个以及与该点的距离。

我该怎么办?

这种方法首先计算到所选点的距离,然后过滤到搜索距离:

import geopandas as gpd

input_file = 'test.geojson'
gdf = gpd.read_file(input_file).to_crs('EPSG:3857')
my_id = 1
search_distance = 10
selected_feature = gdf.loc[gdf['id'].astype(int) == my_id]
result = (
    gdf
    .assign(distance=gdf.apply(lambda x: x.geometry.distance(selected_feature.geometry.iloc[0]), axis=1))
    .query(f'distance <= {search_distance}')
)
print(result)

输出:

  id                         geometry  distance
0  1  POINT (-657346.500 7286537.676)  0.000000
1  2  POINT (-657339.334 7286538.871)  7.264817

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM