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 13. Geospatial REST API

Publishing data for consumption on the web is a major component ofmodern GIS. To transfer data from remote servers to remote clients, most geospatial publishing software stacks use Representational State Transfer (REST) web services. In response to web requests for specific data resources, REST services return JavaScript Object Notation (JSON)-encoded data to the requesting client machine. The web services are combined in an application programming interface, or API, which will contain the endpoints that represent each data resource available for querying.

By combining a Python web framework with object-relational mapping (ORM) and a PostGIS backend, we can create a custom REST API that will respond to web requests with JSON. For this exercise, we will use the Flask web framework and the SQLAlchemy module with GeoAlchemy2 providing spatial ORM capabilities.

In this chapter, we will learn about the following:

  • REST API components
  • JSON response formatting
  • How to process...

Writing a REST API in Python


To understand the components of a REST API with JSON response, we will utilize the Flask web framework, a PostgreSQL/PostGIS database, and SQLAlchemy and GeoAlchemy2 for ORM queries. Flask will be used to create the URL endpoints for the API. PostGIS will store the data in tables defined by SQLAlchemy models, which define the column types for all columns except the geometry columns, which are defined by GeoAlchemy2 column types.

REST

REST is a standard for web services, designed to accept requests and parameters and return a representation of that data, usually in a JSON format but sometimes in XML or HTML format. APIs that use REST architecture must meet these architectural constraints:

  • Client-server interactions
  • Statelessness
  • Cacheablitity
  • Uniform interface 
  • Layered system

The client (a web browser or a remote computer) will send a request to a server at a designated URL endpoint. The request can include parameters that limit the data objects returned, much like conditionals...

The REST API application


To enable access to a database of NBA Arenas, US States, US Counties, and US Congressional Districts, we will build a REST API. The API will allow for queries about tables and about specific table resources, that is, rows of data. It will also allow for geospatial queries.

Application components

The components of this application include:

  • The database, created in Chapter 11Flask and GeoAlchemy2, which contains the tables for NBA Arenas, US States, US Counties, and US Congressional Districts
  • Theapp.py file, which initiates the application when called by a Python executable
  • Theapplication folder, which contains the application code and folders
  • The__init__.py file, which makes the application folder into a module, defines the Flask object and connects to the database
  • Theviews.py file, which defines the API endpoints, the view functions, and the return responses
  • Themodels.py file, which defines the database table models as Python classes that subclass from SQLAlchemy
  • The forms...

Deploying Flask to IIS


To deploy the new API application on a Microsoft Server with Internet Information Services (IIS) installed, we have to download some Python code, and an IIS module called FastCGI. Once configured, the application will respond to web requests from any allowed machine.  

Flask and web servers

While Flask includes a local web server for testing purposes, it is not designed for production deployments. Flask works best with web servers like Apache or IIS. While there is a lot of literature on how to deploy Flask with Apache, it is less common to find good instructions on how to deploy it using IIS. As most GIS professionals work with Windows servers or have access to them, these instructions will focus on deployment with IIS 7.

WSGI

The Web Server Gateway Interface (WSGI) is a Python specification that allows for a Python executable to be used to respond to web requests. WSGI is built into Python web frameworks such as Flask and Django.

To enable the use of the Flask web framework...

Summary


Creating an API with REST specifications is easy with Python web frameworks. Flask makes it simple to coordinate the URL endpoints with request methods and response types. With built-in JSON capabilities, and with the use of the SQLAlchemy and GeoAlchemy2 ORMs, Flask is a perfect framework for creating a geospatial REST API.

In the next chapter, we will cover the use of the CARTOframes module for cloud visualization of geospatial data.

 

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