Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

GeoJSON

These functions operate on any GeoJSON (FeatureCollection, Feature, or Geometry).

intersection

Given any number of GeoJSONs, return the overlapping regions as a new Geometry.

Example

Given this array of Features:

[
  {
    "type": "Feature",
    "geometry": {
      "type": "Polygon",
      "coordinates": [
        [
          [0, 0],
          [2, 0],
          [2, 2],
          [0, 2],
          [0, 0]
        ]
      ]
    },
    "properties": null
  },
  {
    "type": "Feature",
    "geometry": {
      "type": "Polygon",
      "coordinates": [
        [
          [1, 1],
          [3, 1],
          [3, 3],
          [1, 3],
          [1, 1]
        ]
      ]
    },
    "properties": null
  }
]

intersection returns

{
  "type": "Polygon",
  "coordinates": [
    [
      [1, 2],
      [1, 1],
      [2, 1],
      [2, 2],
      [1, 2]
    ]
  ]
}

Try in the gq Playground

difference

Given any number of GeoJSONs, return the regions from the first GeoJSON which are not in any of the following GeoJSONs as a new Geometry.

Example

Given this FeatureCollection:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [0, 0],
            [2, 0],
            [2, 2],
            [0, 2],
            [0, 0]
          ]
        ]
      },
      "properties": null
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [1, 1],
            [3, 1],
            [3, 3],
            [1, 3],
            [1, 1]
          ]
        ]
      },
      "properties": null
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [1, 0],
            [2, 0],
            [2, 1],
            [1, 1],
            [1, 0]
          ]
        ]
      },
      "properties": null
    }
  ]
}

difference returns

{
  "type": "Polygon",
  "coordinates": [
      [
        [0, 2],
        [0, 0],
        [1, 0],
        [1, 2],
        [0, 2]
      ]
    ]
}

Try in the gq Playground

symmetric_difference

Like difference, but excludes overlapping regions (i.e. exclusive or).

Example

Given this FeatureCollection:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [0, 0],
            [2, 0],
            [2, 2],
            [0, 2],
            [0, 0]
          ]
        ]
      },
      "properties": null
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [1, 1],
            [3, 1],
            [3, 3],
            [1, 3],
            [1, 1]
          ]
        ]
      },
      "properties": null
    }
  ]
}

symmetric_difference returns

{
  "type": "MultiPolygon",
  "coordinates": [
    [
      [
        [0, 2],
        [0, 0],
        [2, 0],
        [2, 1],
        [1, 1],
        [1, 2],
        [0, 2]
      ]
    ],
    [
      [
        [1, 3],
        [1, 2],
        [2, 2],
        [2, 1],
        [3, 1],
        [3, 3],
        [1, 3]
      ]
    ]
  ]
}

Try in the gq Playground

union

Given any number of GeoJSONs, merge the regions of both into a new Geometry.

Example

Given this array of Features:

[
  {
    "type": "Feature",
    "geometry": {
      "type": "Polygon",
      "coordinates": [
        [
          [0, 0],
          [2, 0],
          [2, 2],
          [0, 2],
          [0, 0]
        ]
      ]
    },
    "properties": null
  },
  {
    "type": "Feature",
    "geometry": {
      "type": "Polygon",
      "coordinates": [
        [
          [1, 1],
          [3, 1],
          [3, 3],
          [1, 3],
          [1, 1]
        ]
      ]
    },
    "properties": null
  }
]

union returns

{
  "type": "Polygon",
  "coordinates": [
      [
        [0, 2],
        [0, 0],
        [2, 0],
        [2, 1],
        [3, 1],
        [3, 3],
        [1, 3],
        [1, 2],
        [0, 2]
      ]
    ]
}

Try in the gq Playground