team up django and web mapping - djangocon europe 2014

37
Team up Django and Web mapping DjangoCon Europe 2014 Mathieu Leplatre @leplatrem www.makina-corpus.com

Upload: makina-corpus

Post on 08-May-2015

717 views

Category:

Technology


6 download

DESCRIPTION

● Web mapping should be simple ● Google Maps should become unusual ● Demystify cartography !

TRANSCRIPT

Page 1: Team up Django and Web mapping - DjangoCon Europe 2014

Team up Django andWeb mapping

DjangoCon Europe 2014

Mathieu [email protected]

Page 2: Team up Django and Web mapping - DjangoCon Europe 2014

Main goals...

● Web mapping should be simple● Google Maps should become unusual● Demystify cartography !

Page 3: Team up Django and Web mapping - DjangoCon Europe 2014

Main focus...

● Fundamentals of cartography (projections, PostGIS)● Web mapping (GeoJSON, Leaflet)● Django toolbox (a.k.a. GeoDjango)

Page 4: Team up Django and Web mapping - DjangoCon Europe 2014

Fundamentals of cartography

Page 5: Team up Django and Web mapping - DjangoCon Europe 2014

Coordinates : position on Earth

● Longitude (x) – Latitude (y)● Decimal degrees (-180 +180, -90 +90)→ →● GPS

Page 6: Team up Django and Web mapping - DjangoCon Europe 2014

Geodesy : shape of the Earth

Ellipsoid (GPS, WGS 84)

Illustration: http://mapschool.io

Page 7: Team up Django and Web mapping - DjangoCon Europe 2014

Projections

● Equations (lat/lng pixels)↔● Representation on a plane ( compromises)→● Spatial reference transformation

Illustration: http://mapschool.io

Page 8: Team up Django and Web mapping - DjangoCon Europe 2014

Spatial References

● Coordinate system (cartesian)● Units (degrees, meters)● Main axis (equator, Greenwich)● Ellipsoid, geoid (WGS 84)

● Conical, cylindrical, conformal, ...

● …

Store  WGS 84→ (GPS, srid=4326)Display  →Mercator (Google Maps, srid=3857)

Page 9: Team up Django and Web mapping - DjangoCon Europe 2014

Vector data

● Point (x, y, z)● Line (ordered list of points)● Polygon (enveloppe + holes)

Illustration: http://mapschool.io

Page 10: Team up Django and Web mapping - DjangoCon Europe 2014

Raster data

● ~ Images● Map backgrounds (satellite, plan, …)● Atimetry data (DEM)

Illustration: http://mapschool.io

Page 11: Team up Django and Web mapping - DjangoCon Europe 2014

A PostGIS database

● Column types (Point, LineString, Polygon, …Raster...)● Geographic functions (distance, area, extents ...)● Spatial Indexes (rectangles trees...)

$ sudo apt-get install postgis

$ psql -d mydatabase

> CREATE EXTENSION postgis;

Page 12: Team up Django and Web mapping - DjangoCon Europe 2014

Examples (1/2)

CREATE TABLE island (

label VARCHAR(128),code INTEGER,

geom geometry(Polygon, 4326))

Page 13: Team up Django and Web mapping - DjangoCon Europe 2014

Examples (1/2)

CREATE TABLE island (

label VARCHAR(128),code INTEGER,

geom geometry(Polygon, 4326))

Usual table

UsualcolumnsVector

geometrycolumn

Geometry type SpatialReference

Page 14: Team up Django and Web mapping - DjangoCon Europe 2014

Examples (2/2)

SELECT room.idFROM room, islandWHERE ST_Intersects(room.geom, island.geom)

Spatial join

INSERT INTO room (geom) VALUES ('SRID=4326;POINT(3.12, 43.1)'::geometry

)

EWKT format PostgreSQL cast

Page 15: Team up Django and Web mapping - DjangoCon Europe 2014

Web mapping

Page 16: Team up Django and Web mapping - DjangoCon Europe 2014

Web map anatomy... ● JavaScript + DOM● Initialization of a <div>● « Layers »

Page 17: Team up Django and Web mapping - DjangoCon Europe 2014

Web map anatomy... ● JavaScript + DOM● Initialization of a <div>● « Layers »

● <img> (Backgrounds)● Vector SVG→● Lat/Long pixels → (CSS)● DOM Events (interactions)

Page 18: Team up Django and Web mapping - DjangoCon Europe 2014

Leaflet example

<script src="leaflet.js"></script><link rel="stylesheet" href="leaflet.css" />

<div id="simplemap"></div>

<script> var map = L.map('simplemap') .fitWorld(); L.tileLayer('http://tile.osm.org/{z}/{x}/{y}.png') .addTo(map);

L.marker([43.07, -5.78]) .addTo(map);</script>

http://leafletjs.com

Page 19: Team up Django and Web mapping - DjangoCon Europe 2014
Page 20: Team up Django and Web mapping - DjangoCon Europe 2014

See also... D3● SVG● Cool dataviz● Exotic map projections

