Ujaval Gandhi
ujaval@spatialthoughts.com
Introduction to QGIS
Joins
Data Merging in GIS
Techniques for merging data from 2 separate layers in a GIS
Table Joins
Join a vector layer with a table.
Need to have a field in both layers with a common identifier
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.
fid | name | code |
1 | Afghanistan | AF |
2 | Albania | AL |
.. | .. | .. |
country | GDP per capita (USD) |
Afghanistan | 508 |
Albania | 5215 |
.. | .. |
Vector Layer
Table
fid | name | code | GDP per capita (USD) |
1 | Afghanistan | AF | 508 |
2 | Albania | AL | 5215 |
.. | .. | .. | .. |
New Vector Layer
Table Join in QGIS
Join Attributes by Field Value
Spatial Joins
Join 2 vector layers based on their spatial relationships.
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
fid | name | population |
1 | Houston | 2099451 |
2 | Chicago | 2695598 |
.. | .. | .. |
Cities
fid | name | code |
1 | Texas | TX |
2 | Illinois | IL |
.. | .. | .. |
States
fid | name | population | state |
1 | Houston | 2099451 | Texas |
2 | Chicago | 2695598 | Illinois |
.. | .. | .. | |
Cities with State Names
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
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
Earthquake with Closest Populated Place
fid | date | magnitude | place |
1 | 1894-06-20 | 6.6 | Tokyo |
2 | 2018–06-18 | 5.5 | Osaka |
.. | .. | .. | |
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)
fid | segment_id | length_km |
1 | 1234 | 10.5 |
2 | 2345 | 3.4 |
.. | .. | .. |
Roads
fid | name | code |
1 | Bellary | 1 |
2 | Belgaum | 2 |
.. | .. | .. |
Districts
fid | name | code | length_sum |
1 | Bellary | 1 | 1356.4 |
2 | Belgaum | 2 | 756.3 |
.. | .. | .. | |
District with Lengths
Spatial Joins in QGIS
Join Attributes by Location
Join Attributes by Nearest
Join Attributes by Location (Summary)
Raster Sampling
Join a vector layer with a raster layer
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
fid | name | state |
1 | Albany | NY |
2 | El Paso | TX |
.. | .. | .. |
Cities
Temperature (°C)
31.7
fid | city | state | temperature |
1 | Albany | NY | 22.3 |
2 | El Paso | TX | 31.7 |
.. | .. | .. | |
Cities with Temperature
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
fid | basin_name | basin_id |
1 | Cauvery | C1 |
2 | Krishna | K9 |
.. | .. | .. |
River Basins
Gridded Rainfall 2020 (mm/year)
787
fid | basin_name | basin_id | average_rainfall |
1 | Cauvery | C1 | 1156 |
2 | Krishna | K9 | 952.5 |
.. | .. | .. | |
River Basins with Rainfall
Raster Sampling in QGIS
Sample Raster Values
Zonal statistics
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))