qgis

QGIS Short Course

June 27, 2017

Table of Contents

Short course audience and convention

This short course is geared towards working GIS professionals who have a general knowledge of ESRI ArcGIS. We will focus creating a production workflow that uses the unique features of QGIS not found in ArcGIS. This course is not solely intended to replace ESRI products, but rather compliment them. Since QGIS and associated tools are free and open source software (FOSS), it’s a handy dimension to add to any GIS shop.

We use the following written conventions:

/* Block of SQL code that needs to be entered in the DB Manager */

SELECT 
	*
FROM 
	my_table

If the screenshots found in this document are too small to read, then right+click the graphic and open in a new browser window.

Data and project space in lab

The following zip files contain spatial data, project documents, and support files that we’ll use in this lesson.

Create a folder in the Z:/user/ directory called, FirstNameGIS, while substituting with your appropriate name. Please download and extract the data to this folder. Let’s begin!

About QGIS

We often think of QGIS as free software and data. In fact, it is free to use for profit. More importantly, open source is as much about an ethos of contributing your tools, ideas, and data. OpenStreetMap (OSM), the wikipedia of maps, is tightly integrated with QGIS.

QGIS functionality is supported by open source libraries. A few of which are important to note.

Accessing component information about QGIS
Screenshot: Accessing component information about QGIS.

QGIS project is part of http://osgeo.org
Screenshot: QGIS project is part of OSGeo.

The QGIS interface is broken down into 5 major sections:

There are many more panels and tools not listed here. For an item by item rundown of the QGIS user interface, consult the QGIS User Guide.

Adding plugins and base maps

What makes QGIS especially powerful within the mapping community are the wide array of plugins written by various developers. Plugins are small programs that enhance and extend the functionality natively built into QGIS. They are easily installed from within the QGIS application itself. You can peruse the available QGIS plugins.

Step 01: Install OuickOSM at main menu > Plugins > Manage and Install Plugins… > Search “QuickOSM”. User-contributed plugins are one of great assets of QGIS
Screenshot: User-contributed plugins are one of great assets of QGIS.

Step 02: Install OpenLayers at QGIS main menu > Plugins > Manage and Install Plugins… > Search “OpenLayers”. OpenLayers plugin provides many raster base maps to help start a project
Screenshot: OpenLayers plugin provides many raster base maps to help start a project.

Step 03: Add tile set at QGIS Browser Panel > Tile Server (XYZ) [right+click] > New Connection.. and add the URL:

![Tile Server (XYZ) offers lightning fast access to the growing world of raster base map tile sets.](/qgis/images/presentation/05_01.png)   
Screenshot: Tile Server (XYZ) offers fast access to the growing world of raster base map tile sets.

Other tile servers to add include the following:

/* Classy minimal style map */ https://stamen-tiles-{s}.a.ssl.fastly.net/toner-hybrid/{z}/{x}/{y}.png

/* ESRI world imagery for free applications */ https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}

