给定坐标,如何获取 10 英里半径内的所有邮政编码?

发布于 2024-10-02 14:32:41 字数 339 浏览 2 评论 0原文

我有一个位置(纬度和经度)。如何获取部分或全部位于我所在位置 10 英里半径范围内的邮政编码列表?

该解决方案可以是调用众所周知的 Web 服务(google 地图、bing 地图等)或本地数据库解决方案(客户端有 sql server 2005)或算法。

我看到了一些类似的问题,但其中的所有答案几乎都与使用 SQL Server 2008 地理功能有关,而我无法使用该功能。

I have a location (latitude & longitude). How can I get a list of zipcodes that are either partially or fully within the 10 mile radius of my location?

The solution could be a call to a well known web service (google maps, bing maps, etc...) or a local database solution (the client has sql server 2005) or an algorithm.

I have seen the somewhat similar question, but all the answers there pretty much pertain to using SQL Server 2008 geography functionality which is unavailable to me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吾性傲以野 2024-10-09 14:32:41

从包含邮政编码及其相应纬度和经度坐标的邮政编码数据库开始:

http://www.zipcodedownload.com/Products/Product/Z5Commercial/Standard/Overview/

要获取纬度和经度之间的距离,您需要一个好的距离公式。该网站有几个变体:

http: //www.meridianworlddata.com/distance-calculation/

“大圆距离”公式有点极端。根据我的经验,这个效果很好:

sqrt(x * x + y * y)

where x = 69.1 * (lat2 - lat1)
and y = 69.1 * (lon2 - lon1) * cos(lat1/57.3)

然后你的 SQL 查询将如下所示:

select zd.ZipCode
from ZipData zd
where 
    sqrt(
        square(69.1 * (zd.Latitude - @Latitude)) +
        square(69.1 * (zd.Longitude - @Longitude) * cos(@Latitude/57.3))
    ) < @Distance

祝你好运!

Start with a zip code database that contains zipcodes and their corresponding latitude and longitude coordinates:

http://www.zipcodedownload.com/Products/Product/Z5Commercial/Standard/Overview/

To get the distance between latitude and longitude, you will need a good distance formula. This site has a couple variations:

http://www.meridianworlddata.com/distance-calculation/

The "Great Circle Distance" formula is a little extreme. This one works well enough from my experience:

sqrt(x * x + y * y)

where x = 69.1 * (lat2 - lat1)
and y = 69.1 * (lon2 - lon1) * cos(lat1/57.3)

Your SQL Query will then look something like this:

select zd.ZipCode
from ZipData zd
where 
    sqrt(
        square(69.1 * (zd.Latitude - @Latitude)) +
        square(69.1 * (zd.Longitude - @Longitude) * cos(@Latitude/57.3))
    ) < @Distance

Good luck!

等风来 2024-10-09 14:32:41

首先,您需要一个包含所有邮政编码及其相应纬度和经度的数据库。在澳大利亚,只有几千个(而且信息很容易获得),但我认为这在美国可能是一项更困难的任务。

其次,如果您知道自己所在的位置,并且知道要查找的半径,则可以查找该半径内的所有邮政编码。用 PHP 编写的简单内容如下:(抱歉,它不是用 C# 编写的)

function distanceFromTo($latitude1,$longitude1,$latitude2,$longitude2,$km){
  $latitude1  = deg2rad($latitude1);
  $longitude1 = deg2rad($longitude1);
  $latitude2  = deg2rad($latitude2);
  $longitude2 = deg2rad($longitude2);
  $delta_latitude  = $latitude2  - $latitude1;
  $delta_longitude = $longitude2 - $longitude1;
  $temp = pow(sin($delta_latitude/2.0),2) + cos($latitude1) * cos($latitude2) * pow(sin($delta_longitude/2.0),2);
  $earth_radius = 3956;
  $distance = $earth_radius * 2 * atan2(sqrt($temp),sqrt(1-$temp));
  if ($km)
    $distance = $distance * 1.609344;
  return $distance;
}

Firstly, you'll need a database of all zipcodes and their corresponding latitudes and longitudes. In Australia, there are only a few thousand of these (and the information is easily available), however I assume it's probably a more difficult task in the US.

Secondly, given you know where you are, and you know the radius you are looking for, you can look up all zipcodes that fall within that radius. Something simple written in PHP would be as follows: (apologies it's not in C#)

function distanceFromTo($latitude1,$longitude1,$latitude2,$longitude2,$km){
  $latitude1  = deg2rad($latitude1);
  $longitude1 = deg2rad($longitude1);
  $latitude2  = deg2rad($latitude2);
  $longitude2 = deg2rad($longitude2);
  $delta_latitude  = $latitude2  - $latitude1;
  $delta_longitude = $longitude2 - $longitude1;
  $temp = pow(sin($delta_latitude/2.0),2) + cos($latitude1) * cos($latitude2) * pow(sin($delta_longitude/2.0),2);
  $earth_radius = 3956;
  $distance = $earth_radius * 2 * atan2(sqrt($temp),sqrt(1-$temp));
  if ($km)
    $distance = $distance * 1.609344;
  return $distance;
}
断爱 2024-10-09 14:32:41

大多数搜索都使用质心。为了使用 10 英里内的部分邮政编码,您必须购买邮政编码多边形数据库 (*)。然后实施一种算法,检查顶点在 10 英里半径内的邮政编码。为了正确完成,您应该使用半正矢公式进行距离测量。通过一些巧妙的数据结构,您可以显着减少搜索空间。同样,通过存储并最初与 zipcoe 范围(北、西、东、南)进行比较,可以大大加快搜索速度。

(*) 注意:从技术上讲,邮政编码不是多边形!我知道我们都这样认为它们,但实际上它们是数据点(街道地址)的集合,这就是 USPS 真正使用它们的方式。这意味着邮政编码可以包含其他邮政编码;邮政编码可以由多个“多边形”组成;并且邮政编码可以与其他邮政编码重叠。大多数情况应该不是问题,但您必须处理可以定义为多个多边形的邮政编码。

Most searches work with centroids. In order to work with partial zipcodes being within the 10 miles, you are going to have to buy a database of zipcode polygons (*). Then implement an algorithm which checks for zipcodes with vertices within your 10 mile radius. To be done properly, you owuld use the Haversine formula for the distance measurement. With some clever data structures, you can significant reduce the search space. Similarly, searches can be greatly speeded up by storing and initially comparing against zipcoe extents (North,West,East,South).

(*) Note: Technically zipcodes are NOT polygons! I know we all think of them like that, but really they are collections of data points (street addresses) and this is how the USPS really uses them. This means zipcodes can include other zipcodes; zipcodes can be made of multiple "polygons"; and zipcodes can overlap other zipcodes. Most of these situations should not be a problem, but you will have to handle zipcodes that can be defined as multiple polygons.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文