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 11. Flask and GeoAlchemy2

Python has always had strong internet capabilities. The standard library includes models for HTTP processing, STMP messages, and URL requests. Thousands of third-party modules have been written to extend or improve the built-in web functionality. Over time, a few modules coalesced into Python web frameworks—code libraries written to manage the creation and maintenance of complex and dynamic websites.

To better understand how to use a Python web framework and how to add geospatial capabilities, we'll implement the Flask Model View Controller (MVC) framework. A pure Python web framework, Flask can be combined with SQLAlchemy, GeoAlchemy2, and the Jinja2 HTML template system to create geospatially-enabled web pages.

In this chapter, you will learn about:

  • The Flask web framework
  • SQLAlchemy database management
  • GeoAlchemy2
  • Connecting to PostGIS using object-relational mapping (ORM)
  • The Jinja2 web page template system

Flask and its component modules


Flask, as opposed to Django and GeoDjango (covered in Chapter 12, GeoDjango), does not include batteries. Instead, it allows a number of supporting modules to be installed as needed. This gives more freedom to you as the programmer, but it also makes it necessary to install the required components separately.

I've chosen some modules for this chapter that will allow us to create a Flask application with a geospatial component. The following sections will detail how to set up, install, and utilize these modules to generate a website, using a demonstration site with a PostGIS database backend (as covered in Chapter 7, Geoprocessing with Geodatabases) and the ability to perform spatial queries through a web-based interface.

Setup

A number of important Python modules must be in place to ensure that the Flask application and its connection to the PostgreSQL and PostGIS database components, will run as required. These modules will be downloaded and installed using...

Summary


In this chapter, we learned how to use the Flask MVC web framework and some of the available component modules that add extra functionality. These modules include the SQLAlchemy ORM, the GeoAlchemy2 ORM for geospatial queries, WTForms for processing of web data, and the Jinja2 templating system for creating web page templates. We created database tables, added data tables and tables relationships, and created a web application that utilized geospatial and relationship queries to generate dynamic web pages.

A fun challenge, building on the code reviewed here, would be to explore adding editing capabilities to the Arena application, allowing the user to move the arenas into their correct location if the data is out of date. Explore the GeoAlchemy2 ORM documentation for more advanced capabilities.

In the next chapter, we will review a similar MVC web framework, Django, and its GeoDjango spatial component. With more batteries included philosophy, Django solves the same problems inherent...

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