Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to lookup the US county a US city resides in?

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?

like image 471
Cucumber2 Avatar asked Jul 05 '11 21:07

Cucumber2


3 Answers

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.

like image 134
gss Avatar answered Nov 05 '22 08:11

gss


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

like image 12
Paul Sasik Avatar answered Nov 05 '22 06:11

Paul Sasik


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.

like image 2
Matthew Avatar answered Nov 05 '22 07:11

Matthew