TUTORIAL BIVARIATE MAPS & MAP LAYOUT USING CENSUS TRACT DATA

TUTORIAL BIVARIATE MAPS & MAP LAYOUT USING CENSUS TRACT DATA BY ANDREA BLINDENBACHER FALL 2012 QGIS 1.8.0 Lisboa Tutorial Contents Task ..............
Author: Lester Hoover
3 downloads 2 Views 4MB Size
TUTORIAL BIVARIATE MAPS & MAP LAYOUT USING CENSUS TRACT DATA BY ANDREA BLINDENBACHER FALL 2012

QGIS 1.8.0 Lisboa Tutorial

Contents Task .............................................................................................................................................................................3 QGIS 1.8 Overview ......................................................................................................................................................3 DATA ...........................................................................................................................................................................4 Census Tract Data in tabular format: “TotalIncome_PH_2005.xlsx” .....................................................................4 Vector Data (2010_parks_all_parkland.shp) ..........................................................................................................7 Adding the Data to QGIS ............................................................................................................................................9 Adding the Income data: TotalIncome_PH_2005.csv ............................................................................................9 Adding the Census Tract Boundary file ............................................................................................................... 10 Combine the table with the shapefile ................................................................................................................. 11 Clip data to the Ottawa area ............................................................................................................................... 13 Saving the project .................................................................................................................................................... 17 Adding some more data .......................................................................................................................................... 18 Add bar diagrams displaying Bike Thefts and Syringe Finding places ..................................................................... 19 Add Labels ............................................................................................................................................................... 24 Adding a Basemap ................................................................................................................................................... 26 How to create a printable Map Product.................................................................................................................. 28 Troubleshooting ...................................................................................................................................................... 32 “Add Delimited Text Layer”-icon missing: ........................................................................................................... 32 Layers don’t have the same Coordinate Reference System (CRS) ...................................................................... 33

2

QGIS 1.8.0 Lisboa Tutorial

Task Familiarize the students with the use of Canadian Census Tract data using QGIS 1.8 and Excel. The goal is to combine tabular census data about income levels with a census tract boundary file. Furthermore, the students are introduced to the use of the QGIS print composer.

QGIS 1.8 Overview This is how QGIS looks when you open it. How many tools/icons you see might be different from this screen capture.

TOC = Table Of Contents Shapefiles and Tables show here as so called “Layers”.

Main Window or Canvas Data display area.

Coordinates and Scales

Projection Type

3

QGIS 1.8.0 Lisboa Tutorial

DATA Census Tract Data in tabular format: “TotalIncome_PH_2005.xlsx” Download and save the EXCEL-file to a folder that is dedicated to the project. Open the file with Excel and check if your data looks like this:

You should have following columns/fields: Geography Household size Total Private Households Median 2005 household income Median 2005 after-tax household income

Average 2005 after-tax household income Standard error of average household income Average 2005 after-tax household income Standard error of average after-tax household income

The Census Tract identity in the “Geography” field has to be adjusted to include a dot at the third place from the end of the number. Actually this number is in text format and this is also how it should be. So if excel gives you an error message or a warning, ignore it. 1. Select the whole worksheet (Crtl A) and R-Click into the selected area. Go to Format Cells and browse to the “Protection”-Tab. Uncheck the “Locked” checkbox. 2. Create a new column beside the Geography column:

4

QGIS 1.8.0 Lisboa Tutorial

3. Insert this formula to create the needed dot: =LEFT($A3,7)&"."&RIGHT($A3,2)

Make sure to fill in the column for all rows (just drag the formula field down) 4. Insert another column and select the column created in steps 2 and 3. Special paste the content , using “Values”, into the new column.

5. Delete the column that contains the formula (here B). 6. Edit the field/ column names as follows: Original Name New Name Geography GEO GEO_DOT Household size H_SIZE Total Private households INCOME_PH Median 2005 household income MEDIAN_IPH Median 2005 after-tax household income MEDIAN_IPH_AT Average 2005 after-tax household income AVERAGE_IPH_AT Standard error of average household income STERROR_AIPH Average 2005 after-tax household income AVERAGE_IPH_AT2 Standard error of average after-tax household income STERROR_AIPH_AT 5

QGIS 1.8.0 Lisboa Tutorial

Now your table should look like this:

IMPORTANT RULES for data to be used in QGIS Field names (column descriptors like “Latitude”, “Longitude” or “Month”):  Must not be longer than 10 characters  Must not contain spaces, use Underscore instead “_”  Must not contain special characters: *&^%$#!@ Note: CSV stands for Comma Separated Values 7. Save the file as a CSV file (ignore or “ok” any warning messages) 8. In order that QGIS recognizes the format type of each column, we have to provide a “.CSVT” file, which contains the datatype specifications. Open a text editor (like Notepad or Notepad++) and create a new file. Enter following line exactly (watch out that the words are capitalized, that you have all the commas and quotes!): "String","String","String","Integer","Integer","Integer","Integer","Integer" Field name GEO GEO_DOT H_SIZE INCOME_PH MEDIAN_IPH MEDIAN_IPH_AT AVERAGE_IPH_AT STERROR_AIPH AVERAGE_IPH_AT2 STERROR_AIPH_AT

Data type String String String Integer Integer Integer Integer Integer Integer Integer

Each column in the table has a specific data type or also called format. QGIS doesn’t recognize the data type on its own. It would import all the data from the csv-file in text format. Once you try any analysis on that data, it will give you an error saying, that the calculation can’t be done on “Text-Values”, or that the input needs to be a number. The “CSVT-file” contains the additional information, which helps QGIS to identify certain columns as a specific data type. It is important, that the CSVT-file has the exact same name as the CSV-file it accompanies. It also needs to be in the same directory.

9. Save this text file as: TotalIncome_PH_2005.csvt It is important the the csvt-file has the same name as the csv file containing the data. 6

QGIS 1.8.0 Lisboa Tutorial

Vector Data (2010_parks_all_parkland.shp) http://www12.statcan.gc.ca/census-recensement/2011/geo/bound-limit/bound-limit-2006-eng.cfm Select the appropriate file:

7

QGIS 1.8.0 Lisboa Tutorial

Download and save the zip file into your project folder. Unzip the file.

8

QGIS 1.8.0 Lisboa Tutorial

Adding the Data to QGIS Adding the Income data: TotalIncome_PH_2005.csv Use the “Add Vector Layer”-button to add the table to the TOC. 1 .

2 .

3 .

9

QGIS 1.8.0 Lisboa Tutorial

Adding the Census Tract Boundary file

Once added R-Click the “gct_000b06a_e” layer in the table of contents and choose “Zoom to Layer Extent”.

10

QGIS 1.8.0 Lisboa Tutorial

Combine the table with the shapefile Compare the Attribute Table of the Census Tract Boundary file with the income table: Boundary File

Income Tabel

As you can see the fields “CTUID” and “GEO_DOT” contain the same data (except for the first row in the Income table). These two fields will be used to join the tabular data to the boundary shapefile.  R-Click the “gct_000bo6a” layer and choose Properties. Select the “Join-Tab”.

1.

2. 3.

1. 2. 3. 4.

5. 6. 7. 8.

Open the layer properties Go to the “Joins” tab Click the “+”-Sign Choose the layer to be joined to the boundary file: TotalIncome_PH_2005 table Select the field in that table to base the join on: “GEO_DOT” Select the Target field in the boundary shapefile: “CTUID” Ok Apply, and OK

4. 5. 6. 7.

8.

11

QGIS 1.8.0 Lisboa Tutorial

Now go into the table attributes of your boundary file and check out if you have new fields and data in there. Also, make sure that all the columns containing number values (integers) show their content to the right side of the cell.

12

QGIS 1.8.0 Lisboa Tutorial

Clip data to the Ottawa area 1. Zoom to the Ottawa area using the zoom tool.

2. Pick the “Select by Rectangle” Tool and select all polygons in the current view, by drawing a rectangle around them.

13

QGIS 1.8.0 Lisboa Tutorial

3. The selected area will turn yellow:

4. R-Click the “gct_000b06a” layer and choose “Save Selection As”. Pick a name (e.g. Ottawa_CT_Income), output location and checkmark the “Add saved map to file”, OK.

14

QGIS 1.8.0 Lisboa Tutorial

5. At this point you don’t need the table and the “gct_000b06a_e” layer anymoreR-Click/Remove. Since we only want to work with Ottawa, but not Gatineau, we further have to eliminate more Census tracts polygons. 6. R-Click the layer/ Open the Attribute Table

7. Enable Editing by clicking the Blue-Pen-Icon at the bottom of the window.

NOTE: Some more tools will appear in that toolbar

15

QGIS 1.8.0 Lisboa Tutorial

