GeoPandas is a super simple way to work with GIS data using Python. It sits nicely in Jupyter Notebooks as well. This blog is all about displaying and visualising shapefiles in Jupyter Notebooks with GeoPandas. I am going to use a subset of the hexagonal Crop Map of England (CROME) and visualise it in a notebook and then add my own layer. There is potential to combine this CROME data with Earth Observation data using the raster stats python library (I have written something similar here). This would enable users to compare the crop map with the satellite data, perhaps best done on a post classification of the satellite data layer.
What is GeoPandas?
GeoPandas is an open source project to make working with geospatial data in python easier. GeoPandas extends the datatypes used by pandas to allow spatial operations on geometric types. Geometric operations are performed by shapely. Geopandas further depends on fiona for file access and descartes and matplotlib for plotting.
Taken from the GeoPandas documentation here.
If you read the documentation in that link you should see quite quickly how powerful, and hopefully simple to use GeoPandas is. As you become more experienced is acts as a nice bridge between working at scale with PostGIS and working with a smaller subset of data locally on your machine. By that I mean that GeoPandas is more than capable of handling spatial operations on your data; union, intersection and merging data are all handled.
Rural Payments Agency hexagonal polygons
This dataset was built from a classification of Sentinel 1 and Sentinel 2 in 2016 and identified 20 crop types. Here is what the hexagonal polygon looks like on a Sentinel 2 image:
and if I colour by attribute on the ‘lucode’ field, this is what it looks like:
This has all been done in QGIS. So let’s use geopandas to display it in a Jupyter Notebook.
Display shapefiles in Jupyter Notebook
Open a Jupyter Notebook and import geopandas and read a shapefile
import geopandas as gpd gdf = gpd.read_file('../RPA_hexagons.shp') print (gdf)
The print statement will return the attribute table. Three line of code to get the attribute table and it is only one more to view the data. I came across this blog almost a year ago, and was blown away by how simple geopandas was
Let’s view the data now:
%matplotlib inline gdf.plot()
All we have to do is call gdf.plot(). The first line is a command for Jupyter Notebooks to plot the figure, recommended if you use any graphs in your notebooks. Otherwise .plot() will return <matplotlib.axes._subplots.AxesSubplot at 0x2d1c1c1c2b0> (or similar) instead of an image.
What about colouring this data based on a field, as we would in any GIS package?
The result is this:
Add a Legend:
gdf.plot(column='LUCODE', cmap=None, legend=True)
and resize to fit the legend on the screen:
gdf.plot(column='LUCODE', cmap=None, legend=True, figsize=(20, 20))
Finally, let’s add another shapefile and try to plot it over the top of this layer:
gdf2 = gpd.read_file('.../Boundaries.shp')
This adds a new shapefile; this time we call our GeoPandas Data Frame (gdf2). Finally lets plot them both together and shade the new layer by the ‘id’ column:
import matplotlib.pyplot as plt f, ax = plt.subplots(1) gdf.plot(ax=ax) gdf2.plot(ax=ax,column='id',cmap=None,) plt.show()
We need to import matplotlib and create subplots to do this. The f is the figure and ax is the subplot. Then we call gdf.plot(ax=ax) to the newly created subplot. Next, as with all matplotlib plots we need to call plt.show() to display the image. The final plot is shown below.
Hopefully this guide has been a useful introduction. I have tried to keep the code as simple as possible. I use this data in my Geospatial courses to perform more detailed analysis in Python. Please do get in contact if you would like to know more.
All the code is available as a Python 3.6 Jupyter Notebook on my GitHub page.
- GeoPandas is remarkably simple to use and a great way of working with GIS data in Python.
- We have only covered plotting data and overlaying data, but there are a great deal of other things that can be done.
- It represents a good place to start prior to scaling your GIS workflows to PostGIS. That being said you could just use PostGIS locally.
I am a freelancer able to help you with your projects. I offer consultancy, training and writing. I’d be delighted to hear from you.
I have grouped all my previous blogs (technical stuff / tutorials / opinions / ideas) at http://gis.acgeospatial.co.uk.