Search code examples
javascriptuniquedistinctgeojson

How to create GeoJSON collection with distinct features?


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


Solution

  • 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)