Geocoding with Google Maps and Igor
goffog
Below is a simple function that requires the easyHttp XOP to geocode a list of addresses.
Using Igor and exporting the geocoded data to XML, I created a Google maps web page for my postal history collection (link here).
// Function Uses Google maps to return latitude and longitude of an address
// Requires: easyHttp XOP
// Geoff Dutton March 5, 2009
Function GeocodeWithGoogle( )
Make /o/n=6 lat = 0, long = 0
Make /o/n=6 /T city, state
city = {"Denver", "Los Angels", "Boise", "Pensacola", "Dallas", "1600 Pennsylvania Ave NW, Washington"}
state = {"Colorado", "California", "Idaho", "Florida", "Texas", "DC"}
String Win = "Geocode"
Dowindow /K $Win
Edit city, state, lat, long
Dowindow /C $Win
DoUpdate
String url, GmapOutput
Variable i
for (i=0; i<numpnts(lat); i+=1 )
// only Geocode if lat = 0
if ( lat[i] == 0 )
// build a URL for Google maps
Sprintf url, "http://maps.google.com/maps/geo?q=%s,%s&output=csv", city[i], state[i]
// replace spaces in URL with unicode
url = ReplaceString(" ", url, "%20")
// use easyHttp to contact Google maps, Google maps returns lat, long encoded text
easyHttp url
GmapOutput = S_getHttp
// create a list from returned Google text
GmapOutput = ReplaceString(",", GmapOutput, ";")
// Google returns 200 if address was found
if ( str2num(StringFromList( 0, GmapOutput)) == 200 )
lat[i] = str2num(StringFromList(2, GmapOutput))
long[i] = str2num(StringFromList(3, GmapOutput))
Printf "%s, %s is located at %.5f, %.5f\r", city[i], state[i], lat[i], long[i]
else
Print "ERR: " + city[1] + ", " + state[2]
endif
// slow down a little (for googles' servers)
DoUpdate
Sleep /T 1/10
endif
endfor
end
// Requires: easyHttp XOP
// Geoff Dutton March 5, 2009
Function GeocodeWithGoogle( )
Make /o/n=6 lat = 0, long = 0
Make /o/n=6 /T city, state
city = {"Denver", "Los Angels", "Boise", "Pensacola", "Dallas", "1600 Pennsylvania Ave NW, Washington"}
state = {"Colorado", "California", "Idaho", "Florida", "Texas", "DC"}
String Win = "Geocode"
Dowindow /K $Win
Edit city, state, lat, long
Dowindow /C $Win
DoUpdate
String url, GmapOutput
Variable i
for (i=0; i<numpnts(lat); i+=1 )
// only Geocode if lat = 0
if ( lat[i] == 0 )
// build a URL for Google maps
Sprintf url, "http://maps.google.com/maps/geo?q=%s,%s&output=csv", city[i], state[i]
// replace spaces in URL with unicode
url = ReplaceString(" ", url, "%20")
// use easyHttp to contact Google maps, Google maps returns lat, long encoded text
easyHttp url
GmapOutput = S_getHttp
// create a list from returned Google text
GmapOutput = ReplaceString(",", GmapOutput, ";")
// Google returns 200 if address was found
if ( str2num(StringFromList( 0, GmapOutput)) == 200 )
lat[i] = str2num(StringFromList(2, GmapOutput))
long[i] = str2num(StringFromList(3, GmapOutput))
Printf "%s, %s is located at %.5f, %.5f\r", city[i], state[i], lat[i], long[i]
else
Print "ERR: " + city[1] + ", " + state[2]
endif
// slow down a little (for googles' servers)
DoUpdate
Sleep /T 1/10
endif
endfor
end
Forum
Support
Gallery
Igor Pro 9
Learn More
Igor XOP Toolkit
Learn More
Igor NIDAQ Tools MX
Learn More
A couple of other things could be done here, but they are all gilding the lily. There is also an XOP on this site called base64, amongst other things this does URLencoding.
You could do:
url = URLencode(url)
Also, easyHttp can put the output directly into strings, instead of creating S_getHttp. For example:
if(V_flag)
print "Attempted download failed"
endif
March 5, 2009 at 01:08 pm - Permalink
I was thinking a little bit more about this. First of all there are a couple of utilities on this site to deal with XML, one of them is the XMLutils XOP. Secondly I was musing on how to use an IGOR function to get a map of your locality. One way would be to use the snippet you wrote to get a lat and long from an entered address. Then one could use that lat and long to get an exported PNG map from www.openstreetmap.com (I haven't worked out how to do the same with google maps). This could then be displayed in IGOR.
March 6, 2009 at 09:07 pm - Permalink