How does one merge coordinate values of an array of geojson items?



Hello I am working with a large geojson dataset, and I am trying to see if I can merge the coordinate values of each entry based on entries that share the same “User_ID”.

My dataset look like this:

{
     "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
     }
  }

I have tried to merge the entries using the method shown in mwarren’s answer, url: “https://stackoverflow.com/questions/29244116/merge-geojson-based-on-unique-id”.

Yet this comes with the small problem that “attr” is seen as an “Unexpected Identifier” when I try to run a test.

My test of the code so far is as follows:

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];
    }
  }
});

The desired result should look something like this:

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

Any help will be greatly appreciated.

Answer

From the above comment …

“The OP needs to group/collect the data not only by same User_ID values but by both same type AND same User_ID values”

… and maybe even by same geometry.type. A just User_ID based grouping/collecting is not explicit enough because items of same User_ID values might feature different type values or might even differ in their geometry.type values.

A reduce based task which uses a collector object which features an object as lookup and an result array for the aggregated final data does solve the OP’s task within one iteration step …

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; }


Source: stackoverflow