Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering QGIS

You're reading from   Mastering QGIS Go beyond the basics and unleash the full power of QGIS with practical, step-by-step examples

Arrow left icon
Product type Paperback
Published in Mar 2015
Publisher
ISBN-13 9781784398682
Length 420 pages
Edition 1st Edition
Tools
Arrow right icon
Toc

Table of Contents (12) Chapters Close

Preface 1. A Refreshing Look at QGIS FREE CHAPTER 2. Creating Spatial Databases 3. Styling Raster and Vector Data 4. Preparing Vector Data for Processing 5. Preparing Raster Data for Processing 6. Advanced Data Creation and Editing 7. The Processing Toolbox 8. Automating Workflows with the Graphical Modeler 9. Creating QGIS Plugins with PyQGIS and Problem Solving 10. PyQGIS Scripting Index

Working with tables

There are two types of tables you can work with in QGIS: attribute tables and standalone tables. Whether they are from a database or associated with a shapefile or a flat file, they are all treated the same. Standalone tables can be added by clicking on the Add Vector Layer menu by navigating to Layer | Add Layer. QGIS supports the table formats supported by OGR along with database tables. Tables are treated like any other GIS layer; they simply have no geometry. Both types of tables can be opened within Desktop by selecting the layer/table in the Layers panel, and then by either clicking on Open Attribute Table under Layer or by right-clicking on the data layer, and choosing Open Attribute Table from the context menu. They can also be previewed in Browser by choosing the Attribute tab.

The table opens in a new window that displays the number of table rows and selected records in the title bar. Below the title bar are a series of buttons that allow you to toggle between editing, managing selections, and adding and deleting columns. Most of the window is filled with the table body. The table can be sorted by clicking on the column names. An arrow will appear in the column header, indicating either an ascending or a descending sort. Rows can be selected by clicking on the row number on the left-hand side. In the lower-left corner is a Tables menu that allows you to manage what portions of the table should be displayed. You can choose Show All Features (default setting), Show Selected Features, Show Features Visible on Map (only available when you view an attribute table), Show Edited and New Features, create column filters, and advanced filters (expression). The lower-right corner has a toggle between the default table view and a forms view of the table.

Note

Attribute tables are associated with the features of a GIS layer. Typically, one record in the attribute table corresponds to one feature in the GIS layer. The exception to this is multipart features, which have multiple geometries linked to a single record in the attribute table. Standalone tables are not associated with GIS data layers. However, they may have data of a spatial nature from which a spatial data layer can be generated (for more information, see Chapter 6, Advanced Data Creation and Editing. They may also contain data that you wish to join to an existing attribute table with a table join.

Working with tables

Table joins

Let's say that you need to make a map of the total population by county. However, the counties' GIS layer does not have population as an attribute. Instead, this data is contained in an Excel spreadsheet. It is possible to join additional tabular data to an existing attribute table.

There are two requirements, which are as follows:

  • The two tables need to share fields with attributes to match for joining
  • There needs to be a cardinality of one-to-one or many-to-one between the attribute table and the standalone table

To create a join, load both the GIS layer and the standalone table into QGIS Desktop. QGIS will accept a variety of standalone table file formats including Excel spreadsheets, .dbf files, and comma delimited text files. You can load this tabular data using the Add Vector Layer menu by navigating to Layer | Add Layer and setting the file type filter to All files (*) (*.*) as shown in the following screenshot:

Table joins

Once the data is loaded, a join can be completed by following these steps:

  1. Select the GIS layer in the Layers panel that will receive the new data from the join.
  2. Navigate to Layer | Properties and choose the Joins menu.
  3. Click on the add join button (the one with green plus sign).
  4. Choose the Join Layer, Join Field, and Target Field values. The Join Layer and Join Field values represent the standalone table. The Target Field value is the column in the attribute table on which the join will be based.

    Note

    Although in this example the join field and the target field have the same name, this is not a requirement. The two fields merely need to hold the same unique ID.

  5. At this point, you can choose Cache the join in virtual memory, Create attribute index on the join field, and Choose which fields are joined. The last option lets you to choose which fields from the join layer to append to the attribute table. At this point, the Add vector join window will look like the following screenshot.
  6. Once created, the join will be listed on the Joins tab. The extra attribute columns from the join layer will be appended to the attribute table, where the value in the join field matched the value in the target field.
  7. Joins can be removed by clicking on the remove join button (the one with red minus sign).
    Table joins

    Tip

    Joins only exist in virtual memory within the QGIS Desktop document. To preserve the join outside the map document click on Save as... under Layer and save a new copy of the layer. The new layer will include the attributes appended via the join.

You have been reading a chapter from
Mastering QGIS
Published in: Mar 2015
Publisher:
ISBN-13: 9781784398682
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image