【问题标题】:How to use geopandas to find the nearest value of a coordinate如何使用 geopandas 找到坐标的最近值
【发布时间】:2021-10-07 13:23:10
【问题描述】:

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

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

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

例如:

Lat Lon Temperature Mean Temp
51.23 4.234 23.3 Mean temperature within 100m distance
51.29 4.211 26.4 Mean temperature within 100m distance
51.25 4.238 24.3 Mean temperature within 100m distance
51.26 4.221 28.4 Mean temperature within 100m distance
51.30 4.244 19.3 Mean temperature within 100m distance
51.40 4.231 20.4 Mean temperature within 100m distance

我尝试过使用最近点:

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

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

【问题讨论】:

标签: python geopandas


【解决方案1】:

试试这个:

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

【讨论】:

    猜你喜欢
    • 2013-06-28
    • 2014-09-07
    • 1970-01-01
    • 2021-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-06
    相关资源
    最近更新 更多