GeoJSON

GeoJSON is a textual data format for representing spatial information. It is based on JavaScript Object Notation (JSON).

GeoJSON can be used to represent the following spatial objects, which also have Well Known Text (WKT) and Well Known Binary (WKB) representations:

  • Point
  • LineString
  • Polygon
  • MultiPoint
  • MultiLineString
  • MultiPolygon
  • GeometryCollection

GeoJSON introduces the following additional concepts, which are not part of WKT or WKB:

  • A "Feature" object that can contain a geometric shape and some additional properties that describe that shape. This is useful, for example, when drawing maps on the internet in color, such as on geojson.io. For an example showing how to add color to a GeoJSON feature, see below.
  • Features can additionally be grouped together into a "FeatureCollection".
Tip:

For more detailed information, see the GeoJSON RFC.

Note:

GeoJSON should only be used for spatial data that uses the WGS84 geographic spatial reference system. For more information, see SRID 4326.

Example

In the example below, we will convert a shape represented in Well Known Text to GeoJSON using the ST_AsGeoJSON function.

Here is the WKT:

SRID=4326;POLYGON((-87.906471 43.038902, -95.992775 36.153980, -75.704722 36.076944, -87.906471 43.038902), (-87.623177 41.881832, -90.199402 38.627003, -82.446732 38.413651, -87.623177 41.881832))

Convert it to GeoJSON using the ST_AsGeoJSON function:

icon/buttons/copy
SELECT ST_AsGeoJSON('SRID=4326;POLYGON((-87.906471 43.038902, -95.992775 36.153980, -75.704722 36.076944, -87.906471 43.038902), (-87.623177 41.881832, -90.199402 38.627003, -82.446732 38.413651, -87.623177 41.881832))');

This is the JSON output of the above, but formatted:

icon/buttons/copy
{
    "type": "Polygon",
    "coordinates": [
        [
            [
                -87.906471,
                43.038902
            ],
            [
                -95.992775,
                36.15398
            ],
            [
                -75.704722,
                36.076944
            ],
            [
                -87.906471,
                43.038902
            ]
        ],
        [
            [
                -87.623177,
                41.881832
            ],
            [
                -90.199402,
                38.627003
            ],
            [
                -82.446732,
                38.413651
            ],
            [
                -87.623177,
                41.881832
            ]
        ]
    ]
}

The JSON below is modified from the output above: it is grouped into a GeoJSON FeatureCollection in which each Feature has additional styling information (in the properties field) that can be used in visualization tools such as geojson.io:

icon/buttons/copy
{
    "type": "FeatureCollection",
    "features": [
        {
            "properties": {
                "fill-opacity": 0.3,
                "stroke": "#30D5C8",
                "stroke-width": 5,
                "fill": "#30D5C8"
            },
            "geometry": {
                "coordinates": [
                    [
                        [
                            -87.906471,
                            43.038902
                        ],
                        [
                            -95.992775,
                            36.15398
                        ],
                        [
                            -75.704722,
                            36.076944
                        ],
                        [
                            -87.906471,
                            43.038902
                        ]
                    ],
                    [
                        [
                            -87.623177,
                            41.881832
                        ],
                        [
                            -90.199402,
                            38.627003
                        ],
                        [
                            -82.446732,
                            38.413651
                        ],
                        [
                            -87.623177,
                            41.881832
                        ]
                    ]
                ],
                "type": "Polygon"
            },
            "type": "Feature"
        },
        {
            "properties": {
                "stroke": "yellow",
                "fill-opacity": 0.3,
                "stroke-width": 9,
                "fill": "yellow"
            },
            "geometry": {
                "type": "LineString",
                "coordinates": [
                    [
                        -87.623177,
                        41.881832
                    ],
                    [
                        -90.199402,
                        38.627003
                    ],
                    [
                        -82.446732,
                        38.413651
                    ],
                    [
                        -87.623177,
                        41.881832
                    ]
                ]
            },
            "type": "Feature"
        }
    ]
}

Here is the geometry described above as shown on geojson.io:

GeoJSON.io output

See also


Yes No