Georeference, vectorize, and compare two maps of polygons
Territorial control in CAR, 2017
Territorial control in CAR, 2023
Civil war has been raging in the Central African Republic since 2012.
In 2018, Russian mercenaries started actively supporting the government.
After 2021, pro-government forces began a series of offensives to regain control of the country. Our question: How successful has this campaign been?
Overview of lab exercise and problem set
We will use image tracing to vectorize areas of control on a crowd-sourced map
Vectorized polygons
… to calculate percent of each province controlled by the CAR government in 2023
CAR government control, 2023
… and 2017
CAR government control, 2017
You will make this map for your problem set
Save as CAR_2023_CHANGE.png
We have two sources of data:
Category | Type | Format | Data source |
---|---|---|---|
Territorial control in CAR | Raster (non-spatial) | .png |
Wikipedia |
CAR province borders | Vector (polygons) | .geojson |
naturalearthdata.com |
These are all in the PS05.zip
file posted on Canvas.
Let’s open QGIS…
Always save your progress!
Go to Project
\(\to\) Save As...
Unlike the last lab, the map we’re georeferencing here has no graticule lines.
To georeference it, we’ll need to use a basemap of CAR as reference.
How can we georeference this?
Let’s open the basemap we’ll be using for georeferencing.
Go to Layer
\(\to\) Add Layer
\(\to\) Add Vector Layer...
.
Navigate to ne_10m_admin_1_CAR.geojson
in Data/World
This will load province-level (admin-1) boundaries for CAR
To open the georeferencer, click on Layer
\(\to\) Georeferencer
This will open an empty Georeferencer window.
To open our PNG image, go to File
\(\to\) Open Raster
Navigate to the Wiki_CAR_2017.png
image in Data/Control
. Click Open
The map image will appear in the Georeferencer’s top pane.
Go to Settings
menu \(\to\) Transformation settings
Set
Transformation type
\(=\) Polynomial 2
Target CRS
\(=\) EPSG:4326 - WGS 84
Output file
\(=\) select a folder, name the output file Wiki_CAR_2017_georeferenced.tif
Resampling method
\(=\) Nearest neighbor
Compression
\(=\) LZW
Save GCP points
Load in QGIS when done
Click OK
Try to set up the interface so that the Georeferencer window (left) and Project window (right) are both visible, or at least accessible, on your screen
Let’s place our first ground control point (GCP) at the southern-most tip of CAR. Click the point marked in red in the Georeferencer (left)
Rather than entering the \(x\) and \(y\) coordinates manually, click From Map Canvas
The view will switch to the Project window. Click the corresponding point on the administrative boundaries layer
The coordinates you selected will automatically appear in the Enter Map Coordinates
window. Click OK
Repeat this process for every three-way provincial border intersection, and as many recognizable outer boundary points as possible, until the GCPs have broad coverage (there are 32 GCPs in the example shown here)
Once you have a critical mass of GCPs, click on Start georeferencing
(\(\blacktriangleright\)) button. If the process is successful, you will see a notification at the top
Before closing the Georeferencer window, let’s check the quality of fit.
Back in the Project window, move the boundaries above the georeferenced raster in the Layers menu
Let’s make the countries layer semi-transparent. Double-click on the layer, go to Properties
\(\to\) Symbology
. Set Opacity
\(=\) 50
Check the quality of the fit. If you see any problematic mis-alignments, go back to Georeferencer, add more GCPs and run it again. The fit here seems ok…
Let’s now georeference the second map. To open a new Georeferencer window, go to File
\(\to\) Reset Georeferencer
(or just open and re-launch the window)
To open the other PNG image, go to File
\(\to\) Open Raster
Navigate to the Wiki_CAR_2023.png
image in Data/Control
. Click Open
Because the dimensions and extent of this map seem identical to the previous one, we will try to reuse the same GCPs. Go to File
menu \(\to\) Load GCP Points...
Navigate to the file Wiki_CAR_2017.png.points
in Data/Control
. Click Open
If the import was successful, you should see the same GCPs reappear in the Georeferencer window
Go to Settings
menu \(\to\) Transformation settings
Set
Transformation type
\(=\) Polynomial 2
Target CRS
\(=\) EPSG:4326 - WGS 84
Output file
\(=\) select a folder, name the output file Wiki_CAR_2023_georeferenced.tif
Resampling method
\(=\) Nearest neighbor
Compression
\(=\) LZW
Save GCP points
Load in QGIS when done
Click OK
If everything looks fine, click on Start georeferencing
(\(\blacktriangleright\)) button
You will see a notification at the top once the georeferencing is complete
Back in the Project window, both georeferenced rasters should be in the Layers menu.
Now let’s vectorize the raster (create polygons of government control)
Go to Layer
\(\to\) Create Layer
\(\to\) Create New Shapefile Layer...
Set
File name
\(=\) CAR_2017_control.shp
in Output
folderGeography type
\(=\) Polygon
EPSG:4326 - WGS 84
for the CRSLet’s add a new field to the attribute table:
Name
\(=\) Group
, Type
\(=\) Text
. Click Add to Fields List
Click OK
Let’s enable snapping. View
menu \(\to\) Toolbars
\(\to\) Snapping Toolbar
Click on the "Magnet button in the toolbar
For the other Snapping toolbar buttons, set the following parameters:
Active Layer
Vertex
Segment
Enable Topological Editing
Enable Snapping on Intersection
Enable Tracing
Let’s go into editing mode.
Right-click
on CAR_2017_control
in Layer menu \(\to\) Toggle Editing
This will activate the Editing Toolbar
Click on the Add Polygon Feature
icon (looks like a “blob” and a star, just to the right of the pencil, on the Editing Toolbar)
Tip: you may also want to hide the inactive layers while you vectorize.
For example, you don’t need (for now) the administrative boundary basemap (ne_10m_admin_1_CAR
) or the 2023 raster (Wiki_CAR_2023_georeferenced
). In the Layer menu, uncheck the boxes next to everything other than CAR_2017_control
and Wiki_CAR_2017_georeferenced
Make sure CAR_2017_control
is highlighted in the Layer menu. Start tracing the pink area (representing government control) by left-clicking on its boundary
As with polylines, each feature you create is a set of inter-connected line segments. The difference with polygons is that they are “closed sets” of vertices, meaning that the end point will (eventually) be at the same location as the start point
Each left-click ends one straight-line segment and begins another (make sure you add enough segments to capture the curvature of the border). As you keep adding vertices, the polygon will gradually take shape
Every once in a while, you’ll see green x’s appear while creating a new feature. This typically means one of two things:
These are both considered geometry errors, and should be avoided.
Sometimes (but not always), self-intersections “untangle” themselves as you add more vertices and refine the shape of the polygon:
With duplicate vertices, you’ll need to go back to get rid of the double point (by clicking delete/backspace)
As you approach the end of the loop, right-click to add the feature. You’ll be prompted to fill out the field information
For the id
field, enter the number of the polygon (e.g. “1” if it’s the first one you created). For Group
, enter GOVT
.
The new feature should appear on the map
Save your progress by right-clicking on CAR_2017_control
in the Layer menu, and selecting Save Layer Edits
.
Repeat the same steps for the other government-controlled area in the east of the country (there should be two such polygons total in the 2017 map)
The attributes here should be id
\(=\) 2
and Group
\(=\) GOVT
Here’s how the vectorized polygons should look when you’re done (reactivating the ne_10m_admin_1_CAR
layer for context)
Save your progress (again) by right-clicking on CAR_2017_control
in the Layer menu, and selecting Save Layer Edits
.
Turn off editing mode by right-clicking on CAR_2017_control
in the Layer menu, and de-selecting Toggle Editing
.
Let’s calculate the percent of each province under government control.
Open the Processing Toolbox (Geoprocessing
menu \(\to\) Toolbox
)
In the Processing Toolbox in the right, double-click
on Overlap analysis
in the Vector analysis
sub-menu
In the Overlap Analysis window, set
Input layer
\(=\) ne_10m_admin_1_CAR
Set
Overlay layers
\(=\) CAR_2017_control
Set
Overlap
(file output) \(=\) CAR_2017_ADM1.geojson
in Output
folder.Click Run
Check the log to make sure there were no errors
A new layer CAR_2017_ADM1
should appear in the map and Layer menu. Double-click
on it to open up its Properties
Set
Symbology type
\(=\) Graduated
Value
\(=\) CAR_2017_control_pc
Color ramp
\(=\) Blues
Set
Mode
\(=\) Pretty Breaks
Classify
Click OK
The province polygons should now be colored according to percent of territory controlled by the government
Make a New Print Layout
, fixing the legend as always and adding a scale bar
Export the image as CAR_2017_GOVT.png
The output file should look something like this
Problem Set 5
Your assignment:
Wiki_CAR_2023_georeferenced
CAR_2023_CHANGE.png
Can you make this map?
Let’s preview some of the steps. Create a New Shapefile Layer
for 2023: CAR_2023_control.shp
(other parameters same as before)
Set the layer to editing mode by Right-clicking
\(\to\) Toggle Editing
Click on the Add Polygon Feature
tool. The snapping parameters should be the same as before
Trace new polygon features and enter field information like before (remember to hide the inactive layers while you do this)
Save your progress as you go.
But how do we create holes in polygons to account for more complex geometries?
Start by making the CAR_2023_control
layer semi-transparent (Opacity
\(=\) 50%
), so we can see the raster underneath
The tool we need is in the Advanced Digitizing Toolbar. To access it, go to
View
\(\to\) Toolbars
\(\to\) check box next to Advanced Digitizing Toolbar
Click on Add Ring
tool in the Advanced Digitizing Toolbar
(green blob with a hole in it, yellow star)
Using the Add Ring
tool, trace the contour of the first polygon hole, as if you were tracing another polygon
To close the loop, right-click
anywhere on the screen
This should add a hole to the polygon you had vectorized
Repeat this process for each hole you need to carve out
Add the remaining government-controlled polygons (there should be 6 non-contiguous government polygons here)
Save your progress and leave editing mode
Go back to the Overlap analysis
tool
In the Overlap Analysis window, set
Input layer
\(=\) ne_10m_admin_1_CAR
Set
Overlay layers
\(=\) CAR_2017_control
, CAR_2023_control
(select both)Set
Overlap
(file output) \(=\) CAR_2023_ADM1.geojson
in Output
folder.Click Run
The new layer CAR_2023_ADM1
should appear in the map and Layer menu
If you want, you can use this map to create the 2023 counterpart to the first image we created (for practice only; this in not required and not part of the assignment)
Let’s calculate the difference between 2023 and 2017. Open the Attribute Table
for CAR_2023_ADM1
, go to Field Calculator
Create new field called CHANGE_CTR
of type Decimal number (real)
. For the Expression
, write CAR_2023_control_pc - CAR_2017_control_pc
. Click OK
Map this new variable. In Properties
, set
Symbology type
\(=\) Graduated
Value
\(=\) CHANGE_CTR
Color ramp
\(=\) All Color Ramps
\(\to\) RdBu
Try setting Mode
\(=\) Pretty Breaks
. Click Classify
.
Check to make sure the breaks are centered on 0 (as they are here). If not…
If Pretty Breaks
are not centered on 0, switch to Equal Interval
with Symmetric Classification
set to Around
\(=\) 0
. Click Classify
When you’re done, the distribution of colors should look roughly like this
At this point, you’re ready to create a New Print Layout
, export, and you’re done
This is what the exported png
file should look like. Good luck!