0

Given the following GeoJSON collection:

{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    13.55791,
                    52.54459
                ]
            },
            "properties": {
                "title": "Sommer 2022",
                "opening_hours": null,
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    13.39519,
                    52.5203
                ]
            },
            "properties": {
                "title": "Winter",
                "opening_hours": "Sa,Su,PH 11:00-17:00",
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    13.39519,
                    52.5203
                ]
            },
            "properties": {
                "title": "Winter",
                "opening_hours": "Sa,Su,PH 11:00-17:00",
            }
        }
    ]
}

How can I remove duplicate features to finally have a GeoJSON collection with unique features?

Related

JJD
  • 50,076
  • 60
  • 203
  • 339
  • Also related - https://stackoverflow.com/questions/1960473/get-all-unique-values-in-a-javascript-array-remove-duplicates – James Sep 07 '22 at 20:29

1 Answers1

1

Checking property by property, assuming they are well-known:

const data = {
  "type": "FeatureCollection",
  "features": [{
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          13.55791,
          52.54459
        ]
      },
      "properties": {
        "title": "Sommer 2022",
        "opening_hours": null,
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          13.39519,
          52.5203
        ]
      },
      "properties": {
        "title": "Winter",
        "opening_hours": "Sa,Su,PH 11:00-17:00",
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          13.39519,
          52.5203
        ]
      },
      "properties": {
        "title": "Winter",
        "opening_hours": "Sa,Su,PH 11:00-17:00",
      }
    }
  ]
}

let resFeat = []

for (let feat of data.features) {
  let filterRes = resFeat.filter(item => item.type == feat.type && item.geometry.type == feat.geometry.type && item.geometry.coordinates[0] == feat.geometry.coordinates[0] && item.geometry.coordinates[1] == feat.geometry.coordinates[1] && item.properties.title == feat.properties.title && item.properties.opening_hours == feat.properties.opening_hours);
  
  if (filterRes.length == 0) {
    resFeat.push(feat)
  }
}

data.features = resFeat

console.log(data)
GrafiCode
  • 3,307
  • 3
  • 26
  • 31