DjangoCon Europe 2019: Maps with GeoDjango, PostGIS and Leaflet
Writeup of the DjangoCon Europe 2019 talk »Maps with GeoDjango, PostGIS and Leaflet« by Paolo Melchiorre
Paolo Melchiorre: A Python developer who contributes to the Django project and gives talks at tech conferences.
Web map
Web maps are maps on websites, delivered by a GIS system. It can be static or interactive, and use raster or vector tiles. The data is stored in spatial databases, and then displayed using javascript
GeoDjango
django.contrib.gis
provides a geographic framework with spatial field types, spatial ORM queries, Admin support, and
support for four database backends.
PostGIS
PostGIS is the GIS backend for Postgres, and the most complete GeoDjango backend with spatial indexing and functions.
Leaflet
Leaflet is a JavaScript library for maps, smaller than 40kB of gzip'd JS, is desktop and mobile friendly, open source, and performs very well.
Usage
Install packages, and configure settings by adding django.contrib.gis
to the installed apps, and the proper
postgis
as a database. Next up, generate an empty migration, and use the CreateExtension
function to integrate
PostGIS. You can also easily modify the admin to display a default location on the map for GIS fields.
from django.db import models from django.contib.gis.db.models import PointField from django.contib.gis.admin import OSMGeoAdmin class Entry(models.Model) point = PointField() @admin.register(Entry) class EntryAdmin(OSMGeoAdmin): default_latitude = …
The JavaScript integration is easy to do with leaflet
var m = L.map('m').setView([x, y], zoomLevel); L.tileLayer('//{s}.tile.osm.org/{z}/{x}/{y}.png').addTo(m);
Additional tooling
djangorestframework-gis
provides GIS support to Django Rest Framework via a GeoFeatureModelSerializer
and
filtering with django-filter
via InBBoxFilter
.