Slide 1

Slide 1 text

MAPS WITH DJANGO (AND POSTGIS) PAOLO MELCHIORRE ~ @pauloxnet

Slide 2

Slide 2 text

Paolo Melchiorre ~ @pauloxnet 2

Slide 3

Slide 3 text

Paolo Melchiorre ~ @pauloxnet @pauloxnet • CTO @ 20tab • Software engineer • Python developer • Django contributor • Hiking enthusiast Paolo Melchiorre 3 © 2022 Bartek Pawlik (CC BY-NC-SA) - DjangoCon US

Slide 4

Slide 4 text

Paolo Melchiorre ~ @pauloxnet 1.883 m 0 km 27 km 0 km 4 © 2020 Paolo Melchiorre (CC BY-SA) Adriatic Sea from the Italian Apennines

Slide 5

Slide 5 text

Paolo Melchiorre ~ @pauloxnet • Static or Dynamic • Interactive or view-only • Raster or Vector tiles • Spatial databases • Javascript library Web maps features 5

Slide 6

Slide 6 text

Paolo Melchiorre ~ @pauloxnet 6 Web mapping <… process of using the maps delivered by Geographic Information Systems (GIS) on the Internet …= —

Slide 7

Slide 7 text

Paolo Melchiorre ~ @pauloxnet django 7 © 1946 William Gottlieb (Public Domain) - Django Reinhardt at the Aquarium jazz club in New York

Slide 8

Slide 8 text

Paolo Melchiorre ~ @pauloxnet 8 Requirements $ python3.11 -m venv ~/.mymap $ . ~/.mymap/bin/activate $ python -m pip install django~=4.1 … Successfully installed … django-4.1.7 …

Slide 9

Slide 9 text

Paolo Melchiorre ~ @pauloxnet 9 Creating the 8mymap9 project $ cd ~/projects $ python -m django startproject mymap mymap/ ├── manage.py └── mymap ├── asgi.py ├── __init__.py ├── settings.py ├── urls.py └── wsgi.py

Slide 10

Slide 10 text

Paolo Melchiorre ~ @pauloxnet 10 Creating the 8markers9 app $ cd mymap $ python -m django startapp markers markers/ ├── admin.py ├── apps.py ├── __init__.py ├── migrations │ └── __init__.py ├── models.py ├── tests.py └── views.py

Slide 11

Slide 11 text

Paolo Melchiorre ~ @pauloxnet 11 Activating the 8markers9 app # mymap/mymap/settings.py INSTALLED_APPS = [ "django.contrib.admin", "django.contrib.auth", "django.contrib.contenttypes", "django.contrib.sessions", "django.contrib.messages", "django.contrib.staticfiles", "markers", ]

Slide 12

Slide 12 text

Paolo Melchiorre ~ @pauloxnet 12 Adding a template view # mymap/markers/views.py from django.views.generic import TemplateView class MarkersMapView(TemplateView): template_name = "map.html"

Slide 13

Slide 13 text

Paolo Melchiorre ~ @pauloxnet 13 Adding the 8map9 template Markers Map

Slide 14

Slide 14 text

Paolo Melchiorre ~ @pauloxnet 14 Adding 8markers9 urls # mymap/markers/urls.py from django.urls import path from markers.views import MarkersMapView app_name = "markers" urlpatterns = [ path("map/", MarkersMapView.as_view()), ]

Slide 15

Slide 15 text

Paolo Melchiorre ~ @pauloxnet 15 Updating 8mymap9 urls # mymap/mymap/urls.py from django.contrib import admin from django.urls import include, path urlpatterns = [ path("admin/", admin.site.urls), path("markers/", include("markers.urls")), ]

Slide 16

Slide 16 text

Paolo Melchiorre ~ @pauloxnet 2.068 m 0 km 27 km 2 km 16 © 2020 Paolo Melchiorre (CC BY-SA) Sunrise panorama on the Blockhaus

Slide 17

Slide 17 text

Paolo Melchiorre ~ @pauloxnet • JavaScript library for maps • Free Software • Desktop & Mobile friendly • Light (~42 KB of gzipped JS) • Well documented Leaflet 17

Slide 18

Slide 18 text

