Chapter 12. PostGIS 补充

Table of Contents

本章记录了 PostGIS 源 tarball 和源存储库的 extras 文件夹中的功能。 这些并不总是与 PostGIS 二进制版本一起打包,但通常是基于 PL/pgSQL 或可以按原样运行的标准 shell 脚本。

12.1. 地址标准化工具

这是 PAGC 标准化器的一个分支(这部分的原始代码是 PAGC PostgreSQL 地址标准化器)。

地址标准化器是一个单行地址解析器,它获取输入地址并根据存储在表以及辅助 lex 和 gaz 表中的一组规则对其进行规范化。

该代码内置于一个名为 address_standardizer 的 PostgreSQL 扩展库中,可以使用 CREATE EXTENSION address_standardizer; 安装。 除了address_standardizer 扩展之外,还构建了一个名为address_standardizer_data_us 扩展的示例数据扩展,其中包含US 数据的gaz、lex 和规则表。 该扩展可以通过以下方式安装:CREATE EXTENSION address_standardizer_data_us;

此扩展的代码可以在 PostGIS extensions/address_standardizer 中找到,并且当前是独立的。

有关安装说明,请参阅:Section 2.3, “安装和使用地址标准化工具”

12.1.1. 解析器如何工作

解析器从右到左工作,首先查看邮政编码、州/省、城市的宏观元素,然后查看微观元素以确定我们是否正在处理门牌号、街道、十字路口或地标。 目前,它不查找国家/地区代码或名称,但将来可能会引入。

国家代码

根据邮政编码为美国或加拿大,或者州/省为美国或加拿大,否则为美国

邮政编码(Postcode)/邮政编码(zipcode)

这些是使用 Perl 兼容的正则表达式来识别的。 这些正则表达式当前位于 parseaddress-api.c 中,并且在需要时进行更改相对简单。

州/省

这些是使用 Perl 兼容的正则表达式来识别的。 这些正则表达式当前位于 parseaddress-api.c 中,但将来可能会被移至包含中以便于维护。

12.1.2. 地址标准化器类型

Abstract

本节列出了由 Address Standardizer 扩展安装的 PostgreSQL 数据类型。 请注意,我们描述了这些的强制转换行为,这非常重要,尤其是在设计您自己的函数时。

  • stdaddr — 由地址元素组成的复合类型。 这是 standardize_address 函数的返回类型。

12.1.3. 地址标准化表

Abstract

本节列出了 address_standardizer 用于规范化地址的 PostgreSQL 表格式。 请注意,这些表的名称不需要与此处引用的名称相同。 例如,您可以为每个国家/地区或您的自定义地理编码器设置不同的 lex、gaz、规则表。 这些表的名称被传递到地址标准化器函数中。

打包的扩展address_standardizer_data_us包含用于标准化美国地址的数据。

  • rules table — 规则表包含一组规则,将地址输入序列标记映射到标准化的输出序列。规则被定义为一组输入标记,后跟 -1(终止符),然后是一组输出标记,后跟 -1,后跟表示规则类型的数字,最后是规则的排名。
  • lex table — lex 表用于对字母数字输入进行分类,并将该输入与 (a) 输入标记(请参阅the section called “输入令牌”一节)和 (b) 标准化表示相关联。
  • gaz table — gaz 表用于标准化地名,并将该输入与 (a) 输入令牌(请参阅“the section called “输入令牌””一节)和 (b) 标准化表示相关联。

12.1.4. 地址标准化器功能

12.2. Tiger地理编码器

Abstract

一个基于 PL/pgSQL 编写的地理编码器,美国人口普查局发布的 TIGER(拓扑集成地理编码与参照系统)/ Line and Master Address database export 配合使用。

地理编码器有四个组件:数据加载器函数、地址标准化器、地址地理编码器和反向地理编码器。

尽管它是专门为美国设计的,但许多概念和功能都是适用的,并且可以适应其他国家的地址和道路网络。

The script builds a schema called tiger to house all the TIGER-related functions, reusable lookup data such as road type prefixes, suffixes, states, various control tables for managing data load, and skeleton base tables from which all the TIGER-loaded tables inherit.

Another schema called tiger_data is also created which houses all the census data for each state that the loader downloads from the Census site and loads into the database. In the current model, each set of state tables is prefixed with the state code e.g ma_addr, ma_edges etc with constraints to enforce only that state data. Each of these tables inherits from the tables addr, faces, edges, etc located in the tiger schema.

All the geocode functions only reference the base tables, so there is no requirement that the data schema be called tiger_data or that data can't be further partitioned into other schemas -- e.g. a different schema for each state, as long as all the tables inherit from the tables in the tiger schema.

有关如何在数据库中启用扩展程序以及如何使用它加载数据的说明,请参阅Section 2.4.1, “在 PostGIS 数据库中启用Tiger地理编码器”

[Note]

If you are using the TIGER Geocoder (tiger_2010), you can upgrade the scripts using the accompanying upgrade_geocoder.bat / .sh scripts in extras/tiger. One major change between tiger_2010 and tiger_2011+ is that the county and state tables are no longer broken out by state. If you have data from tiger_2010 and want to replace with tiger_2015, refer to Section 2.4.4, “升级您的 Tiger 地理编码器 安装和数据”

[Note]

You can install the TIGER Geocoder with the PostgreSQL extension model. Refer to Section 2.4.1, “在 PostGIS 数据库中启用Tiger地理编码器” for details.

Pagc_Normalize_Address函数可替代内置 Normalize_Address。 有关编译和安装说明,请参阅Section 2.3, “安装和使用地址标准化工具”

设计:

