-
Notifications
You must be signed in to change notification settings - Fork 18
/
introduction-to-qgis.Rmd
1485 lines (957 loc) · 82.9 KB
/
introduction-to-qgis.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Introduction to QGIS (Full Course)"
author: "Ujaval Gandhi"
subtitle: "A Comprehensive Introduction to Mapping and Spatial Analysis with QGIS."
output:
# pdf_document:
# toc: yes
# toc_depth: '3'
# latex_engine: xelatex
html_document:
df_print: paged
highlight: pygments
includes:
after_body: comment.html
toc: yes
toc_depth: 3
fontsize: 12pt
header-includes:
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \renewcommand{\footrulewidth}{0.4pt}
- \fancyhead[LE,RO]{\thepage}
- \geometry{left=1in,top=0.75in,bottom=0.75in}
- \fancyfoot[CE,CO]{{\includegraphics[height=0.5cm]{images/cc-by-nc.png}} Ujaval Gandhi
http://www.spatialthoughts.com}
classoption: a4paper
---
\newpage
***
```{r echo=FALSE, fig.align='center', out.width='75%', out.width='250pt'}
knitr::include_graphics('images/spatial_thoughts_logo.png')
```
***
\newpage
# Introduction
This is an introductory course that covers QGIS from the very basics. You will learn to use QGIS for mapping, spatial data processing, and spatial analysis. This class is ideal for participants with a basic knowledge of GIS and who want to learn how to use QGIS to carry out everyday GIS tasks.
[![Watch the video](https://img.youtube.com/vi/pGm7w-LywO0/mqdefault.jpg)](https://www.youtube.com/watch?v=pGm7w-LywO0){target="_blank"}
[Watch the Video ↗](https://www.youtube.com/watch?v=pGm7w-LywO0){target="_blank"}
[Access the Presentation ↗](https://docs.google.com/presentation/d/1Lw0NQlLQXUSW1Mf1rzy9pLb_m2wxSUHQDxF-vjPTBaU/edit?usp=sharing){target="_blank"}
# Software
This course requires QGIS LTR version 3.34.x.
Please review [QGIS-LTR Installation Guide](install-qgis-ltr.html) for step-by-step instructions.
# Get the Data Package
The exercises and challenges in this course use a variety of datasets. All the required datasets are supplied in the ``introduction_to_qgis.zip`` file. Unzip this file to a directory - preferably to the ``<home folder>/Downloads/introduction_to_qgis/`` folder.
Download [introduction_to_qgis.zip](https://github.com/spatialthoughts/courses/releases/download/data/introduction_to_qgis.zip).
> Note: Certification and Support are only available for participants in our paid instructor-led classes.
# Get the Course Video
The course is accompanied by a video covering the all the modules. This videos is recorded from our live instructor-led class and is edited to make them easier to consume for self-study. We have 2 versions of the videos:
## YouTube
The video on YouTube is ideal for online learning and sharing. You may also turn on *Subtitles/closed-captions* and adjust the playback speed to suit your preference. [Access the YouTube Video ↗](https://www.youtube.com/watch?v=pGm7w-LywO0){target="_blank"}
## Vimeo
We have also made full-length video available on Vimeo. This video can be downloaded for offline learning. [Access the Vimeo Video ↗](https://vimeo.com/showcase/11316863?share=copy){target="_blank"}
# Configuration and Setup
## Enable Required Toolbars
We will be using several toolbars in this course. To ensure you have the required tools for the exercises, go to *View* menu, select *Toolbars* and ensure that the following toolbars are checked.
- Attributes Toolbar
- Data Source Manager Toolbar
- Digitizing Toolbar
- Label Toolbar
- Map Navigation Toolbar
- Project Toolbar
- Selection Toolbar
- Snapping Toolbar
```{r echo=FALSE, fig.align='center', out.width='60%'}
knitr::include_graphics('images/introduction_to_qgis/toolbars.png')
```
## Install Required Plugins
We will be using the following plugins during the course. Go to From the *Plugins* menu, choose *Manage and Install Plugins...*. Under the *All* tab, search for the plugin name and click on the *Install Plugin* button to install it.
- QuickMapServices
- QuickOSM
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/plugins1.png')
```
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/plugins2.png')
```
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/plugins3.png')
```
# 1. Creating Maps
This section is designed to help you get familiar with the basic workflow of importing data layers, applying symbology, adding labels, and designing layouts for maps. We will take a text file containing historical records of earthquakes and turn it into an informative visualization like the one below.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquake_map.png')
```
## 1.1 Importing Vector Data
1. Open QGIS. The first step is to import the source datasets. Click on the *Open Data Source Manager* button.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part1_1.png')
```
2. Select the *Vector* tab. Click the *...* button next to *Vector Dataset(s)* and browse to the data directory.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part1_2.png')
```
3. Select the `ne_10m_land.shp` file and click *Open*. In the *Data Source Manager* window, click *Add*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part1_3.png')
```
4. A new layer, `ne_10m_land` will be added to the *Layers* panel and displayed on the Canvas. This layer contains polygons representing the land areas of the world. Click on the *Open Data Source Manager* button again.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part1_4.png')
```
5. Click the *...* button next to *Vector Dataset(s)* and browse to the data directory. Select the `gem_active_faults_harmonized.gpkg` file and click *Open* followed by *Add*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part1_5.png')
```
6. A new layer, `gem_active_faults_harmonized` will be added to the *Layers* panel and displayed on the Canvas. This is a global layer containing lines representing all the active faults. We will now import another layer of earthquake points. Click on the *Open Data Source Manager* button again.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part1_6.png')
```
7. Select the *Delimited Text* tab. Click the *...* button next to *File name* and browse to the data directory. Select the `significant_earthquakes_2000_2020.tsv` file. This is a text file in the *Tab-Separated Values (TSV) format*. In the *File Format* section, select *Custom delimiters*.
> Note: Windows users may need to change the *File Type* as **All** in *Choose a Delimited Text File to Open* dialog to see the TSV file.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part1_7.png')
```
8. Check the *Tab* checkbox. In the *Geometry Definition* section, ensure **Longitude** is selected as the *X Field* and **Latitude** is selected as the *Y Field*. Choose **EPSG:4326** as *Geometry CRS*. Leave other options to their default values and click *Add*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part1_8.png')
```
9. A new layer, `significant_earthquakes_2000_2020` will be added to the *Layers* panel and displayed on the Canvas. This layer contains over 1000 records of significant earthquakes recorded between 2000 and 2020. Right-click on the `significant_earthquakes_2000_2020` layer and select *Open Attribute Table*. Examine all the attributes and their values.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part1_9.png')
```
10. We will now learn about some of the tools to query and select records. From the *Selection Toolbar*, click the *Select Features by Value...* button.
> Note: If the selection toolbar is not enabled, right-click on the toolbar panel and check **Selection Toolbar**.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part1_10.png')
```
11. In the *Select Features* dialog, enter **2020** as the *Year* and click the *Select Features* button. You will see all earthquakes that occurred during 2020 will be highlighted in yellow. You may also click the *Flash Features* button to see the selected records blink.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part1_11.png')
```
12. Let's refine the query a little more. Enter **7** as the *Mag* parameter and set the criteria as *Greater than (>)*. Click *Select Features*. You will now see only those points where the earthquake occurred in 2020, and its magnitude was greater than 7. Close the window.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part1_12.png')
```
13. Right-click on the `significant_earthquakes_2000_2020` layer and select *Open Attribute Table*. You will see that there are 6 selected features in the layer. If you want to examine their attributes, there is a handy shortcut. Click the *Move selection to top* button.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part1_13.png')
```
14. All the selected rows will be displayed on the top of the attribute table - making it easy to examine the selected features. Click the *Deselect all features from the layer* button.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part1_14.png')
```
15. For our map, we need another layer of 10 largest earthquakes - so we can style it differently than other earthquakes. For our visualization, we will define the largest earthquakes like the ones that resulted in the highest number of deaths. Locate the *Total Deaths* attribute and click twice on the column header. This will sort the features in descending order of the values in this column.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part1_15.png')
```
16. Hold the *Shift* key and select the first 10 rows. This selection will be the 10 earthquakes with the high fatalities.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part1_16.png')
```
17. We will save the selected 10 features as a new layer. Right-click the `significant_earthquakes_2000_2020` layer and go to *Export → Save Selected Features As..*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part1_17.png')
```
18. Select **GeoPackage** as the *Format*. Click the *...* button next to *File name* and browse to the data directory. Name the layer as `large_earthquakes.gpkg`. Click *Save*. Click *OK*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part1_18.png')
```
19. A new layer, `large_earthquakes` will be added to the *Layers* panel.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part1_19.png')
```
20. Our data preparation is now complete. Let's save our work. Go to *Project → Save*. Browse to the data directory and enter the name as **Earthquakes**. Click *Save*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part1_20.png')
```
21. The project will be saved in the *QGZ* format as a file.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part1_21.png')
```
We have now finished the first part of this exercise. Your output should match the contents of the `Earthquakes_Checkpoint1.qgz` file in the `solutions` folder.
### 1.1.1 Challenge
Do you know about [Null Island](https://en.wikipedia.org/wiki/Null_Island)? The `ne_10m_land` contains a polygon for this feature. Locate this polygon on the map.
Hint: Open the attribute table, find and select the feature for Null island. Then use the *Zoom map to the selected rows* button.
## 1.2 Symbology
The symbology of a layer is its visual appearance on the map. We will now learn different techniques for styling each layer to convey the information visually.
1. Select the ``ne_10m_land`` layer and click *Open the Layer Styling Panel*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part2_1.png')
```
2. We will style this layer with a simple grey color. Click *Simple Fill* to reveal more options. Click the dropdown next to *Fill color*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part2_2.png')
```
3. Use the color picker to select a light shade of grey color. The *Layer Styling Panel* is interactive, so you can immediately preview your styling changes in the map canvas.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part2_3.png')
```
4. Similarly, change the *Stroke color* to white.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part2_4.png')
```
5. Next, we will style the faults layer. Select ``gem_active_faults_harmonized`` layer in the *Layers* panel. Click the *Simple Line* symbol to reveal more styling options. Change the *Color* to a shade of brown. Set the *Stroke width* to **0.1**.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part2_5.png')
```
6. Now we will change the style of the earthquake points. Select the ``significant_earhtquakes_2000_2020`` layer. Click the *Simple Marker* symbol. Change the *Size* option to **0.7** *Millimeters*. Select red as the *Fill color* and white as the *Stroke color*. Change the *Stroke width* to **0.1**.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part2_6.png')
```
7. We have now styled the three background layers. We will style the ``large_earthquakes`` layer, which features the main information we want to convey through this map. We will use a *Proportional Circle* style and have the size of the circle represent the total fatalities caused by the respective earthquake. Click *Simple Marker* to see more styling options. Click the *Data defined override* button next to *Size* and choose **Assistant**.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part2_7.png')
```
8. In the *Input* section, select **Total Deaths** as the *Source*. Set the range of *Values from* **5000** *to* **500000**. In the *Output* section, set the range of *Size from* **3** *to* **10**. This will use the attribute value in the *Total Deaths* field to set the size of the circles. Click the *Go Back* button at the top of the panel to return to the previous menu.
> The default *Scale method* used by QGIS is **Flannery**. This method applies a non-linear scaling to compensate for human perception of areas. [Learn more. ↗](https://makingmaps.net/2007/08/28/perceptual-scaling-of-map-symbols/){target="_blank"}
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part2_8.png')
```
9. Click the *Fill color* and adjust the transparency. This will help show the information under the large circles.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part2_9.png')
```
10. Set the *Stroke color* to white.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part2_10.png')
```
11. The circles represent the number of deaths caused by each earthquake. But the reader of our map will not know what these sizes represent. It would help interpret the map better if we had a legend. Let's set a legend for this layer. Click the *Marker* symbol. At the bottom of the panel, select *Advanced → Data-defined Size Legend*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part2_11.png')
```
12. Choose the *Collapsed legend* option. Check the *Manual size classes* option. Click the *+* button to add the class definition manually.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part2_12.png')
```
13. Set three different size classes for **5000**, **50000** and **500000**. You will see a legend in the *Layers* panel showing the circle sizes and corresponding fatality values.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part2_13.png')
```
14. Click the *Save Project* button to save your work.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part2_14.png')
```
We have now finish the second part of this exercise. Your output should match the contents of the `Earthquakes_Checkpoint2.qgz` file in the `solutions` folder.
### 1.2.1 Challenge
QGIS has many rich cartography features. One of my favorites is called *Live Layer Effects*. This allows you to add effects such as *Outer Glow*, *Drop Shadow*, etc., to each symbol. This takes your symbology to the next level and helps highlight certain features. Select the `large_earthquakes` layer and open the *Layer Styling Panel*. Expand the *Layer Rendering* section and enable *Draw effects*. Click the *Customize effects* button and add a drop shadow effect to the layer.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part2_challenge.png')
```
## 1.3 Labelling
Labels are a useful way to convey additional information for any features. Labels are associated with each feature and can be configured to show information from the attributes. We will now add labels to each of the large earthquake points to show the name of the location as well as the deaths caused due to that earthquake.
1. Before we proceed further, let's change the projection of our map to a more appropriate one. The preferred and modern choice for global maps is the [Equal Earth](http://equal-earth.com/) projection. It is much more visually appealing and also preserves relative areas of continents. Go to *Project → Properties...*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part3_1.png')
```
2. Switch to the *CRS* tab. Search *8857* and select the **WGS84 / Equal Earth Greenwich** CRS. Click *OK*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part3_2.png')
```
3. Back in the QGIS Window, you will notice that the shape of the map looks different now. The bottom-right corner also displayed the current project CRS *EPSG:8857*. Select the ``large_earthquakes`` layer and open the *Layer Styling Panel*. Switch to the *Labels* tab.
> Note: Changing the Project CRS does not change the CRS of the layers, but reprojects them on-the-fly to the chosen CRS for display.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part3_3.png')
```
4. Select *Single Labels*. We will combine the values from multiple attributes for the label using an expression. Click the *Expression* button next to *Value*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part3_4.png')
```
5. Locate the **Location Name** attribute under *Fields and Values* group. Double-click to add it to the expression. You can check the *Preview* at the bottom to see the result of the expression.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part3_5.png')
```
6. We will create a longer label text by combining multiple attributes. You can use the **||** operator in the QGIS expression to concatenate strings. Create the expression as shown below. Click *OK*.
```
"Location Name" || ';' || 'Deaths:' || "Total Deaths"
```
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part3_6.png')
```
7. The labels will be rendered next to the points on the canvas. Select the *Formatting* tab. At the bottom, enter **;** as the value for *Wrap on character* and **20** characters as the value for *Wrap lines to*. This will break the labels into multiple lines and make them readable.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part3_7.png')
```
8. Next, we will change the appearance of the label text. Switch to the *Text* tab. Change the *Size* to **8** and set the *Color* to white.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part3_8.png')
```
9. A useful labeling technique is to add a background to the labels to improve legibility. Switch to the *Background* tab and enable *Draw background*. Set the *Color* to black. Also, set the *Size X* and *Size Y* of *Buffer* to be **1** point. At the bottom, set the *Radius X,Y* to **5**.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part3_9.png')
```
10. To attach the labels to each symbol, you can use a leader line. Switch to the *Callouts* tab and check *Draw callouts*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part3_10.png')
```
11. As we have only a few labels, we can adjust their placement manually to fit the layout better. Close the *Layer Styling Panel*. Right-click anywhere on the *Toolbar* and select the *Label Toolbar* to activate it.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part3_11.png')
```
12. Once the *Label Toolbar* is activated, you will see new tools. Select the *Move a Label, Diagram, or Callout* tool.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part3_12.png')
```
13. Click on any of the labels. The first time you do this, you will be prompted with an *Auxiliary Storage: Choose Primary Key* dialog. This is used to store the position of the labels for each label. You can choose any field containing unique values. In our case, the default value **fid** is fine. Click *OK*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part3_13.png')
```
14. Click on the label again to start moving it. Drag your mouse to the new label location and click again to move the label.
> Tip: If a label disappears after moving it, it means that it has been placed at a position that cannot be displayed on the map without colliding with other labels. You can see the unplaced labels by clicking the *Toggle Display of Unplaced Labels* button on the *Label Toolbar*. Once the label is shown, you can move it to another spot where it can be displayed.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part3_14.png')
```
15. Similarly, move other labels to appropriate locations. Once you are satisfied, save your work.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part3_15.png')
```
We have now finished the third part of this exercise. Your output should match the contents of the `Earthquakes_Checkpoint3.qgz` file in the `solutions` folder.
### 1.3.1 Challenge
The numbers displayed in the labels can be hard to read since they are not formatted. We can make them readable by adding a thousand-separator. So a number such as *227899* is displayed as *227,899* and a number like *5749* as *5,749*. Update the expression for the labels, so the numbers are formatted. To achieve this, you can use the `format_number()` function in the QGIS expression editor.
## 1.4 Print Layout
QGIS comes with a rich set of tools to create map layouts that allow you to add elements such as labels, images, legend, scale bar, north arrow, etc., to your map. It also allows you to export the map layout as an image or a PDF. We will now take the visualization created in the QGIS map canvas and create a print layout.
1. Go to *Project → New Print Layout...*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part4_1.png')
```
2. You will be prompted by a *Create Print Layout* dialog asking you to *Enter a unique print layout title*. You can leave it blank and click *OK*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part4_2.png')
```
3. A new window will open with the title *Layout 1*. First, let's set the page size. Right-click on the canvas and select *Page Properties...*. Select *Size* as **A4** and *Orientation* as **Landscape**.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part4_3.png')
```
4. Next, go to *Add Item → Add Map*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part4_4.png')
```
5. Hold the left mouse button and select the canvas part where you want the map to appear. We will use the full width for the map but leave some space at the top for a title and at the bottom for additional information.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part4_5.png')
```
6. The map will be rendered at the chosen location. You can adjust the scale to make the map fit the area and enter **120000000** as the value for *Scale*. You can also select the *Interactively Edit Map Extent* button and click on the map to move it.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part4_6.png')
```
7. Once you are satisfied with the map view, go to *Add Item → Add Label*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part4_7.png')
```
8. Drag a rectangle at the top. On the right-hand side *Item Properties* panel, enter **10 Largest Earthquakes (2000-2020)** as the text. Click on the *Font* button under *Appearance* and set the *Font Size* to **24**. You can also center align the label by selecting **Center** as the *Horizontal alignment* and **Middle** as the *Vertical Alignment*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part4_8.png')
```
9. We will now add a legend to the map. Go to *Add Item → Add Legend*. Draw a rectangle at an empty region on the map.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part4_9.png')
```
10. Once the legend is added, un-check the *Auto update* box. This will allow us to customize the items in the legend. Select the ``ne_10m_land`` layer and click the *Remove selected item(s) from legend*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part4_10.png')
```
11. You can also edit the layer name to be more suitable for the map. Select the ``gem_active_faults_harmonized`` layer and click the *Edit selected item properties* button. Change the name of the layer to ``Faults``. Similarly, change the ``large_earthquakes`` layer name to ``Deaths``.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part4_11.png')
```
12. Select the ``Total Deaths`` layer and click *Remove selected item(s) from legend*. The legend now contains only the items that are easy to interpret and add context to the symbols on the map.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part4_12.png')
```
13. You can also add a border to all the elements by enabling the *Frame* under the respective *Item Properties*. Next, we will add some logos to the map. Go to *Add Item → Add Picture*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part4_13.png')
```
14. Add the picture to the bottom-right corner. Locate the *App Symbols → logos* folder in the *Item Properties* panel. Select the **Made with QGIS** logo. You can also add your organization's logo by browsing an SVG/PNG file on your computer.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part4_14.png')
```
15. A good practice is to credit the data sources used to create the map. This can be added using *Add Item → Add Label*. Once you are satisfied with the layout, you can export it. Go to *Layout → Export as Image...*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part4_15.png')
```
16. Browse to the data folder and enter the name of the image as **large_earthquakes.png**. Click *Save*. You will be prompted to choose *Image Export Options*. Leave the settings to default and click *Save*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part4_16.png')
```
17. The layout will be saved as a static image. You can embed this image in a report, presentation, or share it on social media.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part4_17.png')
```
We come to the end of this exercise. Your output should match the contents of the `Earthquakes_Checkpoint4.qgz` file in the `solutions` folder.
### 1.4.1 Challenge
Export your layout as a PDF.
Hint: The *PDF Export Options* dialog has an option *Simplify geometries to reduce output file size* at the bottom. While useful, this can have unexpected effect on the output. Un-check it while doing the export.
```{r echo=FALSE, fig.align='center', out.width='75%', fig.cap='Print Layout Exported as a PDF'}
knitr::include_graphics('images/introduction_to_qgis/earthquakes_part4_challenge.png')
```
# 2. Visualizing Spatial Data
In this section, we will learn basic data processing and visualization techniques. We will use geographic boundaries and population count data for the City of New York and create a population density map. This requires doing a table join and using a graduated symbology to create a choropleth map.
## Concept: Joins
[![View Presentation](images/introduction_to_qgis/joins_presentation.png){width="400px"}](https://docs.google.com/presentation/d/1YJfWrjSDeriYJMsA6bmVi4zZmzLmGOo5dm13Lr5JaiI/edit?usp=sharing){target="_blank"}
[View the Presentation ↗](https://docs.google.com/presentation/d/1YJfWrjSDeriYJMsA6bmVi4zZmzLmGOo5dm13Lr5JaiI/edit?usp=sharing){target="_blank"}
## 2.1 Table Join
1. Open QGIS. The first step is to import the source datasets. Click on the *Open Data Source Manager* button.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/population_part1_1.png')
```
2. Select the *Vector* tab. Click the *...* button next to *Vector Dataset(s)* and browse to the data directory. Select the `nynta2010.shp` file and click *Open*. In the *Data Source Manager* window, click *Add*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/population_part1_2.png')
```
3. You may be prompted to *Select Transformation for nynta2010*. The source shapefile is in the `EPSG:2263 NAD83 / New York Long Island (ftUS)` projection whereas the default projection in QGIS is `EPSG:4326 - WGS84`. This dialog presents several transformations to convert between the coordinates between these projections. Choose the first option and click *OK*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/population_part1_3.png')
```
4. A new layer `nyta2010` will be added to the *Layers* panel and will be displayed on the Canvas. This layer contains polygons representing the *Neighborhood Tabulation Areas (NTAs)* for New York city. Right-click on the `nyta2010` layer and select *Open Attribute Table*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/population_part1_4.png')
```
5. Examine the attributes of the layer. The `NTACode` field contains a unique identifier for each polygon. Notice that we do not have any population or demographic attributes in this layer.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/population_part1_5.png')
```
6. The population and other demographic datasets are typically distributed as tables. These tables would have the same unique identifier as the shapefile that can be used to merge the relevant fields to the vector layer. Let's import a table representing *New York City Population By Neighborhood Tabulation Areas*. Click on the *Open Data Source Manager* button.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/population_part1_6.png')
```
7. Switch to the *Delimited Text* tab. Browse to the ``New_York_City_Population_By_Neighborhood _Tabulation_Areas.csv`` file and select it. Since this CSV file is just tabular data, select *No geometry (attribute only table)* option and click *Add*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/population_part1_7.png')
```
8. Once the new tabular layer ``New_York_City_Population_By_Neighborhood _Tabulation_Areas`` is added to the *Layers* panel, right-click on it and select *Open Attribute Table*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/population_part1_8.png')
```
9. This table has a **Population** column with the population for each of the tabulation areas. We also have the **NTA Code** column containing the same codes as our `nynta2020` layer. We can use this column to join this table with the vector layer.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/population_part1_9.png')
```
10. If you double-click the *NTA Code* column to sort the table by the code, you will notice that each neighborhood has 2 records of population. For this exercise, we want to use the population for the year 2010. We will apply a filter to select only the population records for 2010.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/population_part1_10.png')
```
11. In the main QGIS window, right-click the ``New_York_City_Population_By_Neighborhood _Tabulation_Areas`` layer and select *Filter*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/population_part1_11.png')
```
12. In the *Query Builder* dialog, enter the filter expression as below. You can also double-click the column name to insert them in the expression. Click *OK*.
```
"Year" = 2010
```
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/population_part1_12.png')
```
13. Now we will do the table join. Open **Processing → Toolbox** from the main menu at the top.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/population_part1_13.png')
```
14. Search and locate the algorithm **Vector general → Join attributes by field value** and double-click to launch it.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/population_part1_14.png')
```
15. In the *Join Attributes by Field Value* dialog, select ``nynta2010`` as the *Input layer* and ``NTACode`` as the *Table field*. Select ``New_York_City_Population_By_Neighborhood _Tabulation_Areas`` as the *Input layer 2* and ``NTA Code`` as the *Table field 2*. Click the *...* button next to *Layer 2 fields to copy*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/population_part1_15.png')
```
16. We want to copy only the population data, so select the `Population` field and click *OK*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/population_part1_16.png')
```
17. Next, we need to configure the output. Click the *...* button next to *Joined layer* and select *Save to File...*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/population_part1_17.png')
```
18. Browse to the data directory and name the output as `nynta_with_population`. Make sure the file type is selected as *GPKG files (\*.gpkg)*. Click *Save*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/population_part1_18.png')
```
19. Once the configuration is complete, click the *Run* button.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/population_part1_19.png')
```
20. Upon completion of processing, a new layer `nynta_with_population` will be added to the *Layers* panel. Right-click the layer and select *Open Attribute Table*. You will see that we now have an additional column **Population** in the attribute table. The table also has a column **Shape_Area** containing the area of each polygon in Sq.Ft.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/population_part1_20.png')
```
21. Our goal is to map the population density. We can use the population count and area columns and calculate a new column for population density. From the Processing Toolbox, search and locate the algorithm **Vector table → Field Calculator** and double-click to launch it.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/population_part1_21.png')
```
22. In the *Field Calculator* dialog, enter ``Density`` as the *Field Name*. We will now build the expression to calculate population density. From the function groups next to the *Expression* panel, expand the *Fields and Values* section. Double-click the ``Population`` field to add it to the expression editor. Note that fields are referred using double-quotes (") in QGIS.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/population_part1_22.png')
```
23. Select the **/** button to enter the division operator and then click the ``Shape_Area`` field to enter it. You may also type the expression instead of picking the values from the dialog. The final expression should look like below.
```
"Population" / "Shape_area"
```
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/population_part1_23.png')
```
24. This will give us the population density in persons per square feet. A more appropriate unit for population density is persons per square miles. Let's convert the value to miles using the conversion factor or *1 mile = 5280 ft*. Change the expression as shown below. Once done, select *Save to File*.
```
5280 * 5280 * ("Population" / "Shape_area")
```
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/population_part1_24.png')
```
25. Name the output as `nynta_population_density.gpkg` and click *Run*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/population_part1_25.png')
```
26. Once the processing finishes as new layer `nynta_population_density` will be added to the *Layers* panel. Open the attribute table and verify that you have a new column named *Density*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/population_part1_26.png')
```
27. We don't need the other layers in our project. Hold the *Shift* key and select all layers except `nynta_population_density`. Right-click and select *Remove Layer...*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/population_part1_27.png')
```
28. Let's save our work so we can retrieve it later. Go to **Project → Save**.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/population_part1_28.png')
```
29. Save the project as `NYC_Population_Density` and click *Save*. QGIS will save the project file in the *QGZ* format.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/population_part1_29.png')
```
We have now finish the first part of this exercise. Your output should match the contents of the `NYC_Population_Density_Checkpoint1.qgz` file in the `solutions` folder.
### 2.1.1 Challenge
Round the population density values to the nearest integer and store them in another column named **Density_Round**.
- Hint1: Use the *Field Calculator* algorithm from the processing toolbox.
- Hint2: The QGIS expression engine has a function named `round()` that can round a fraction to the chosen number of decimal places.
## Concept: Data Normalization
[![View Presentation](images/introduction_to_qgis/normalization_presentation.png){width="400px"}](https://docs.google.com/presentation/d/1cB6sF_Lo3w2YKhhxSPZdKKaYVT2ID91Jcvf-E0P4MUk/edit?usp=sharing){target="_blank"}
[View the Presentation ↗](https://docs.google.com/presentation/d/1cB6sF_Lo3w2YKhhxSPZdKKaYVT2ID91Jcvf-E0P4MUk/edit?usp=sharing){target="_blank"}
## 2.2 Creating a Choropleth Map
1. Continuing the exercise, we will now visualize the spatial distribution of population density in form of a choropleth map. From the *Layers* panel, click the *Open the Layer Styling panel* button.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/population_part2_1.png')
```
2. Select the ``Graduated`` renderer.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/population_part2_2.png')
```
3. As we want to map the population density, choose ``Density`` as the *Value*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/population_part2_3.png')
```
4. Next we select a color ramp. Click the drop-down button next to *Color Ramp*, select *All Color Ramps* and pick the ``YlOrBr`` (Yellow-Orange-Brown) ramp.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/population_part2_4.png')
```
5. Change the *Classes* value to ``6`` and click *Classify*. You will see each polygon colored according to the population density attribute.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/population_part2_5.png')
```
6. The default mode of classification is *Quantile* - which divides the input data such that all 6 classes have approximately equal number of features. There are other modes of classification as well. You can learn more about Data Classification Modes in the [QGIS Documentation](https://docs.qgis.org/testing/en/docs/user_manual/working_with_vector/vector_properties.html?#graduated-renderer). We can also define custom data ranges for each class. Click on the *Values* column for the first row in the classification table. Change the *Upper value* to `20000`.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/population_part2_6.png')
```
7. Similarly change other class ranges so they become easy to interpret. The last row contains the upper value to the maximum value in the dataset. Instead of displaying the maximum value, we can update the label. Click the *Legend* column for the last row.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/population_part2_7.png')
```
8. Change the label to `> 100000`.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/population_part2_8.png')
```
9. Now you have an informative visualization of population density in New York city with an easy to interpret legend. Click the *Save* button to save your work. All your visualization settings are saved along with the project, so next time when you load this project in QGIS, you will see the same visualization.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/population_part2_9.png')
```
We have now finish the second part of this exercise. Your output should match the contents of the `NYC_Population_Density_Checkpoint2.qgz` file in the `solutions` folder.
### 2.2.1 Challenge
Create a new layer containing all the neighborhood tabulation areas having a population density > 100000.
Hint: You can use the *Extract by Attribute* algorithm from the Processing Toolbox.
# Assignment
The following assignment is designed to help you practice the skills learnt so far in the course and explore the Print Layout.
Your task is to design a map of *Population Density of New York City* in QGIS Print Layout. You can take the choropleth map created in the previous section [2.2 Create a Choropleth Map](#creating-a-choropleth-map) and design a map in the Print Layout. In addition to the rendered vector layer, the map must have at least the following elements:
* Scale Bar
* Legend
* Data Attribution
You may also optionally add other elements such a north arrow, logos etc. Feel free to use your creativity to customize the style and map layout. Below is an example map for inspiration.
```{r echo=FALSE, fig.align='center', out.width='60%', fig.cap='Example Map Design for Assignment'}
knitr::include_graphics('images/introduction_to_qgis/assignment.png')
```
# 3. Georeferencing
Georeferencing is the process of assigning real-world coordinates to each pixel of the raster. This is an important step in preparing your data for further analysis. Many projects, particularly machine learning projects - need continuous historic records to build a model. Many of the older datasets may come in form of scanned maps or aerial photos that needs to be georeferenced. Similarly, some organizations may only share a PDF or a static map image of the dataset which will need to be converted into a GIS-ready format using georeferencing process.
Georeferencing process involves collecting GCPs (Ground Control Points) or Tie-Points. These GCPs are easily identifiable features in the image or map whose real-world coordinates are obtained from field-survey using a GPS device or identified from already georeferenced sources within a GIS.
In this exercise, you're going to georeference an old scanned map of Bangalore, India created in 1924. This map is possibly hand-drawn and has no coordinate markings, so we will use a tiled basemap layer to locate the features and obtain the GCPs.
## 3.1 Using Basemaps
1. Open QGIS. We will use a plugin called **QuickMapServices** to load a basemap. From the *Plugins* menu choose *Manage and Install Plugins...*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/georef_part1_1.png')
```
2. The *Plugins* dialog contains all the available plugins in QGIS. Under the *All* tab, search for **quickmapservices**. It has different basemaps that can be used based on your purpose. Click on the *Install Plugin*, to add this plugin to QGIS.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/georef_part1_2.png')
```
3. Once installed, check the box next to the *QuickMapServices* label to enable it. Click *Close*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/georef_part1_3.png')
```
4. Now you will see a new *Web* menu added to the menu-bar. Go to *Web → QuickMapServices* menu. You will see some map providers and available basemaps. We can enable a few more providers to have many more options. Click on the *Web → QuickMapServices → Settings*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/georef_part1_4.png')
```
5. In the *Settings* dialog, switch to the *More services* tab. Click on the ``Get contributed pack`` to download 3rd-party basemaps.
> You will see a warning against using contributed services. Some of these services may have restrictions on their usage and/or attribution requirement that you need to follow. Please review them before using them in your project.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/georef_part1_5.png')
```
6. Once the new services are added, you will see many more options in the *Web → QuickMapServices* menu.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/georef_part1_6.png')
```
7. For our current task, we will use a basemap based on OpenStreetMap data. Since we need to locate the features in the scanned maps let's add the *OSM Standard*. Click on the **Web → QuickMapServices → OSM → OSM Standard**.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/georef_part1_7.png')
```
8. Now in the canvas, the basemap will be loaded. This map is georeferenced and projected in **EPSG:3857** CRS. This information can be viewed on the bottom-right of the QGIS, where the project CRS will be updated.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/georef_part1_8.png')
```
We have now finished the first part of this exercise. Your output should match the contents of the ``Georeferencing_Checkpoint1.qgz`` file in the solutions folder.
### 3.1.1 Challenge
Load the **Dark Matter** basemap by [CartoDB](https://stamen.com/introducing-positron-dark-matter-new-basemap-styles-for-cartodb-d02172610baa/). This is an minimalist basemap that renders OpenStreetMap data in a dark background.
## 3.2 Using the Georeferencer
In this section, you will learn how to load a scanned image, collect GCPs (ground control points) and warp the image to create a GeoTiff file.
1. Click on the **Layer → Georeferencer** from the menu-bar to open the georeferencing tool.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/georef_part2_1.png')
```
2. A new *Georeferencer* window will open. This tool primarily contains two sections, the top is for viewing images, and the bottom is for tabular data.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/georef_part2_2.png')
```
3. Click on the **Open Raster..** button from the ribbon to load the scanned map. Browse to the ``Bangalore_1924.png`` file in your data package and click *open*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/georef_part2_3.png')
```
4. The image will be loaded in the *Georeferencer* window. The ribbon at the top has buttons essential operations like Zoom/Pan that you can use to navigate around the image. There are also buttons for adding and editing GCPs (Ground Control Points) that we will use next.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/georef_part2_4.png')
```
> Note: You may need to change the settings if the docking doesn't work for the Georeferencer.
For Mac users : QGIS → Preferences → Configure Georeferencer
For Windows users : Georeferencer → Configure Georeferencer
Check the 'Show Georeferencer window docked' to enable docking.
5. Before we start collecting GCPs, you may also dock the *Georeferencer* window to the main QGIS window. This makes it easier to find the tie-points. Close the *GCP table* window. Click and drag the georeferencer title bar to the bottom of the canvas.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/georef_part2_5.png')
```
6. Locate a feature that is visible in both the scanned map and the basemap. Click on the *Add Point* button.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/introduction_to_qgis/georef_part2_6.png')
```