Step 04: Add 2016 [NAIP imagery](https://gis.apfo.usda.gov/arcgis/rest/services/NAIP) at QGIS Add data menu > **Add WMS/WMTS Layer... > New** and add the URL:
```https://gis.apfo.usda.gov/arcgis/services/NAIP/Kentucky_2016_60cm/ImageServer/WMSServer```
![Web Mapping Services (WMS) is a traditional method of adding layers to a mapping project.o](/qgis/images/presentation/06_01.png)   
Screenshot: Web Mapping Services (WMS) is a traditional method of adding layers to a mapping project.

Other WMS services might be faster. Check out Kentucky's [WMS services](http://kyraster.ky.gov/arcgis/rest/services):

```http://kyraster.ky.gov/arcgis/services/ImageServices/Ky_NAIP_2016_2FT/ImageServer/WMSServer ```

![Layers in QGIS always have transparency options](/qgis/images/presentation/07_01.png)   
Screenshot: Layers in QGIS always have transparency options.

### Spatial Bookmarks

![Use spatial bookmarks to set map canvas views.](/qgis/images/presentation/08_01.png)   
Screenshot: Use spatial bookmarks to set map canvas views.

-----

## Using OpenStreetMap data: Where to eat Lunch?

OpenStreetMap is the world's largest crowdsourced mapping project. It is a free, open-source, volunteer-generated global map that aims to be the "Wikipedia of maps" and global GIS data repository. As of February 2017 [OSM stats](http://www.openstreetmap.org/stats/data_stats.html), there were 3.5 million registered users and 5.7 billion GPS points uploaded. 

OSM has a basic data structure with a few [important elements](http://wiki.openstreetmap.org/wiki/Elements). The _node_, or coordinate point, is the basic geographic object. A node represents a specific point on the earth's surface. Each node comprises at least an id number and a pair of coordinates. Nodes can be combined into _ways_; an open way is a polyline and a closed way is a polygon. OSM data uses only WGS84 coordinates. _Relations_ group geographic objects in familiar relationships, such as routes (bus routes in a city) and multi-polygons (many closed ways belong to the same group).

OpenStreetMap uses _tags_ of the form key=value to add meaning to geographic objects. For example, we could use the following query to find all building polygons that are tagged as 'residential': ```"building" = 'residential'```. Using familiar SQL statements, we can easily filter large datasets to map precisely what we want.

Step 05: Add features from OSM using QGIS main menu > **Web > QuickOSM > Key: "Admin_level"**.
![QuickOSM is one of the easiest ways to get OSM data in a mapping project](/qgis/images/presentation/09_01.png)   
Screenshot: QuickOSM is one of the easiest ways to get OSM data in a mapping project.

Before you **Submit Query** change the **Parameters tab > Output** to **GeoJSON**.
![GeoJSON is the preferred format on Win and Mac OS and it __is__ editable.](/qgis/images/presentation/10_01.png)   
Screenshot: GeoJSON is the preferred format on Win and Mac OS and it __is__ editable.

Step 06: Alter polygon symbology at QGIS main menu > **Layers Panel > admin_level [double+click] > Style > Fill Style > No Brush**.
![Accessing the Style panel to change the appearance of features.](/qgis/images/presentation/11_01.png)   
Screenshot: Accessing the layer's Style tab to change the appearance of features.

### Measuring and interrogating OSM features

![Accessing Project Properties to control project.](/qgis/images/presentation/12_01.png)   
Screenshot: Accessing Project Properties to control project.

Step 07: Change measurement options in the QGIS main menu > **Project Properties > General tab > Measurements**.
![Change measurement units in the General tab.](/qgis/images/presentation/13_01.png)   
Screenshot: Change measurement units in the General tab.

Step 08: Measure polygon in the QGIS main menu > **Identify tool > feature [click] > Derived**.
![Use the Identity tool to find attributes and measurements](/qgis/images/presentation/14_01.png)   
Screenshot: Use the Identity tool to find attributes and measurements.

Step 09: Change CRS of project 'on the fly' in the QGIS main menu > **Project Properties > CRS > Filter > EPSG: 3089**. This will project our layers (temporarily) to the Kentucky single-zone CRS. Change the **Project Properties > General tab > Measurements > Ellipsoid > Planimetric**. This will ensure that we're measuring in the right CRS.

![QuickOSM data adds actions to the Action Identify tool to find source information on OSM](/qgis/images/presentation/15_01.png)   
Screenshot: QuickOSM data adds actions to the Action Identify tool to find source information on OSM.

### Rule-based symbology of OSM layers


Step 10: Add features from OSM using QGIS main menu > **Web > QuickOSM > Key: "Highway"**.
![Add highway (travel) features to your project.](/qgis/images/presentation/16_01.png)   
Screenshot: Add highway (travel) features to your project.

OSM data is attributed the same everywhere in the world, e.g., what is *trunk* road? The levels of road symbology might not be intuitive so please look at the key:value pairs for [http://wiki.openstreetmap.org/wiki/Key:highway](http://wiki.openstreetmap.org/wiki/Key:highway). 

Step 11: Enable rule-based symbology at QGIS main menu > **Layers Panel > highway [double+click] > Style > Rule-based**.
![Rule-based symbology uses SQL to select features.](/qgis/images/presentation/17_01.png)   
Screenshot: Rule-based symbology uses SQL to select features.

![Symbol levels control what features draw first (on the bottom) and last (on the top).](/qgis/images/presentation/18_01.png)   
Screenshot: Symbol levels control what features draw first (on the bottom) and last (on the top).

Step 12: Practice labeling in the **Layer Properties > Labels tab**
![ Label Panel adds dynamic labels for features.](/qgis/images/presentation/19_01.png)   
Screenshot: Label Panel adds dynamic labels for features.

Step 13: Import layer style in the **Layer Properties > Style tab > Style > Load Style > Load from file...** and load the following file:
```osm-road-line-style.qml```
![Save and share complex layers styles with the QGIS .qml file.](/qgis/images/presentation/20_01.png)   
Screenshot: Save and share complex layers styles with the QGIS .qml file.


![A simple travel map of campus.](/qgis/images/presentation/21_01.jpg)   
Screenshot: A simple travel map of campus.

### Filtering OSM attributes

Step 14: Add Amenity features from OSM using **Web > QuickOSM > Key: "Amenity"**.

Step 15: Show only certain amenities using **Layers Panel > amenity [right+click] > Filter...** using the following expression:
```"amenity" IN ('cafe','fast_food','pub','restaurant')```

![Filter layers with SQL.](/qgis/images/presentation/23_01.png)   
Screenshot: Filter layers with SQL.


![Easily use SVG artwork for point symbols.](/qgis/images/presentation/24_01.png)   
Screenshot: Easily use SVG artwork for point symbols.

### Publish map in Print Composer


Step 16: Create new layout using **Project > New Print Composer**
![Map layouts for export or print use the QGIS Print Composer, which can contain multiple layouts.](/qgis/images/presentation/25_01.png)   
Screenshot: Map layouts for export or print use the QGIS Print Composer, which can contain multiple layouts.

Add various map elements with the Add Items menu bar, such as Map Canvas frame, text frame, legend, and scale.

Step 17: Publish finish map using **Composer > Export as Image...**
![Automatically create georeferenced image on Export.](/qgis/images/presentation/26_01.png)   
Screenshot: Automatically create georeferenced image on Export.


![Add the exported map into a QGIS Map Canvas.](/qgis/images/presentation/27_01.png)   
Screenshot: Add the exported map into a QGIS Map Canvas.

Finally, use the **Measure tool** to measure a walk from Whitehall to your favorite restaurant.

![Measure the distance to the your preferred restaurant.](/qgis/images/presentation/28_01.png)   
Screenshot: Measure the distance to the your preferred restaurant.

Exporting to a GeoJSON is a common workflow in open source web mapping, but just remember to use the WGS84 CRS (EPSG:4326). You can check your layer at [http://geojson.io](http://geojson.io)

![Export the point layer as GeoJSON while controlling for CRS.](/qgis/images/presentation/29_01.png)   
Screenshot: Export the point layer as GeoJSON while controlling for CRS.

-----

## Geoprocessing with QGIS: Wildfire rates in Kentucky

In this part we will analyze wildfire rates in Kentucky using two different workflows:

* A tool-based workflow that is similar to ArcGIS 
* An SQL workflow that is native to QGIS

Step 01: Add a spatial database at QGIS **Browser Panel > SpatiaLite [right+click] > New Connection..** and find the following SQLite database, "wildfire-kentucky-EPSG-3089.sqlite".

Step 02: Open **Database > DB Manager**, add the *ky_wildfire* layer to the Map Canvas, and access its Style tab.
![Feature blending modes can create visualizations of density.](/qgis/images/presentation/02_02.png)   
Screenshot: Feature blending modes can create visualizations of density.

Step 03: Change the background color of the Map Canvas to a dark color using **Project > Project Properties > General tab > Background color** and use the **HTML notation value** of ```#404040```.
![Wildfire point locations](/qgis/images/presentation/01_02.png)   
Screenshot: Wildfire point locations

Step 04: Open **Database > DB Manager > SQL window** and execute the following statement: 

```SQL
/* Analysis of wildfire point file */

-- Copy and paste into DB Manager

select 
	sum(fire_size) as total_fire_acres,
	fips_name
from 
	ky_wildfire
group by 
	fips_name 
order by 
	total_fire_acres;

Using DB Manager and SpatiaLite spatial database to calculate rate of wildfire

The SpatiaLite spatial database stores geometry in a table and provides spatial functions to analyze and process that geometry. In the open source world, we have two popular spatial databases, PostGIS and SpatiaLite. Both share the GEOS Library (Geometry Engine, Open Source) to do spatial functions, like calculate areas of polygons and intersecting layers based on spatial proximity. They both use SQL (Structured Query Language) to access these functions and other data management operations, which can be scripted, shared, and reused as plain text files.

While we call these spatial databases PostGIS and SpatiaLite, they are actually extensions to popular open source databases. PostGIS extends PostgreSQL and SpatiaLite extends SQLite. While both databases are free and open source software, full-featured, share the same functions, and natively supported in QGIS, we have some notable differences.

We’ll use SpatiaLite, but the SQL statements we execute can be done in both databases.

Step 05: Open Database > DB Manager > Table > Create table and create the following table: Create table in DB Manager menu with close attention to parameters.
Screenshot: Create table in DB Manager menu with close attention to parameters.

Step 06: Use DB Manager > SQL window and execute the following statement:

/* Spatial join (aggregate) wildfire points by counties */

-- Copy and paste into DB Manager

insert into ky_wildfires_by_county 
	
	(
	name,
	population,
	cumulative_acres,
	geom 
	)

select
	ky_counties.name, 
	ky_counties.POP_ESTIMATE_2013, 
	sum(fire_size) as cumulative_acres,
	ky_counties.geom
from
	ky_counties, ky_wildfire
where
	st_intersects(ky_counties.geom,ky_wildfire.geom)
group by 
	ky_counties.name;

If you make a mistake with your query, just delete all attributes from the new table with the following statement:

delete from ky_wildfires_by_county;

and rerun the corrected query.

Step 07: Symbolize and visualize your wildfire by county. Which is appropriate?

Map total acres by county (not normalized).
Screenshot: Map total acres by county (not normalized).

Map total acres by county (not normalized).
Screenshot: Map total acres by county (not normalized).

Map cumulative acres per person by county.
Screenshot: Map cumulative acres per person by county.

Map cumulative acres per person by county.
Screenshot: Map cumulative acres per person by county.

Map cumulative percentage burned by county.
Screenshot: Map cumulative percentage burned by county.

Use the following expression in the Style Panel to normalize by area: cumulative_acres/($area/43560).

Map cumulative percentage burned by county.
Screenshot: Map cumulative percentage burned by county.

Use QGIS Vector tools for analysis

Step 08: Save as… the Census block group and wildfire layers as new shapefiles with limited attributes and in the EPSG: 3089 CRS.

Save Shapefile to a new layer.
Screenshot: Save Shapefile to a new layer.

Select attributes and CRS of new layer.
Screenshot: Select attributes and CRS of new layer.

Select attributes and CRS of new layer.
Screenshot: Select attributes and CRS of new layer.

Step 09: Do a spatial join point in polygon analysis using the Vector > Data Management Tools > Join attributes by location.

Select attributes and CRS of new GeoJSON.
Screenshot: Join attributes by location does a Spatial Join

Pay close attention to tool parameters.
Screenshot: Pay close attention to tool parameters.

Compare to intersect analysis in SpatiaLite

Step 10: Open Database > DB Manager > Table > Create table and create the following table:

Create new table in DB Manager
Screenshot: Create new table in DB Manager

Step 11: Use DB Manager > SQL window and execute the following statement:


/* Spatial join with point in polygon using SpatiaLite spatial index. PostGIS automatically uses the spatial index. */

-- Copy and paste into DB Manager

insert into ky_wildfires_by_blockgroup
	(
	name,
	area_acres,
	cumulative_fire_arces,
	population,
	geom 
	)

select
	ky_blockgroups.geoid10, 
	st_area(ky_blockgroups.geom)/43560,
	sum(fire_size) as cumulative_acres,
	ky_blockgroups.b01001e1, 
	ky_blockgroups.geom
from
	ky_blockgroups, ky_wildfire
where
	st_intersects(ky_blockgroups.geom,ky_wildfire.geom)
and

/* Use the spatial index query to limit our candidate points. */
    
    ky_wildfire.rowid 

    in (

        select rowid from SpatialIndex         
        where 
            f_table_name = 'ky_wildfire'
        and
            search_frame = ky_blockgroups.geom
        )
group by ky_blockgroups.geoid10;

Step 12: Symbolize and publish a wildfire by block group map:

Normalize output layer by population.
Screenshot: Normalize output layer by population.

Example of choropleth map.
Screenshot: Example of choropleth map.

Publish the choropleth map in Print Composer
Screenshot: Publish the choropleth map in Print Composer.

Helpful references

Data sources

Credits

Test for photo