该项目的目标是构建一个功能齐全的地理编码器,可以处理任意美国地址字符串并使用标准化的 TIGER 人口普查数据,生成反映给定地址位置和位置可能性的点几何和评级。 评级数字越高,结果越差。

The reverse_geocode function is useful for deriving the street address and cross streets of a GPS location.

对于熟悉 PostGIS 的任何人来说,地理编码器都应该易于安装和使用,并且应该可以在 PostGIS 支持的所有平台上轻松安装和使用。

尽管存在格式和拼写错误,但它应该足够强大,能够正常运行。

它应该具有足够的可扩展性,以便与未来的数据更新一起使用,或者以最少的编码更改来替代数据源。

[Note]

必须将 Tiger 模式添加到数据库搜索路径中,函数才能正常工作。

  • Drop_Indexes_Generate_Script — 生成一个脚本,删除 Tiger 架构和用户指定架构上的所有非主键和非唯一索引。 如果未指定架构,则默认架构为 Tiger_data
  • Drop_Nation_Tables_Generate_Script — 生成一个脚本,删除指定架构中以 County_allstate_all 或state代码开头,后跟countystate的所有表。
  • Drop_State_Tables_Generate_Script — 生成一个脚本,该脚本删除指定架构中以state缩写为前缀的所有表。 如果未指定架构,则默认架构为 Tiger_data
  • Geocode — 将地址作为字符串(或其他标准化地址)输入,并输出一组可能的位置,其中包括 NAD 83经纬度中的点几何图形、每个位置的标准化地址以及评级。 评级越低,匹配的可能性越大。 结果首先按最低评分排序。 可以选择传入最大结果数,默认为10,以及restrict_region(默认为NULL)
  • Geocode_Intersection — 接收 2 条相交的街道以及state、city、zip,并输出位于交叉路口的第一个交叉街道上的一组可能位置,还包括一个 geomout 作为 NAD 83 经纬度中的点位置,一个标准化地址 (addy )每个位置以及评级。 评级越低,匹配的可能性越大。 结果首先按最低评分排序。 可以选择传入最大结果,默认为 10。使用 Tiger 数据(edges、faces、addr)、PostgreSQL 模糊字符串匹配(soundex、levenshtein)。
  • Get_Geocode_Setting — 返回存储在 Tiger.geocode_settings 表中的特定设置的值。
  • Get_Tract — 从几何图形所在的区域表中返回人口普查区域或字段。 默认返回区域的短名称。
  • Install_Missing_Indexes — 查找具有在地理编码器连接和过滤条件中使用的键列的所有表,这些表缺少这些列上使用的索引,并将添加它们。
  • Loader_Generate_Census_Script — 为指定states的指定平台生成 shell 脚本,该脚本将下载 Tiger 人口普查state区、bg 和 tabblocks 数据表、暂存并加载到 Tiger_data 架构中。 每个状态脚本都作为单独的记录返回。
  • Loader_Generate_Script — 为指定平台的指定状态生成 shell 脚本,该脚本将下载 Tiger 数据、暂存并加载到 Tiger_data 模式中。 每个状态脚本都作为单独的记录返回。 最新版本支持 Tiger 2010 结构变化,还加载人口普查区、区块组和区块表。
  • Loader_Generate_Nation_Script — 为指定平台生成加载到县和州查找表中的 shell 脚本。
  • Missing_Indexes_Generate_Script — 查找具有在地理编码器联接中使用的键列的所有表,这些表缺少这些列上的索引,并将输出 SQL DDL 来定义这些表的索引。
  • Normalize_Address — 给定文本街道地址,返回复合的norm_addy类型,该类型具有道路后缀、前缀和标准化类型、街道、街道名称等,分为单独的字段。 该函数仅适用于与 Tiger_geocoder 打包的查找数据(不需要tiger普查数据)。
  • Pagc_Normalize_Address — 给定文本街道地址,返回复合的norm_addy类型,该类型具有道路后缀、前缀和标准化类型、街道、街道名称等,分为单独的字段。 该函数仅适用于与 Tiger_geocoder 打包的查找数据(不需要tiger普查数据)。 需要 address_standardizer 扩展。
  • Pprint_Addy — 给定一个norm_addy复合类型对象,返回它的漂亮的打印表示。 通常与normalize_address结合使用。
  • Reverse_Geocode — 获取已知空间参考系统中的几何点并返回一条记录,其中包含理论上可能的地址数组和交叉街道数组。 如果 include_strnum_range = true,则包括交叉街道中的街道范围。
  • Topology_Load_Tiger — 将tiger数据的定义区域加载到 PostGIS 拓扑中,并将老虎数据转换为拓扑的空间参考,并捕捉到拓扑的精度公差。
  • Set_Geocode_Setting — 设置影响地理编码器功能行为的设置。

There are a couple other open source geocoders for PostGIS, that unlike the TIGER Geocoder have the advantage of multi-country geocoding support

  • Nominatim uses OpenStreetMap gazeteer formatted data. It requires osm2pgsql for loading the data together with PostgreSQL and PostGIS. It is packaged as a webservice interface and seems designed to be called as a webservice. Just like the TIGER Geocoder, it has both a geocoder and a reverse geocoder component. From the documentation, it is unclear if it has a pure SQL interface like the TIGER Geocoder, or if a good deal of the logic is implemented in the web interface.

  • GIS Graphy can utilize PostGIS and like Nominatim uses OpenStreetMap (OSM) data along with some other sources. It comes with a loader to load OSM data and similar to Nominatim is capable of geocoding not just US. Much like Nominatim, it runs as a webservice and relies on Java 1.5, Servlet apps, Solr. GisGraphy is cross-platform and also has a reverse geocoder among some other neat features.