Python geometry format conversion cheat-sheet
Posted on 2026-02-16, by Racum.
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.Polygonshapely_geo: instance of shapely.geometry.polygon.Polygongeojson_str: instance of strgeojson_dict: instance of dictwkt_str: instance of str
Snippets:
| From | To | Snippet |
|---|---|---|
| GeoDjango | Shapely | from_wkb(django_field.wkb.tobytes()) |
| GeoDjango | GeoJSON string | django_field.geojson |
| GeoDjango | GeoJSON dict | json.loads(django_field.geojson) |
| GeoDjango | WKT | django_field.wkt |
| Shapely | GeoDjango | GEOSGeometry(memoryview(shapely_geo.wkb)) |
| Shapely | GeoJSON string | to_geojson(shapely_geo) |
| Shapely | GeoJSON dict | mapping(shapely_geo) |
| Shapely | WKT | shapely_geo.wkt |
| GeoJSON string | GeoDjango | GEOSGeometry(geojson_str) |
| GeoJSON string | Shapely | from_geojson(geojson_str) |
| GeoJSON string | GeoJSON dict | json.loads(geojson_str) |
| GeoJSON string | WKT | from_geojson(geojson_str).wkt |
| GeoJSON dict | GeoDjango | GEOSGeometry(json.dumps(geojson_dict)) |
| GeoJSON dict | Shapely | shape(geojson_dict) |
| GeoJSON dict | GeoJSON string | json.dumps(geojson_dict) |
| GeoJSON dict | WKT | shape(geojson_dict).wkt |
| WKT | GeoDjango | GEOSGeometry(wkt_str) |
| WKT | Shapely | from_wkt(wkt_str) |
| WKT | GeoJSON string | to_geojson(from_wkt(wkt_str)) |
| WKT | GeoJSON dict | mapping(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.