# Introduction to spatial data analysis with geopandas#

We will use geopandas 1 as our main tool for spatial data analysis in Python. In the first part of this book, we covered the basics of data analysis using the pandas library. Geopandas extends the capacities of pandas with geospatial operations. The main data structures in geopandas are GeoSeries and GeoDataFrame which extend the capabilities of Series and DataFrames from pandas. This means that we can use all our pandas skills also when working with geopandas.

The main difference between geopandas GeoDataFrames and pandas DataFrames is that a GeoDataFrame should contain one column for geometries. By default, the name of this column is 'geometry'. The geometry column is a GeoSeries which contains the geometries (points, lines, polygons, multipolygons etc.) as shapely objects.

Figure 6.1. Geometry column in a GeoDataFrame.

Similar to importing import pandas as pd, we will import geopandas as gpd:

import geopandas as gpd


Esri Shapefile is the default file format when reading in data usign geopandas, so we only need to pass the file path in order to read in our data:

from pathlib import Path

input_folder = Path("../data/NLS")
fp = input_folder / "m_L4132R_p.shp"

# Read file using gpd.read_file()


Let’s check the data type:

type(data)

geopandas.geodataframe.GeoDataFrame


Here we see that our data -variable is a GeoDataFrame. GeoDataFrame extends the functionalities of pandas.DataFrame in a way that it is possible to handle spatial data using similar approaches and datastructures as in pandas (hence the name geopandas).

Let’s check the first rows of data:

data.head()

TEKSTI RYHMA LUOKKA TASTAR KORTAR KORARV KULKUTAPA KOHDEOSO AINLAHDE SYNTYHETKI ... KARTOGLK ALUEJAKOON VERSUH SUUNTA SIIRT_DX SIIRT_DY KORKEUS ATTR2 ATTR3 geometry
0 None 64 32421 5000 0 0.0 0 1812247077 1 20180125 ... 0 0 0 0 0 0 0.0 0 0 POLYGON ((379394.248 6689991.936, 379389.790 6...
1 None 64 32421 5000 0 0.0 0 1718796908 1 20180120 ... 0 0 0 0 0 0 0.0 0 0 POLYGON ((378980.811 6689359.377, 378983.401 6...
2 None 64 32421 20000 0 0.0 0 411167695 1 20180120 ... 0 0 0 0 0 0 0.0 0 0 POLYGON ((378804.766 6689256.471, 378817.107 6...
3 None 64 32421 20000 0 0.0 0 411173768 1 20180120 ... 0 0 0 0 0 0 0.0 0 0 POLYGON ((379229.695 6685025.111, 379233.366 6...
4 None 64 32421 20000 0 0.0 0 411173698 1 20180120 ... 0 0 0 0 0 0 0.0 0 0 POLYGON ((379825.199 6685096.247, 379829.651 6...

5 rows × 21 columns

• Check all column names:

data.columns.values

array(['TEKSTI', 'RYHMA', 'LUOKKA', 'TASTAR', 'KORTAR', 'KORARV',
'KULKUTAPA', 'KOHDEOSO', 'AINLAHDE', 'SYNTYHETKI', 'KUOLHETKI',
'KARTOGLK', 'ALUEJAKOON', 'VERSUH', 'SUUNTA', 'SIIRT_DX',
'SIIRT_DY', 'KORKEUS', 'ATTR2', 'ATTR3', 'geometry'], dtype=object)


As you might guess, the column names are in Finnish. Let’s select only the useful columns and rename them into English:

data = data[["RYHMA", "LUOKKA", "geometry"]]


Define new column names in a dictionary:

colnames = {"RYHMA": "GROUP", "LUOKKA": "CLASS"}


Rename:

data.rename(columns=colnames, inplace=True)


Check the output:

data.head()

GROUP CLASS geometry
0 64 32421 POLYGON ((379394.248 6689991.936, 379389.790 6...
1 64 32421 POLYGON ((378980.811 6689359.377, 378983.401 6...
2 64 32421 POLYGON ((378804.766 6689256.471, 378817.107 6...
3 64 32421 POLYGON ((379229.695 6685025.111, 379233.366 6...
4 64 32421 POLYGON ((379825.199 6685096.247, 379829.651 6...

Figure out the following information from our input data using your pandas skills:

• Number of rows?

• Number of classes?

• Number of groups?

print("Number of rows", len(data["CLASS"]))
print("Number of classes", data["CLASS"].nunique())
print("Number of groups", data["GROUP"].nunique())

Number of rows 4311
Number of classes 20
Number of groups 1


It is always a good idea to explore your data also on a map. Creating a simple map from a GeoDataFrame is really easy: you can use .plot() -function from geopandas that creates a map based on the geometries of the data. Geopandas actually uses matplotlib for plotting which we introduced in Lesson 7 of the Geo-Python course.

Let’s try it out, and plot our GeoDataFrame:

data.plot()

<AxesSubplot:>


Voilá! As we can see, it is really easy to produce a map out of your Shapefile with geopandas. Geopandas automatically positions your map in a way that it covers the whole extent of your data.

If you are living in the Helsinki region, you might recognize the shapes plotted on the map!

## Geometries in geopandas#

Geopandas takes advantage of Shapely’s geometric objects. Geometries are stored in a column called geometry that is a default column name for storing geometric information in geopandas.

Let’s print the first 5 rows of the column ‘geometry’:

data["geometry"].head()

0    POLYGON ((379394.248 6689991.936, 379389.790 6...
1    POLYGON ((378980.811 6689359.377, 378983.401 6...
2    POLYGON ((378804.766 6689256.471, 378817.107 6...
3    POLYGON ((379229.695 6685025.111, 379233.366 6...
4    POLYGON ((379825.199 6685096.247, 379829.651 6...
Name: geometry, dtype: geometry


As we can see the geometry column contains familiar looking values, namely Shapely Polygon -objects. Since the spatial data is stored as Shapely objects, it is possible to use Shapely methods when dealing with geometries in geopandas.

Let’s have a closer look at the polygons and try to apply some of the Shapely methods we are already familiar with.

Let’s start by checking the area of the first polygon in the data:

# Access the geometry on the first row of data
data.at[0, "geometry"]

# Print information about the area
print("Area:", round(data.at[0, "geometry"].area, 0), "square meters")

Area: 76.0 square meters


Let’s do the same for the first five rows in the data;

• Iterate over the GeoDataFrame rows using the iterrows() -function that we learned during the Lesson 6 of the Geo-Python course.

• For each row, print the area of the polygon (here, we’ll limit the for-loop to a selection of the first five rows):

# Iterate over rows and print the area of a Polygon
for index, row in data[0:5].iterrows():

# Get the area from the shapely-object stored in the geometry-column
poly_area = row["geometry"].area

# Print info
print(
"Polygon area at index {index} is: {area:.0f} square meters".format(
index=index, area=poly_area
)
)

Polygon area at index 0 is: 76 square meters
Polygon area at index 1 is: 2652 square meters
Polygon area at index 2 is: 3186 square meters
Polygon area at index 3 is: 13075 square meters
Polygon area at index 4 is: 3981 square meters


As you see from here, all pandas methods, such as the iterrows() function, are directly available in Geopandas without the need to call pandas separately because Geopandas is an extension for pandas.

In practice, it is not necessary to use the iterrows()-approach to calculate the area for all features. Geodataframes and geoseries have an attribute area which we can use for accessing the area for each feature at once:

data.area

0          76.027392
1        2652.054186
2        3185.649995
3       13075.165279
4        3980.682621
...
4306     2651.800270
4307      376.503380
4308      413.942555
4309     3487.927677
4310     1278.963199
Length: 4311, dtype: float64


Let’s next create a new column into our GeoDataFrame where we calculate and store the areas of individual polygons:

# Create a new column called 'area'
data["area"] = data.area


Check the output:

data["area"]

0          76.027392
1        2652.054186
2        3185.649995
3       13075.165279
4        3980.682621
...
4306     2651.800270
4307      376.503380
4308      413.942555
4309     3487.927677
4310     1278.963199
Name: area, Length: 4311, dtype: float64


These values correspond to the ones we saw in previous step when iterating rows.

Let’s check what is the min, max and mean of those areas using familiar functions from our previous Pandas lessions.

# Maximum area
round(data["area"].max(), 2)

4084558.15

# Minimum area
round(data["area"].min(), 2)

0.67

# Average area
round(data["area"].mean(), 2)

11522.29


## Writing data into a file#

It is possible to export GeoDataFrames into various data formats using the to_file() method. In our case, we want to export subsets of the data into Shapefiles (one file for each feature class).

Let’s first select one class (class number 36200, “Lake water”) from the data as a new GeoDataFrame:

# Select a class
selection = data.loc[data["CLASS"] == 36200]


Check the selection:

selection.plot()

<AxesSubplot:>

• write this layer into a new Shapefile using the gpd.to_file() -function:

# Create a output path for the data
output_folder = Path("results")

if not output_folder.exists():
output_folder.mkdir()

output_fp = output_folder / "Class_36200.shp"

# Write those rows into a new file (the default output file format is Shapefile)
selection.to_file(output_fp)


### Question 6.2#

Read the output Shapefile in a new geodataframe, and check that the data looks ok.

# Use this cell to enter your solution.

# Solution

# Check first rows

GROUP CLASS area geometry
0 64 36200 1318.878221 POLYGON ((379089.473 6687069.722, 379093.838 6...
1 64 36200 22918.867073 POLYGON ((376732.156 6687178.141, 376731.301 6...
2 64 36200 5759.318345 POLYGON ((377939.741 6684539.678, 377929.192 6...
3 64 36200 265899.648379 POLYGON ((372948.857 6688594.047, 372935.951 6...
4 64 36200 128221.314258 POLYGON ((370900.963 6689201.649, 370890.077 6...
# Solution

# You can also plot the data for a visual check
temp.plot()

<AxesSubplot:>


## Grouping the GeoDataFrame#

Next we will automate the file export task. we will group the data based on column CLASS and export a shapefile for each class.

This can be achieved using the groupby() familiar from the pandas library and also available in GeoDataFrames. This function groups data based on values on selected column(s).

Before continuing, let’s check the first rows of our input data:

data.head()

GROUP CLASS geometry area
0 64 32421 POLYGON ((379394.248 6689991.936, 379389.790 6... 76.027392
1 64 32421 POLYGON ((378980.811 6689359.377, 378983.401 6... 2652.054186
2 64 32421 POLYGON ((378804.766 6689256.471, 378817.107 6... 3185.649995
3 64 32421 POLYGON ((379229.695 6685025.111, 379233.366 6... 13075.165279
4 64 32421 POLYGON ((379825.199 6685096.247, 379829.651 6... 3980.682621

The CLASS column in the data contains information about different land use types. With .unique() -function we can quickly see all different values in that column:

# Print all unique values in the column
data["CLASS"].unique()

array([32421, 32200, 34300, 34100, 34700, 32500, 32112, 32111, 32611,
32612, 32800, 32900, 35300, 35412, 35411, 35421, 33000, 33100,
36200, 36313])


Now we can use that information to group our data and save all land use types into different layers:

# Group the data by class
grouped = data.groupby("CLASS")

# Let's see what we have
grouped

<pandas.core.groupby.generic.DataFrameGroupBy object at 0x7f2b1b55a0d0>


As we can see, groupby -function gives us an object called DataFrameGroupBy which is similar to list of keys and values (in a dictionary) that we can iterate over.

Check group keys:

grouped.groups.keys()

dict_keys([32111, 32112, 32200, 32421, 32500, 32611, 32612, 32800, 32900, 33000, 33100, 34100, 34300, 34700, 35300, 35411, 35412, 35421, 36200, 36313])


The group keys are unique values from the column by which we grouped the dataframe.

Check how many rows of data each group has:

# Iterate over the grouped object
for key, group in grouped:

# Let's check how many rows each group has:
print("Terrain class:", key)
print("Number of rows:", len(group), "\n")

Terrain class: 32111
Number of rows: 1

Terrain class: 32112
Number of rows: 1

Terrain class: 32200
Number of rows: 2

Terrain class: 32421
Number of rows: 110

Terrain class: 32500
Number of rows: 2

Terrain class: 32611
Number of rows: 257

Terrain class: 32612
Number of rows: 11

Terrain class: 32800
Number of rows: 80

Terrain class: 32900
Number of rows: 28

Terrain class: 33000
Number of rows: 5

Terrain class: 33100
Number of rows: 118

Terrain class: 34100
Number of rows: 3005

Terrain class: 34300
Number of rows: 1

Terrain class: 34700
Number of rows: 3

Terrain class: 35300
Number of rows: 134

Terrain class: 35411
Number of rows: 35

Terrain class: 35412
Number of rows: 449

Terrain class: 35421
Number of rows: 5

Terrain class: 36200
Number of rows: 56

Terrain class: 36313
Number of rows: 8


There are, for example, 56 lake polygons in the input data.

We can also check how the last group looks like (we have the variables in memory from the last iteration of the for-loop):

group.head()

GROUP CLASS geometry area
4303 64 36313 POLYGON ((377127.305 6688073.257, 377116.045 6... 9619.307973
4304 64 36313 POLYGON ((371141.897 6677999.999, 371139.757 6... 25266.167705
4305 64 36313 POLYGON ((371498.720 6680399.799, 371497.585 6... 364.087680
4306 64 36313 POLYGON ((375668.607 6682942.062, 375671.489 6... 2651.800270
4307 64 36313 POLYGON ((368411.063 6679328.990, 368411.424 6... 376.503380

Notice that the index numbers refer to the row numbers in the original data -GeoDataFrame.

Check also the data type of the group:

type(group)

geopandas.geodataframe.GeoDataFrame


As we can see, each set of data are now grouped into separate GeoDataFrames, and we can save them into separate files.

### Saving multiple output files#

Let’s export each class into a separate Shapefile. While doing this, we also want to create unique filenames for each class.

When looping over the grouped object, information about the class is stored in the variable key, and we can use this information for creating new variable names inside the for-loop. For example, we want to name the shapefile containing lake polygons as “terrain_36200.shp”.

String formatting

There are different approaches for formatting strings in Python. Here are a couple of different ways for putting together file-path names using two variables:

basename = "terrain"
key = 36200

# OPTION 1. Concatenating using the + operator:
out_fp = basename + "_" + str(key) + ".shp"

# OPTION 2. Positional formatting using % operator
out_fp = "%s_%s.shp" %(basename, key)

# OPTION 3. Positional formatting using .format()
out_fp = "{}_{}.shp".format(basename, key)


Let’s now export terrain classes into separate Shapefiles.

• First, create a new folder for the outputs:

# Determine output directory
output_folder = Path("results")

# Create a new folder called 'Results'
result_folder = output_folder / "Results"

# Check if the folder exists already
if not result_folder.exists():

print("Creating a folder for the results..")
# If it does not exist, create one
result_folder.mkdir()

Creating a folder for the results..


At this point, you can go to the file browser and check that the new folder was created successfully.

• Iterate over groups, create a file name, and save group to file:

# Iterate over the groups
for key, group in grouped:
# Format the filename
output_name = Path("terrain_{}.shp".format(key))

# Print information about the process
print("Saving file", output_name.name)

# Create an output path
outpath = result_folder / output_name

# Export the data
group.to_file(outpath)

Saving file terrain_32111.shp
Saving file terrain_32112.shp
Saving file terrain_32200.shp
Saving file terrain_32421.shp
Saving file terrain_32500.shp
Saving file terrain_32611.shp
Saving file terrain_32612.shp
Saving file terrain_32800.shp
Saving file terrain_32900.shp
Saving file terrain_33000.shp
Saving file terrain_33100.shp
Saving file terrain_34100.shp
Saving file terrain_34300.shp
Saving file terrain_34700.shp
Saving file terrain_35300.shp
Saving file terrain_35411.shp
Saving file terrain_35412.shp
Saving file terrain_35421.shp
Saving file terrain_36200.shp
Saving file terrain_36313.shp


Excellent! Now we have saved those individual classes into separate Shapefiles and named the file according to the class name. These kind of grouping operations can be really handy when dealing with layers of spatial data. Doing similar process manually would be really laborious and error-prone.

### Save attributes to a text file#

We can also extract basic statistics from our geodataframe, and save this information as a text file.

Let’s summarize the total area of each group:

area_info = grouped.area.sum().round()

area_info

CLASS
32111        1834.0
32112        2148.0
32200      105737.0
32421      702073.0
32500      109747.0
32611    13135597.0
32612      107343.0
32800     1465278.0
32900      617209.0
33000      659465.0
33100     3777595.0
34100    12381611.0
34300        1627.0
34700        2786.0
35300     1382940.0
35411      411198.0
35412     4710133.0
35421       67864.0
36200     9986966.0
36313       43459.0
Name: area, dtype: float64


Save area info to csv using pandas:

# Create an output path