使用R和Googlemaps API进行地理编码

时间:2014-05-14 06:55:13

标签: r google-maps-api-3 geocode rjson

我经常使用R进行统计分析,但我正在慢慢尝试使用R转移到GIS和空间可视化。

我正在尝试复制

中提供的地理编码代码

https://github.com/smholloway/data-mashups-in-r/blob/master/mapping-foreclosures.r#L14

然而,在运行地理编码功能时,我收到以下错误消息

geocoding: http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=5833%20Ashland%20Ave.+Philadelphia+PA 
parsing or http error: $ operator is invalid for atomic vectors 

geocoding: http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=219-221%20E.%20Tioga%20St.+Philadelphia+PA 
parsing or http error: arguments imply differing number of rows: 1, 0 

关于我应该做些什么来使这段代码工作的任何建议?

这里是我正在运行的代码的剪辑

library("XML")
library("rjson")
library("RCurl")
library("PBSmapping")

if (!file.exists("properties.html")) {
  download.file(url="http://web.archive.org/web/20080610132249/www.phillysheriff.com/properties.html", destfile="properties.html")
}


########################
# getAddressesFromHTML
# input:html filename
# returns:dataframe of geocoded addresses that can be plotted by PBSmapping
########################
getAddressesFromHTML<-function(myHTMLDoc){
  myStreets<-vector(mode="character",0)
  stNum<-"^[0-9]{2,5}(\\-[0-9]+)?"
  stName<-"([NSEW]\\. )?([0-9A-Z ]+)"
  stSuf<-"(St|Ave|Place|Blvd|Drive|Lane|Ln|Rd)(\\.?)$"
  badStrings<-
    "(\\r| a\\/?[kd]\\/?a.+$| - Premise.+$| assessed as.+$|, Unit.+
     |<font size=\"[0-9]\">|Apt\\..+| #.+$|[,\"]|\\s+$)"
  myStPat<-paste(stNum,stName,stSuf,sep=" ")
  for(line in readLines(myHTMLDoc)){
    line<-gsub(badStrings,'',line,perl=TRUE)
    matches<-grep(myStPat,line,perl=TRUE,
                  value=FALSE,ignore.case=TRUE)
    if(length(matches)>0){
       myStreets<-append(myStreets,line)
    }
  }
  myStreets 
}

####################
# geocodeAddresses
# input:vector of streets
# output:data frame containing lat/longs in PBSmapping-acceptable format
####################
geocodeAddresses<-function(myStreets){
  myGeoTable<-data.frame(address=character(),lat=numeric(),long=numeric(),EID=numeric())
  for(myStreet in myStreets){
     requestUrl<-paste(
          "http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=", 
          URLencode(myStreet),
          "+Philadelphia+PA",
          sep="")
    cat("geocoding:", requestUrl, "\n")
    tryCatch({
        json_data <- fromJSON(paste(readLines(requestUrl), collapse=""))
        lat <- unlist(lapply(json_data$results, function(x) {x$geometry[1]$location$lat}))
        lng <- unlist(lapply(json_data$results, function(x) {x$geometry[1]$location$lng}))

        myGeoTable<-rbind(myGeoTable,data.frame(address = myStreet, Y = lat, X = lng, EID=NA))
    }, error=function(err) {
        cat("parsing or http error:", conditionMessage(err), "\n")
    })
    Sys.sleep(0.1)
  }

  #let's use the built-in numbering as the event id that PBSmapping wants
  myGeoTable$EID<-as.numeric(rownames(myGeoTable))
  myGeoTable
}

streets<-getAddressesFromHTML("properties.html")



geoTable<-geocodeAddresses(streets)

0 个答案:

没有答案