I've tried running the code to geocode locations in R via Google Maps and the XML
package from this blog post: http://www.r-chart.com/2010/07/maps-geocoding-and-r-user-conference.html
Here are his functions:
getDocNodeVal=function(doc, path){ sapply(getNodeSet(doc, path), function(el) xmlValue(el)) } gGeoCode=function(str){ library(XML) u=paste('http://maps.google.com/maps/api/geocode/xml?sensor=false&address=',str) doc = xmlTreeParse(u, useInternal=TRUE) str=gsub(' ','%20',str) lng=getDocNodeVal(doc, "/GeocodeResponse/result/geometry/location/lat") lat=getDocNodeVal(doc, "/GeocodeResponse/result/geometry/location/lng") c(lat,lng) }
When I run gGeoCode()
, I get the following error:
> gGeoCode("Philadelphia, PA") failed to load external entity "http%3A//maps.google.com/maps/api/geocode/xml%3Fsensor=false&address=%20Philadelphia,%20PA" Error: 1: failed to load external entity "http%3A//maps.google.com/maps/api/geocode/xml%3Fsensor=false&address=%20Philadelphia,%20PA"
If I just paste into a browser the API url with Philadelphia, PA
appended to the end, like the string passed to xmlParseTree
, I get a result that looks like legitimate xml when I download it.
Is this an issue with the code, or have I failed to configure something or another?
Try Google Maps Platform Forward Geocoding is the process of converting addresses (like a street address) into geographic coordinates (latitude and longitude), which you can use to place markers on a map or position the map.
Geocoding can be simply achieved in R using the geocode() function from the ggmap library. The geocode function uses Googles Geocoding API to turn addresses from text to latitude and longitude pairs very simply. There is a usage limit on the geocoding service for free users of 2,500 addresses per IP address per day.
Geocoding outside of R and Google Alternatively, you can geocode CSV files with address data outside of R, then import the CSV with the coordinates to map the data using the read_csv() function of Package 'readr'.
Have you thought about using the json call instead? Looking at your code, you could achieve the same doing this (you'll need to install packages RCurl and RJSONIO from omegahat.com).
Copy and paste this into R:
library(RCurl) library(RJSONIO) construct.geocode.url <- function(address, return.call = "json", sensor = "false") { root <- "http://maps.google.com/maps/api/geocode/" u <- paste(root, return.call, "?address=", address, "&sensor=", sensor, sep = "") return(URLencode(u)) } gGeoCode <- function(address,verbose=FALSE) { if(verbose) cat(address,"\n") u <- construct.geocode.url(address) doc <- getURL(u) x <- fromJSON(doc,simplify = FALSE) if(x$status=="OK") { lat <- x$results[[1]]$geometry$location$lat lng <- x$results[[1]]$geometry$location$lng return(c(lat, lng)) } else { return(c(NA,NA)) } }
Here's how you use the above functions:
x <- gGeoCode("Philadelphia, PA")
This is the result you get. I think in the original code, lat and lng are mixed up? But hopefully this is what you want:
> x [1] 39.95233 -75.16379
Hope that helps a little mate,
Tony Breyal
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With