Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

132
Vistas
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.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

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

about 4 years ago · Juan Pablo Isaza Denunciar

0

You have spelled var to "war" which needs to be 'var'. and if this is not a mistake please specify what is 'features2' array.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda