Skip to content
Advertisement

How can you combine GeoJson features by their property value?

TLDR: Can you join together GeoJSON features that have a common property, such that the final polygon is a combination of the smaller features?

I’m building a web application that requires me to modify some existing GeoJSON by removing lines between counties, essentially creating larger polygons out of smaller ones. Here’s what the GeoJSON looks like:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "feature",
      "geometry": {
        "type": "MultiPolygon",
        "coordinates": [[[[ ..some coordinates ]]]],
      },
      "properties": {
        "joiner": 1,
        "PUMA": "Alameda County"
      }
    },
    {
      "type": "feature",
      "geometry": {
        "type": "MultiPolygon",
        "coordinates": [[[[ ..some coordinates ]]]],
      },
      "properties": {
        "joiner": 2,
        "PUMA": "San Jose County"
      }
    },
    {
      "type": "feature",
      "geometry": {
        "type": "MultiPolygon",
        "coordinates": [[[[ ..some coordinates ]]]],
      },
      "properties": {
        "joiner": 1,
        "PUMA": "Fremont West"
      }
    },
  ]
}

In this dummy example, I’d like to join together Alameda County and Fremont West, because they both contain the “joiner” property of 1 (they are next to each other, this is a given). Visually speaking, this would mean that certain adjacent polygons on the map would be joined into a single polygon.

This is what the data currently looks like when projected using MapboxGL:

Original GeoJSON

The final projection would join together adjacent polygons with a common property and remove the dividing lines, creating larger ones:

Joined GeoJSON

Advertisement

Answer

You can use Turf’s “dissolve” method, which does exactly this:

var dissolved = turf.dissolve(features, {propertyName: 'joiner'});

Documentation: http://turfjs.org/docs/#dissolve

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement