14
GIS Exercises Lab 8: Importing, Joining Tables 1 Lab 8: Spatial Selection, Importing, Joining Tables-QGIS What You’ll Learn: This lesson introduces spatial selection, importing text files into an ArcMap table, combining rows, and navigating tricky joins. The work is organized as two small projects, the first with step-by-step instructions, and the second less so. You should have read, and be ready to refer to Chapters 7 and 8 in the GIS Fundamentals text. This project requires synthesis of what you’ve learned up until now. Data are in the L8\ directory, separated by projects. What You’ll Produce: Three maps, one of U.S. NASS data, one of California county income, and one of California counties with parks or forests. Project 1: Select by Proximity and Adjacency Work in GIS often involves analyzing the locations of features in relation to other features. Two types of relationships are proximity (how close features are) and adjacency (features that share the same boundary). Finding points near lines Open a new data frame, and add the data from L8\Project1 the data:-twocity_Mercator.shp, -35W_Mercator_Q.shp, -USA_48_Mercator.shp. Ensure ftools plug-in is installed and enabled Choose Vector Spatial Query from main menu (see figure or toolbar icon. Video: Lab 8 (Select Adjacent) Specify to: - select source features from twocity_Mercator-Where the feature “Intersects” -Reference feature of 35w_MercatorQThen click Apply, and then Close.

Lab 8 FR3131-5131 - GIS Courses · GIS Exercises Lab 8: Importing, Joining Tables 1 Lab 8: Spatial Selection, Importing, Joining Tables-QGIS What You’ll Learn: This lesson introduces

  • Upload
    others

  • View
    38

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lab 8 FR3131-5131 - GIS Courses · GIS Exercises Lab 8: Importing, Joining Tables 1 Lab 8: Spatial Selection, Importing, Joining Tables-QGIS What You’ll Learn: This lesson introduces

GIS Exercises Lab 8: Importing, Joining Tables

1

Lab 8: Spatial Selection, Importing, Joining Tables-QGIS

What You’ll Learn: This lesson introduces spatial selection, importing text files into an ArcMap table, combining rows, and navigating tricky joins. The work is organized as two small projects, the first with step-by-step instructions, and the second less so. You should have read, and be ready to refer to Chapters 7 and 8 in the GIS Fundamentals text. This project requires synthesis of what you’ve learned up until now. Data are in the L8\ directory, separated by projects. What You’ll Produce: Three maps, one of U.S. NASS data, one of California county income, and one of California counties with parks or forests.

Project 1: Select by Proximity and Adjacency

Work in GIS often involves analyzing the locations of features in relation to other features. Two types of relationships are proximity (how close features are) and adjacency (features that share the same boundary). Finding points near lines Open a new data frame, and add the data from L8\Project1 the data:-twocity_Mercator.shp, -35W_Mercator_Q.shp, -USA_48_Mercator.shp. Ensure ftools plug-in is installed and enabled

Choose Vector Spatial Query from main menu (see figure or toolbar icon. Video: Lab 8 (Select Adjacent) Specify to: - select source features from “twocity_Mercator” -Where the feature “Intersects” -Reference feature of “35w_MercatorQ” Then click Apply, and then Close.

Page 2: Lab 8 FR3131-5131 - GIS Courses · GIS Exercises Lab 8: Importing, Joining Tables 1 Lab 8: Spatial Selection, Importing, Joining Tables-QGIS What You’ll Learn: This lesson introduces

GIS Exercises Lab 8: Importing, Joining Tables

2

On the Map, the point that represents the city of Minneapolis should be yellow, which means it has been selected. We often use this type of operation prior to some other analyses. For example, we may wish to locate a shipping center on a major highway. If we wished to find a location “near” a major highway we would need to add a proximity value to the search by buffering one of the features (this will be covered in the next lab exercise). Now, we will find all states (polygons) that are intersected by 35W. First, clear the previously selected features by View->Select-> Deselect from all Layers You could also use the toolbar button Perform another Spatial Query (as

above), with - The Target Layer(s) as 35w_MercatorQ. - Source layer as USA_48_Mercator. - Select “Where the feature Intersects”. - Then Apply and OK. The six polygons representing states that intersect the 35W should be selected, similar to the figure to the right. Selecting by adjacency Now we’ll select polygons that are adjacent to a selected set of polygons, in this case, all states that share a boundary with a selected set of states. -First, Clear Selected Features (as described above). - Select the USA_48_Mercator layer on the Layers panel. Then activate the Select Single Feature (See next page, it is a tool on the top menu bar)

Page 3: Lab 8 FR3131-5131 - GIS Courses · GIS Exercises Lab 8: Importing, Joining Tables 1 Lab 8: Spatial Selection, Importing, Joining Tables-QGIS What You’ll Learn: This lesson introduces

GIS Exercises Lab 8: Importing, Joining Tables

3

The Select Single Feature allows selection of one state; the other Select cursors allow selection of groups of states by left clicking and dragging the cursor. For now, just select one state. For the example below, select Colorado, a rectangular state in approximately the middle of the USA. , The initially selected state (Colorado) is shown at the left, now switch to the Select Features by Rectangle and carefully drag a rectangle just slightly larger than Colorado

Project 2: Adding a Text Table, Joining to a Shapefile This project introduces something quite common, joining ASCII tabular data with a shapefile. Here, we will combine a text file on corn production for US counties with a county shapefile, but there are many other types of tabular data that are available as text files, summarized on a county basis, including population, voting, education, income, crime, air pollution, and many other social, political, and environmental data. Here we import a text file, convert it to a QGIS compatible table, and edit the table, deleting columns, creating join items, and combining rows before joining it with a polygon shape file. These are all common operations when ingesting tabular data. Start QGIS, and add lwr48.shp from the L8\Project2\ subdirectory.

Target feature, Colorado

Adjacent selected set of features

Page 4: Lab 8 FR3131-5131 - GIS Courses · GIS Exercises Lab 8: Importing, Joining Tables 1 Lab 8: Spatial Selection, Importing, Joining Tables-QGIS What You’ll Learn: This lesson introduces

GIS Exercises Lab 8: Importing, Joining Tables

4

Now add the text file cnty26.csv to this data view. Select No geometry

(attribute only table) then accept all the defaults when Creating a Layer from a Delimited Text File.

Right click on the layer and select Open the Attribute Table for viewing. Video: Lab 8 (Adding Text) This file contains 1996 seed corn production, in bushels, for counties in the United States. These data were downloaded from the National Agricultural Statistical Service website, www.nass.usda.gov/, and we’re most interested in the columns: Stfips: the state Federal Information Processing System (FIPS) code, CoFips: county FIPS code, Harvested: the acres harvested for a given yield category in a county, Yield: Bushels per acre harvested for the yield category, Production: Total bushels produced (yield times harvested) for the given yield level. Unfortunately, we can’t directly edit the .csv file, so we must convert it to a DBF file (also called dBase table). Close the table viewer. Select the cnty26 layer in the Layers window, right click and Save As and save all records to the Project2 subdirectory, naming it something like “raw_corn_dat”. Select DBF file as the “Format”. Check the box for “Add saved file to the map” Then remove the cnty26.csv from the data frame, to reduce clutter. Open the raw_corn_dat attribute table, and delete all the columns except the following: Stfips, CoFips, Harvested, Yield, and Production.

Page 5: Lab 8 FR3131-5131 - GIS Courses · GIS Exercises Lab 8: Importing, Joining Tables 1 Lab 8: Spatial Selection, Importing, Joining Tables-QGIS What You’ll Learn: This lesson introduces

GIS Exercises Lab 8: Importing, Joining Tables

5

Toggle the editing button (to start editing), Delete columns by left clicking Delete Attributes button then select

the all the columns you wish to delete and select OK. Untoggle the editing button and save your changes to raw_corn_dat.dbf Close the Attribute table.

We now want to join this data with the county shapefile, lwr48.shp. Unfortunately, there are two problems. First, we don’t have a ready-made key for the join. There is no column that maps cleanly from the raw_corn_dat.dbf file to the lwr48 shapefile. Open the Attribute Table for the lwr48 shapefile. Notice that lwr48.shp also has the county and state FIPS codes, in the COUNTY and STATE columns, respectively. Each state has a unique FIPS code, and each county within a STATE has a unique code. If we combine the STATE-COUNTY codes, we can create a unique ID for each county in the country.

Toggle the editing button (to start editing), Add columns by left clicking New Column button then create a new

column. Name this column sta_count and assign the Type to be a Whole number (integer), with at width of 8 (precision). Use the Field calculator button to assign sta_count a value according to the formula: "STATE" * 10000 + "COUNTY" Remember to “Update existing field”

Multiplying the STATE by 10000 and adding to COUNTY creates a unique 5-digit code, with the value for STATE in the first two digits, and the value for COUNTY in the next three digits. Untoggle the editing button and save your changes to lwr48.shp.

Page 6: Lab 8 FR3131-5131 - GIS Courses · GIS Exercises Lab 8: Importing, Joining Tables 1 Lab 8: Spatial Selection, Importing, Joining Tables-QGIS What You’ll Learn: This lesson introduces

GIS Exercises Lab 8: Importing, Joining Tables

6

Open the raw_corn_dat.dbf file, add a sta_count column similar to the one in lwr48, and create a value for a new column using the field calculator, according to: “Stfips” *10000 + “CoFips” Now, sort the raw_corn_dat table in ascending order according to this new column, by right clicking/selecting in the sta_count column. You should have a window that looks something like the figure above, with the sta_count shown at the far right of the table. The next step requires one of the measurement fields to be summarized. However you must change the format of the field from Integer to Real for the summary to work. Chose you field Harvested, Yield or Production and convert it to Real. This is done by using the Field calculator as show to the right. I used Harvested but you could use either of the measurement fields. I named my field Harvest_R Untoggle the editing button and save your changes to raw_corn_dat.dbf

Page 7: Lab 8 FR3131-5131 - GIS Courses · GIS Exercises Lab 8: Importing, Joining Tables 1 Lab 8: Spatial Selection, Importing, Joining Tables-QGIS What You’ll Learn: This lesson introduces

GIS Exercises Lab 8: Importing, Joining Tables

7

Now select Plugins and ensure you have the Group Stats plugin installed. It should have a black check beside the Group Stats line. Next find the Group Stats toolbar. It is most likely in the extreme lower left. Select Group Stats and select raw_corn_dat in the Layers dropdown and drag sta_count to the Rows box, the drag sum and Harvest_R to the Value box. (See example below)

Press calculate. Your output should appear as shown to the right. Next select Data and Save All to CSV file. Name it SumCorn .csv

Now add the SumCorn to you Layers panel with the Create a Layer from a Delimited Text File. Make sure you check the no geometry (attribute only table) option and select Custom delimiters. (See below)

Page 8: Lab 8 FR3131-5131 - GIS Courses · GIS Exercises Lab 8: Importing, Joining Tables 1 Lab 8: Spatial Selection, Importing, Joining Tables-QGIS What You’ll Learn: This lesson introduces

GIS Exercises Lab 8: Importing, Joining Tables

8

This adds the SumCorn to your Layers Panel. Open the SumCorn file and sort ascending by sta_count. Your summary field should be names “None”. We will rename this field later for now just remember you summarized measurement field is called “None”. Check to see that the sta_count with a

value of 10001 has a None value of 5700 (this assumes you use Harvested as the measurement field. If not check back and verify the accuracy of your summarized field; hint sort raw_corn_dat by sta_count and add up the values for the 10001 value.) Remove the raw_corn_dat file from the map; we don’t need it any more.

Now, join the summary table you just created to the lwr48.shp file (right click on lwr48.shp in Layers panel, select Layer Properties of lwr48.shp, then Joins. Use the green Plus button to begin the join.

The join layer is Sum_Crop, the join fields and target fields are sta_count

Select OK and then Apply, OK

Examine the lwr48.shp file to see the new columns added to the right. Note there are some counties that do not grow corn. They will not have any joined data from the Sum_Crop file. Your lwr48.shp file should look like this:

Page 9: Lab 8 FR3131-5131 - GIS Courses · GIS Exercises Lab 8: Importing, Joining Tables 1 Lab 8: Spatial Selection, Importing, Joining Tables-QGIS What You’ll Learn: This lesson introduces

GIS Exercises Lab 8: Importing, Joining Tables

9

We want to further process this combined data. Because many operations are restricted on joined files, it is best to save a copy to a new file.

Right click on the lw48.shp in the Layers panel then left click “Save as” and name and save the file appropriately, something like US_corn.shp. Make sure you select ESRI Shapefile for the format. Add this new file to your data frame, and remove the lwr48 file and summary production tables. Open the Attribute table, toggle Editing, Select all the records where SumCorn_No 1 >= 0 then, Invert the Selection (see button)

Use the Field calculator to assign a value of 0 to all the records that have NULL values. Clear selected features.

Now, prepare your data for output. First, change the Projection (CRS) of the Project to EPSG:102003 which is: Projected->Albers

Equal Area, USA_Contiguous_Albers_Equal_Area_Conic.

Symbolize the US_corn.shp file, using the Layer Properties, Graduated, use SumCorn_No. Use a gradient color ramp between two distinct colors (below from RdYlGn). Specify about 10 classes in a Natural Breaks scheme. Right click on the first range and select Symbol, then Simple fill then No Pen for the border style, shown to the above. Select Ok, Apply, OK.

Page 10: Lab 8 FR3131-5131 - GIS Courses · GIS Exercises Lab 8: Importing, Joining Tables 1 Lab 8: Spatial Selection, Importing, Joining Tables-QGIS What You’ll Learn: This lesson introduces

GIS Exercises Lab 8: Importing, Joining Tables

10

Refine the display of the labels of the Quanties of Sum_No (really Sum Harvest) again by right clicking on the first range manually updating the label for each of the 10 categories. Select Ok, Apply and OK to complete the changes to your Style.

Add the states.shp with a hollow symbology and dark outline color. Create an appropriately annotated layout, with title, north arrow, name, legend, and other descriptive elements, and produce and submit a pdf of the layout.

Page 11: Lab 8 FR3131-5131 - GIS Courses · GIS Exercises Lab 8: Importing, Joining Tables 1 Lab 8: Spatial Selection, Importing, Joining Tables-QGIS What You’ll Learn: This lesson introduces

GIS Exercises Lab 8: Importing, Joining Tables

11

Project 3 Your third task is to produce two (2) maps on the same layout, one showing average income by county in California, and a second map showing counties with state parks or forests. These instructions will be less detailed than previous lessons, as we omit most steps we’ve covered in previous lessons. The goal is for you to synthesize these previously-taught tools on your own. Start a new blank ArcMap project. Do not add any layer yet. Maps must be produced in a UTM Zone 11, NAD83 projection (EPSG: 26911). The original data has map units of decimal degrees. Use QGIS to reproject the data, to the UTM coordinate system. Video: Lab 8 (Reproject)

Data for this project are in L8\project3\ subdirectory.

California county boundaries are in Cal.shp.

After you reproject the Cal.shp file add the reprojected layer to a NEW Project. This will eliminate the “on-the-fly-reprojection” for the rest of the exercise.

Income.dbf is a database file that lists average per capita income by county; add this to file to the Income data frame.

Rec.dbf is a database file that list recreation features an properties, including county location, add this file to the Recreation data frame.

The income, recreation, and county files have a common attribute – cnty_name. This common field allows you to join these files together. Income Map You need to produce a map showing only those counties with an average per capita income greater than $16,000. Create this map in a new data frame. Video: Lab 8 (Income) The simplest approach uses binary indicator attribute. Select values from the table and then create a new attribute and assign the value 0 (counties below 16,000 per capita income) or the value 1 (counties above 16,000)]. Park/Forest Map You need to create a map (to turn in) of California that colors counties that contain a park, a forest, or both. Create this map in a new data frame. The database file named rec.dbf lists many recreation types, including parks and forest. We must combine this

Page 12: Lab 8 FR3131-5131 - GIS Courses · GIS Exercises Lab 8: Importing, Joining Tables 1 Lab 8: Spatial Selection, Importing, Joining Tables-QGIS What You’ll Learn: This lesson introduces

GIS Exercises Lab 8: Importing, Joining Tables

12

with the state county outline layer through a join, but this join is problematic, because the source rec.dbf table does not have a proper key for this join. We have to create a proper key before joining, removing many-to-one relationship for the counties in rec.dbf with counties in the Cal.shp file. There may be multiple entries including for each county, one for each park, forest, reservoir, or other features found in a county. You need to develop a list of counties with parks or forests from this rec.dbf. Video: Lab 8 (Park_Forest). One way is to open the rec.dbf table and select all those with parks, and save your selected records to a Parks table. Repeat this process, selecting and saving only the records with forests to a Forests table. Then create indicator (binary, or 0/1) variables in both tables, for example, create a new field named something like “has forest” in the forest table, and set this value equal to one for each record in the table, and create a similar “has park” column of ones in the parks table. Now join the parks and forests tables to Cal.shp, and then select those rows with a park or a forest. There is a map showing the correct set of counties near the end of these instructions. Another, bit trickier option is to create both the parks table and the forests table, as above, but instead of adding the 0/1 binary variable to each, do a serial join. First join the parks.dbf you created with the list of counties that have parks to the Cal.shp table, then join the forests.dbf table to the Cal.shp table (with parks still joined). Then select the records that have either a park or forest in the respective columns, and assign these an indicator variable that you’ll use to symbolize your map. Video: Lab 8 (Correct Legend) A note of caution; this is a tricky exercise, and many students do not produce this final map correctly. The main problem comes from multiple entries (parks or forests) for each county. You need to be very careful in the table joins, and look at the maps you produce. Make sure your final product makes sense. One helpful guide may be the flowchart or the maps of the respective component maps; in this case a map of those counties with forests, and a separate map of those counties with parks. The “OR” condition should include data from both joined files, so your final project 2 map should have both colored in. The figure below as a guide to ensure you’ve identified counties correctly, but these are not guides for your final layouts – this is just to illustrate some characteristics of the selection. The figure after this one is an example of what part of your final map might look like.

Page 13: Lab 8 FR3131-5131 - GIS Courses · GIS Exercises Lab 8: Importing, Joining Tables 1 Lab 8: Spatial Selection, Importing, Joining Tables-QGIS What You’ll Learn: This lesson introduces

GIS Exercises Lab 8: Importing, Joining Tables

13

Your Park file should have 37 records (rows) and 24 counties, as some counties have more than one park

Your Forest file should have 20 rows, and 19 distinct counties as some counties contain more than one Forest

.

Your Park file should have

37 records (rows) and 24

counties, as some counties

have more than one park.

Page 14: Lab 8 FR3131-5131 - GIS Courses · GIS Exercises Lab 8: Importing, Joining Tables 1 Lab 8: Spatial Selection, Importing, Joining Tables-QGIS What You’ll Learn: This lesson introduces

GIS Exercises Lab 8: Importing, Joining Tables

14

Remember to both the income and park/forest maps on the same layout (see example below). Each map should be in its own data frame, and both using the UTM Zone 11 projection. Include the following elements in your maps: A descriptive title (not Lab 8. Think, what does your map contain?) The legend should not list file names, for example, cal_utm11.shp. Scale bar and north arrow- use the same scale for each data frame and only 1 north arrow.

To submit as .pdf via Moodle:

1) The corn map 2) The California Income and Recreation map