我在 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-radius給予的
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 中的任何一個以及與該點的距離。
我該怎么辦?
uj5u.com熱心網友回復:
這種方法首先計算到所選點的距離,然后過濾到搜索距離:
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/492215.html
標籤:Python python-3.x 熊猫 数据框 大熊猫
上一篇:為什么在賦值之前參考區域變數?
