community health maps lab series - toxmap · pdf filecommunity health maps lab series ... is a...
TRANSCRIPT
Community Health Maps Lab Series
Lab 4 – Basic Spatial Analysis in QGIS
Objective –Understand how to do basic spatial analysis with QGIS Desktop
Document Version: 2017-03-31(Final)
This course is a collaborative effort between the National Library of Medicine, the Center for
Public Service Communications, and Bird’s Eye View
Author: Kurt Menke, GISP
Community Health Mapping Lab Series - Lab 4 – Basic Spatial Analysis
3/31/2017 Page 1 of 27
Contents 1 Introduction .................................................................................................................... 2
2 Objective: Understand the How to Conduct Basic Spatial Analyses in QGIS
Desktop............................................................................................................................... 2
3 How Best to Use Video Walk Through with this Lab ............................................... 2
Task 1 Clipping Data .................................................................................................. 2
Task 2 Measuring Proximity ...................................................................................... 7
Task 3 Selecting Features ......................................................................................... 14
Interactive Selection...................................................................................................... 14
Using Expressions to Query the Attributes of a Layer ................................................. 15
Spatial Selection............................................................................................................ 18
Task 4 Calculate Areas and Density ........................................................................ 21
4 Conclusion ................................................................................................................... 27
Community Health Mapping Lab Series - Lab 4 – Basic Spatial Analysis
3/31/2017 Page 2 of 27
1 Introduction The real power of bringing public health data into QGIS Desktop is realized when you
are able to conduct spatial analyses. Spatial analysis means creating new data and
information, which is derived from existing datasets. The information provided from
measuring proximity, conducting spatial queries and intersecting datasets leads to
insights unobtainable in tabular data.
2 Objective: Understand the How to Conduct Basic Spatial Analyses in
QGIS Desktop
In this lab exercise, you will explore some basic ways to turn data into information. You
will learn some basic spatial analysis techniques including clipping a data layer, buffering
features, creating heat maps, and selecting records by both attributes and location. The
data for this lab covers Baltimore City and focuses on diabetes.
Task 1 – Clipping Data
Task 2 – Measuring Proximity
Task 3 – Feature Selection
Task 4 – Calculate Areas
3 How Best to Use Video Walk Through with this Lab To aid in your completion of this lab, each lab task has an associated video that
demonstrates how to complete the task. The intent of these videos is to help you move
forward if you become stuck on a step in a task, or if you wish to see every step required
to complete the tasks.
We recommend that you do not watch the videos before you attempt the tasks. The
reasoning for this is that while you are learning the software and searching for buttons,
menus, and other feature, you will better remember where these items are and, perhaps,
discover other features along the way if you discover them on your own. With that being
said, please use the videos in the way that will best facilitate your learning and successful
completion of this lab.
Task 1 Clipping Data
Task 1 Video Walkthrough
In this task, you will learn how to spatially constrain data to the study area using a GIS
spatial analysis operation known as clipping.
1) Open QGIS Desktop and open the Lab4.qgs project file
2) With one exception, this map has the same data you worked with in Lab 3. There
is a new dataset representing fast food restaurant locations.
Community Health Mapping Lab Series - Lab 4 – Basic Spatial Analysis
3/31/2017 Page 3 of 27
3) This layer obviously extends beyond the limits of Baltimore City. Right click on
the FACCESS_MD_FastFood_CLF layer and choose Zoom to Layer from the
context menu to see the full extent of the layer.
4) This layer covers all of Maryland.
5) Click the Zoom Last button to return to the Baltimore City extent.
6) You are only interested in Baltimore City. Eventually you will clip this layer to
the spatial extent of Baltimore City. First, familiarize yourself with the data.
Highlight the layer in the Layers Panel, click the Identify Features tool and click
on one of the FACCESS_MD_FastFood_CLF icons on the map to see what type
of attributes the layer has.
Community Health Mapping Lab Series - Lab 4 – Basic Spatial Analysis
3/31/2017 Page 4 of 27
7) Close the Identify Results window when finished.
8) Open the Layer Properties for the FACCESS_MD_FastFood_CLF layer. Select
the General tab.
9) Is this dataset in the same CRS as the other layers?
10) This layer is in WGS 84 Pseudo Mercator. Close Layer Properties.
11) Before you clip this layer to the Baltimore City limits, it needs to be in the same
Coordinate Reference System (CRS) as the Baltimore City Boundary. This is true
anytime you are intersecting two layers in some way to produce new output. You
will first re-project this layer.
12) Right click on the FACCESS_MD_FastFood_CLF layer and choose Save
As…from the context menu.
13) The Save vector layer as… window opens.
a. Select ESRI Shapefile for the Format
b. You will be creating a new copy of this layer in the same Maryland State
Plane CRS used by the other map layers. Browse to the Lab4/MyData
folder and name it MD_FastFood_SPCS.shp. Here you will employ a
naming convention. Ending the layer name with SPCS will remind you
that this layer is in the State Plane Coordinate System (SPCS).
c. Click the Browse button for the CRS . This opens the Coordinate
Reference System Selector window. At this point you should have
NAD_1983_StatePlane_Maryland_FISP_1900_Feet (EPSG: 102685)
Community Health Mapping Lab Series - Lab 4 – Basic Spatial Analysis
3/31/2017 Page 5 of 27
listed in the Recently used coordinate reference systems window. If not
type 102685 in the Filter text box and select this CRS.
d. Click OK when done.
e. Back at the Save vector layer as… window click OK to reproject this
shapefile.
Community Health Mapping Lab Series - Lab 4 – Basic Spatial Analysis
3/31/2017 Page 6 of 27
14) A blue bar across the top of the map canvas will signify the process has
completed and the MD_FastFood_SPCS layer will be added to the Layers panel.
It is exactly the same data as the original data, but is now in a different CRS.
15) Now you are ready to clip this layer to the Baltimore City Boundary polygon.
16) Click Vector Geoprocessing Tools Clip to open the Clip tool.
a. The Input layer will be MD_FastFood_SPCS [EPSG:102685]
Note: the CRS is listed after each layer making it easy to know that you
are choosing two appropriate layer for this operation.
b. The Clip layer will be Baltimore City Boundary [EPSG:102685]
c. Clipped is the output layer. QGIS gives you several options for saving
the resulting layer. Click the ellipsis button to the right and a context
menu opens. Choose Save to File. Navigate to the Lab4/MyData folder,
and name this new layer Baltimore_FastFood.shp
NOTE: Create temporary layer creates a layer that is like any other but
it doesn’t exist on disk and disappears when QGIS exits. If needed, it can
be saved to disk. Use expression allows you to filter the resulting output
with an expression and Spatialite and PostGIS are options for storing the
output in a spatial database.
d. Click OK.
Community Health Mapping Lab Series - Lab 4 – Basic Spatial Analysis
3/31/2017 Page 7 of 27
17) Once the operation has finished, turn off the two other Fast food layers and you
will see the new layer (Clipped) consisting of just the restaurants within the
Baltimore City boundary.
18) To Style this new layer right click on the original Fast food layer
(FACCESS_MD_FastFood_CLF) and choose Styles Copy Style from the
context menu.
19) Right click on the Baltimore_FastFood layer and choose Styles Paste Style
from the context menu. Copying and pasting styles between layers is a quick way
to restyle a new layer!
20) Remove the other two Maryland wide Fast food layers form the Layers panel.
21) Rename the new layer from Clipped to Baltimore Fast Food
22) Congratulations, you clipped your first layer!
23) Save your QGIS project.
Task 2 Measuring Proximity
Task 2 Video Walkthrough
In this task, you will explore two ways to measure proximity. You will be learn how to
apply both the buffer tool and the heatmap tool to measure proximity to fast food
restaurants in Baltimore City.
1) Open QGIS Desktop and open the QGIS map document from Lab 4 Task 1.
Community Health Mapping Lab Series - Lab 4 – Basic Spatial Analysis
3/31/2017 Page 8 of 27
2) First, you will buffer the fast food restaurants. Buffer is an operation that creates a
new polygon layer. The resulting polygon is a zone around a map feature
measured in units of distance. Click the Vector menu Geoprocessing
Buffer(s) to open the Fixed distance buffer tool.
a. Input layer Baltimore_FastFood
b. Distance 1320 (¼ mile in feet)
c. Segments 20 (QGIS cannot create true curves, but the higher this
number the smoother the output will be.)
d. Check Dissolve results (Checking this option will merge buffer polygons
for each point together if they overlap.)
e. Buffer Here you are telling QGIS where to save the new GIS layer
(shapefile) and what to name it. Save it to Lab4/MyData/ and name it
FastFood_Buffer.shp
f. Click Save and then OK. Close the Buffer window.
3) The new layer will appear on the map and in the Layers panel. Drag it beneath
the Baltimore_FastFood layer. This new dataset shows the area of Baltimore that
is within ¼ mile of a fast food restaurant. With this data you could calculate the
percentage of Baltimore that is within ¼ mile of a fast food restaurant. You will
learn how to calculate acreages in Task 4.
Community Health Mapping Lab Series - Lab 4 – Basic Spatial Analysis
3/31/2017 Page 9 of 27
That created some good information. Now you will learn another method of calculating
proximity. You will create a ‘heat map’ of the fast food restaurants in Baltimore City. A
heat map is really more of a measure of density computed from point data. A raster
surface (pixel based data) will be interpolated from the point data. Interpolation mean that
the raster values (density) will be estimated from the point locations.
4) To do this you will be using a QGIS plugin named Heatmap. It is a core QGIS
plugin, so it is already installed. You just need to enable it. Click on the Plugins
menu and choose Manage and Install Plugins.
5) Select the Installed tab and click the box next to Heatmap.
Community Health Mapping Lab Series - Lab 4 – Basic Spatial Analysis
3/31/2017 Page 10 of 27
6) Click Close.
7) This tool will now appear under the raster menu. Click Raster Heatmap
Heatmap.
8) Complete the tool with the following inputs:
a. Input point layer Baltimore_FastFood
b. Output raster Lab4/MyData/FastFood_Heatmap.img
c. Output format Erdas Imagine Images (.img) (There are many raster
file formats. Common ones include GeoTiff and Erdas img files. There is
no specific reason IMG is being chosen here. GeoTiff would serve you
just as well. Here you are simply being shown the choices and picking
one.)
d. Radius 5280 map units
e. Click the Advanced checkbox to access some advanced settings
f. Enter Cell size X and Cell size Y values of 100
g. Kernel shape Epanechnikov
NOTE: the Kernel shape controls how the influence of a given point
decays as the distance from it increases. For example, a triweight kernel
gives features greater weight for distances closer to the point then the
Epanechnikov kernel does. Consequently, triweight results in “sharper”
hotspots, and Epanechnikov results in “smoother” hotspots. A number of
standard kernel functions are available in QGIS, which are described and
illustrated on Wikipedia
(http://en.wikipedia.org/wiki/Kernel_(statistics)#Kernel_functions_in_co
mmon_use).
h. Click OK.
Community Health Mapping Lab Series - Lab 4 – Basic Spatial Analysis
3/31/2017 Page 11 of 27
9) A raster image will appear styled on a black to white color ramp.
10) Now you will style this raster layer. Open the Layer Properties Style tab for
the FastFood_Heatmap layer.
Community Health Mapping Lab Series - Lab 4 – Basic Spatial Analysis
3/31/2017 Page 12 of 27
a. Change the Render type to Singleband pseudocolor.
b. Next to Color choose the YlOrRd color ramp
c. Click Classify
d. Click OK.
11) The map will now resemble the figure below
Community Health Mapping Lab Series - Lab 4 – Basic Spatial Analysis
3/31/2017 Page 13 of 27
12) This layer extends beyond the Baltimore City boundary. Now you will clip the
heat map layer. Remember there are two main types of GIS data: vector (points,
lines and polygons) and raster (pixel based). There are different tools for working
with each data type. Here you will learn how to clip raster data.
13) Click on the Raster menu Extraction Clipper to open the Clipper window.
a. Input file (Raster) FastFood_Heatmap
b. Output file Lab4/MyData/FastFood_Heatmap_BC.img
c. Click the Mask Layer radio button. (This allows you to specify a layer as
the object within which the data will be clipped to, instead of coordinates
defining a box.)
d. Mask layer Baltimore City Boundary
e. Click OK.
14) Right click on the original heat map layer and choose Styles Copy Style.
15) Right click on the new clipped heat map layer and choose Styles Paste style.
16) Drag the Baltimore City Boundary above the FastFood_Heatmap_BC layer.
17) Remove the original layer.
Community Health Mapping Lab Series - Lab 4 – Basic Spatial Analysis
3/31/2017 Page 14 of 27
18) Additional documentation on this plugin can be found here:
http://docs.qgis.org/2.14/en/docs/user_manual/plugins/plugins_heatmap.html
19) Save your QGIS project file.
Task 3 Selecting Features
Task 3 Video Walkthrough
Selecting features is one of the core functions of a GIS. You may only be interested in a
subset of a data layer. When you select spatial features the corresponding records in the
attribute table are also selected and vice versa. Any GIS operation can be run on just the
selected set of records/features. For example, if you have counties of the US and you
select those in Vermont, you can save the selected features/records as a new GIS layer.
Any GIS operation can be run on just the selected set. Selections can be done: 1)
interactively, 2) using expressions to query the attributes of a layer or 3) spatially. Here
you will be introduced to all three methods.
Interactive Selection First let’s explore interactive selection.
1) Open QGIS Desktop and open the QGIS map document from Lab 4 Task 2.
Community Health Mapping Lab Series - Lab 4 – Basic Spatial Analysis
3/31/2017 Page 15 of 27
2) Turn off the FastFood_Heatmap_BC layer and turn on the
Neighborhood_diabetes layer.
3) Select the Neighborhood_diabetes layer in the Layer panel.
4) Click the Select Features by area or single click button. There is a dropdown
tool menu that provides several ways to select features interactively.
5) Choose Select Features(s). Click on a feature on the map. It will be highlighted in
yellow.
6) Open the attribute table for this layer. You will see that the corresponding record
is also selected. Note this is a large table and you may need to scroll down to find
the selected record.
7) Whenever you have a selected set of records, you can use the Show All Features
dropdown menu, located in the lower left corner of the attribute table, to show
only the selected records. There are several other options available on this table
menu. Click Show Selected Features.
8) Now you can see just the selected records and can sort just the selected records by
the various columns.
9) Close the table when done.
10) This tool can also be used to select multiple features. Using the tool click and drag
a box with your left mouse button across a portion of the city. All the polygons
you interacted with will become selected.
11) To unselect these features click the Deselect Features from all layers button
Using Expressions to Query the Attributes of a Layer
Now you will explore attribute queries using the select by expression tool to select
neighborhoods with high poverty rates
1) Open the Neighborhood_diabetes attribute table.
Community Health Mapping Lab Series - Lab 4 – Basic Spatial Analysis
3/31/2017 Page 16 of 27
2) In addition to the diabetes data you joined to this table (Lab3 Task 2), there are a
lot of columns with socio-economic data.
3) Click on the Select features using an expression button to open the Select
by expression window.
4) On the left is the window where you will form your SQL expression. In the
middle are the available Functions. On the right is a context window that
provides help and sample field values. Across the top are commonly used
Operators.
5) Expand the Fields and Values functions and the attribute columns in this layer
will be listed.
6) Select the Poverty field.
7) The right window splits into help and Values windows. On top are some hints.
Next to Load values click all unique. QGIS will scan the column and print all the
unique values in the Poverty column for this layer. These represent the poverty
rate.
Community Health Mapping Lab Series - Lab 4 – Basic Spatial Analysis
3/31/2017 Page 17 of 27
8) Double-click on Poverty to add it to the Expression area.
9) Scroll up the list of Functions and expand the Operators section.
10) Double click on the > operator to add it to the Expression area.
11) Scroll back down and reselect the Fields and Values Poverty column
12) Again next to Load values click all unique
13) Double click on 20 from the Values section to complete your expression. It is also
possible to simply type a 20 after the greater than sign.
14) With your expression complete, click the Select button.
Community Health Mapping Lab Series - Lab 4 – Basic Spatial Analysis
3/31/2017 Page 18 of 27
15) 117 neighborhoods match your query. They are both selected on the map, and in
the table.
16) Close the Select by Expression window and close the attribute table.
17) To unselect these features click the Deselect Features from all layers button
18) For more information on using the Select by expression tool visit the QGIS
documentation page for this tool.
http://docs.qgis.org/2.14/en/docs/user_manual/working_with_vector/expression.html
Spatial Selection Another useful technique is feature selection by location. This technique allows you to
select features in one layer that intersect or touch features in another layer.
1) Here you will use the select by location tool to identify neighborhoods with a fast
food restaurant. Click the Vector menu Research Tools Select by
Location.
a. Layer to select from: Neighborhood_diabetes [EPSG:102685]
b. Additional layer: Baltimore_FastFood [EPSG:102685]
c. Geometric predicates: Check intersects
d. Click Run
e. Click Close.
Community Health Mapping Lab Series - Lab 4 – Basic Spatial Analysis
3/31/2017 Page 19 of 27
2) The neighborhood polygons that intersect a fast food restaurant point are now
selected in yellow. Right click on the Neighborhood_diabetes layer and choose
Open Attribute Table.
3) There are 271 records in the table meaning there are 271 neighborhoods in
Baltimore City. Of those 271 neighborhoods, 85 (31%) are now selected meaning
they have a fast food restaurant.
Community Health Mapping Lab Series - Lab 4 – Basic Spatial Analysis
3/31/2017 Page 20 of 27
4) Scroll to the right side of the table where the BaltCity_D column is. This is the
column that contains the number of people diagnosed with diabetes. Remember
you joined this column to the neighborhoods layer in Lab 3 Task 2.
5) Click the column header (BaltCity_D) to sort the table based on these diabetes
values. Scroll up and down and observe the values. Notice that the majority of
neighborhoods with high diabetes totals are selected. Close the table.
6) Click on the Show statistical summary button to open the Statistics Panel
a. Select Neighborhood_diabetes as the layer
b. Target field BaltCity_D
c. Check Use only selected features
Basic statistics on the number of diabetics in the selected neighborhoods are now
displayed in the main window.
Community Health Mapping Lab Series - Lab 4 – Basic Spatial Analysis
3/31/2017 Page 21 of 27
7) This shows that 40,433 diabetics live in these neighborhoods with easy fast food
access.
8) Repeat this, but uncheck the Selected features only option. Click OK. This
shows that there are a total of 82,831 diabetics living in Baltimore City. You now
know that of those, almost 50% are within neighborhoods with easy fast food
access! Close the Statistics panel.
9) Using the same workflow in steps 1-8, determine what percentage of diabetics
live in neighborhoods within ¼ mile of a fast food restaurant. For this, you will
use the FastFood_Buffer layer.
10) You can use the Deselect features from all layers button to unselect the
records when done.
11) Save your QGIS project when done.
Task 4 Calculate Areas and Density
Task 4 Video Walkthrough
It is often useful to be able to calculate areas and distances. In this task, you will learn
how to calculate the square mileage of each neighborhood and then calculate the
population density.
1) Open QGIS Desktop and open the QGIS map document from Lab 4 Task 3.
2) Turn off the Baltimore_FastFood layer.
3) Open the attribute table for the Neighborhood_diabetes layer.
4) You will add a column and then populate that new column with the area in square
miles. Click the Toggle editing mode button located in the upper left corner
of the attribute table.
5) Clicking that button puts the table into edit mode. Several buttons along the top of
the attribute table now become active.
Community Health Mapping Lab Series - Lab 4 – Basic Spatial Analysis
3/31/2017 Page 22 of 27
All the buttons you are about to use will be explained. You can visit the QGIS
documentation to learn what the remainder of the buttons are for.
6) You have also put the corresponding features on the map into edit and you may
see the vertices of the neighborhoods show up as red X’s.
7) Click the New column button to open the Add column window.
a. Name SqMi
b. Type Decimal number (real) c. Width 5 (the total number of digits the column will hold)
d. Precision 1 (the number of decimal places the column will hold)
e. Click OK
NOTE: Once a field has been created, you cannot change the type. For example,
if you make a field set up to hold text, but meant to set it up to store numbers, you
cannot change it. In that case you would need to delete it and recreate it.
Therefore, it is important to ensure that new attribute columns are set up store the
appropriate data and that the length and precision are suitable. You can review
field types for a layer from Layer Properties Fields.
8) Scroll to the right to see the new column. It will have NULL values in it.
9) Click on the Open Field Calculator button to open the Field Calculator
window. This allows you to populate the new column with values
a. Check Update existing field option
b. Choose SqMi as the field to update
c. You will set up an expression in the left most window. There are operators
in the middle window with some information in the right window. From
the list of Functions, expand Geometry.
d. Double click on $area. Click on the * (multiplication symbol)
e. Enter the conversion factor from square feet to square miles:
0.000000358700642792
f. Click OK. The new field will be populated with the square mileage.
Community Health Mapping Lab Series - Lab 4 – Basic Spatial Analysis
3/31/2017 Page 23 of 27
NOTE: It is possible to create a new field from the Field Calculator and populate
it with values all at the same time! This option is under the Create a new field
section. Neither method is more correct than the other. It is simply personal
preference.
10) Add a second column named PopDensity, Type = Decimal number (real),
Width = 6 and Precision = 1.
11) Now open the Field Calculator. This time you will expand Fields and Values
from the Functions list. You will divide the population by the square mileage to
generate people per square mile (density).
a. Set it up so that you are updating the existing field PopDensity.
Community Health Mapping Lab Series - Lab 4 – Basic Spatial Analysis
3/31/2017 Page 24 of 27
b. Double click on the population column F2010_Popu under Fields and
Values to begin your expression.
c. Click the division symbol /
d. Double click on the square mileage column SqMi.
e. Click OK.
12) Click the Toggle editing mode button to end the editing session. Save your
edits by clicking the Save edits button at the top left corner of the table. .
13) Close the attribute table.
14) Right click on the Neighborhood_diabetes shapefile and choose Duplicate from
the context menu. This creates a copy of the layer in the Layers Panel. You now
have a second layer from the same shapefile. Next you will rename this new layer
and re-style it. It is possible to have multiple copies of the same shapefile styled in
different ways, displaying different aspects of that shapefile!
15) Open the Layer Properties General tab for the new layer. Rename the layer
Population Density.
16) Switch to the Style tab.
17) Style the layer as follows:
a. Switch the Column PopDensity
b. Change the Color ramp Blues
c. Choose Quantile (Equal count) as the Mode
d. Click the Classify button to change the values to those in the new column.
Community Health Mapping Lab Series - Lab 4 – Basic Spatial Analysis
3/31/2017 Page 25 of 27
e. Click OK.
18) Now you have generated a map of population density.
Community Health Mapping Lab Series - Lab 4 – Basic Spatial Analysis
3/31/2017 Page 26 of 27
19) Using what you know, add another column to the Population Density layer named
DiabDen with the same parameters as the population density column.
20) Populate the DiabDen column with the expression: "BaltCity_D" / "SqMi". This
will generate density of diabetics per square mile.
21) Duplicate the Population Density layer. Rename this layer Diabetic Density.
22) Style this layer as you did the Population density layer, but with the DiabDen
column and a different color ramp (OrRd).
23) Turn on just the new Diabetic Density layer and the Dialysis Centers layer.
Community Health Mapping Lab Series - Lab 4 – Basic Spatial Analysis
3/31/2017 Page 27 of 27
24) Save your QGIS Map document.
25) Close QGIS.
4 Conclusion
In this lab you have learned the basics of performing spatial analysis within QGIS
Desktop. QGIS has very robust spatial analysis capabilities. Almost anything you can
envision is possible, if you have the data to support it. Continue to explore the Raster and
Vector menus. There are also hundreds of additional spatial analysis tools in the
Processing Toolbox. The GeoAcademy GST 102 course offers additional spatial
analysis resources. This same material has been updated to QGIS 2.14 in the Discover
QGIS workbook available through Locate Press.