Djangocon: pair up django and web mapping - Mathieu Leplatre

Tags: djangocon, django

(One of the summaries of a talk at 2014 djangocon.eu)

Mathieu Leplatre (who works on makina corpus) says web mapping should be simple and google maps should become unusual.

Fundamentals of cartography

Projections and postgis. Cartography is based on location: longitude (x) and latitude (y). Normally you use decimal degrees: -180 to +180 longitude, -90 to +90 latitude. Well known from GPS tools.

Problem: the earth isn’t perfectly round. GPS uses the “WGS 84” reference ellipsoid to standardize the latitude and longitude.

Problem 2: you often have to show it on a flat map or flat display. So you have to project the 3D data on a 2D surface. Naturally this means compromises. Projections are for instance the mercator projection that google uses.

For more background, examples and especially illustrations, look at mapschool.io

Transformation often happens between “WGS 84” (srid=4326, that is the most-used one for storing it in databases) and google mercator for display (srid=3857). Those scary-looking “srid” numbers? Spatial reference ID. But don’t worry, those two numbers are almost always the default value for databases and tools.

Another fundamental concept is the data format. Basically you have either vector data (points, lines, polygons) or rasters (bitmap data).

Vector data is most often stored in a postgis database, which is a regular postgres database with the “postgis” extension loaded. The extension gives you:

  • New column types like point and linestring.

  • Geographical functions like distance, area, extents.

  • Special spatial indexes to speed up geographic queryies.

He showed some elaborate-looking SQL with geographic queries. (Django handles most of that for you behind the scenes, see later on in this summary).

Web mapping

Leaflet is a javascript library to show map layers in the browser. It places layers on a background layer and handles the interaction like zooming, panning and clicking.

There are alternatives to leaflet, like D3 (especially for cool data visualization) and openlayers3 (for the more elaborate use cases).

What’s shown is often a bunch of PNG raster images (“tiles”) in the google mercator projection. It can be “cold” data (pre-rendered tiles) or “warm” data (rendered live by some application).

Geojson is used for vector data. The problem is that it is quite verbose and can get quite big.

Django’s toolbox

Basically: from django.contrib.gis import .... Spatial model fields (point, line, polygon), GeoQuerySet for spatial queries, etc. The geo part of SQL is all handled for you, just like you’re used to with django.

Handy: the django-geojson add-on. Class based views for returning geojson. It helps out with generalization and approximation (which means less detail and fewer decimals in what you send over). It adds serialization (dumpdata/loaddata). Vector layer support.

Also recommended: django-leaflet with a quick {% leafletmap "djangocon" %} template tag. It includes the staticfiles assets. He showed an example template: yeah, it looked nice! There’s also django admin integration in case you want to use leaflet instead of openlayers in your admin.

Conclusion: anyone can do web mapping. Cartography is simple with the right tools. It is also hard because of performance, volume and data refresh rate (caching).

 
vanrees.org logo

About me

My name is Reinout van Rees and I work a lot with Python (programming language) and Django (website framework). I live in The Netherlands and I'm happily married to Annie van Rees-Kooiman.

Weblog feeds

Most of my website content is in my weblog. You can keep up to date by subscribing to the automatic feeds (for instance with Google reader):