Paolo Melchiorre ~ @pauloxnet 18 Updating the 8map9 template - header {% load static %}

Slide 19

Slide 19 text

Paolo Melchiorre ~ @pauloxnet 19 Updating the 8map9 template - body {% load static %}

Slide 20

Slide 20 text

Paolo Melchiorre ~ @pauloxnet 20 Adding the 8map9 CSS /* mymap/markers/static/map.css */ html, body { height: 100%; margin: 0; } #map { height: 100%; width: 100%; }

Slide 21

Slide 21 text

Paolo Melchiorre ~ @pauloxnet 21 Adding the 8map9 JavaScript // mymap/markers/static/map.js const osm = "https://www.osm.org/copyright"; const copy = `© OpenStreetMap`; const url = "https://{s}.tile.osm.org/{z}/{x}/{y}.png"; const layer = L.tileLayer(url, { attribution: copy }); const map = L.map("map", { layers: [layer] }); map.fitWorld();

Slide 22

Slide 22 text

Paolo Melchiorre ~ @pauloxnet 22 Show the empty web map $ python -m manage runserver $ python -m webbrowser -t localhost:8000/markers/map

Slide 23

Slide 23 text

Paolo Melchiorre ~ @pauloxnet 23

Slide 24

Slide 24 text

Paolo Melchiorre ~ @pauloxnet 2.117 m 0 km 27 km 6 km 24 © 2020 Paolo Melchiorre (CC BY-SA) Trail signs near the Sella Acquaviva fountain

Slide 25

Slide 25 text

Paolo Melchiorre ~ @pauloxnet 25 GeoDjango django.contrib.gis (v1.0 ~2008) Fields, backends, queries, admin, … Spatialite backend (v1.1 ~2009) Multiple backends (v1.2 ~2010) OpenLayers-based widgets (v1.6 ~2013) GeoJSON serializer (v1.8 ~2015) GeoIP2 Geolocation (v1.9 ~2016)

Slide 26

Slide 26 text

Paolo Melchiorre ~ @pauloxnet • OSGeo library • Free Software • Read/Write geospatial data • Raster/Vector formats • Command line interface GDAL 26

Slide 27

Slide 27 text

Paolo Melchiorre ~ @pauloxnet 27 Installing GDAL $ sudo apt install gdal-bin

Slide 28

Slide 28 text

Paolo Melchiorre ~ @pauloxnet 28 Activating GeoDjango # mymap/mymap/settings.py INSTALLED_APPS = [ "django.contrib.admin", "django.contrib.auth", "django.contrib.contenttypes", "django.contrib.sessions", "django.contrib.messages", "django.contrib.staticfiles", "django.contrib.gis", "markers", ]

Slide 29

Slide 29 text

Paolo Melchiorre ~ @pauloxnet • SQLite spatial extension • Vector geodatabase functions • Free Software • Simple architecture • Single file SpatiaLite 29

Slide 30

Slide 30 text

Paolo Melchiorre ~ @pauloxnet 30 Installing SpatiaLite $ sudo apt install libsqlite3-mod-spatialite

Slide 31

Slide 31 text

Paolo Melchiorre ~ @pauloxnet 31 Activating SpatiaLite # mymap/settings.py DATABASES = {"default": { "ENGINE": "django.contrib.gis.db.backends.spatialite", "NAME": BASE_DIR / "db.sqlite3", }}

Slide 32

Slide 32 text

Paolo Melchiorre ~ @pauloxnet 32 Adding the Marker model # mymap/markers/models.py from django.contrib.gis.db import models class Marker(models.Model): name = models.CharField(max_length=255) location = models.PointField() def __str__(self): return self.name

Slide 33

Slide 33 text

Paolo Melchiorre ~ @pauloxnet 33 Updating the database $ python -m manage makemigrations $ python -m manage migrate

Slide 34

Slide 34 text

Paolo Melchiorre ~ @pauloxnet 34 Adding the Marker admin # mymap/markers/admin.py from django.contrib.gis import admin from markers.models import Marker @admin.register(Marker) class MarkerAdmin(admin.GISModelAdmin): list_display = ("name", "location")

Slide 35

Slide 35 text

Paolo Melchiorre ~ @pauloxnet 35 Adding some markers $ python -m manage createsuperuser $ python -m manage runserver $ python -m webbrowser -t http://localhost:8000/admin

Slide 36

Slide 36 text

Paolo Melchiorre ~ @pauloxnet 36

Slide 37

Slide 37 text

Paolo Melchiorre ~ @pauloxnet 37 Updating the view - part 1 # markers/views.py import json from django.core.serializers import serialize from django.views.generic.base import TemplateView from markers.models import Marker class MarkersMapView(TemplateView): template_name = "map.html" def get_context_data(self, **kwargs): ...

Slide 38

Slide 38 text

Paolo Melchiorre ~ @pauloxnet 38 Updating the view - part 2 # markers/views.py class MarkersMapView(TemplateView): ... def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) markers = Marker.objects.all() geojson = serialize("geojson", markers) context["markers"] = json.loads(geojson) return context

Slide 39

Slide 39 text

Paolo Melchiorre ~ @pauloxnet 39 Generated GeoJSON {"type": "FeatureCollection", "crs": { "type": "name", "properties": {"name": "EPSG:4326"}}, "features": [{ "type": "Feature", "properties": { "name": "Monte Amaro", "pk": "1" }, "geometry": { "type": "Point", "coordinates": [14.085, 42.086] } }] }

Slide 40

Slide 40 text

Paolo Melchiorre ~ @pauloxnet 40 Inserting markers in the template {% load static %} {{ markers|json_script:"markers-data" }}

Slide 41

Slide 41 text

Paolo Melchiorre ~ @pauloxnet 41 Rendering all markers in the map // markers/static/map.js // ... // map.fitWorld(); const data = document.getElementById("markers-data"); let feature = L.geoJSON(JSON.parse(data.textContent)) .bindPopup(function (layer) { return layer.feature.properties.name; }) .addTo(map); map.fitBounds(feature.getBounds());

Slide 42

Slide 42 text

Paolo Melchiorre ~ @pauloxnet 42 Show the populated web map $ python -m manage runserver $ python -m webbrowser -t localhost:8000/markers/map

Slide 43

Slide 43 text

Paolo Melchiorre ~ @pauloxnet 43

Slide 44

Slide 44 text

Paolo Melchiorre ~ @pauloxnet 2.450 m 0 km 27 km 7 km 44 © 2020 Paolo Melchiorre (CC BY-SA) Trail signs near the Fusco bivouac

Slide 45

Slide 45 text

Paolo Melchiorre ~ @pauloxnet • PostgreSQL extension • Best* GeoDjango backend • Spatial data types • Spatial indexing • Spatial functions PostGIS 45

Slide 46

Slide 46 text

Paolo Melchiorre ~ @pauloxnet 46 GeoDjango Compatibility Table ● Spatial Lookups (18/31) ● Database functions (15/32) ● Aggregate Functions (0/5) MariaDB 49% ● Spatial Lookups (31/31) ● Database functions (32/32) ● Aggregate Functions (5/5) PostGIS 100% ● Spatial Lookups (22/31) ● Database functions (30/32) ● Aggregate Functions (4/5) Spatialite 82% ● Spatial Lookups (18/31) ● Database functions (16/32) ● Aggregate Functions (0/5) MySQL 50%

Slide 47

Slide 47 text

Paolo Melchiorre ~ @pauloxnet 47 Installing PostgreSQL C client library $ sudo apt install libpq5

Slide 48

Slide 48 text

Paolo Melchiorre ~ @pauloxnet 48 Requirements # requirements.txt django-filter~=22.1.0 djangorestframework-gis~=1.0.0 djangorestframework~=3.14.0 django~=4.1.0 psycopg2-binary~=2.9.0

Slide 49

Slide 49 text

Paolo Melchiorre ~ @pauloxnet 49 Installing requirements $ python -m pip install -r requirements.txt

Slide 50

Slide 50 text

Paolo Melchiorre ~ @pauloxnet 50 Activating PostGIS # mymap/mymap/settings.py DATABASES = {"default": { "ENGINE": "django.contrib.gis.db.backends.postgis", "HOST": "database", "NAME": "mymap", "PASSWORD": "password", "PORT": 5432, "USER": "postgres", }}

Slide 51

Slide 51 text

