This document introduces building a web GIS application using GeoDjango and Leaflet. It discusses prerequisites like Python, PostgreSQL with PostGIS, Django, and Leaflet. It describes using GDAL to generate Django models from spatial data and LayerMapping to load the data. It shows configuring the Django project and creating an application to handle the spatial data. Finally, it demonstrates serving the spatial data via GeoJSON to Leaflet maps in the web application's frontend.
2. PREAMBLE
• Why FOSS?
• Embracing FOSS role in GIS/GIT and education domains.
• Sustainable Development Goals (SDGs) and Geospatial technology.
joehene@gmail.com GIS Day 2016
3. PREREQUISITES
• Python with pip or easy_install.
• PostgreSQL with PostGIS support.
• Django, django-leaflet, django-geojson, psycopg2, gdal (installed into virtual environment
using pip or easy_install).
• Code editor (Notepad++ or SublimeText preferred)
joehene@gmail.com GIS Day 2016
4. WHAT IS DJANGO?
• Free an Open Source (FOSS) web application framework, written in Python programming
language.
• Django grew from real-world applications developed in Lawrence, Kansas City in 2003,
growing popular in 2005 and released publicly in 2005.
• 1.0 version released in 2008, current version ==1.10
joehene@gmail.com GIS Day 2016
5. DJANGO CONCEPT
• HTTP in, HTTP out
HTTP Request HTTP Response
URLs
VIEWS
Models
Templates
joehene@gmail.com GIS Day 2016
6. DJANGO MVC / MVT ARCHITECTURE
• Django is designed to enhance loose-coupling and strict separation between a web
application constituents promoting easy customization of web application part(s) without
affecting others.
• M – Model
• V –View
• C – Controller / T -Template
joehene@gmail.com GIS Day 2016
7. WHAT IS LEAFLET?
• LEAFLET is an Open Source JavaScript Library for interactive maps.
• Supports mobile mapping and can be extended through use of plugins.
• It takes advantage of HTML5 and CSS3 on browsers.
• Source code can be obtained from http://github.com/Leaflet/Leaflet
• Leaflet API has ‘Map’ as the central class and is used to create a map on a page and
manipulate it.
joehene@gmail.com GIS Day 2016
12. SETTINGS.PY
• DEBUG is a boolean that turns on/off the debug mode of the project. If set to
True, Django will display detailed error pages when an uncaught exception
is thrown by your application.
• ALLOWED_HOSTS is not applied while debug mode is on or when running tests
• INSTALLED_APPS is a setting you will have to edit in all projects.This setting tells Django which
applications are active for this site. By default, Django includes the following applications:
joehene@gmail.com GIS Day 2016
13. SETTINGS.PY (CONT…)
• django.contrib.admin:This is an administration site.
• django.contrib.auth:This is an authentication framework.
• django.contrib.contenttypes:This is a framework for content types.
• django.contrib.sessions:This is a session framework.
• django.contrib.messages:This is a messaging framework.
• django.contrib.staticfiles:This is a framework for managing static files.
• leaflet: Allows use of leaflet in our Django project.
• djgeojson: allows (de)serialization (Geo)django objects into GeoJSON.
• onyeshamap: This is application created for purposes of this project.
joehene@gmail.com GIS Day 2016
17. GDAL (LAYER MAPPING)
• Inspects the given OGR-compatible data source (e.g., a shapefile) and outputs a GeoDjango
model with the given model name. In this example: ./manage.py ogrinspect
onyeshamap/data/CCTV_locations.shp CCTV ---srid=32737 –mapping –multi
• Makes use of positional arguments which are
~ data_source – path to the data source
~ model_name – name of the model to be created
~ SRID – Spatial Reference Identifier
~ mapping - Generate mapping dictionary for use with `LayerMapping`
joehene@gmail.com GIS Day 2016
25. DEMO
• Performed in Ubuntu Linux Desktop 14.04LTS operating system
• DataVisualization in Q-GIS
• Django production server start
• Django-Leaflet web application showcase
joehene@gmail.com GIS Day 2016