This is a repository of various geo/spatial analysis techniques using Python libraries, chiefly Numpy, Pandas, Shapely, Fiona, Descartes, Matplotlib, and Matplotlib-Basemap.
These tutorials, visualisations, and libraries are an occasional side effect of being embroiled in a PhD at the Bartlett Centre for Advanced Spatial Analysis, at UCL, and teaching on the undergraduate Data Science and Visualisation course.
Convert: demonstrates point, choropleth, and hexbin mapping techniques using pandas and Matplotlib Basemap
Convert_Folium: demonstrates the use of the Folium library for creating web-based maps from Python data (pandas) using Leaflet to generate a choropleth map
Router Comparison: analyses route characteristics of journeys from the London Cycle Hire network centroid to 554 stations, calculated using OSRM, Mapzen's new Valhalla router, and the Google Maps Directions API. There's also some outlier identification using RANSAC, and hopefully useful wrappers for route retrieval in helpers.py
.
WLS: demonstrates the use of weighted least-squares estimation of similarity and affine transforms to calculate parameters for a transformation matrix which can be used to transform and align coordinates. This approach is useful for e.g. georeferencing raster data, and map feature alignment and correction.
Contour: demonstrates interpolation of irregularly-spaced point data (mean rainfall) into a regular grid, calculating a contour plot, and imposing it onto a basemap (see graphic below). Two approaches for calculating continuous surfaces are then compared – Delaunay Natural Neighbour (matplotlib.griddata
), and refinement of a coarse Delaunay mesh using matplotlib.UniformTriRefiner
, which uses recursive subdivision and cubic interpolation. High-res images are available in the data folder, all beginning with rainfall_
. ☔☔☔
Finally, the map is partitioned into clipped Voronoi cells based on the sensor locations, and some plotting methods (more flexible than scipy.spatial.voronoi2d
) are shown.
Bikepath: takes a subset of London bicycle hire stations, creates a DataFrame of all possible origin and destination pairs, and then uses MapZen's wonderful open Valhalla endpoint to get valid bicycle routes between them. These routes are then plotted (it's just the first 400 – there are over 490k origin / destination pairs in total) on a map.
Isochrone: an isochrone is computed for the complete London bike network from an origin at its centroid. Given a network of stations, Single-Source Shortest Path Length, weighted by the travel time between the origin and destinations can be used to generate travel times – though this isn't necessary for a single origin.
Plaques_Geopandas: demonstrates Geopandas and its spatial join functionality, used to create a choropleth.
Circles: demonstrates drawing circles with correct distortion characteristics on a map (the dot-shaded circle is erroneously non-distorted). The helper library used to plot the circles is available here.
Rust BNG: A demonstration of writing a Rust library, and linking it to Python using FFI, in order to carry out fast Longitude and Latitude to British National Grid transformations, using multithreading. I've blogged about the process here.
Note that for most users, the Enthought Canopy Python distribution is probably the best way to get the required libraries (You'll have to install Folium and GeoPandas separately – they aren't included). However, feel free to install the libraries manually using requirements.txt
if you know what you're doing, in which case you'll also need various compilers (GCC, Fortran), and libraries (GDAL, GEOS). The use of a virtualenv is advised.
rust_bng
Requires a Rust installation. Install it using homebrew, or one of the installers, for *nix or Windows.
© Stephan Hügel 2014
Unless otherwise specified, the wards.geojson file and any UK shapefiles are provided under Crown Copyright, and their use must be acknowledged in any output by reproducing the following notice:
Contains Ordnance Survey data © Crown copyright and database right 2015
Unless otherwise specified, all other files are provided under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International license.