Paolo Melchiorre ~ @pauloxnet Applying migrations to PostgreSQL 51 $ python -m manage migrate

Slide 52

Slide 52 text

Paolo Melchiorre ~ @pauloxnet 52 Activating Django REST Framework # mymap/settings.py INSTALLED_APPS = [ ... "rest_framework", "rest_framework_gis", "markers", ]

Slide 53

Slide 53 text

Paolo Melchiorre ~ @pauloxnet 53 Adding the Marker serializer # mymap/markers/serializers.py from rest_framework_gis import serializers as srlzs from markers.models import Marker class MarkerSerializer(srlzs.GeoFeatureModelSerializer): class Meta: fields = ("id", "name") geo_field = "location" model = Marker

Slide 54

Slide 54 text

Paolo Melchiorre ~ @pauloxnet 54 Adding the Marker viewset # mymap/markers/viewsets.py from rest_framework import viewsets from rest_framework_gis import filters from markers.models import Marker from markers.serializers import MarkerSerializer class MarkerViewSet(viewsets.ReadOnlyModelViewSet): bbox_filter_field = "location" filter_backends = (filters.InBBoxFilter,) queryset = Marker.objects.all() serializer_class = MarkerSerializer

Slide 55

Slide 55 text

Paolo Melchiorre ~ @pauloxnet 55 Adding API 8markers9 urls # mymap/markers/api.py from rest_framework import routers from markers.viewsets import MarkerViewSet router = routers.DefaultRouter() router.register(r"markers", MarkerViewSet) urlpatterns = router.urls

Slide 56

Slide 56 text

Paolo Melchiorre ~ @pauloxnet 56 Updating 8mymap9 urls # mymap/mymap/urls.py from django.contrib import admin from django.urls import include, path urlpatterns = [ path("admin/", admin.site.urls), path("api/", include("markers.api")), path("markers/", include("markers.urls")), ]

Slide 57

Slide 57 text

Paolo Melchiorre ~ @pauloxnet 57 Trying to locate the user // mymap/markers/static/map.js // ... map.locate() .on("locationfound", (e) => map.setView(e.latlng, 8)) .on("locationerror", () => map.setView([0, 0], 5)); // ...

Slide 58

Slide 58 text

Paolo Melchiorre ~ @pauloxnet 58 Fetch required markers // mymap/markers/static/map.js // ... async function load_markers() { const bounds = map.getBounds().toBBoxString(); const markers_url = `/api/markers/?in_bbox=${bounds}`; const response = await fetch(markers_url); const geojson = await response.json(); return geojson; } // ...

Slide 59

Slide 59 text

Paolo Melchiorre ~ @pauloxnet 59 Rendering markers incrementally // mymap/markers/static/map.js // ... async function render_markers() { const markers = await load_markers(); L.geoJSON(markers) .bindPopup((layer) => layer.feature.properties.name) .addTo(map); } map.on("moveend", render_markers);

Slide 60

Slide 60 text

Paolo Melchiorre ~ @pauloxnet 60

Slide 61

Slide 61 text

Paolo Melchiorre ~ @pauloxnet 61 Demo

Slide 62

Slide 62 text

Paolo Melchiorre ~ @pauloxnet 2.793 m 0 km 27 km 14 km 62 © 2020 Paolo Melchiorre (CC BY-SA) Summit of Monte Amaro (2.793 meters)

Slide 63

Slide 63 text

Paolo Melchiorre ~ @pauloxnet 63 What9s next • Markers customization • Relational filtering • Clustering frontend/backend • Geocoding services • …

Slide 64

Slide 64 text

Paolo Melchiorre ~ @pauloxnet 64 Tips • docs in djangoproject.com • details in postgis.net • source code in github.com • questions in gis.stackexchange.com

Slide 65

Slide 65 text

Paolo Melchiorre ~ @pauloxnet 65 License CC BY-SA 4.0 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Slide 66

Slide 66 text

Paolo Melchiorre ~ @pauloxnet @20tab 20tab 20tab [email protected] 20tab.com 66

Slide 67

Slide 67 text

Paolo Melchiorre ~ @pauloxnet @pauloxnet paolomelchiorre pauloxnet www.paulox.net 67 @[email protected]