给定邮政编码(或坐标)和半径,如何获得邮政编码 (GB) 列表?

How can I get a list of postcodes (GB) given a postcode (or coordinates) and a radius?

关于这个主题的文章很多,但我找不到适合我需要的文章。我唯一需要的是获取另一个邮政编码 x 英里半径内的邮政编码列表。我没有邮政编码数据库,虽然有一项免费服务可以完全满足我的需要 (http://postcodes.io/),但它确实有一个交易破坏者限制(最大半径 = 2 公里),所以我不能真正使用它。

我想也许我需要更详细地查看 google 地图 API,我相信它可能会提供我需要的东西,但从文档中看不清楚,而且它非常专注于地图(我现在不关心)。如果有人能阐明一些问题,我将不胜感激。

这是一个 C# 项目,因此欢迎使用任何有用的库。

下载 post 代码的数据库,例如this one or this one. If you have high accuracy and completeness requirements on the data, consider buying access to the official database. You can retrieve missing coordinates using Google's GeoCoding API。如果你需要解析的代码超过允许的每日免费配额,我会建议购买更大的配额,它很便宜。 Google 在文档中介绍了这一点。

拥有数据库后,将其存储在具有地理扩展名的 SQL 数据库中,例如Postgis, and then do the lookups using SQL, or preprocess it into a form that allows fast lookup of adjacent postcodes. In my experience, the best option for lookup by postcode is to add some overhead and store a hashmap of postcode → [ (postcode, distance), (postcode, distance), .. ] relations, with the lists containing every postcode for the largest radius you're ever going to look up. That is, don't use tree traversal in your searches. For the coordinate to postcodes relation you can either use a sophisticated data structure for spatial data, like a BSP tree,或者保持简单并使用粗略的二维网格结构,其中每个节点包含网格内相关方块内的所有 post 代码。

为了补充已接受的答案,postcodes.io 还提供了它使用的数据集的 pg_dump(需要启用 Postgis 的 Postgresql)。

下载并解压缩文件后,Postgresql 恢复程序,例如$ cat postcodeiodb.sql | psql postcodesiodb 是导入整个数据集所需的全部。不需要额外的导入脚本。此外,pg_dump 将为您创建所有必要的架构和索引。

该项目在此处保留指向最新数据集的指针:https://github.com/ideal-postcodes/postcodes.io/blob/master/latest

免责声明:我维护 postcodes.io