OpenLayers 3● Canvas● GIS-oriented (advanced use-cases)

Page 21: Team up Django and Web mapping - DjangoCon Europe 2014

Formats (1/2)

● Raster images (PNG ou JPG)● z/x/y.png● Projection 3857 (Mercator, Google Maps)

→ Cold data (tile provider)

… → or warm (rendering engine)

Page 22: Team up Django and Web mapping - DjangoCon Europe 2014

Formats (2/2)

● GeoJSON for vector data● Projection 4326 (GPS, WGS84)

● Interactive ?● Volume ? ● Frequency ?

→ Dynamic (from database)

SELECT ST_AsGeoJSON(geom)FROM ...

Page 23: Team up Django and Web mapping - DjangoCon Europe 2014

Nothing new...Web Server Browser

P o s t G I S D j a n g o

R e n d e r i n gE n g i n e

V e c t o r o v e r l a y

R a s t e ro v e r l a y

R a s t e rb a c k g r o u n d

I m a g e s

z/x/y.png

GeoJSON

z/x/y.png, UTFGrid, ...

Page 24: Team up Django and Web mapping - DjangoCon Europe 2014

GeoDjango

Page 25: Team up Django and Web mapping - DjangoCon Europe 2014

Django Ecosystem for Cartography

from django.contrib.gis import ...●Models fields (syncdb)●GeoQuerySet (spatial queries) ●Form fields (Django 1.6)●System libraries deps (GEOS, GDAL)

●Widgets & AdminSite (OpenLayers 2)

Page 26: Team up Django and Web mapping - DjangoCon Europe 2014

Examples (1/2)

from django.contrib.gis.db import (models as gismodels

)

class Island(gismodels.Model):

label = models.CharField(max_length=128),code = models.IntegerField(),

geom = gismodels.PolygonField(srid=4326)

objects = gismodels.GeoManager()

Page 27: Team up Django and Web mapping - DjangoCon Europe 2014

Examples (2/2)

from django.contrib.gis.geos import Polygon

embiez = Island(label='Embiez', geom=Polygon(((0.34, 0.44), (0.36, 0.42), …)))

~

from django.contrib.gis.geos import fromstr

myroom = fromstr('SRID=4326;POINT(3.12, 43.1)')Island.objects.filter(geom__intersects=myroom)

Page 28: Team up Django and Web mapping - DjangoCon Europe 2014

● Views (Class-based)

● Generalization & approximation (less details and decimals)● Serialization (dumpdata, loaddata)● Model fields (text, no GIS !)● Vector tiles (huge datasets)

django-geojson

from djgeojson.views import GeoJSONLayerView

urlpatterns = patterns('',url(r'^buildings.geojson$',

GeoJSONLayerView.as_view(model=Building)))

Page 29: Team up Django and Web mapping - DjangoCon Europe 2014

django-leaflet

●Quick-start kit {% leafletmap "djangocon" %}●Assets (collecstatic)●Easy plugins integration●Global configuration (settings.py, plugins, ...)●Leaflet projections machinery (reprojection)

Page 30: Team up Django and Web mapping - DjangoCon Europe 2014

django-leaflet (view) {% load leaflet_tags %} ... {% leaflet_js %} {% leaflet_css %}</head><body>

{% leafletmap "buildingmaps" callback="initMap" %}

<script type="text/javascript">

function initMap(map) {

// Ajax Load $.getJSON("{% url app:layer %}", function (data) {

// Add GeoJSON to the map L.geoJSON(data).addTo(map); }); }

GeoJSONview

Page 31: Team up Django and Web mapping - DjangoCon Europe 2014

LAYER

Page 32: Team up Django and Web mapping - DjangoCon Europe 2014

django-leaflet (edit)

Forms●Fields (Django 1.6+ API)●Widgets (hidden <textarea> + Leaflet.draw)class BuildingForm(forms.ModelForm):

class Meta:model = Buildingwidgets = {'geom': LeafletWidget()}

fields = ('code', 'geom')

●Leaflet AdminSiteadmin.site.register(Building, LeafletGeoAdmin)

Page 33: Team up Django and Web mapping - DjangoCon Europe 2014

SCREENSHOT FORM

Page 34: Team up Django and Web mapping - DjangoCon Europe 2014

Conclusion

Page 35: Team up Django and Web mapping - DjangoCon Europe 2014

Anyone can do Web mapping...

Cartography is simple.● Just some special data types● Follows Django conventions● Few lines of code

Illustration: Chef Gusteau, Pixar Inc.

Page 36: Team up Django and Web mapping - DjangoCon Europe 2014

… with appropriate tools and strategies !

Cartography is hard.● Performance (Web)● Volume (precision)● Data refresh rate (caching)● User stories too far from data (modeling)

Illustration: Anton Ego, Pixar Inc.

Page 37: Team up Django and Web mapping - DjangoCon Europe 2014

Makina Corpus - Free Software | Cartography | Mobile

Questions ?...and answers !

PostGIS - Leaflet – JavaScript

http://makina-corpus.com