shapely.to_geojson#

to_geojson(geometry, indent=None, **kwargs)#

Converts to the GeoJSON representation of a Geometry.

Note

‘to_geojson’ requires at least GEOS 3.10.0.

The GeoJSON format is defined in the RFC 7946. NaN (not-a-number) coordinates will be written as ‘null’.

The following are currently unsupported:

  • Geometries of type LINEARRING: these are output as ‘null’.

  • Three-dimensional geometries: the third dimension is ignored.

Parameters
geometrystr, bytes or array_like
indentint, optional

If indent is a non-negative integer, then GeoJSON will be formatted. An indent level of 0 will only insert newlines. None (the default) selects the most compact representation.

**kwargs

For other keyword-only arguments, see the NumPy ufunc docs.

Examples

>>> from shapely import Point
>>> point = Point(1, 1)
>>> to_geojson(point)
'{"type":"Point","coordinates":[1.0,1.0]}'
>>> print(to_geojson(point, indent=2))
{
  "type": "Point",
  "coordinates": [
      1.0,
      1.0
  ]
}