Geopandas!

This notebook is to highlight how easy it is to display shapefiles in a jupyter notebook
Shapefiles have many files, but are still one of the most common GIS file format to be sent or requested
Often we just want to display them, flicking back from python and GIS can be frustrating. That is one of the reasons why command line python in a GIS is so convient
However sometimes we just want to show out data, perhaps in a presentation and then get on with the processing and geopandas in jupyter notebooks is perfect for that
I came across this blog almost a year ago, and was blown away by how simple geopandas was
https://agilescientific.com/blog/2017/8/10/x-lines-of-python-read-and-write-a-shapefile


This notebook is all about viewing data. We will start by viewing 1 shapefile and then 2 as layers - as you would in a GIS

Data used in this notebook is a sample adapted from
https://data.gov.uk/dataset/d43263ba-6f09-458e-995e-69720909365f/crop-map-of-england-crome-2016-south-west
and a layer I created myself as part of my geospatial python course
http://www.acgeospatial.co.uk/training/

Start by reading in a shapefile

In [1]:
import geopandas as gpd
gdf = gpd.read_file('.../RPA_hexagons.shp')
print (gdf)
              CROMEID   REFDATE LUCODE  name  \
0     RPA484726101452  20171123   AC66     1   
1     RPA484186098369  20171123   AC17     2   
2     RPA478366099304  20171123   AC66     3   
3     RPA481726097919  20171123   PG01     4   
4     RPA482446100205  20171123   PG01     5   
5     RPA482086099373  20171123   PG01     6   
6     RPA479026098300  20171123   PG01     7   
7     RPA483046101383  20171123   PG01     8   
8     RPA482506099339  20171123   AC66     9   
9     RPA479386097607  20171123   NA01    10   
10    RPA480106100447  20171123   WO12    11   
11    RPA480946098507  20171123   AC66    12   
12    RPA484846101175  20171123   AC66    13   
13    RPA480226099893  20171123   WO12    14   
14    RPA477766097780  20171123   AC01    15   
15    RPA483046096810  20171123   PG01    16   
16    RPA477946097607  20171123   PG01    17   
17    RPA481666101625  20171123   AC67    18   
18    RPA479206100205  20171123   AC03    19   
19    RPA479086097572  20171123   LG03    20   
20    RPA482146097607  20171123   PG01    21   
21    RPA480706101972  20171123   PG01    22   
22    RPA479326099928  20171123   AC66    23   
23    RPA477646098126  20171123   AC01    24   
24    RPA481426100309  20171123   AC01    25   
25    RPA478786098646  20171123   WO12    26   
26    RPA479986100170  20171123   PG01    27   
27    RPA483766098958  20171123   AC17    28   
28    RPA479626097745  20171123   AC63    29   
29    RPA479386099200  20171123   WO12    30   
...               ...       ...    ...   ...   
8117  RPA483526101244  20171123   AC44  8118   
8118  RPA482806100690  20171123   WO12  8119   
8119  RPA482866097537  20171123   AC17  8120   
8120  RPA480586100794  20171123   AC27  8121   
8121  RPA484426100932  20171123   WO12  8122   
8122  RPA480526100551  20171123   LG03  8123   
8123  RPA484546099408  20171123   LG03  8124   
8124  RPA482926101798  20171123   PG01  8125   
8125  RPA479686098819  20171123   AC66  8126   
8126  RPA479986098300  20171123   PG01  8127   
8127  RPA481486097849  20171123   PG01  8128   
8128  RPA481486099235  20171123   PG01  8129   
8129  RPA482866101487  20171123   NA01  8130   
8130  RPA477766097641  20171123   NA01  8131   
8131  RPA483946101625  20171123   PG01  8132   
8132  RPA479326101106  20171123   AC17  8133   
8133  RPA479806099789  20171123   AC66  8134   
8134  RPA481606097503  20171123   PG01  8135   
8135  RPA482986100794  20171123   WO12  8136   
8136  RPA481426098161  20171123   WO12  8137   
8137  RPA481366097018  20171123   NA01  8138   
8138  RPA483046099997  20171123   AC65  8139   
8139  RPA478726097503  20171123   AC01  8140   
8140  RPA483166100274  20171123   FA01  8141   
8141  RPA480826098369  20171123   AC66  8142   
8142  RPA481366097156  20171123   WO12  8143   
8143  RPA478846097295  20171123   NA01  8144   
8144  RPA481666101764  20171123   AC67  8145   
8145  RPA479686100621  20171123   AC03  8146   
8146  RPA484846097988  20171123   TC01  8147   

                                               geometry  
0     POLYGON ((655102.7069389356 5630575.115807624,...  
1     POLYGON ((654604.3879486 5627484.486516733, 65...  
2     POLYGON ((648770.3225310852 5628410.402612799,...  
3     POLYGON ((652150.2753111218 5627000.825681773,...  
4     POLYGON ((652858.9270146162 5629331.984175636,...  
5     POLYGON ((652490.6165434893 5628460.74632061, ...  
6     POLYGON ((649444.904873959 5627345.37681831, 6...  
7     POLYGON ((653423.5017500889 5630483.07862567, ...  
8     POLYGON ((652911.1200482894 5628431.786879702,...  
9     POLYGON ((649814.3070869881 5626657.37388213, ...  
10    POLYGON ((650495.9231660233 5629507.902132113,...  
11    POLYGON ((651362.243518027 5627579.217360455, ...  
12    POLYGON ((655226.4688877971 5630299.583398947,...  
13    POLYGON ((650623.4343037286 5628955.224783191,...  
14    POLYGON ((648191.839994164 5626808.67133612, 6...  
15    POLYGON ((653485.3828191966 5625910.085498325,...  
16    POLYGON ((648374.196972942 5626637.890569176, ...  
17    POLYGON ((652040.1051758965 5630706.909638424,...  
18    POLYGON ((649618.6676750828 5629288.131421645,...  
19    POLYGON ((649514.7525405248 5626618.671572273,...  
20    POLYGON ((652574.5281747868 5626694.717336406,...  
21    POLYGON ((651075.3384015957 5631040.347699216,...  
22    POLYGON ((649722.8950781652 5628977.687357037,...  
23    POLYGON ((648067.1438295111 5627153.487561466,...  
24    POLYGON ((651776.9631129148 5629455.94011945, ...  
25    POLYGON ((649200.1989036256 5627688.569839598,...  
26    POLYGON ((650379.6645753465 5629229.131157499,...  
27    POLYGON ((654176.3828486223 5628067.75173317, ...  
28    POLYGON ((650071.983859909 5626834.10845309, 6...  
29    POLYGON ((649792.7448353325 5628250.981992862,...  
...                                                 ...  
8117  POLYGON ((653905.417855708 5630351.00224922, 6...  
8118  POLYGON ((653192.8599379412 5629786.951810867,...  
8119  POLYGON ((653295.5254170129 5626635.172201923,...  
8120  POLYGON ((650971.2722981849 5629860.840546262,...  
8121  POLYGON ((654809.7149308852 5630051.392689548,...  
8122  POLYGON ((650914.5497873464 5629617.516939123,...  
8123  POLYGON ((654950.3553159594 5628528.682302361,...  
8124  POLYGON ((653297.8643194558 5630897.184311542,...  
8125  POLYGON ((650117.4575358517 5627908.879252974,...  
8126  POLYGON ((650404.9796294608 5627358.366905879,...  
8127  POLYGON ((651911.1931707291 5626928.291446742,...  
8128  POLYGON ((651911.9756357463 5628348.958720988,...  
8129  POLYGON ((653242.079800875 5630584.580447236, ...  
8130  POLYGON ((648193.7147724432 5626670.098360197,...  
8131  POLYGON ((654280.2917554963 5630737.233293616,...  
8132  POLYGON ((649706.9534052582 5630155.575285839,...  
8133  POLYGON ((650204.8077615334 5628845.610312893,...  
8134  POLYGON ((652035.8899643586 5626583.480688744,...  
8135  POLYGON ((653371.4682045095 5629893.326541094,...  
8136  POLYGON ((651846.9696955733 5627239.278319384,...  
8137  POLYGON ((651802.4320172141 5626095.217506003,...  
8138  POLYGON ((653461.7901560719 5629132.243911067,...  
8139  POLYGON ((649155.6622334684 5626544.514255125,...  
8140  POLYGON ((653498.0426676901 5629409.933557474,...  
8141  POLYGON ((651244.1088708208 5627439.02004139, ...  
8142  POLYGON ((651800.5573536189 5626233.791152189,...  
8143  POLYGON ((649278.4835062125 5626338.278086607,...  
8144  POLYGON ((652038.2294014232 5630845.483508008,...  
8145  POLYGON ((650073.5459041351 5629675.433903837,...  
8146  POLYGON ((655269.6012132881 5627112.337542046,...  

[8147 rows x 5 columns]

3 lines of code


Some examples in org are here
https://pcjericks.github.io/py-gdalogr-cookbook/vector_layers.html#get-shapefile-feature-count
geopandas seems easy to read for a beginner and it is fast

What about viewing the data in the notebook.
that is just one line of code

In [3]:
%matplotlib inline
gdf.plot()
Out[3]:
<matplotlib.axes._subplots.AxesSubplot at 0x2d1c1c1c2b0>

What about colouring the data based on a field


(look at the table in the first cell for the field names)

In [4]:
gdf.plot(column='LUCODE', cmap=None)
Out[4]:
<matplotlib.axes._subplots.AxesSubplot at 0x2d1c1c5a940>

What about a legend?


parse legend=True

In [8]:
gdf.plot(column='LUCODE', cmap=None, legend=True)
Out[8]:
<matplotlib.axes._subplots.AxesSubplot at 0x26483c1fa20>

Legend is too big! Too many attributes?


make the figure larger
use figsize

In [5]:
gdf.plot(column='LUCODE', cmap=None, legend=True, figsize=(20, 20))
Out[5]:
<matplotlib.axes._subplots.AxesSubplot at 0x2d1c1c18e80>