I have a data set that includes a list of zip codes. Is there a way in R to convert these zip codes to latitude and longitude coordinates?
4 Answers
There's no strict algorithmic relationship between latitude and longitude and zip code - they're all custom areas generated by the postal service. You need access to a dataset that codes polygons / polygon centroids by zip code.
1) Complex
Traditionally, this task (coupled with address lookup) is termed 'Geocoding'. The most convenient method for full address lookup is to find a preexisting online geocoder, whether by Google Maps, Bing Maps, or ESRI.
2) Simpler
For just US zip codes, though, there is data available from the Census:
http://www.census.gov/geo/www/cob/z52000.html
I'm not familiar with R geospatial capability, but all you should need is polygon->centroid, and centroid->coordinates.
3) Simplest
This task is common enough that a preexisting centroid dataset exists: http://coolthingoftheday.blogspot.com/2008/04/free-us-zip-code-database-as-csv.html
-
1Census Bureau link is broken. It probably referred to ZCTAs which are a census bureau invention. Originally based on zipcodes (which are NOT polygons and regularly change) these are polygons that are intended to be fairly constant. Ie. making them more usable for demographic purposes.– winwaedJun 23, 2016 at 13:31
have you looked at the zipcode
package? it's basically a dataframe with ~45,000 zipcodes along with their city, state, latitude and longitude.
I use the geocode
function in the ggmap
package.
-
will this allow zip+4 or does it only use zip centroids?– B_MinerJun 22, 2012 at 17:49
-
It's using Google Maps, so works however Google would. (Only get a limited number of geocodes per day.)– WayneJun 22, 2012 at 17:50
-
...and apparently you aren't allowed to store the data (you'd basically have to query the API about every time you need it)– MattJun 22, 2012 at 19:09
-
Do they mean you can't store them for future geocoding (i.e. creating your own cache of mappings), or that you can't store the results at all? I've used it to locate crimes in my neighborhood and certainly store the Lat/Lon of each crime for future reference. I haven't created a cache that I use to avoid Google for future geocoding, though.– WayneJun 22, 2012 at 19:40
-
1My reading of the TOS (thanks for the link!) is that they have to do with the presenting of maps to other people. That is, you can't make your own map service that uses Google as a back end and doesn't credit them. It doesn't appear to have anything directly to do with geocoding data. Though who knows in this litigious era...– WayneJun 22, 2012 at 21:08
Seems like you are using Ruby on Rails
Try this tutorial from Ryan Bates
http://railscasts.com/episodes/273-geocoder?view=asciicast
I've tried it before and it works perfectly. When you type address or zipcode , it will show the longitude and latitude.