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

如何合并GeoJSON数组中相同User_ID条目的坐标值?

问题原因分析

  • 路径访问错误:原代码直接读取feature.User_ID,但User_ID存储在properties属性下,正确路径为feature.properties.User_ID,这是触发报错的核心原因之一
  • 逻辑不匹配:原代码逻辑是合并不同Feature的属性字段,没有处理坐标合并的需求
  • 预期输出不符合GeoJSON规范:标准Point类型的coordinates字段只能是单个一维坐标数组,存储多个坐标需要将geometry.type改为MultiPoint,coordinates改为二维数组,否则大部分GIS解析工具无法识别该格式。

解决方案代码

直接遍历所有Feature,以User_ID为分组键合并坐标,输出符合标准规范的GeoJSON结构:

// 存储User_ID与合并后Feature的映射关系
const userFeatureMap = {};

features.forEach(feature => {
  const userId = feature.properties.User_ID;
  const currentCoord = feature.geometry.coordinates;

  // 首次遇到该用户时初始化对应Feature
  if (!userFeatureMap[userId]) {
    userFeatureMap[userId] = {
      type: "Feature",
      properties: {
        User_ID: userId
      },
      geometry: {
        type: "MultiPoint",
        coordinates: []
      }
    };
  }

  // 追加当前坐标到对应用户的坐标列表
  userFeatureMap[userId].geometry.coordinates.push(currentCoord);
});

// 转换为最终的合并后Feature数组
const mergedFeatures = Object.values(userFeatureMap);

合并后的输出结构示例:

{
  "type": "Feature",
  "properties": {
    "User_ID": 1002848324
  },
  "geometry": {
    "type": "MultiPoint",
    "coordinates": [
      [-3.231658, 51.687026],
      [-3.231659, 51.687016]
    ]
  }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 11:15:04