reading address and lat,long from xml_node in R (mapsapi package)

Leon Source

I'm trying to get informations from an address over the package mapsapi in R. So my code looks like follows:

library(mapsapi)
library(XML)
library(RCurl)

string <- "Pariser Platz 1, 10117 Berlin"
test <- mp_geocode(string)
xml <- xml_child(test[[string]],2)
xml

Now I'm getting this kind of xml file:

{xml_node}
<result>
 [1] <type>street_address</type>
 [2] <formatted_address>Pariser Platz 1, 10117 Berlin, Germany</formatted_address>
 [3] <address_component>\n  <long_name>1</long_name>\n  <short_name>1</short_name>\n  <type>street_number</type>\n</address_component>
 [4] <address_component>\n  <long_name>Pariser Platz</long_name>\n  <short_name>Pariser Platz</short_name>\n  <type>route</type>\n</address_component>
 [5] <address_component>\n  <long_name>Mitte</long_name>\n  <short_name>Mitte</short_name>\n  <type>political</type>\n  <type>sublocality</type>\n  <type>sublocality_level_1</type>\n</address_component>
 [6] <address_component>\n  <long_name>Berlin</long_name>\n  <short_name>Berlin</short_name>\n  <type>locality</type>\n  <type>political</type>\n</address_component>
 [7] <address_component>\n  <long_name>Berlin</long_name>\n  <short_name>Berlin</short_name>\n  <type>administrative_area_level_1</type>\n  <type>political</type>\n</address_component>
 [8] <address_component>\n  <long_name>Germany</long_name>\n  <short_name>DE</short_name>\n  <type>country</type>\n  <type>political</type>\n</address_component>
 [9] <address_component>\n  <long_name>10117</long_name>\n  <short_name>10117</short_name>\n  <type>postal_code</type>\n</address_component>
[10] <geometry>\n  <location>\n    <lat>52.5160964</lat>\n    <lng>13.3779369</lng>\n  </location>\n  <location_type>ROOFTOP</location_type>\n  <viewport>\n    <southwest>\n      <lat>52.5147474</lat>\n      <lng>13.37658 ...
[11] <place_id>ChIJnYvtVcZRqEcRl6Kftq66b6Y</place_id>

So how can I export the street number, address, city, zip, lat and long out of this xml into decent variables? Thanks for your help! regards

rxmlgoogle-maps-api-3

Answers

answered 2 months ago SymbolixAU #1

I've made accessing this type of information easy in my googleway package

library(googleway)

## you're using Google's API, and they require you to have an API key
## so you'll need to get one 
set_key("GOOGLE_API_KEY")

## perform query
res <- google_geocode("Pariser Platz 1, 10117 Berlin")

With the res result you can use geocode_coordinates() to extract the coordinates, and geocode_address_components() to get the street number

## coordinates
geocode_coordinates(res)
#       lat      lng
# 1 52.5161 13.37794

geocode_address_components(res)
#       long_name    short_name                                       types
# 1             1             1                               street_number
# 2 Pariser Platz Pariser Platz                                       route
# 3         Mitte         Mitte political, sublocality, sublocality_level_1
# 4        Berlin        Berlin                         locality, political
# 5        Berlin        Berlin      administrative_area_level_1, political
# 6       Germany            DE                          country, political
# 7         10117         10117                                 postal_code

You can look at str(res) to see the full list of items returned from Google's API

answered 2 months ago Gautam #2

Alternatively, you can also use ggmap::geocode():

> library(ggmap)
> geocode(location = "Pariser Platz 1, 10117 Berlin", output = 'latlon' )
Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Pariser%20Platz%201,%2010117%20Berlin&sensor=false
       lon     lat
1 13.37794 52.5161

Changing the output parameter can give you a very detailed list output (if required):

> geocode(location = "Pariser Platz 1, 10117 Berlin", output = 'all' )
Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Pariser%20Platz%201,%2010117%20Berlin&sensor=false
$results
$results[[1]]
$results[[1]]$address_components
$results[[1]]$address_components[[1]]
$results[[1]]$address_components[[1]]$long_name
[1] "1"

$results[[1]]$address_components[[1]]$short_name
[1] "1"

$results[[1]]$address_components[[1]]$types
[1] "street_number"


$results[[1]]$address_components[[2]]
$results[[1]]$address_components[[2]]$long_name
[1] "Pariser Platz"

$results[[1]]$address_components[[2]]$short_name
[1] "Pariser Platz"

$results[[1]]$address_components[[2]]$types
[1] "route"


$results[[1]]$address_components[[3]]
$results[[1]]$address_components[[3]]$long_name
[1] "Mitte"

$results[[1]]$address_components[[3]]$short_name
[1] "Mitte"

$results[[1]]$address_components[[3]]$types
[1] "political"           "sublocality"         "sublocality_level_1"


$results[[1]]$address_components[[4]]
$results[[1]]$address_components[[4]]$long_name
[1] "Berlin"

$results[[1]]$address_components[[4]]$short_name
[1] "Berlin"

$results[[1]]$address_components[[4]]$types
[1] "locality"  "political"


$results[[1]]$address_components[[5]]
$results[[1]]$address_components[[5]]$long_name
[1] "Berlin"

$results[[1]]$address_components[[5]]$short_name
[1] "Berlin"

$results[[1]]$address_components[[5]]$types
[1] "administrative_area_level_1" "political"                  


$results[[1]]$address_components[[6]]
$results[[1]]$address_components[[6]]$long_name
[1] "Germany"

$results[[1]]$address_components[[6]]$short_name
[1] "DE"

$results[[1]]$address_components[[6]]$types
[1] "country"   "political"


$results[[1]]$address_components[[7]]
$results[[1]]$address_components[[7]]$long_name
[1] "10117"

$results[[1]]$address_components[[7]]$short_name
[1] "10117"

$results[[1]]$address_components[[7]]$types
[1] "postal_code"



$results[[1]]$formatted_address
[1] "Pariser Platz 1, 10117 Berlin, Germany"

$results[[1]]$geometry
$results[[1]]$geometry$location
$results[[1]]$geometry$location$lat
[1] 52.5161

$results[[1]]$geometry$location$lng
[1] 13.37794


$results[[1]]$geometry$location_type
[1] "ROOFTOP"

$results[[1]]$geometry$viewport
$results[[1]]$geometry$viewport$northeast
$results[[1]]$geometry$viewport$northeast$lat
[1] 52.51745

$results[[1]]$geometry$viewport$northeast$lng
[1] 13.37929


$results[[1]]$geometry$viewport$southwest
$results[[1]]$geometry$viewport$southwest$lat
[1] 52.51475

$results[[1]]$geometry$viewport$southwest$lng
[1] 13.37659




$results[[1]]$place_id
[1] "ChIJnYvtVcZRqEcRl6Kftq66b6Y"

$results[[1]]$types
[1] "street_address"



$status
[1] "OK"

You can find more info in the function help section.

Sometimes the call may fail with the following message:

Warning message:
geocode failed with status OVER_QUERY_LIMIT, location = "Pariser Platz 1, 10117 Berlin"

Generally if you try after a few seconds it works fine. You can always check the remaining queries left in your quota with geocodeQueryCheck:

> geocodeQueryCheck()
2490 geocoding queries remaining.

comments powered by Disqus