如何使用geopandas找到坐标的最近值

How to use geopandas to find the nearest value of a coordinate

我有一个坐标列表,每个坐标都有温度。数据框如下所示: 例如:

纬度 经度 温度
51.23 4.234 23.3
51.29 4.211 26.4
51.25 4.238 24.3
51.26 4.221 28.4
51.30 4.244 19.3
51.40 4.231 20.4

在 geopandas 中有没有办法直接找到每行 100 米距离内的观测值,并使用最近观测值的平均值创建一个新列

例如:

纬度 经度 温度 平均温度
51.23 4.234 23.3 100m距离内的平均温度
51.29 4.211 26.4 100m距离内的平均温度
51.25 4.238 24.3 100m距离内的平均温度
51.26 4.221 28.4 100m距离内的平均温度
51.30 4.244 19.3 100m距离内的平均温度
51.40 4.231 20.4 100m距离内的平均温度

我试过使用 nearest_point:

def get_nearest_values(row, other_gdf, point_column='geometry', 
value_column="predictions_precipitation_type"):


    # Create an union of the other GeoDataFrame's geometries:
    other_points = other_gdf["geometry"].unary_union

    # Find the nearest points
    nearest_geoms = nearest_points(row[point_column], other_points)

    # Get corresponding values from the other df
    nearest_data = other_gdf.loc[other_gdf["geometry"] == 
    nearest_geoms[1]]

    nearest_value = nearest_data[value_column].values[0]

return nearest_value

但它找到了最近的观测值及其值.. 我想找到 100 米半径内的所有观测值,然后找到平均值

试试这个:

import geopandas as gpd
from shapely.geometry import Point

s = """Lat  Lon Temperature
51.23   4.234   23.3
51.29   4.211   26.4
51.25   4.238   24.3
51.26   4.221   28.4
51.30   4.244   19.3
51.40   4.231   20.4"""

n = 3 # Columns
data =  [s.split()[i:i + n] for i in range(0, len(s.split()), n)]
df = gpd.pd.DataFrame(data[1:], columns=data[0])

for col in df.columns:
    df[col] = gpd.pd.to_numeric(df[col])

geometry = [Point(xy) for xy in zip(df.Lon, df.Lat)]

gdf = gpd.GeoDataFrame(df, geometry=geometry)

for index, row in gdf.iterrows():
    buffer = row.geometry.buffer(0.1)
    points_inside_buffer = gdf[gdf.geometry.within(buffer)]
    points_temperatures = points_inside_buffer['Temperature'].tolist()
    mean_temp = sum(points_temperatures)/len(points_temperatures)
    gdf.at[index, "Mean Temp within 100m"] = mean_temp