Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

136
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda