地理数据框以选择某些ID并在指定距离内找到几何形状

发布于 2025-02-07 19:38:57 字数 1957 浏览 2 评论 0原文

我创建了一个测试形状文件,其中包含EPSG:2157中的15个点功能,并导出了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.stac.stackexchange.com/questions/questions/349637/given - 列表lat-lat-long-how-how-to-find-all-points-within-radius of-a-give

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

但这只是在打印我的原始geopandas dataframe,其中所有15个ID和经度/经度/纬度,

我需要一种从数据框架中指定所需的ID的方法,在其上设置了10米缓冲区,以及从该点剩下的14点ID和该点的距离中的任何一个打印中。

我该如何做这样的事情?

I have created a test shapefile containing 15 point features in EPSG:2157 and exported it geojson. Each point has been assigned an ID - e.g. 1, 2 ,3 , etc. They look like so:

{
    "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

I now want to use Python to essentially:

  • Specify the ID of the point of interest
  • Add a search distance in metres
  • Print the ID's of the points within the specified distance and their total distance from the point of interest

I have tried geopandas so far to get me going as per https://gis.stackexchange.com/questions/349637/given-list-of-points-lat-long-how-to-find-all-points-within-radius-of-a-give

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

But this is just printing my original geopandas dataframe with all 15 IDs and longitude/latitudes,

I need a way of specifying which ID I want from the dataframe, set the 10 metre buffer on it and from that print whichever of the remaining 14 points ID and distance from that point.

How do I go about such a thing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

杀手六號 2025-02-14 19:38:57

该方法首先计算从选定点,然后过滤到搜索距离的距离:

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

This approach first calculates the distance from the selected point and then filters to the search distance:

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)

Output:

  id                         geometry  distance
0  1  POINT (-657346.500 7286537.676)  0.000000
1  2  POINT (-657339.334 7286538.871)  7.264817
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文