> ## Documentation Index
> Fetch the complete documentation index at: https://cockroachlabs.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# GeoJSON

export const InternalLink = ({version, path = "", children, ...props}) => {
  let detectedVersion = version || "stable";
  if (typeof window !== 'undefined' && !version) {
    const match = window.location.pathname.match(/\/docs\/([^/]+)/);
    if (match) {
      detectedVersion = match[1];
    }
  }
  const normalizedPath = path.startsWith("/") ? path.slice(1) : path;
  return <a href={`/docs/${detectedVersion}/${normalizedPath}`} {...props}>
      {children}
    </a>;
};

GeoJSON is a textual data format for representing spatial information. It is based on [JavaScript Object Notation (JSON)](https://www.json.org/).

GeoJSON can be used to represent the following spatial objects, which also have <InternalLink path="well-known-text">Well Known Text (WKT)</InternalLink> and <InternalLink path="well-known-binary">Well Known Binary (WKB)</InternalLink> 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](http://geojson.io/). For an example showing how to add color to a GeoJSON feature, [see below](#geojson-features-example).
* Features can additionally be grouped together into a "FeatureCollection".

<Tip>
  For more detailed information, see the [GeoJSON RFC](https://www.rfc-editor.org/rfc/rfc7946.txt).
</Tip>

<Note>
  GeoJSON should only be used for spatial data that uses the <InternalLink path="architecture/glossary">WGS84</InternalLink> geographic spatial reference system. For more information, see <InternalLink path="srid-4326">SRID 4326</InternalLink>.
</Note>

## Example

In the example below, we will convert a shape represented in <InternalLink path="well-known-text">Well Known Text</InternalLink> to GeoJSON using the `ST_AsGeoJSON` <InternalLink path="functions-and-operators#spatial-functions">function</InternalLink>.

Here is the WKT:

```text theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
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:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
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:

```json theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
{
    "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
            ]
        ]
    ]
}
```

<a id="geojson-features-example" />

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](http://geojson.io/):

```json theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
{
    "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](http://geojson.io/):

<img src="https://mintcdn.com/cockroachlabs/Z2623UEgCBYdavu9/images/v24.1/geospatial/geojson_example.png?fit=max&auto=format&n=Z2623UEgCBYdavu9&q=85&s=82c566d3585d2f0d9335963bd73dfcfa" alt="GeoJSON.io output" width="1852" height="963" data-path="images/v24.1/geospatial/geojson_example.png" />

## See also

* [GeoJSON RFC](https://www.rfc-editor.org/rfc/rfc7946.txt)
* <InternalLink path="spatial-data-overview">Spatial Data Overview</InternalLink>
* <InternalLink path="spatial-tutorial">Spatial tutorial</InternalLink>
* <InternalLink path="spatial-indexes">Spatial indexes</InternalLink>
* <InternalLink path="architecture/glossary">Spatial and GIS Glossary of Terms</InternalLink>
* <InternalLink path="well-known-text">Well known text</InternalLink>
* <InternalLink path="well-known-binary">Well known binary</InternalLink>
* <InternalLink path="srid-4326">SRID 4326 - longitude and latitude</InternalLink>
* <InternalLink path="geoserver">Using GeoServer with CockroachDB</InternalLink>
