Database Reference
In-Depth Information
"&city=Philadelphia&state=PA"
,sep="")
We need to get a hold of the attribute tags within
<Result>
to distinguish bad geocoding
events, or else we could accidentally record events in the center of the city as foreclosures. By
parameter to our
xmlTreeParse
call if we are to see the
precision
tag:
>
xmlResult<-xmlTreeParse(requestUrl,isURL=TRUE,addAttributeNamespaces=TRUE)
Now we can dig down to get that precision tag, which is an element of
$attributes
, a named
list:
>
xmlResult$doc$children$ResultSet$children$Result$attributes['precision']
precision
"zip"
We can add this condition to our geocoding function:
> if(xmlResult$doc$children$ResultSet$children
$Result$attributes['precision'] == 'address'){
cat("I have address precision!\n")
}else{
cat("I don't know where this is!\n")
}
No Connection
Finally we need to account for unforeseen exceptions—such as losing our Internet connection
or the Yahoo web service failing to respond. It is not uncommon for this free service to drop
out when bombarded by requests. A
tryCatch
clause will alert us if this does happen and pre-
vent bad data from getting into our results.
> tryCatch({
xmlResult<-xmlTreeParse(requestUrl,isURL=TRUE,addAttributeNamespaces=TRUE)
#...other code...
}, error=function(err){
cat("xml parsing or http error:", conditionMessage(err), "\n")
})
We will compile all this code into a single function once we know how to merge it with a map
(see
Developing the Plot
).