Skip to content

In response to popular demand, a simple server for querying geometries

Notifications You must be signed in to change notification settings

MUSA611-CPLN692-spring2019/server-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A Simple, Queryable Vector Backend

Setup

To play around with this example, a couple prerequisites:

  1. You must have a working python installation with GDAL bindings. This can be tricky! Feel free to use a docker container or a python distribution (like Anaconda) if installation of GDAL + python bindings proves difficult. This is required to read shapefiles and deal with geometries.
  2. The python package flask. This provides the code which handles requests and serves responses.

Data

The data we will be querying is a census tracts shapefile (manually reprojected from EPSG:2272 to EPSG:4326 via QGIS, though you can do this programmatically!). Here's what it looks like, in full: census tracts

Running the Server

Once these pieces are in place, we should be able to spin up the server and start querying the provided census_tracts by typing:

FLASK_RUN_HOST=0.0.0.0 FLASK_APP=serve.py flask run

If successful, you'll see some startup messages like this:

 * Serving Flask app "serve.py"
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)

To actually use the server, make requests against these endpoints:

  • localhost:5000/lt/{ID} (return geojson for all OBJECTIDs less than ID)
  • localhost:5000/eq/{ID} (return geojson for all OBJECTIDs equal to ID)
  • localhost:5000/gt/{ID} (return geojson for all OBJECTIDs greater than ID)

Next Steps

  • The actual code in serve.py is fairly minimal but provides a few examples that each alter the SQL query very slightly. Try changing the SQL (open the shapefile in QGIS to investigate the properties available to your query).
  • At the top of the file, a few global variables are defined which point to the shapefile location. You should be able to use another shapefile with a few small changes. WATCH OUT FOR PROJECTIONS!

Further Reading

Want to write code that manipulates GIS data? Check out The OGR Cookbook (we're mostly using the vector layers section. Want to learn more about writing servers in python? Flask is a great starting point because it is so simple. Flask documentation Interested in writing complex URL patterns? Here are the docs about url converters

About

In response to popular demand, a simple server for querying geometries

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages