+ All Categories
Home > Documents > Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Date post: 04-Mar-2015
Category:
Upload: joao-felipe-campos-villar
View: 343 times
Download: 0 times
Share this document with a friend
50
Introduction to QGIS Presented at the 2011 CAR Conference, Raleigh, NC Timothy C. Barmann The Providence Journal / projo.com [email protected] 401-277-7369 Page 1 of 50 5/8/2011 http://www.projo.com/temp/tim/nicar/qgis_tutorial/
Transcript
Page 1: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Introduction to QGIS

Presented at the 2011 CAR Conference, Raleigh, NC

Timothy C. Barmann

The Providence Journal / projo.com [email protected]

401-277-7369

Page 1 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 2: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Download

� QGIS open source software

� North Carolina counties shapefile [Check the box for County and Equivalent (Current) and click download.]

� North Carolina population estimates Excel spreadsheet

� jQuery Can link to a jQuery CDN - no need to download

� jQuery plugins: Tooltips, Maphilight, Metadata

� Web page map template

OR ...

� One zip file with everything except QGIS. (2.5 mb) http://www.projo.com/temp/tim/nicar/qgis_files.zip

� One zip file with this tutorial, everything except QGIS. (9.5 mb) http://www.projo.com/temp/tim/nicar/qgis_files_and_tutorial.zip

Page 2 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 3: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Workshop objective To make simple choropleth interactive map for a web page using QGIS, free open source mapping software.

Page 3 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 4: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Choropleth mapping A choropleth map is a thematic map that has shaded or colored geographic regions to represent differing values, such as population density or per-capita income. It gives a snapshot of how a particular measurment or characteristic varies from place to place.

Page 4 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 5: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

What we need

� Shapefile

� Data

� GIS software

Page 5 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 6: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Shapefile A collection of files that describe points, lines, and polygons. A shapefile can be used to represent town boundaries, roads, rivers, and lakes, etc.

It can also contain data about the things it describes, such as population, temperature, road types.

A shapefile has a file extension of .shp, and usually comes with one or more companion files with extensions dbf, prj, xml and shx.

The Census Bureau, as well as many state and local government agencies provide shapefiles.

Page 6 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 7: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Data The data you want to map, usually in the form of an Excel spreadsheet, Access or MySQL database, or CSV file.

Page 7 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 8: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

GIS Software Quantum GIS, or QGIS, is a widely-used open-source program for GIS work. It's available for free for Windows, Linux and Mac OSX platforms at http://www.qgis.org/.

We'll use QGIS to join together the data we want to map with the geographic information stored in the shapefile. Then we'll export an image of the map, and some html code so we can create a web version of the map with live tooltips.

Page 8 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 9: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Other software I'll assume you have access to Microsoft Excel. If not, you can use Open Office, a free office suite similar to Microsoft Office. You'll also need a simple text editor to create the web page for the map.

Page 9 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 10: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Let's get started We're going to build a map that shows the change in population for each county in North Carolina between 2000 and 2008.

The shapefile we need can be found at http://www2.census.gov/cgi-bin/shapefiles2009/state-files?state=37. Check the box for County and Equivalent (Current) and click download.

If you haven't done so already, download the files and software from the links at the top.

Page 10 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 11: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Shapefile and companion files Unzip the shapefile you downloaded. These are the files contained in the zip file.

Page 11 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 12: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Preparing the data Open the population spreadsheet. To make this exercise simpler, remove all the population columns except those for 2000 and 2008.

Delete the other extra rows and notes, as shown in the next slide.

Page 12 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 13: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Preparing the data (cont.) Search and replace the county column to get rid of the space and the word "County" that follows the county names. There may be a period in front of each county name that needs to be removed. Add two more columns called "change" and "pct_chg" with the appropriate formulas.

Format the four columns with numbers as "general." We'll make the percent value look nice later.

Page 13 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 14: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Save the data file Save the spreadsheet as a CSV file.

Page 14 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 15: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Load the shapefile into QGIS Click on Layer and choose Add Vector Layer

Page 15 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 16: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Load the shapefile into QGIS (cont.) Choose the shapefile of North Carolina counties you downloaded and unzipped from the Census bureau.

Page 16 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 17: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Working with the shapefile Here's how it should look. The color may vary.

Page 17 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 18: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Open attribute table Click on Layer and select Open Attribute Table.

Page 18 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 19: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Attribute table The attribute table holds the data that came with the map as part of the shapefile.

We won't need to worry about most of these fields, except "NAME" which is going be our join field to match up the data from the CSV file we've prepared.

Page 19 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 20: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Get needed plugins There are two plugins we need to complete the exercise: mmqgis and Html Image Map plugin.

Click on Plugins and if you don't see those listed, you'll need to download and install.

To do so, choose Fetch Python Plugins

Page 20 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 21: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Get needed plugins (cont.) Click Add 3rd party repositories

Page 21 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 22: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Get needed plugins (cont.) Click on the plugins tab and search for mmqgis, then click on install.

Page 22 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 23: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Get needed plugins (cont.) Follow the same procedure for the Html Image Map plugin.

Page 23 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 24: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Import the CSV file With the plugins installed, we're ready to import the CSV file that contains our Census population data.

Click on Plugins and choose mmqgis and Attributes Join from CSV File.

Page 24 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 25: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Import the CSV file (cont.) Click browse to select the Census CSV file we saved earlier from Excel.

In the CSV File Field, choose County. And in the Join Layer Attribute, choose NAME. These are our two join fields.

For Output Shapefile, you can accept the default name. Save it to your working directory and do the same for the Not Found CSV Output List.

Make sure Add joined layer to project is checked, and click OK.

Page 25 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 26: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Import the CSV file (cont.) If everything went correctly, you will see the new layer added to your project.

This would be a good time to save the project, if you haven't done so already.

Page 26 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 27: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Attribute Table with new data If you open the attribute table again (click on Layer, then Open Attribute Table), you'll see that the new data has been joined with the data that was in the shapefile.

Page 27 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 28: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Format string fields to numbers There's a problem with the data: the mmqgis plugin did not recognize the new number fields as being numbers. They are stored as strings instead. So QGIS can't properly use the data until it is converted to integers or reals.

To do that, first click on the pencil icon on the bottom row of icons to enter Editing mode. Then click on the calculator icon, the last icon on that row.

This launches the field calculator.

Page 28 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 29: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Format string fields to numbers (cont.) In the field calculator, in the New field section, enter the name for a new column that will hold the number value for all the string values in the july_2008 field.

Choose Whole number (integer) for Output field type.

In the Operators section, click to real and that will begin an expression in the Field calculator expression box. Finish the expression by entering the source field, july_2008, and close the parenthesis.

Click OK.

Repeat this process for the July 2000 field as well.

Page 29 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 30: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Format string fields to numbers (cont.) For the pct_chg field, in Output field type, choose Decimal number (real).

Change the Output field precision to 3.

Click OK.

Page 30 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 31: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Format string fields to numbers (cont.) Finally, let's create a new column that will hold a string value we'll use as a tooltip. This will be the message the reader sees when the mouse is moved over a particular region of the map.

For Output field name, enter pct_str, choose Text (string) and make the Output field width 50.

In the Field calculator expression section, enter this formula:

NAME + ':' + to string(pct_chg * 100) + '%'

This concatenates the NAME field, which holds the county name, with the percent change value and formats it to look like a percentage.

Click OK.

Page 31 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 32: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Format string fields to numbers (cont.)

Now in the Attribute table, you'll see the new fields. Note that the values that are stored as strings appear flush left, while those that are stored as numbers are flush right.

Page 32 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 33: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Format string fields to numbers (cont.)

Important step: to save these changes, you must click the editing pencil icon again. Otherwise, these new columns will not be saved.

Page 33 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 34: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Coloring the map We're ready to color the map, based on the Census data we've imported.

Click on Layers and choose Properties.

Page 34 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 35: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Coloring the map (cont.) In the Layers Properties dialog box, on the left side, choose Symbology. You may need to move the scroll bar to see it.

For Legend type, choose Graduated Symbol.

For Classification field, choose pct_int.

For Mode, choose Equal Interval.

For Number of class, choose 6.

Click on Classify and the six classes will appear below, with colors and the ranges they represent.

Click Apply.

Page 35 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 36: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Coloring the map (cont.) You should see the colors applied to the map, and the legend of colors and values in the Layers window.

This is the default color palette selected by QGIS. But we can change the colors and the ranges. To do that, go back into the Symbology options (Layers | Properties | Symbology).

Page 36 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 37: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Coloring the map (cont.) Click on the range next to the first color and a dialog box will appear to allow you to change the range upper and lower values.

Page 37 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 38: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Coloring the map (cont.) Let's make all the negative values the same color by entering 0 for Upper value.

Page 38 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 39: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Coloring the map (cont.) We'll make that color bright red. Click on the green swatch next to the first range, and choose a new color.

Click OK.

Page 39 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 40: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Coloring the map (cont.) We have to adjust the second range so it is large enough to hold the values that were part of the first range.

Click on the numbers for the second range, and change the Lower bound to .001.

Click Apply and OK.

Page 40 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 41: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Coloring the map (cont.) Now we can immediately see which counties had population declines.

Page 41 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 42: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Making an interactive web map Let's get our map

ready to export.

Whatever you see in the map window will be in your final Html map.

I prefer to get rid of the default icons that QGIS places on maps: the copy right notice, a measurement bar and a North marker. These are the icons to change these settings.

Page 42 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 43: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Making an interactive web map (cont.)

Resize the QGIS window to get the map the size you want it to be on your web page.

Click the magnifying glass icon with the four outward-pointing areas to make sure you fill up any extra space.

Page 43 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 44: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Making an interactive web map (cont.)

We're ready to export our map and html code to create an interactive web map.

Click on Plugins, and choose Html Image Map Plugin and Image Map.

Page 44 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 45: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Making an interactive web map (cont.) In the Html Image Map plugin, the image size is already set based on the dimensions of the QGIS map window.

If you don't like these dimensions, click cancel, resize the map window and try again.

Choose a filename. Note, enter the filename without the extension. The plugin will automatically append the .html to the filename you enter.

Uncheck all the boxes except onMouseOver attr. For that value, select pct_str, the field that holds the mouseover message.

Click OK.

Page 45 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 46: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Making an interactive web map (cont.)

If everything works properly, the plugin will create two files: An html page and an image with the extension png. Open up the html file with a text editor and copy all the code between the start tag and the end tag (including those tags).

Page 46 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 47: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Making an interactive web map (cont.)

Open up the web map template page you downloaded earlier. Paste the map code into this template file.

Change the src attribute to the name of the png file created by the Html Image Map plugin.

Save the web page file.

Page 47 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 48: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

The final product

Open the web page file in your browser. If all went well, this is what you should see. Move your mouse over individual counties to see the population change percentage.

Page 48 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 49: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

What we didn't cover This only

scratches the surface of what QGIS can do. You may want to explore the labeling functions; adding additional layers to the map to include roads and bodies of water; and the other ways to divide up the data into different classes, such as quantiles, which can produce a very different looking map.

Page 49 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/

Page 50: Tutorial Como Unir Dados de Uma Tabela Em Um Shape Quantum Gis

Page 50 of 50

5/8/2011http://www.projo.com/temp/tim/nicar/qgis_tutorial/


Recommended