【问题标题】:How does one merge coordinate values of an array of geojson items?如何合并一组geojson项目的坐标值?
【发布时间】:2021-12-16 05:20:20
【问题描述】:

您好,我正在处理一个大型 geojson 数据集,我正在尝试查看是否可以根据共享相同“User_ID”的条目合并每个条目的坐标值。

我的数据集如下所示:

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

我尝试使用 mwarren 的答案中显示的方法合并条目,网址:“https://stackoverflow.com/questions/29244116/merge-geojson-based-on-unique-id”。

然而,当我尝试运行测试时,“attr”被视为“意外标识符”。

到目前为止我对代码的测试如下:

features.map(function(feature){
var matchedArray = features2.filter(function(feature2){
    return feature2.User_ID === feature.User_ID;
});
   if(matchedArray && matchedArray[0]){
      for(var attr in matchedArray[0].properties){
          feature.properties[attr] = matchedArray[0].properties[attr];
    }
  }
});

想要的结果应该是这样的:

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

任何帮助将不胜感激。

【问题讨论】:

  • 你的预期输出是什么?
  • OP 不仅需要按相同的User_ID 值分组/收集数据,还需要按相同的type 和相同的User_ID 值分组/收集数据

标签: javascript arrays merge geojson reduce


【解决方案1】:

从上面的评论...

“OP 不仅需要通过相同的 User_ID 值分组/收集数据,还需要通过相同的 type 和相同的 User_ID 值分组/收集数据”

...甚至可能是相同的geometry.type。仅基于 User_ID 的分组/收集不够明确,因为具有相同 User_ID 值的项目可能具有不同的 type 值,甚至可能在它们的 geometry.type 值上有所不同。

基于reduce 的任务使用collector 对象,该对象具有lookup 对象和用于聚合最终数据的result 数组,确实在一个迭代步骤内解决了OP 的任务...

function collectSameGeoCategoryItems(collector, item) {
  const { lookup, result } = collector;
  const { properties: { User_ID }, type } = item;

  const groupKey = `${ type }_${ User_ID }`;
  let groupItem = lookup[groupKey];

  if (groupItem) {
    // push coordinates copy into existing array of coordinate arrays.

    groupItem.geometry.coordinates.push([...item.geometry.coordinates]);
  } else {
    // create full copy of geo item in order
    // to not mutate the original reference.

    groupItem = lookup[groupKey] = {
      geometry: {
        type: item.geometry.type,
        coordinates: [ [...item.geometry.coordinates] ],
      },
      type,
      properties: { User_ID },
    };
    result.push(groupItem);
  }
  return collector;
}

const sampleData = [{
  geometry: {
    type: "Point",
    coordinates: [
      -3.231658,
      51.687026,
    ],
  },
  type: "Feature",
  properties: {
    User_ID: 1002848324,
  },
}, {
  geometry: {
    type: "Point",
    coordinates: [
      -3.231659,
      51.687016,
    ],
  },
  type: "Feature",
  properties: {
    User_ID: 1002848324,
  },
}, {
  geometry: {
    type: "Point",
    coordinates: [
      -3.231658,
      51.687026,
    ],
  },
  type: "Foo",
  properties: {
    User_ID: 1002848324,
  },
}, {
  geometry: {
    type: "Point",
    coordinates: [
      -3.231659,
      51.687016,
    ],
  },
  type: "Bar",
  properties: {
    User_ID: 1002848324,
  },
}, {
  geometry: {
    type: "Point",
    coordinates: [
      -3.231658,
      51.687026,
    ],
  },
  type: "Foo",
  properties: {
    User_ID: 1000000000,
  },
}, {
  geometry: {
    type: "Point",
    coordinates: [
      -3.231659,
      51.687016,
    ],
  },
  type: "Bar",
  properties: {
    User_ID: 1002848324,
  },
}];

console.log(
  'aggregated `result` ...',
  sampleData.reduce(collectSameGeoCategoryItems, { lookup: {}, result: [] }).result
);
console.log('unmutated sample data ... ', { sampleData });

console.log(
  'aggregated `lookup`, not needed, just for demonstration purpose ...',
  sampleData.reduce(collectSameGeoCategoryItems, { lookup: {}, result: [] }).lookup
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

【讨论】:

  • 谢谢,这已经成功了,感谢您对代码的详细说明。
  • 遇到一个问题,在导入我的文件以通过您的方法进行排序后,它会标记一个错误“reduce is not a function”,您知道它可能出了什么问题吗?跨度>
  • @TML ... 检查数据是否为数组类型。如果错误是“reduce is not a function”,那么也不应该存在/调用其他数组方法。除非你运行一个完全古老的 JavaScript 环境。
  • 现在整理好了,有几个语法错误解决了reduce无法识别的问题。
【解决方案2】:

您已将 var 拼写为“war”,需要为“var”。 如果这不是错误,请指定什么是 'features2' 数组。

【讨论】:

  • 'features2' 变量只是第二个 GEOjson 条目的名称,根据我在问题中链接的帖子中用户“mwarren”编写的代码。
  • 也感谢您指出我的错误,遗憾的是它没有给我预期的输出。
猜你喜欢
  • 1970-01-01
  • 2013-08-05
  • 1970-01-01
  • 2021-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多