Установление связей между таблицами

Не каждый набор данных, который вы хотите использовать, поставляется как shape-файл или в пространственном формате. Часто данные будут получены в виде таблицы или листа Excel, и вам нужно будет связать его с существующими пространственными данными для использования в анализе. Эта операция известна как Соединение таблиц, и этот урок покажет, как осуществлять соединения таблиц в QGIS.

Обзор задачи

We will use a shapefile of census tracts for California and population data table from US Census Bureau to create a population map for california.

Вы научитесь также

  • создавать файлы .csvt с указанием типов данных в столбцах CSV-файла.

  • Загружать CSV-файлы, не содержащие данных о геометрии, в QGIS.

Получение данных

US Census Bureau has various spatial extracts from the MAF/TIGER database. You can query and download census tracts shapefile for California. Download Census Tracts for California file.

Americal FactFinder is a repository of all census data for the US. You can use Advanced Search and query for the Topic - Total Population and Geographies - All Census Tracts in California to create a custom CSV and download it. This tutorial uses Total Population 2010 Census Summary File 1 data.

For convenience, you may directly download a copy of both the datasets from the links below:



Источники данных: [TIGER] [USCENSUS]


  1. We will first load the census tracts shapefile. Go to Layer ‣ Add Vector Layer.
  1. Browse to the downloaded zip file tl_2013_06_tract.zip and select it. QGIS can open zip files directly so no need to uncompress it first.
  1. Select the tl_2013_06_tract.shp layer and click OK.
  1. You will see the census tracts loaded into QGIS.
  1. Right-click on the layer and select Open Attribute Table.
  1. Examine the attributes of the tracts shapefile. To join a table with this shapefile, we need a unique and common attribute for each feature. In this case, the GEOID field is a unique identifier for each tract and can be used to link this shapefile with any other table containing the same ID.
  1. Open the CSV file ca_tracts_pop.csv in a text editor. You will notice that each row of the file contains information about a tract along with the unique identifier we saw in the previous step. Note that this field is called GEO.id2 in the CSV. You will also note that the D001 column has population value for each of the census tract.
  1. We could import this csv file without any further action and it would be imported. But, the default type of each column would be a String (text). That is ok except for the D001 field which contains numbers for the population. Having those imported as text would not allow us to run any mathematical operations on this column. To tell QGIS to import the field as a number, we need to create a sidecar file with a .csvt extension. This file will have only 1 row specifying data types for each column. Save this file as ca_tracts_pop.csvt in the same directory as the original .csv file. You can also download the csvt file from here.
  1. Now we are ready to import the CSV file to QGIS. Go to Layer ‣ Add Delimited Text Layer.
  1. Browse to the folder containing the CSV file and select it. Make sure you have selected File format as CSV (comma separated values). Since we are importing this as a table, we must specify that our file contains no geometry. Select the No geometry (attribute only table) option. Click OK.
  1. CSV-файл будет импортирован в QGIS как таблица.

  1. Select the tl_2013_06_tract layer. Right-click on it and select Properties.
  1. In the Layer Properties dialog, select the Joins tab. Click on the + button at the bottom to create a new table join.
  1. In the Add vector join dialog, select ca_tracts_pop as the Join layer. Next we have to select the field with unique ids in both the shapefile and the CSV. Select GEO.id2 and GEOID as the Join field and Target field respectively. Click OK.
  1. Close the Layer Properties dialog and return to the main QGIS window. At this point, the fields from the CSV file are joined with the shapefile. Right-click on the tl_2013_06_tract layer and select Open Attribute Table.
  1. You can now see a new set of fields, including ca_tracts_pop_D001 field added to each feature. Now you have access to the population value of each tract from the CSV file. Close the attribute table and return to the main QGIS window.
  1. Right-click the tl_2013_06_tract layer and select Properties.
  1. Select the Style tab. Select the Graduated from the drop-down menu. As we are looking to create a population map, we want to assign different color to each census tract feature based on the population count. Select ca_tracts_pop_D001 as the Column. Select a color ramp of your liking from the Color ramp drop-down. In the Mode, select Quantile (Equal Count). Next click Classify. You will see a different color assigned to certain population ranges. Click OK.
  1. You will now see a nice visualization of the census tracts as styled using population values. Use the Zoom in tool to select a smaller area from the layer.
  1. You have a detailed and accurate population map of California. You can use the same technique to create maps based on variety of census data.
comments powered by Disqus

This work is licensed under a Creative Commons Attribution 4.0 International License