Python geometry format conversion cheat-sheet


Posted on 2026-02-16, by Racum. Tags: Django GeoJSON GIS Python Shapely

If your GIS system is based on the Python/Django/DRF/PostGIS stack, it is very common to perform geometry format conversions between its different layers. This short article includes a cheat-sheet of the most common conversions needed.

Formats

  • GeoDjango Geometry Field: this is how Django exposes its geometry model fields; it is basically a thin wrapper over the C/C++ GEOS library's objects.
  • Shapely Geometry: Shapely is a feature-rich Python library used for in-memory geometry manipulation. It also uses GEOS underneath, but operates in a higher abstraction layer.
  • GeoJSON: this is a JSON-based format described by the RFC-7946, and it is very popular for payloads on web APIs.
  • GeoJSON-like Python dict: same as GeoJSON, but parsed as a Python dict.
  • WKT stands for “Well-Known Text”, it is a very simple text representation. The same specification also defines the WKB counterpart (“Well-Known Binary”), for direct binary transfer.

Conversions

Required imports:

import json
from shapely import from_wkb, from_wkt, from_geojson, to_geojson
from shapely.geometry import mapping, shape
from django.contrib.gis.geos import GEOSGeometry

Examples used in the table:

  • django_field: instance of django.contrib.gis.geos.polygon.Polygon
  • shapely_geo: instance of shapely.geometry.polygon.Polygon
  • geojson_str: instance of str
  • geojson_dict: instance of dict
  • wkt_str: instance of str

Snippets:

FromToSnippet
GeoDjangoShapelyfrom_wkb(django_field.wkb.tobytes())
GeoDjangoGeoJSON stringdjango_field.geojson
GeoDjangoGeoJSON dictjson.loads(django_field.geojson)
GeoDjangoWKTdjango_field.wkt
ShapelyGeoDjangoGEOSGeometry(memoryview(shapely_geo.wkb))
ShapelyGeoJSON stringto_geojson(shapely_geo)
ShapelyGeoJSON dictmapping(shapely_geo)
ShapelyWKTshapely_geo.wkt
GeoJSON stringGeoDjangoGEOSGeometry(geojson_str)
GeoJSON stringShapelyfrom_geojson(geojson_str)
GeoJSON stringGeoJSON dictjson.loads(geojson_str)
GeoJSON stringWKTfrom_geojson(geojson_str).wkt
GeoJSON dictGeoDjangoGEOSGeometry(json.dumps(geojson_dict))
GeoJSON dictShapelyshape(geojson_dict)
GeoJSON dictGeoJSON stringjson.dumps(geojson_dict)
GeoJSON dictWKTshape(geojson_dict).wkt
WKTGeoDjangoGEOSGeometry(wkt_str)
WKTShapelyfrom_wkt(wkt_str)
WKTGeoJSON stringto_geojson(from_wkt(wkt_str))
WKTGeoJSON dictmapping(from_wkt(wkt_str))

Please notice: the snippets listed above are the most performant conversion paths. You may find alternative snippets somewhere else, but those listed here, especially the two directions between GeoDjango and Shapely, are the fastest versions possible.