class: center, middle # GeoPandas ## Easy, fast and scalable geospatial analysis in Python Joris Van den Bossche, FOSS4G Belgium, October 25, 2018 https://github.com/jorisvandenbossche/talks/ [@jorisvdbossche](https://twitter.com/jorisvdbossche) --- # About me Joris Van den Bossche - Background: PhD bio-science engineer, air quality research - Open source enthusiast: pandas core dev, geopandas maintainer, scikit-learn contributor - Currently working at the Université Paris-Saclay Center for Data Science (Inria) https://github.com/jorisvandenbossche Twitter: [@jorisvdbossche](https://twitter.com/jorisvdbossche)
.affiliations[ ![:scale 65%](img/logoUPSayPlusCDS_990.png) ![:scale 25%](img/inria-logo.png) ] --- class: middle, center # Open source geospatial software .center[ ![:scale 70%](img/Open_Source_Geospatial_Foundation.svg) ] ??? Open Source Geospatial Foundation OSGeo was created to support the collaborative development of open source geospatial software, and promote its widespread use. --- # GDAL / OGR ### Geospatial Data Abstraction Library.
* The swiss army knife for geospatial. * Read and write Raster (GDAL) and Vector (OGR) datasets * More than 200 (mainly) geospatial formats and protocols. .center[ ![:scale 100%](img/gdal_formats) ] .credits[ Slide from "GDAL 2.2 What's new?" by Even Rouault (CC BY-SA) ] ??? GDAL is a translator library for raster and vector geospatial data formats. As a library, it presents a single raster abstract data model and single vector abstract data model to the calling application for all supported formats. It also comes with a variety of useful command line utilities for data translation and processing. --- # GEOS
## Geometry Engine Open Source * C/C++ port of a subset of Java Topology Suite (JTS) * Most widely used geospatial C++ geometry library * Implements geometry objects (simple features), spatial predicate functions and spatial operations Used under the hood by many applications (QGIS, PostGIS, MapServer, GRASS, GeoDjango, ...) [geos.osgeo.org](http://geos.osgeo.org) --- # Python geospatial packages -- count:false Interfaces to widely used libraries: - Python bindings to GDAL/OGR (`from osgeo import gdal, ogr`) - [`pyproj`](https://jswhit.github.io/pyproj/): python interface to PROJ.4. - Pythonic binding to GDAL/OGR: - [`rasterio`](https://mapbox.github.io/rasterio/) for GDAL - [`fiona`](http://toblerity.org/fiona/README.html) for OGR - [`shapely`](https://shapely.readthedocs.io/en/latest/): python package based on GEOS. --- # Shapely Python package for the manipulation and analysis of geometric objects
Pythonic interface to GEOS -- count:false .mmedium[ ```python >>> from shapely.geometry import Point, LineString, Polygon >>> point = Point(1, 1) >>> line = LineString([(0, 0), (1, 2), (2, 2)]) >>> poly = line.buffer(1) ``` ]
.mmedium[ ```python >>> poly.contains(point) True ``` ] -- count: false Nice interface to GEOS, but: single objects, no attributes ??? # Shapely typical predicates and operations (images from shapely docs) --- ![:scale 100%](img/pandas_logo.svg) One of the packages driving the growing popularity of Python for data science, machine learning and academic research * High-performance, easy-to-use data structures and tools * Suited for tabular data (e.g. columnar data, spread-sheets, database tables) ```python import pandas as pd df = pd.read_csv("myfile.csv") subset = df[df['value'] > 0] subset.groupby('key').mean() ``` --- class: middle, center # GeoPandas # .darkred[.fat[Easy]], fast and scalable geospatial analysis in Python --- # GeoPandas Make working with geospatial data in python easier * Started by Kelsey Jordahl in 2013 * Extends the pandas data analysis library to work with geographic objects and spatial operations * Combines the power of whole ecosystem of (geo) tools (pandas, geos, shapely, gdal, fiona, pyproj, rtree, ...) * Bridge between geospatial packages and the scientific / data science stack Documentation: http://geopandas.readthedocs.io/ ??? make working with geospatial data like working with any other kind of data in python (data stack, numpy, pandas and other tools around those) analysis for which you otherwise would need desktop GIS applications (QGIS, ArcGIS) or geospatial databases (PostGIS) makes pandas objects geometry aware --- class: middle, center # Demo time! See [static version](http://nbviewer.jupyter.org/github/jorisvandenbossche/talks/blob/master/2017_EuroScipy_geopandas/geopandas_demo.ipynb) --- # Summary * Read and write variety of formats (fiona, GDAL/OGR) * Familiar manipulation of the attributes (pandas dataframe) * Element-wise spatial predicates (intersects, within, ...) and operations (intersection, union, difference, ..) (shapely) * Re-project your data (pyproj) * Quickly visualize the geometries (matplotlib, descartes) * More advanced spatial operations: spatial joins and overlays (rtree) -- count:false **-> Interactive exploration and analysis of geospatial data** --- # Ecosystem [geoplot](http://www.residentmar.io/geoplot/index.html) (high-level geospatial visualization), [cartopy](http://scitools.org.uk/cartopy/) (projection aware cartographic library) [folium](https://github.com/python-visualization/folium) (Leaflet.js maps) [OSMnx](http://geoffboeing.com/2016/11/osmnx-python-street-networks/) (python for street networks) [PySAL](http://pysal.readthedocs.io/en/latest/index.html) (Python Spatial Analysis Library) [rasterio](https://mapbox.github.io/rasterio/) (working with geospatial raster data) ... --- class: middle, center # GeoPandas # Easy, .darkred[.fat[fast]] and scalable geospatial analysis in Python --- # However ... --- count: false # However ... it can be slow --- # Comparison with PostGIS ![:scale 49%](img/timings_distance2.png) ![:scale 49%](img/timings_sjoin.png) Disclaimer: dummy benchmark, and I am not a PostGIS expert! Example from [Boundless tutorial](http://workshops.boundlessgeo.com/postgis-intro/) (CC BY SA) --- # Comparison with PostGIS .small[ ```sql -- What is the population and racial make-up of the neighborhoods of Manhattan? SELECT neighborhoods.name AS neighborhood_name, Sum(census.popn_total) AS population, 100.0 * Sum(census.popn_white) / NULLIF(Sum(census.popn_total),0) AS white_pct, 100.0 * Sum(census.popn_black) / NULLIF(Sum(census.popn_total),0) AS black_pct FROM nyc_neighborhoods AS neighborhoods JOIN nyc_census_blocks AS census ON ST_Intersects(neighborhoods.geom, census.geom) GROUP BY neighborhoods.name ORDER BY white_pct DESC; ``` ] .small[ ```python res = geopandas.sjoin(nyc_neighborhoods, nyc_census_blocks, op='intersects') res = res.groupby('NAME')[['POPN_TOTAL', 'POPN_WHITE', 'POPN_BLACK']].sum() res['POPN_BLACK'] = res['POPN_BLACK'] / res['POPN_TOTAL'] * 100 res['POPN_WHITE'] = res['POPN_WHITE'] / res['POPN_TOTAL'] * 100 res.sort_values('POPN_WHITE', ascending=False) ``` ] Disclaimer: dummy benchmark, and I am not a PostGIS expert! Example from [Boundless tutorial](http://workshops.boundlessgeo.com/postgis-intro/) (CC BY SA) --- # Why is GeoPandas slower? - GeoPandas stores custom Python objects in arrays - For operations, it iterates through those objects - Those Python objects each call the GEOS C operation
.center[ ![:scale 60%](img/geopandas-shapely-1.svg) ] --- # New version in development .center[ ![:scale 70%](img/geopandas-shapely-2.svg) ] Remove python overhead by only storing pointers to C GEOS objects and iterating in C TL;DR: same API, but better performance and less memory use Many thanks to Matthew Rocklin (Anaconda, Inc.) for his work! --- # New timings ![:scale 40%](img/timings_within.png) ![:scale 40%](img/timings_distance.png) ![:scale 40%](img/timings_distance2.png) ![:scale 40%](img/timings_sjoin.png) --- count:false # New timings ![:scale 40%](img/timings_within_all.png) ![:scale 40%](img/timings_distance_all.png) ![:scale 40%](img/timings_distance2_all.png) ![:scale 40%](img/timings_sjoin_all.png) --- # Sounds interesting? Blogpost of me and Matthew with more background: * http://matthewrocklin.com/blog/work/2017/09/21/accelerating-geopandas-1 * https://jorisvandenbossche.github.io/blog/2017/09/19/geopandas-cython/ Try out development version (binary builds): .medium[ ```bash conda install --channel conda-forge/label/dev geopandas ``` ] .small[ ```bash conda create -n geo_test python=3.6 geopandas --channel conda-forge/label/dev ``` ] --- class: middle, center # GeoPandas # Easy, fast and .darkred[.fat[scalable]] geospatial analysis in Python ??? Python has a fast and pragmatic data science ecosystem ... restricted to in-memory and a single core --- .center[ ![:scale 55%](img/dask_horizontal.svg) ] ## A flexible library for parallelism -- count: false * A parallel computing framework, written in pure Python * Lets you work on larger-than-memory datasets * That leverages the excellent Python ecosystem * Using blocked algorithms and task scheduling http://dask.pydata.org/ --- # An experiment with taxi data [Ravi Shekhar](http://people.earth.yale.edu/profile/ravi-shekhar/about) published a blogpost [Geospatial Operations at Scale with Dask and GeoPandas](https://medium.com/towards-data-science/geospatial-operations-at-scale-with-dask-and-geopandas-4d92d00eb7e8) in which he counted the number of rides originating from each of the official taxi zones of New York City Matthew Rocklin re-ran the experiment with the in-development version: 3h -> 8min ([see his blogpost](http://matthewrocklin.com/blog/work/2017/09/21/accelerating-geopandas-1)) [dask-geopandas](https://github.com/mrocklin/dask-geopandas): experimental library with parallelized geospatial operations and joins -- count: false ## Demo time! See [static version](http://nbviewer.jupyter.org/gist/jorisvandenbossche/67be41a246c1281d7046b31690988321) -- count: false Experimental, but ready to be tried out! --- # Contributions welcome! ### Feedback, bug reports, feature requests ### Extend functionality ### Contribute code: https://github.com/geopandas/geopandas --- class: middle http://geopandas.readthedocs.io # Thanks for listening! ## Thanks to all contributors! ## Those slides: - https://github.com/jorisvandenbossche/talks/ - [jorisvandenbossche.github.io/talks/2018_GeoPython_geopandas]( http://jorisvandenbossche.github.io/talks/2018_GeoPython_geopandas) http://geopandas.readthedocs.io