Reader small image

You're reading from  Mastering Geospatial Analysis with Python

Product typeBook
Published inApr 2018
Reading LevelBeginner
PublisherPackt
ISBN-139781788293334
Edition1st Edition
Languages
Right arrow
Authors (3):
Silas Toms
Silas Toms
author image
Silas Toms

Silas Toms is a long-time geospatial professional and author who has previously published ArcPy and ArcGIS and Mastering Geospatial Analysis with Python. His career highlights include developing the real-time common operational picture used at Super Bowl 50, building geospatial software for autonomous cars, designing computer vision for next-gen insurance, and developing mapping systems for Zillow. He now works at Volta Charging, predicting the future of electric vehicle adoption and electric charging infrastructure.
Read more about Silas Toms

Paul Crickard
Paul Crickard
author image
Paul Crickard

Paul Crickard authored a book on the Leaflet JavaScript module. He has been programming for over 15 years and has focused on GIS and geospatial programming for 7 years. He spent 3 years working as a planner at an architecture firm, where he combined GIS with Building Information Modeling (BIM) and CAD. Currently, he is the CIO at the 2nd Judicial District Attorney's Office in New Mexico.
Read more about Paul Crickard

Eric van Rees
Eric van Rees
author image
Eric van Rees

Eric van Rees was first introduced to Geographical Information Systems (GIS) when studying Human Geography in the Netherlands. For 9 years, he was the editor-in-chief of GeoInformatics, an international GIS, surveying, and mapping publication and a contributing editor of GIS Magazine. During that tenure, he visited many geospatial user conferences, trade fairs, and industry meetings. He focuses on producing technical content, such as software tutorials, tech blogs, and innovative new use cases in the mapping industry.
Read more about Eric van Rees

View More author details
Right arrow

Chapter 12. GeoDjango

The Django Python web framework was made available in 2005 and has been steadily supported and improved throughout the years. One major improvement was additional support for spatial data types and queries. This effort produced GeoDjango, allowing Django to support geospatial database models and web views that utilize geospatial queries.

GeoDjango is now a standard Django component, which can be activated using a specific configuration. In December 2017, Django 2 was released as the new long-term support version. It currently supports Python 3.4, 3.5, and 3.6.

In this chapter, we will learn about the following:

  • Installation and configuration of Django and GeoDjango
  • Django admin panel functionality, including map editing
  • How to load shapefiles into database tables using LayerMapping
  • GeoDjango queries
  • Django URL patterns
  • Django views

Installing and configuring Django and GeoDjango


Django, compared to Flask, is a batteries-included framework. It includes modules that allow for database backend support, without requiring a separate database code package (unlike Flask, which relies on SQLAlchemy). Django also includes an admin panel that allows for easy data editing and management through a web interface. This means fewer modules are installed and more code is included to handle database interactions and web processing.

There are some major differences between Flask and Django. Django separates URLs from views and models in a more structured manner than Flask. Django also uses Python classes for databases tables, but it has built-in database support. For geospatial databases, no extra module is required. Django also supports geometry columns in a wider range of databases, though PostgreSQL and PostGIS are used the most often. 

Like many Python 3 modules, Django development is geared towards Linux development environments...

Creating the application


This application will perform geospatial analysis using the geometry fields of database tables. To make this possible, we have to create and populate the database tables using shapefiles and a built-in method called LayerMapping.

The completed application will need URL pattern matching to link URLs with the views that will process the requests and return the response. Templates will be used to pass processed data to the browser. Views will be written to be able to handle both POST and GET requests and to redirect to other views.

Now that GeoDjango is configured, the NBA Arenas application can be created using the Django project management script called manage.py.

manage.py

The script manage.py performs a number of jobs to help set up and manage the project. For testing purposes, it can create a local web server (using runserver as the argument); it manages database schema migrations, generating tables from data models (using makemigration and migrate); it even has a...

Summary


Django, with its batteries-included philosophy, creates complete applications with very few outside libraries required. This application performs data management and data analysis using only the Django built-in tools and the GDAL/OGR library. Enabling the GeoDjango functionality is a relatively seamless experience because it is an integral part of the Django project.

Creating web applications with Django allows for a lot of instant functionality, including the administrative panel. The LayerMapping makes it easy to import data from shapefiles. The ORM model makes it easy to perform geospatial filters or queries. The templating system makes it easy to add web maps as well as location intelligence to a website.

In the next chapter, we will use a Python web framework to create a geospatial REST API. This API will accept requests and return JSON encoded data representing geospatial features. 

 

 

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering Geospatial Analysis with Python
Published in: Apr 2018Publisher: PacktISBN-13: 9781788293334
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (3)

author image
Silas Toms

Silas Toms is a long-time geospatial professional and author who has previously published ArcPy and ArcGIS and Mastering Geospatial Analysis with Python. His career highlights include developing the real-time common operational picture used at Super Bowl 50, building geospatial software for autonomous cars, designing computer vision for next-gen insurance, and developing mapping systems for Zillow. He now works at Volta Charging, predicting the future of electric vehicle adoption and electric charging infrastructure.
Read more about Silas Toms

author image
Paul Crickard

Paul Crickard authored a book on the Leaflet JavaScript module. He has been programming for over 15 years and has focused on GIS and geospatial programming for 7 years. He spent 3 years working as a planner at an architecture firm, where he combined GIS with Building Information Modeling (BIM) and CAD. Currently, he is the CIO at the 2nd Judicial District Attorney's Office in New Mexico.
Read more about Paul Crickard

author image
Eric van Rees

Eric van Rees was first introduced to Geographical Information Systems (GIS) when studying Human Geography in the Netherlands. For 9 years, he was the editor-in-chief of GeoInformatics, an international GIS, surveying, and mapping publication and a contributing editor of GIS Magazine. During that tenure, he visited many geospatial user conferences, trade fairs, and industry meetings. He focuses on producing technical content, such as software tutorials, tech blogs, and innovative new use cases in the mapping industry.
Read more about Eric van Rees