Database Reference
In-Depth Information
Developing the Plot
With foreclosures represented as geographic coordinates, the addPoints function in the
PBSmapping package can plot each foreclosure as a point on the shapefile, provided the func-
tion is given a properly formatted data frame object.
Preparing to Add Points to Our Map
To use the PBSmapping's addPoints function, the reference manual suggests that we treat
our foreclosures as EventData . The EventData format is a standard R data frame (more on
data frames later) with required columns X, Y, and a unique row identifier, an event ID (EID).
With this in mind, we can write a function around our geocoding code that will accept a list of
streets and return a kosher EventData -like data frame:
#input:vector of streets
#output:data frame containing lat/longs in PBSmapping-like format
> geocodeAddresses<-function(myStreets){
appid<-'<put your appid here>'
myGeoTable<-data.frame(
address=character(),lat=numeric(),long=numeric(),EID=numeric())
for(myStreet in myStreets){
requestUrl<-paste(
"http://local.yahooapis.com/MapsService/V1/geocode?appid=",
appid,
"&street=",URLencode(myStreet),
"&city=Philadelphia&state=PA",sep="")
cat("geocoding:",myStreet,"\n")
tryCatch({
xmlResult<-
xmlTreeParse(requestUrl,isURL=TRUE,addAttributeNamespaces=TRUE)
geoResult<-xmlResult$doc$children$ResultSet$children$Result
if(geoResult$attributes['precision'] == 'address'){
lat<-xmlValue(geoResult[['Latitude']])
long<-xmlValue(geoResult[['Longitude']])
myGeoTable<-rbind(myGeoTable,
data.frame(address = myStreet, Y = lat, X =
long,EID=NA))
}
}, error=function(err){
cat("xml parsing/http error:", conditionMessage(err), "\n")
})
Sys.sleep(0.5) #this pause helps keep Yahoo happy
}
Search WWH ::




Custom Search