8. The field “PRUID” contains the province code. All records with a Province Identity (PRUID) of 24 belong to the province of Quebec. Therefore all records with a value 24 as PRUID will be selected and deleted. (Don’t forget to sort the PRUID column before starting to select. There should be 59 census tracts with a value of 24 and therefore belong to Quebec.

9. Once selected click the “Delete selected features”-button in the bottom toolbar of the window (If this button is not enabled, make sure to enable “editing” first, by clicking the “Blue-Pen”-tool). 10. Disable editing by clicking the “Blue-Pen-Button” again. There should be 190 records left in the attribute table and the map should only show Ottawa area in the province of Ontario.

16

QGIS 1.8.0 Lisboa Tutorial

Saving the project 1. R-Click the layer in your TOC and browse to “Set Project CRS from Layer”.

2. Go to Settings/Project Properties/Coordinate Reference System(CRS) – Enable ‘on the fly’ CRS transformation

3. File/Save Project – select name and output location 17

QGIS 1.8.0 Lisboa Tutorial

Adding some more data Use the “Add Vector Layer”- Button Button

or if Geocoding again, the “Add Delimited Text Layer”-

.

1. Add the Wards_2010.shp 2. Add the Bike thefts a. Geocode them again from the CSV file b. If you have the shapefile that contains the bike thefts as points you can add it directly 3. Add Syringes places a. Geocode them again from the CSV file b. If you have the shapefile that contains the syringes as points you can add these directly Please refer to the other 2 tutorials if you don’t remember how to geocode the bike thefts and the syringes. NOTE: if your layers don’t show up right on top of each other, remove them again and go into Settings/Project Properties/ Coordinate Reference System (CRS)-Tab and make sure to check the “Enable ‘on the fly’ transformation”-checkbox.

18

QGIS 1.8.0 Lisboa Tutorial

Add bar diagrams displaying Bike Thefts and Syringe Finding places The data will be displayed as a bivariate map. This means that more than one variable is displayed per census tract. The map will the income level of each census tract by classifying the income data into different classes and displaying each class as a different color. On top of that, the Bike thefts and Syringes are counted per census tract and displayed as an overlaying Bar-diagram. A. Reprojecting the Bike thefts Point Layer and Syringes Point layer to enable further analysis: 1. R-Click the Bike Thefts layer and choose “Save As”. Specify the dialogs as follows:

This procedure ensures that all the layers are in the same projection, which allows you to run analysis. If the layers are in different projections, you will get warnings or even errors that state that the tool can’t be run, because of differently projected input data.

2. Do the same thing for the Syringes Point layer. 3. Remove the old Bike and Syringes layer from the TOC. B. Add Bike thefts and Syringes counts per census tract to the Ottawa_CT_Income layer. 1. Vector/Analysis/Points in Polygon

19

QGIS 1.8.0 Lisboa Tutorial

When asked if you want the new layer to be added to the TOC, select “Yes”. 2. Do the same thing for the Syringes-points layer, but: a) Input polygon vector layer is now “Ottawa_CT_Income_BT.shp” b) Nam the “Output count field” differently, e.g. SyrCNT (NOTE: A field name can’t be longer than 10 characters.) c) Rename the output file, e.g. “Ottawa_CT_Income_BTSyr.shp” 3. Only keep the newest “Ottawa_CT_Income_BTSyr”- layer in your TOC and remove the other two. The Ottawa_CT_Income_BTSyr - layer should contain 2 more attribute fields/columns, which state the number of Bike thefts and the number of Syringe-findings in each census tract.

20

QGIS 1.8.0 Lisboa Tutorial

C. Style the Ottawa_CT_Income_BTSyr layer 1. R-Click the Ottawa_CT_Income_BTSyr layer and choose “Properties 2. Adjust the “Style-Tab”

1. 2. 4. 5.

3.

1. Select “Graduated” 2. Select the column to apply the classification on:”AVERAGE_IP” 3. Choose a color ramp 4. Pick the number of classes: 5 5. Statistical Method used to divide the data into 5 classes: “Natural Breaks” 6. Hit “Classify” 7. Apply NOTE: if you can’t choose a field name in step 2, then you are missing the “CSVT” file which was created earlier on in the tutorial.

6.

To further adjust the style of this data, click on “change”. In the appearing window you can adjust the transparency of the layer. For the purpose of this tutorial it is advisable to set the transparency to 40%.

21

QGIS 1.8.0 Lisboa Tutorial

If you click “change” again, this dialog opens. Here you can specify fill color, fill style, border color and border style. Don’t adjust the fill-color or style, since we already chose a specific color ramp to display the data in graduated colors. But feel free to play with the border settings.

NOTE: “No Pen” makes your borders transparent.

Once you’re done, just click OK and Apply on all windows. 3. Add a Bar Diagram using the “Overlay”Tab

1. 2. 3. a+b

3.

1. Check “Display diagrams” 2. Select “Bar chart” as diagram type 3. Pick the Attributes to be displayed in the Bar chart: a. Browse to BikeCNT, click Add b. Browse to SyrCNT, click Add 4. Classification attribute: SyrCNT – this specifies the attribute from which the maximum value will be picked to scale the Bars. 5. Click “Find maximum value” 6. Size in mm: 20

4.

5.

6.

22

QGIS 1.8.0 Lisboa Tutorial

If your map looks cluttered, just zoom in a little like here. Note, that yours might look different, because of different colors or border styles you may have chosen.

23

QGIS 1.8.0 Lisboa Tutorial

Add Labels 1. Open the Properties window of the Wards_2010 layer (R-Click/Properties) 2. Go to the “Labels”-Tab: a. Check “Display labels” b. Pick the field containing the value you want as label: WARD_EN (contains the names of the wards in English) c. Set font size and color

3. Since the ward polygons and boundaries are of no interest for this tutorial, but we still want the labels, it is recommended to set the ward-polygons to transparent: In the Properties window to go the “Style”tab and set Transparency to 100%

24

QGIS 1.8.0 Lisboa Tutorial This is the map with labels, bar diagrams and income by census tract.

25

QGIS 1.8.0 Lisboa Tutorial

Adding a Basemap By adding the QGIS Plugin “OpenLayers” one can get access to Google Maps, BingMaps and OCM data. It is not possible to run analysis on these layers, but they can be used to underlay your data and give the user orientation. 1. Plugins/Fetch Plugin 2. Search for the Plugin “OpenLayers” in the Filter-window. Select it from the result list and click “install”.

3. Under Plugins there is now the option to select “OpenLayers plugin”. Select “OpenLayers Overview”. This will open a new panel in the lower left corner of your QGIS canvas.

26

QGIS 1.8.0 Lisboa Tutorial

Enable the OpenLayers-Panel by checking the checkbox “Enable map”. From the Dropdown pick any Layer you like. In the small map window below, a preview of your selected layer will open. If you like it, click on the button “Add map” .

NOTE: rendering this layer might take a while, be PATIENT! NOTE: Ensure that you have the “Enable ‘on the fly’ transformation” option in your Project settings enabled! The layer will be added to your TOC. Just drag it to the bottom of your layer-list, this way you can see all your data on top of this baselayer. RECOMMENDATION: While you work on your data (styling, analysing, geoprocessing, etc) disable the baselayer. Otherwise it will render the layer each time you move or zoom around in your data, which causes you to wait until that basemap layer has finished rendering.

27

QGIS 1.8.0 Lisboa Tutorial

How to create a printable Map Product The data is displayed on a map and almost ready to be placed in a story or to be printed. It is “almost” ready because it is missing a legend, which explains the coloring and details, a potential map reader has to be familiar with. 1. File/New Print Composer

A new Canvas will open. In here you will create a printable version of your map:

This toolbar allows you to zoom, pan and refresh your data. Print to pdf or image, create map windows, labels, legends, scale bars and North arrows.

In these to tabs you can adjust the properties of the features (e.g. size, font color, label names, symbol sizes, etc. of the legend, title, and north arrow). Always select the feature you want to adjust and the tabs will automatically be set to this features properties.

28

QGIS 1.8.0 Lisboa Tutorial

Adding a Map window: Selet the “Add Map” Tool from the top toolbar. Click and hold your mouse down anywhere on in the white are and draw a rectangle.

Once you let go, the rectangle will automatically fill with the map content we created before.

Create a legend by selecting the “Add Legend”-tool from the top toolbar. Click anywhere in the canvas and a legend will be added according to your map layout.

29

QGIS 1.8.0 Lisboa Tutorial

If you now want to adjust anything within the legend, like getting rid of the “Wards_2010” legend entry (It is not needed, because only the labels are displayed from the wards shapefile and labels don’t need further explanation). Click into the “Item Properties” Tab in the lower right corner.

Under Legend items, select the “Wards_2010” layer and click the big minus button below. Do the same for “Google Physical”, which is the basemap layer. By clicking the “yellow-Pen”-Tool, one can enable editing. And this again lets you edit layer and label names in the legend. If you change a lot, but your change doesn’t appear on the canvas, click “Update” to refresh the canvas content.

Play with other tools from the top-toolbar to create Titles, copy right, explanatory text, any shapes etc.

30

QGIS 1.8.0 Lisboa Tutorial

Once you are happy with the looks of your map product, you can print it, save it as a pdf file or as an image: File/Export to pdf:

31

QGIS 1.8.0 Lisboa Tutorial

Troubleshooting “Add Delimited Text Layer”-icon missing: If you don’t find the “Add Delimited Text Layer” icon, follow this procedure: 1. Open the Plugin-Manager

2. Enable the “Add Delimited Text Layer” Plugin:

If you don’t find the “Add Delimited Text Layer” Tool in your Plugin Manager, follow this procedure, of course choosing the appropriate tool. http://qgis.spatialthoughts.com/2010/10/using-plugins-in-quantum-gis_05.html 32

QGIS 1.8.0 Lisboa Tutorial

Layers don’t have the same Coordinate Reference System (CRS) If this warning appears, stop whatever you were up to.

This means that your layers don’t have the same coordinate reference system(CRS). Do this for every layer in your TOC (Table of Content): 1. R-Click on layer 2. Save As 3. Specify the coordinate Reference system by using the “Browse” button. Specify the same CRS for each layer in your TOC.

33