You need to enable JavaScript to run this app. 如何使用geopandas计算坐标点100米范围内邻近观测值的温度均值
优惠活动
大模型
产品
解决方案
定价
更多

如何使用geopandas计算坐标点100米范围内邻近观测值的温度均值

实现方案

前置说明

你之前的逻辑是查询最近单点,要实现范围均值计算,核心要解决两个问题:1. 把经纬度坐标系转换为米级投影坐标系,才能做准确的100米范围判断;2. 用空间索引做范围查询,保证大数据量下的运行效率。

完整实现代码

import geopandas as gpd
import pandas as pd
from shapely.geometry import Point

# 1. 读取并转换为GeoDataFrame
# 此处替换为你自己的原始DataFrame读取逻辑
df = pd.DataFrame({
    'Lat': [51.23, 51.29, 51.25, 51.26, 51.30, 51.40],
    'Lon': [4.234, 4.211, 4.238, 4.221, 4.244, 4.231],
    'Temperature': [23.3, 26.4, 24.3, 28.4, 19.3, 20.4]
})

# 初始指定为WGS84经纬度坐标系
gdf = gpd.GeoDataFrame(
    df,
    geometry=gpd.points_from_xy(df.Lon, df.Lat),
    crs="EPSG:4326"
)

# 转换为UTM投影坐标系(单位为米,示例为坐标对应区域的Zone31N,可根据你的数据实际范围调整EPSG编码)
gdf_utm = gdf.to_crs("EPSG:32631")

# 2. 范围查询&均值计算
search_radius = 100  # 搜索半径100米
temp_col = "Temperature"
mean_col = "Mean Temp"

# 构建空间索引加速查询
sindex = gdf_utm.sindex

for idx, row in gdf_utm.iterrows():
    # 生成当前点的100米缓冲区
    buffer = row.geometry.buffer(search_radius)
    # 空间索引粗筛可能匹配的点
    possible_matches_index = list(sindex.intersection(buffer.bounds))
    possible_matches = gdf_utm.iloc[possible_matches_index]
    # 精筛实际在100米范围内的点
    precise_matches = possible_matches[possible_matches.intersects(buffer)]
    # 若需要排除当前点自身的温度,可加一行:precise_matches = precise_matches[precise_matches.index != idx]
    gdf_utm.loc[idx, mean_col] = precise_matches[temp_col].mean()

# 转回WGS84经纬度坐标系输出结果
gdf_result = gdf_utm.to_crs("EPSG:4326")
print(gdf_result[["Lat", "Lon", "Temperature", "Mean Temp"]])

注意事项

  • 必须做坐标系转换:不能直接在经纬度坐标系下做米级buffer,经纬度单位是度,1度对应约111公里,直接计算会完全错误。
  • 空间索引可以大幅提升查询效率,当数据量超过1万点时性能优势会非常明显。

内容的提问来源于stack exchange,提问作者rv1994

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.09 08:06:03