What is the best way to find US county in an American city?

I’m looking for the best / easiest way to programmatically capture the name of the US county in which this US city resides. It seems there is no simple API available for such a (seemingly simple) task?

+9
source share
6 answers

You can download a freely accessible database of the county / city / zip code, for example, this: http://www.unitedstateszipcodes.org/zip-code-database/ (registration and payment are not required)

Import it in its entirety or its subsection into a local permanent data warehouse (for example, into a database) and request it whenever you need to find a city district

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

+8
source

1) Cities cover counties

2) Zip codes cover both cities and counties, not even on the same line

Any solution that uses zip as an intermediary can corrupt your data (and no, "zip + 4" will usually not fix this). You will find that the city-index-county data map (# 2) has more city-county matches than the more accurate model (# 1) --these, and all of them are erroneous.

What you are looking for is free census data. The required data set of Federal Information Processing Standards (FIPS) is called "ANSI 2010 codes for locations": https://www.census.gov/geographies/reference-files/time-series/geo/name-lookup-tables.2010.html

The "places" of the census are the "cities" for our question. These files display "locations" in one or more counties.

+3
source

It is not easy to use geospatial functions for this task due to the odd polygonal shape of counties and city locations.

It is best to refer to a database of cities and their respective districts, although I do not know where it can be found. Maybe Texas publishes one? CommonDataHub does not contain this information.

+2
source

See this link for all cities in TX from the 2000 census

http://www.tsl.state.tx.us/ref/abouttx/popcity32000.html

+1
source

Here is some code to programmatically grab the name of a US county provided to one US city / state using the Google Maps API. This code is slow / inefficient and has no error handling. However, it worked reliably for me to fit counties with a list of ~ 1000 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 gives:

 >>> county_name Fulton County 

Cautions:

  • code will be broken if google_maps.geocode () is not passed a valid address
  • some addresses will not return data matching 'Administrative_area_level_2'
  • this does not solve the problem of US cities that span multiple counties. Instead, I think the API just returns the county associated with a single latitude / longitude associated with address_string
+1
source

The fastest and most inconvenient way is to use a JSON / XML request from the free geolocation API (easy to find on Google). This way you do not need to create / host your own database.

0
source

Source: https://habr.com/ru/post/892073/


All Articles