Practical Exercise 4: Databases and Attributes
Practical Exercise 4: Databases and Attributes
GISA01
Databases and Attributes
4.1. Purpose
The aim of this exercise is to provide you with an introduction to databases and how to
handle and query attributes in ArcGIS. More specifically, you will learn the following:
- How to add data to an already existing attribute table.
- How to import data from Excel.
- How to join two tables together.
- How to convert fields containing coordinates to a shapefile
- How to structure simple SQL-queries.
4.2. Data
In Moodle, you can find a zip-file with the name DATA4_Databases. Download and save
the file to your computer and use WinZip to unzip the files. The unzipped file contain the
following data:
Srilanka Shapefile: .shp, .dbf, .sbn, .sbx, .shx.
Gamdiv Shapefile: .shp, .dbf, .sbn, .sbx, .shx.
Ham_districts Shapefile: .shp, .dbf, .sbn, .sbx, .shx.
Hambantota_wind Excel file: .xls.
Gampop94-97 Excel file: .xls.
Un_employ dBase: .dbf.
Create a folder in your local working directory with the name 4_Databases (e.g.
C:\GISA01\Practical\4_Databases). It is recommended that you keep an organized file
structure on your computer.
14-01-2010 Copyright 2010. Lund University GIS Centre. All rights reserved. 1
Databases and Attributes
4.4. Method
14-01-2010 Copyright 2010. Lund University GIS Centre. All rights reserved. 2
Databases and Attributes
Kegalle 1413
Unfortunately there are data missing for the districts of Ampara, Batticaloa, Jaffna,
Kilinochchi, Mannar, Mullaitivu, Trincomalee, and Vavuniya. For these the average
income will be set to 0.
- Right click Srilanka in the Table of Contents (TOC) and select Open Attribute
Table from the context menu.
- Make sure the field named DISTRICT is placed furthest to the right in the
attribute table. If not, select the field by clicking its header (the field turns light
blue). Then click the header again and drag it so that the field is placed furthest
to the right in the table.
- Right click on the header of DISTRICT and select Sort Ascending. The names
of the districts should now appear in alphabetical order.
- Click Options in the lower right corner in Attributes of srilanka choose Add
Field. The Add field dialog window should appear.
- Name the field INCOME and choose Short Integer as Type. Leave all other
parameter settings as-is.
14-01-2010 Copyright 2010. Lund University GIS Centre. All rights reserved. 3
Databases and Attributes
Now, type the data from the table with average monthly income values for the districts in
the field INCOME.
- Start editing Attributes of srilanka by clicking Editor > Start Editing in the
Editor toolbar.
Remember that you can activate the Editor toolbar through clicking View > Toolbars if it
is not visible.
- Type in the average monthly income values for each of the districts in the field
INCOME. Press Enter to move to the cell below.
- When finished with the typing, save your edits by clicking Editor > Stop
Editing and Editor > Save Edits.
Check that the values are correct and that all of them have been added. If not, fill in the
missing values.
- Close Attributes of srilanka.
14-01-2010 Copyright 2010. Lund University GIS Centre. All rights reserved. 4
Databases and Attributes
The map now shows the variation in income in the different districts of Sri Lanka. The
colors of districts where income data is missing or set to 0 are shown in yellow.
You will now produce a layout map with a title, legend, scale bar and north arrow and
export it as a .jpg image file.
14-01-2010 Copyright 2010. Lund University GIS Centre. All rights reserved. 5
Databases and Attributes
14-01-2010 Copyright 2010. Lund University GIS Centre. All rights reserved. 6
Databases and Attributes
- In the Legend Item selector dialog window click Properties. In the tab General
uncheck Show Layer Name and Show Heading and click OK thrice to apply.
- Finally, export your layout by clicking File > Export Map in the main menu
when in Layout View. Choose .jpg as file type and name the file
Srilanka_Income and save it in your working directory together with the rest of
your data.
- Insert the map in a Word document using Insert > Picture > From File. As you
will later have to add another map and answers to a couple of questions, prepare
the document by adding your name and all other student info as well.
- Return to ArcMap and save your map document.
14-01-2010 Copyright 2010. Lund University GIS Centre. All rights reserved. 7
Databases and Attributes
- Change the drawing order and color of the layers. In the TOC, drag the layer
Ham_districts and place it at the top, above Srilanka. Click the symbol for a
layer in the TOC to open the Symbol Selector dialog window. Choose an
appropriate color for each of the layers.
Imagine that you have been given an assignment to construct a map showing the
suggested locations for ten new wind power stations in the Hambantota district. The
names of the wind power stations should be labeled on the map. At your disposal, you
have the two layers Ham_districts and Srilanka and an Excel table (Hambantota_wind)
containing the names and locations of the ten planned wind power stations in
Hambantota.
ID NAME X Y
ArcGIS can handle a number of types of tabular data formats including text files (.txt),
Microsoft Excel files (.xls) and of course dBase files (.dbf). All of these data formats can
be added to ArcMap, previewed in ArcCatalog, and used as inputs in various
geoprocessing tools. Now, before we add the Excel table with wind power stations to
ArcMap lets go through some of the basics.
Excel files and other tabular data formats are added to ArcMap just like shapefiles, by
clicking Add Data. As you most probably know an Excel file is made up of a workbook
containing one or several worksheets or arrays (three is the default number of worksheets
in a workbook). When you add an Excel file in ArcMap or preview it in ArcCatalog a
worksheet is shown with a $ at the end of its file name. Once the worksheet has been
added to ArcMap it can be accessed from the tab Source at the bottom in the TOC. A
worksheet is read only in ArcGIS, meaning that it is not possible to edit or export it.
Note: You can read more about Excel files and other tabular data sources
supported in ArcGIS in ArcGIS Desktop Help.
14-01-2010 Copyright 2010. Lund University GIS Centre. All rights reserved. 8
Databases and Attributes
14-01-2010 Copyright 2010. Lund University GIS Centre. All rights reserved. 9
Databases and Attributes
Tip: You can change the names of the layers in the legend to something more
appropriate by simply right clicking the layer in the TOC and in the tab General
specifying another name in the box Layer Name.
- When you are satisfied with your layout, export your map as a .jpg image file
by clicking File > Export Map when in Layout View. Save it as
Windpower_Hambantota, insert the .jpg image file in the document containing
the Sri Lanka income map and save it.
14-01-2010 Copyright 2010. Lund University GIS Centre. All rights reserved. 10
Databases and Attributes
- Add the layer Srilanka and the .dbf file Un_employ to your newly created map
document.
- Open the attribute table for Un_employ and explore its contents. The table
should contain five fields or columns: OID, DISTCODE, DISTRICT,
EMPLOYMENT and UNEMPLOYED. Reflect on what these columns could
represent and then close Attributes of un_employ.
We will now join the data in Un_employ to the shapefile layer Srilanka. In ArcGIS, you
can join tabular data based on a common field. A tabular data source might for instance
contain descriptive attributes on features and by joining tables together you can
symbolize, label, query and analyze features using the data from the tabular data source.
- Open the table for Srilanka (destination table) and Un_employ (source table).
Move the table windows so you can view both of them simultaneously.
Both tables have a field with common records. The name or heading of these fields are
however different. In Srilanka it is called SLHE4_ID and in Un_employ it is called
DISTCODE.
- Right click Srilanka in the TOC and select Join and Relates > Join. The Join
Data dialog box appears.
- Select to Join attributes from a table. In drop down menu 1 select SLHE4_ID as
the field that the join will be based on. At 2 choose the table Un_employ and at
3 choose DISTCODE as the field to base the join. Choose to keep all records
and then click OK (you can click About Joining Data to learn more).
14-01-2010 Copyright 2010. Lund University GIS Centre. All rights reserved. 11
Databases and Attributes
Now take a look at the attribute table of Srilanka. The fields from Un_employ should
have been joined to the table.
- In Attributes of srilanka select the field SLHE4_ID and then click and drag it so
that it is placed next to DISTCODE. Compare the records in both fields to
ensure that they match.
14-01-2010 Copyright 2010. Lund University GIS Centre. All rights reserved. 12
Databases and Attributes
Operator Description
= Equal
You will now use Select by Attributes to structure a simple WHERE clause to find out
which districts in Sri Lanka that have an employment rate of 90% or more.
- In the main menu click Selection > Select by Attributes. The Select by
Attributes dialog window appears. Make sure Unemployed is selected in the
drop down menu Layers.
14-01-2010 Copyright 2010. Lund University GIS Centre. All rights reserved. 13
Databases and Attributes
Just above the query box at the bottom of the window, the first part of the SQL
expression, SELECT * FROM, is supplied. This part of the SQL query is hardcoded. The
SELECT statement is used to select data and the asterisk (*) indicates that all columns
will be selected from the table. The WHERE clause allows you select records that fulfill a
specified criteria. Lets structure a simple query.
- In the Select by Attribute dialog box, make sure Unemployed is selected in the
drop down menu Layers.
- Right click in the field box listing all of the fields in the attribute table of
Unemployed and select Sort Ascending. Double click the field EMPLOYMENT
to add the field to the query box. Click the larger than/equal to operator button
and type in 90 (for 90%) in the query box and finally click Apply.
- Open the attribute table of Unemployed. In the attribute table, you can choose to
list selected records by clicking the button Selected at the bottom of the table.
You can revert to show all by clicking All.
Question 4.1: Name the districts that have an employment larger than 90%?
Question 4.2: Name the districts have an employment less than 85% (do not include
districts with zero values)?
14-01-2010 Copyright 2010. Lund University GIS Centre. All rights reserved. 14
Databases and Attributes
- Open a new map document and add the Excel file Gampop94-97 and the
shapefile Gamdiv.
- Right click Gamdiv and join the two tables using the Join Data dialog box.
Base the join on the field ID in Gamdiv and the field Div_id in Gampop94-97.
- Save the join as a new shapefile. Name it gampop.shp, add it to ArcMap and
delete the layer Gamdiv.
When exploring a table, you can get statistics describing the values in numeric fields.
You can see how many values the field has as well as the sum, minimum, mean,
maximum, and standard deviation of the values in a field.
14-01-2010 Copyright 2010. Lund University GIS Centre. All rights reserved. 15
Databases and Attributes
- Open the attribute table of Gampop, right click the heading of the field named
Pop1994 and click Statistics. A window named Statistics of gampop appears.
- Click the drop down menu Field and choose Pop1997 to generate statistics for
the population values from 1997 in the same way.
The hardcoded text Popdiff = just above the text area in the Field Calculator indicates
that it is the result field. You can use the list Fields and the functions and/or you can type
to build a calculation expression in the text area.
- Use the Field Calculator functions to create the expression [Pop1997]-
[Pop1994]. Click OK to perform calculations and make sure to click Editor >
Save Edits and Editor > Stop Editing when done.
14-01-2010 Copyright 2010. Lund University GIS Centre. All rights reserved. 16
Databases and Attributes
Question 4.3: Which three districts had the largest population increase between
1994 and 1997 (give the name of the districts and their population increase)?
14-01-2010 Copyright 2010. Lund University GIS Centre. All rights reserved. 17
Databases and Attributes
- Name the output table stat_area_income.dbf. Click OK, add the table to the
map document and then open it.
The resulting table should contain five fields with the statistics you specified for each of
the provinces.
14-01-2010 Copyright 2010. Lund University GIS Centre. All rights reserved. 1