Skip to content

Instantly share code, notes, and snippets.

@actuaryactually
Last active February 22, 2017 15:22
Show Gist options
  • Save actuaryactually/a7e6469ab0f88110f87c64eca305f06f to your computer and use it in GitHub Desktop.
Save actuaryactually/a7e6469ab0f88110f87c64eca305f06f to your computer and use it in GitHub Desktop.
Quake Visualisations (QV)
#PRELIMINARIES:
pacman::p_load(XML)
#recode as data frame :
output.addresses.df<-data.frame(c(output.addresses))
output.addresses.df<-cbind(output.addresses.df,long=1,lat=2)
#assign long/lat
for (i in 1:length(output.addresses)){
addr <-output.addresses[i]
url = paste('http://maps.google.com/maps/api/geocode/xml?address=', addr,'&sensor=false',sep='') # construct the URL
doc = xmlTreeParse(url)
root = xmlRoot(doc)
output.addresses.df[i,2] = xmlValue(root[['result']][['geometry']][['location']][['lng']])
output.addresses.df[i,3] = xmlValue(root[['result']][['geometry']][['location']][['lat']])
}
print(output.addresses.df)
#row 35 and 38 have problems - 35 is returning something with long/lats that are way off; and 38 is throwing an error.
#Manually searching google maps shows that the value for this row, needs a slight change. Overwriting using the following
#approach solves the problem....
addr <-" Cnr West Street & Moore Street, Ashburton"
url = paste('http://maps.google.com/maps/api/geocode/xml?address=', addr,'&sensor=false',sep='') # construct the URL
doc = xmlTreeParse(url)
root = xmlRoot(doc)
output.addresses.df[35,2] = xmlValue(root[['result']][['geometry']][['location']][['lng']])
output.addresses.df[35,3] = xmlValue(root[['result']][['geometry']][['location']][['lat']])
addr <-"Cnr Maclaggan Street & Rattray Street, Dunedin"
url = paste('http://maps.google.com/maps/api/geocode/xml?address=', addr,'&sensor=false',sep='') # construct the URL
doc = xmlTreeParse(url)
root = xmlRoot(doc)
output.addresses.df[38,2] = xmlValue(root[['result']][['geometry']][['location']][['lng']])
output.addresses.df[38,3] = xmlValue(root[['result']][['geometry']][['location']][['lat']])
#all locations assigned now:
print(output.addresses.df)
#format long lat as numbers (needed later)
colnames(output.addresses.df)<-c("Address","long","lat")
output.addresses.df$Address<-as.character(output.addresses.df$Address)
output.addresses.df$long<-as.numeric(output.addresses.df$long)
output.addresses.df$lat<-as.numeric(output.addresses.df$lat)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment