1 of 27

Ujaval Gandhi

ujaval@spatialthoughts.com

Introduction to QGIS

Joins

2 of 27

Data Merging in GIS

Techniques for merging data from 2 separate layers in a GIS

  1. Table Joins
  2. Spatial Joins
  3. Raster Sampling

3 of 27

Table Joins

Join a vector layer with a table.

Need to have a field in both layers with a common identifier

4 of 27

Table Join Example

You have a shapefile of all countries with an attribute called “name” containing the name of the country

You also have an Excel file with 2 columns. A “country” column with country names and a “GDP per Capita” column.

You need to create a map of GDP per capita.

5 of 27

fid

name

code

1

Afghanistan

AF

2

Albania

AL

..

..

..

country

GDP per capita (USD)

Afghanistan

508

Albania

5215

..

..

Vector Layer

Table

6 of 27

fid

name

code

GDP per capita (USD)

1

Afghanistan

AF

508

2

Albania

AL

5215

..

..

..

..

New Vector Layer

7 of 27

Table Join in QGIS

Join Attributes by Field Value

8 of 27

Spatial Joins

Join 2 vector layers based on their spatial relationships.

9 of 27

Spatial Join Example 1

You have a Point layer of ‘cities’.

You have a Polygon layer of ‘states’.

You want to add the name of the state that each city belongs to.

Join Attributes by Location

10 of 27

fid

name

population

1

Houston

2099451

2

Chicago

2695598

..

..

..

Cities

fid

name

code

1

Texas

TX

2

Illinois

IL

..

..

..

States

11 of 27

fid

name

population

state

1

Houston

2099451

Texas

2

Chicago

2695598

Illinois

..

..

..

Cities with State Names

12 of 27

Spatial Join Example 2

You have a Point layer of ‘earthquakes’.

You have a Point layer of ‘populated places’.

You want to find the nearest populated place for each earthquake.

Join Attributes by Nearest

13 of 27

fid

date

magnitude

1

1894-06-20

6.6

2

2018–06-18

5.5

..

..

..

Earthquakes

fid

name

population

1

Tokyo

37393000

2

Osaka

2753862

..

..

..

Populated Places

14 of 27

Earthquake with Closest Populated Place

fid

date

magnitude

place

1

1894-06-20

6.6

Tokyo

2

2018–06-18

5.5

Osaka

..

..

..

15 of 27

Spatial Join Example 3

You have a Polygon layer of “districts”.

You have a Line layer of “roads”.

You want to calculate the length of roads in each district.

Join Attributes by Location (Summary)

16 of 27

fid

segment_id

length_km

1

1234

10.5

2

2345

3.4

..

..

..

Roads

fid

name

code

1

Bellary

1

2

Belgaum

2

..

..

..

Districts

17 of 27

fid

name

code

length_sum

1

Bellary

1

1356.4

2

Belgaum

2

756.3

..

..

..

District with Lengths

18 of 27

Spatial Joins in QGIS

Join Attributes by Location

Join Attributes by Nearest

Join Attributes by Location (Summary)

19 of 27

Raster Sampling

Join a vector layer with a raster layer

20 of 27

Raster Sampling Example 1

You have a point layer of ‘cities’.

You have a Raster layer of ‘Maximum Temperature’.

You need the maximum temperature at each city.

Sample Raster Values

21 of 27

fid

name

state

1

Albany

NY

2

El Paso

TX

..

..

..

Cities

Temperature (°C)

31.7

22 of 27

fid

city

state

temperature

1

Albany

NY

22.3

2

El Paso

TX

31.7

..

..

..

Cities with Temperature

23 of 27

Raster Sampling Example 2

You have a Polygon layer of “river basins”.

You have a Raster layer of “rainfall”.

You want to calculate the average rainfall in each basin

Zonal statistics

24 of 27

fid

basin_name

basin_id

1

Cauvery

C1

2

Krishna

K9

..

..

..

River Basins

Gridded Rainfall 2020 (mm/year)

787

25 of 27

fid

basin_name

basin_id

average_rainfall

1

Cauvery

C1

1156

2

Krishna

K9

952.5

..

..

..

River Basins with Rainfall

26 of 27

Raster Sampling in QGIS

Sample Raster Values

Zonal statistics

27 of 27

Raster Sampling Example 3

You have a Line layer of ‘streets’.

You have a Raster layer of ‘Elevation’.

You need to know the gradient of each street.

Drape (Set Z value from Raster))