You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python提取高程数据导出CSV报数组长度不一致错误如何解决

报错根因
  • desired_lat_idx、desired_lon_idx是for循环内部的临时变量,每轮循环都会被覆盖为当前点位对应的单个网格整数值,循环结束后仅保存最后一个点位的索引,本质是长度为1的标量;而height是长度等于输入CSV总点位数的数组,三者长度不匹配,直接触发长度不一致报错。
  • 原有DataFrame构造逻辑存在偏差:存入的是高程网格索引而非实际经纬度,同时遗漏了输入CSV中的原始数值字段,无法生成「经度/纬度/原始数值/海拔高度」结构的目标文件。

问题截图

修正方法

直接使用前期从输入CSV读取得到的lon(经度)、lat(纬度)、value(原始数值)数组,和计算完成的height(海拔高度)数组构造DataFrame即可,这几个数组长度和输入点位数量完全一致,不会出现长度不匹配问题。

将原有的DataFrame构造、导出代码替换为以下内容:

dfl = pd.DataFrame({
    '经度': lon,
    '纬度': lat,
    '原始数值': value,
    '海拔高度': height
})
# 替换为你的实际输出文件路径,参数index=False表示导出时不额外生成行号列
dfl.to_csv('./高程匹配结果.csv', index=False)

可选补充:如果后续需要使用每个点位对应的高程网格索引,可在循环前提前初始化等长的索引存储数组,在循环内同步赋值,参考实现如下:

# 提前初始化索引存储数组
lat_idx_arr = np.empty_like(num_el, dtype=int)
lon_idx_arr = np.empty_like(num_el, dtype=int)

height = np.empty_like(num_el)
for i in range(len(num_el)): 
    desired_lat_idx = np.abs(etopo_lat - lat[i]).argmin()
    desired_lon_idx = np.abs(etopo_lon - lon[i]).argmin()
    height[i] = topo[desired_lat_idx, desired_lon_idx]
    # 同步存储当前点位的网格索引
    lat_idx_arr[i] = desired_lat_idx
    lon_idx_arr[i] = desired_lon_idx
height[height < 0] = 0

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.28 23:57:14