Can I get a city and state out of a list of latitude/longitude pairs?
September 29, 2014 10:56 AM   Subscribe

I've found a lot of folks that promise the opposite. What I want to do is take a csv of lat/long pairs and get back something pairing them with the corresponding city and state. (I'd even take zip codes if it came to that bit that would be somewhat less useful). Is this something easily done?
posted by Karmakaze to Computers & Internet (9 answers total) 4 users marked this as a favorite
OpenStreetMap has a reverse geocoding API. Seems simple enough.
posted by Poldo at 11:07 AM on September 29, 2014 [7 favorites]

That API Poldo mentioned looks pretty sweet. If for some reason you don't want to use their API, I think an R programmer could do something like this pretty quickly with open-source resources.
posted by paper chromatographologist at 11:14 AM on September 29, 2014

Response by poster: I think where I'm failing is figuring out how to automate the process for a long list, that is, how to feed a list into one of these and get a list out instead of doing it one by one. I know it should be simple, but...
posted by Karmakaze at 11:28 AM on September 29, 2014

This Batch Reverse Geocoding site might do the trick.
posted by JDHarper at 11:44 AM on September 29, 2014 [5 favorites]

Zip code lists have the lat/long of some point in the middle of the zip code area, as well as the municipality name. It would be simple to write as program to find the name of the town associated with the nearest zip code, but it would not be 100% correct for what you want. I'm talking here of a list with a list of about 50,000 5-digit zip codes. There are also much bigger lists. I have not worked with them, but I suppose the are for zip+4. If that has lat/long, it would be pretty accurate.
posted by SemiSalt at 2:01 PM on September 29, 2014

ISTR finding an option somewhere on Google Earth where you can upload a spreadsheet of Lat/Long and get it automatically plotted. Seems to me, you could then take that data and have it spit out your city/state information, assuming you can export the data from Google Earth.
posted by suelac at 4:49 PM on September 29, 2014

1. Download Urban Areas shapefile from here.
2. Unzip that file
3. Download rStudio and install it from here.
4. Open rStudio and run this code.
posted by one_bean at 10:26 PM on September 29, 2014

Response by poster: I'm getting this:

> require(rgdal)
Loading required package: rgdal
Loading required package: sp
rgdal: version: 0.9-1, (SVN revision 518)
Geospatial Data Abstraction Library extensions to R successfully loaded
Loaded GDAL runtime: GDAL 1.11.0, released 2014/04/16
Path to GDAL shared files: C:/Users/rudysx/Google Drive/Documents/R/win-library/3.1/rgdal/gdal
GDAL does not use iconv for recoding strings.
Loaded PROJ.4 runtime: Rel. 4.8.0, 6 March 2012, [PJ_VERSION: 480]
Path to PROJ.4 shared files: C:/Users/rudysx/Google Drive/Documents/R/win-library/3.1/rgdal/proj
Warning messages:
1: package ‘rgdal’ was built under R version 3.1.1
2: package ‘sp’ was built under R version 3.1.1
> cities <> Error: '\G' is an unrecognized escape in character string starting ""F:\G"
> layer="tl_2013_us_uac10")
Error: unexpected ')' in " layer="tl_2013_us_uac10")"
> my.pts <> Error: '\G' is an unrecognized escape in character string starting ""F:\G"
> # Make sure your csv is only two columns, longitude and then latitude
> # These aren't projected coordinates, right?
> my.sp.pts <> Error in coordinates(coords) :
error in evaluating the argument 'obj' in selecting a method for function 'coordinates': Error: object 'my.pts' not found
> my.sp.pts@proj4string <> Error: object 'cities' not found
> #This next line will take a while to run
> my.pts$city <> Error in over(my.sp.pts, cities) :
error in evaluating the argument 'x' in selecting a method for function 'over': Error: object 'my.sp.pts' not found
> write.csv(my.pts, "my_new_city_file.csv")
Error in : object 'my.pts' not found

posted by Karmakaze at 7:44 AM on October 1, 2014

Two things. R doesn't like backslashes so you need two in a row, or use forward slashes instead. it looks like your code is messed up, too, but I'm guessing the formatting just got screwy when you pasted over here. But double check that what you're running in R matches what's in my code link.
posted by one_bean at 3:02 PM on October 5, 2014

« Older Reasonable Repair Estimate for MacBook Air Display...   |   How can I sell my 50% stake in a 10 acre property... Newer »
This thread is closed to new comments.