I want to create a network connection world map in R. I have the longitude and latitude for one node but I only have the city and state/country for the other node. Can ggplot or RGoogleMaps still work with this? What should I do? I am working in Windows.
Asked
Active
Viewed 811 times
2
-
Anyone? Is this really complicated? – Concerned_Citizen May 05 '12 at 22:55
-
2You'll might want to also search using the keyword 'geocoding'. Typing it in the SO search bar (like this: `[r] [geocoding]`) turns up many results, including this one, which should give you a good start: http://stackoverflow.com/questions/9068941/obtain-latitude-and-longitude-from-address-without-the-use-of-google-api – Josh O'Brien May 05 '12 at 23:26
1 Answers
-2
If you have the city and state/country for the other node, you can get the longitude and latitude using the Googlemaps API in Python. Suppose you have your list of city names in a CSV file, then you can just loop through them one at a time, retrieving the longitude and latitude and writing it to a new file. The results can then be loaded into R as a data frame.
Here is the sample Python code:
from googlemaps import GoogleMaps
import csv
ifile=csv.reader(open('CapitalCities.csv','rb'),delimiter=',')
ofile=open('CapCoordinates.csv','wb')
w=csv.writer(ofile,delimiter=',')
w.writerow(['Name','State','Latitude','Longitude'])
gmaps=GoogleMaps(API_KEY)
count=0
for row in ifile:
if count!=0:
address=str(row[0])+" "+str(row[1])
lat, lng = gmaps.address_to_latlng(address)
w.writerow([str(row[0]),str(row[1]),str(lat),str(lng)])
print row[0],lat,lng
count+=1
ofile.close()
There are also methods for doing the same thing in R. A quick read of this post might prove useful: http://forgetfulfunctor.blogspot.com.au/2012/02/undiscovered-country-tutorial-on.html