资料表连接 (QGIS3)

并非您要使用的每个数据集都采用空间格式。通常,数据会以表格或电子表格的形式出现,您需要将其与现有空间数据链接起来以用于分析。此操作称为 表联接 ,使用 按字段值联接属性 处理算法完成。

内容说明

我们会使用美国加州的人口统计分区 shapefile,与美国人口调查局提供的人口数资料表,做一份加州的人口分布图。

你还会学到这些

  • 在 QGIS 中载入不含任何地理资讯的 CSV 档案。

  • 使用数据库管理器执行 SQL 查询以计算组统计信息。

取得资料

US Census Bureau 提供 TIGER / Line Shapefiles。 您可以访问 FTP site 并下载加利福尼亚州的人口普查区shapefile。下载 California Census Tracts for California 文件。

Americal FactFinder 是美国所有人口普查数据的存储库。您可以使用 高级搜索 并查询 主题-基本计数/估算地理-所有加利福尼亚人口普查区 来创建自定义 CSV 并下载。本教程使用 总人口| 2017 ACS 5年估算 数据。

../../_images/data13.png

为了方便起见,你也可以直接用下面的连结下载这两份资料集:

tl_2018_06_tract.zip

ACS_17_5YR_B01003.zip

资料来源 [TIGER] [USCENSUS]

操作流程

  1. 在 QGIS 浏览器中找到 tl_2018_06_tract.zip 文件并展开它。选择 tl_2018_06_tract.shp 文件并将其拖到画布上。

../../_images/1104.png
  1. 您将在 Layers 面板中看到加载的 tl_2018_06_tract 层。该层包含加利福尼亚人口普查区域的边界。右键单击 tl_2018_06_tract 层,然后选择 Open Attribute Table

../../_images/257.png
  1. 检查图层的属性。要将表与该层连接,我们需要为每个功能部件使用唯一且通用的属性。在这种情况下, GEOID 字段是每个区域的唯一标识符,可用于将该层与包含相同 ID 的任何其他层或表链接起来。

../../_images/335.png
  1. 解压缩 ACS_17_5YR_B01003.zip 文件并在文本编辑器中打开 ACS_17_5YR_B01003_with_ann.csv 文件。您会注意到,文件的每一行都包含有关区域的信息以及我们在上一步中看到的唯一标识符。请注意,此字段在 CSV 中称为 GEO.id2 。 您还将注意到, HD01_VD01 列具有每个普查区域的人口值。

../../_images/420.png
  1. 在导入此 CSV 文件之前,我们需要进行少量修改。QGIS CSV 导入程序希望文件的第一行包含列标题,而所有其余行都包含这些列的数据。该文件包含带有列标签的额外第2行。删除该行并保存文件。

../../_images/520.png
  1. 现在,我们准备将 CSV 文件导入 QGIS 。转到 Layer-> Add Layer-> Add Delimited Text Layer

../../_images/620.png
  1. Data Source Manager 窗口中,单击 按钮,然后浏览到CSV文件并选择它。确保已将 文件格式 选择为 CSV(逗号分隔值) 。由于我们将其导入为表格,因此必须使用 No geometry (attribute table only) 选项指定文件中不包含几何图形。验证底部的 Sample Data 预览看起来不错,然后单击 Add,然后单击 Close

../../_images/719.png
  1. CSV 现在将作为表格导入到 QGIS 中,并在 Layers 面板中显示为 ACS_17_5YR_B01003_with_ann 。现在我们准备创建表联接。 转到 Processing ‣ Toolbox

../../_images/819.png
  1. 首先,我们需要在 Processing Toolbox 中更改默认设置。点击 Options 按钮。

../../_images/919.png
  1. Processing Options 标签中,选中 Use filename as layer name 选项。 使用处理工具箱中的算法时,此选项使输出图层名称更加直观和有用。点击 OK

../../_images/1019.png
  1. 返回 Processing Toolbox 中,搜索并找到 Vector General ‣ Join attributes by field value 算法然后双击将其打开。

../../_images/1122.png
  1. Join Attributes by Field Values 对话框中,选择 tl_2018_06_tract 作为 Input layer ,并选择 GEOID 作为 Table field 。 选择 ACS_17_5YR_B01003_with_ann 作为 Input layer 2 的名称,并选择 GEO.id2 作为 :guilabel: Table field 2 的名称。将其他选项保留为默认值,然后单击 按钮以选择输出文件位置,然后选择 保存到GeoPackage ...

../../_images/1220.png
  1. 将输出地理包命名为 joined.gpkg ,将输出层命名为 joined 。 点击 Run

../../_images/1319.png
  1. 处理完成后,请验证算法是否成功,然后单击 Close

../../_images/1418.png
  1. 您会在 Layers 面板看到一个新的 joined 图层。此时,CSV 文件中的字段将与人口普查区域图层结合在一起。您可以立即关闭 Processing Toolbox 。右键单击 joined 层,然后选择 Open Attribute Table

../../_images/1517.png
  1. 您将看到一组新字段,包括包含人口估计值的 HD01_VD01 字段。

../../_images/1616.png
  1. 现在我们已经在人口普查区域中拥有了人口数据,我们可以对其进行样式设置以创建人口分布的可视化。选择 joined 图层并单击 Open the Layer Styling Panel 按钮。

../../_images/1717.png
  1. In the Layer Styling panel, select Graduated from the drop-down menu. As we are looking to create a population density map, we want to assign different color to each census tract feature based on the population density. We have the population in the HD01_VD01 field, but we don’t have population density in any fields to select as the Value. Fortunately, QGIS allows us to input an expression here. Click Expression button.

../../_images/1816.png

注解

When creating a thematic (choropleth) map such as this, it is important to normalize the values you are mapping. Mapping total counts per polygon is not correct. It is important to normalize the values dividing by the area. If you are displaying totals such as crime, you can normalize them by diving by total population, thus mapping crime rate and not crime. Learn more

  1. Enter the following expression to calculate the population density. $area calculates the area of the feature in square meters. We then convert it to square miles and calculate the population density with the formula Population/Area. Click OK.

"HD01_VD01"/ (0.386*$area/1e6)
../../_images/1913.png
  1. Back in the Layer Styling Panel, choose a color ramp of your choice and click Classify. You can adjust the class ranges to be more appropriate to the region.

../../_images/2010.png
  1. The visualization feels a bit cluttered because of the polygon borders. Click on the dropdown next to Symbol. Select Simple fill and check Transparent stroke.

../../_images/2115.png
  1. Now we have a nice looking information visualization of population density in California.

../../_images/2213.png