shapely.to_geojson

Contents

shapely.to_geojson#

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

Convert to the GeoJSON representation of a Geometry.

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.

Note that to_geojson does not enforce the winding order of polygons to be compliant with the RFC 7946 GeoJSON specification. If you want this, you can first order exterior rings counterclockwise, and interior rings (holes) clockwise with shapely.orient_polygons().

Parameters:
geometrystr, bytes or array_like

Geometry or geometries to convert to GeoJSON.

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

See NumPy ufunc docs for other keyword arguments.

See also

shapely.geometry.mapping

Return a GeoJSON-like mapping of a Geometry or object which implements __geo_interface__.

Examples

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