使用EXTENSION address_standardizer从parse_address输出错误

时间:2019-03-30 20:32:36

标签: sql postgresql postgis

我正在尝试清理和解析地址,并且没有使用相同的扩展名“ address_standardizer”在两个查询之间获得解析结果。在我的主要查询中,仅分析了某些城市。状态和拉链都可以。但是,在测试以了解为什么缺少大多数城市时,我在测试查询中看到了这些城市。

我正在使用相同的基本查询here修改为:

SELECT city, (a).num, (a).street, (a).city, (a).state, (a).zip, (a).zipplus
FROM (SELECT city, parse_address(city) As a
 from add_data
where city ~ ', *\D\D \d{5}$') AS p;

结果是:

city                    |num|street |city|state |zip  |zipplus
HONOLULU, HI 96819      |   |       |    |HI    |96819|
NEW YORK, NY 10018      |   |       |    |NY    |10018|
NEWHALL, CA 91321       |   |       |    |CA    |91321|
LONG BEACH, CA 90815    |   |       |    |CA    |90815|
LYNNFIELD, MA 01940     |   |       |    |MA    |01940|
CHATTANOOGA, TN 37407   |   |       |    |TN    |37407|

所以我测试了:

select city, parse_address(city) as cty
from add_data
where city ~ ', *\D\D \d{5}$';

这给了我对城市,州和邮编的预期解析:

city                        |cty
BEVERLY HILLS, CA 90213     |(,,,"BEVERLY HILLS",,CA,90213,""US)
OXNARD, CA 93031            |(,,,"OXNARD",,CA|93031,""US)
STREETSBOSO, OH 44241       |(,,,"STREETSBOSO",,OH,44241,""US)
OMAHA, NE 68114             |(,,,"OMAHA",,NE,68114,""US)
CHATTANOOGA, TN 37401       |(,,,"CHATTANOOGA",,TN,37401,""US)
CUYAHOCA HEIGHTS, OH 44125  |(,,,"CUYAHOCA HEIGHTS",,OH,44125,""US)

我不确定要在哪里丢失要解析的城市,但仍然可以处理州和邮政编码。我将如何重写查询以获取正确的解析结果?

1 个答案:

答案 0 :(得分:0)

parse_address期望输入完整,或者某些迹象表明地址的一部分丢失了。

首先,它有助于显示输出列的名称:我们看到没有对城市进行解析,就像在address1列中一样,它是街道信息的汇总。

select (parse_address('BEVERLY HILLS, CA 90213')).*;
 num | street | street2 |   address1    | city | state |  zip  | zipplus | country
-----+--------+---------+---------------+------+-------+-------+---------+---------
     |        |         | BEVERLY HILLS |      | CA    | 90213 |         | US
(1 row)

如果我们添加街道信息,则可以正确识别城市。

select (parse_address('123 ab street BEVERLY HILLS, CA 90213')).*;
 num |  street   | street2 |   address1    |     city      | state |  zip  | zipplus | country
-----+-----------+---------+---------------+---------------+-------+-------+---------+---------
 123 | ab street |         | 123 ab street | BEVERLY HILLS | CA    | 90213 |         | US
(1 row)

要正确解析不包含街道的地址的城市名称,您需要通过添加前导逗号来明确告知没有街道信息。

select (parse_address(',BEVERLY HILLS, CA 90213')).*;
 num | street | street2 | address1 |     city      | state |  zip  | zipplus | country
-----+--------+---------+----------+---------------+-------+-------+---------+---------
     |        |         |          | BEVERLY HILLS | CA    | 90213 |         | US
(1 row)

请注意,对于非标准街道类型或复杂情况,还需要在街道和城市之间添加逗号:

select (parse_address('123 ab MystreetType BEVERLY HILLS, CA 90213')).*;
 num |         street          | street2 |          address1           | city  | state |  zip  | zipplus | country
-----+-------------------------+---------+-----------------------------+-------+-------+-------+---------+---------
 123 | ab MystreetType BEVERLY |         | 123 ab MystreetType BEVERLY | HILLS | CA    | 90213 |         | US
(1 row)


select (parse_address('123 ab MystreetType, BEVERLY HILLS, CA 90213')).*;
 num |     street      | street2 |      address1       |     city      | state |  zip  | zipplus | country
-----+-----------------+---------+---------------------+---------------+-------+-------+---------+---------
 123 | ab MystreetType |         | 123 ab MystreetType | BEVERLY HILLS | CA    | 90213 |         | US