查找美国城市所在的美国县的最佳方式是什么?

发布于 2024-11-18 15:00:07 字数 74 浏览 4 评论 0原文

我正在寻找最好/最简单的方法来以编程方式获取特定美国城市所在的美国县的名称。似乎没有一个简单的 API 可用于此类(看似简单)的任务?

I'm looking for the best/easiest way to programmatically grab the name of the US county a given US city resides in. It doesn't seem there's a straightforward API available for such a (seemingly simple) task?

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

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

发布评论

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

评论(5

岁月静好 2024-11-25 15:00:07

您可以免费下载县/市/邮政编码信息数据库,例如:
http://www.unitedstateszipcodes.org/zip-code-database/(无需要注册或付费)

将其全部或一部分导入本地持久数据存储(例如数据库),并在需要查找城市的县时查询它

注意:县信息已消失自发布此答案以来最初链接的 .csv 文件。
此链接不再包含县信息:http://federalgovernmentzipcodes.us/free-zipcode-database.csv

You can download a freely-available database of county/city/zip code info such as this one:
http://www.unitedstateszipcodes.org/zip-code-database/ (no need to register or pay)

Import it whole, or a subsection of it, into a local, persistent data store (such as a database) and query it whenever you need to look up a city's county

Note: County info has disappeared from the originally-linked .csv file since this answer was posted.
This link no longer contains county information: http://federalgovernmentzipcodes.us/free-zipcode-database.csv

最终幸福 2024-11-25 15:00:07

1) 城市跨越县

2) Zip 跨越城市和县,甚至不在同一条线上

任何使用 zip 作为中介的解决方案都会损坏您的数据(不,“zip+4”通常不会修复它) 。您会发现城市到邮政编码到县的数据地图 (#2) 比更准确的模型 (#1) 具有更多的城市到县的匹配数量 - 这些都是不良匹配。

您正在寻找的是免费的人口普查数据。您需要的联邦信息处理标准 (FIPS) 数据集称为“2010 ANSI 地点代码”:https://www.census.gov/geographies/reference-files/time-series/geo/name-lookup-tables.2010.html

人口普查“地点”是“城市”对于我们的问题。这些文件将“地点”映射到一个或多个县。

1) Cities span counties

2) Zips span both cities and counties, not even on the same lines

Any solution that uses zip as an intermediary is going to corrupt your data (and no, "zip+4" won't usually fix it). You will find that a city-to-zip-to-county data map (#2) has a larger number of city-to-county matches than the more accurate model (#1)--these are all bad matches.

What you're looking for is free census data. The Federal Information Processing Standards (FIPS) dataset you need is called "2010 ANSI Codes for Places": https://www.census.gov/geographies/reference-files/time-series/geo/name-lookup-tables.2010.html

Census "places" are the "cities" for our question. These files map "places" to one or more county.

成熟稳重的好男人 2024-11-25 15:00:07

下面是一些代码,用于使用 Google 地图 API 以编程方式获取给定单个美国城市/州的美国县的名称。该代码速度慢/效率低,并且没有任何错误处理。然而,它对我来说可以可靠地将县与约 1,000 个城市的列表进行匹配。

#Set up googlemaps API
import googlemaps
google_maps = googlemaps.Client(key='API_KEY_GOES_HERE')

#String of city/state
address_string = 'Atlanta, GA'

#Geocode
location = google_maps.geocode(address_string)

#Loop through the first dictionary within `location` and find the address component that contains the 'administrative_area_level_2' designator, which is the county level
target_string = 'administrative_area_level_2'
for item in location[0]['address_components']:     
    if target_string in item['types']: #Match target_string
        county_name = item['long_name'] #Or 'short_name'
        break #Break out once county is located
    else:
        #Some locations might not contain the expected information
        pass

这会产生:

>>> county_name
Fulton County

警告:

  1. 如果 google_maps.geocode() 未传递有效的代码,代码将中断
    地址
  2. 某些地址不会返回与“administrative_area_level_2”对应的数据,
  3. 这并不能解决美国城市跨多个县的问题。相反,我认为 API 只是返回与 address_string 关联的单个纬度/经度关联的县

Here is a bit of code to programmatically grab the name of a US county given a single US city/state using the Google Maps API. This code is slow/inefficient and does not have any error handling. However, it has worked reliably for me to match counties with a list of ~1,000 cities.

#Set up googlemaps API
import googlemaps
google_maps = googlemaps.Client(key='API_KEY_GOES_HERE')

#String of city/state
address_string = 'Atlanta, GA'

#Geocode
location = google_maps.geocode(address_string)

#Loop through the first dictionary within `location` and find the address component that contains the 'administrative_area_level_2' designator, which is the county level
target_string = 'administrative_area_level_2'
for item in location[0]['address_components']:     
    if target_string in item['types']: #Match target_string
        county_name = item['long_name'] #Or 'short_name'
        break #Break out once county is located
    else:
        #Some locations might not contain the expected information
        pass

This produces:

>>> county_name
Fulton County

Caveats:

  1. code will break if google_maps.geocode() is not passed a valid
    address
  2. certain addresses will not return data corresponding to 'administrative_area_level_2'
  3. this does not solve the problem of US cities that span multiple counties. Instead, I think the API simply returns the county associated with the single latitude/longitude associated with address_string
千秋岁 2024-11-25 15:00:07

由于县和城市的点位置呈奇数多边形,因此使用地理空间函数来完成此任务并不容易。

最好的选择是参考城市及其各自县的数据库,尽管我不知道在哪里可以找到该数据库。
也许德克萨斯州会出版一本?
CommonDataHub 不包含此信息。

It will not be easy to use geospace functions for this task because of the odd polygon shaped of counties and the point locations of cities.

Your best bet is to reference a database of cities and their respective counties, though I don't know where you could find one.
Maybe Texas publishes one?
CommonDataHub doesn't contain this information.

指尖上得阳光 2024-11-25 15:00:07

最快、最直接的方法可能是使用来自免费地理定位 API(很容易在 Google 上找到)的 JSON/XML 请求。这样您就不需要创建/托管自己的数据库。

The quickest and most non-evasive way might be to use a JSON/XML request from a free geolocation API (Easily found on Google). That way you don't need to create/host your own database.

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