Question

I am trying to retrieve the longitude and latitude of a city. I have created a script that uses JSON via the openstreetmap.org.

library("RJSONIO")
CityName <- "Rotterdam"
CountryCode <- "NL"
CityName <- gsub(' ','%20',CityName)

url <- paste(
  "http://nominatim.openstreetmap.org/search?city="
      , CityName
      , "&countrycodes="
      , CountryCode
      , "&limit=1&format=json"
    , sep="")
x <- fromJSON(url,simplify=FALSE)

x
[[1]]
[[1]]$place_id
[1] "98036666"

[[1]]$licence
[1] "Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright"

[[1]]$osm_type
[1] "relation"

[[1]]$osm_id
[1] "324431"

[[1]]$boundingbox
[[1]]$boundingbox[[1]]
[1] "51.842113494873"

[[1]]$boundingbox[[2]]
[1] "52.0045318603516"

[[1]]$boundingbox[[3]]
[1] "3.94075202941895"

[[1]]$boundingbox[[4]]
[1] "4.60184574127197"


[[1]]$lat
[1] "51.9228958"

[[1]]$lon
[1] "4.4631727"

[[1]]$display_name
[1] "Rotterdam, Stadsregio Rotterdam, Zuid-Holland, Nederland"

[[1]]$class
[1] "boundary"

[[1]]$type
[1] "administrative"

[[1]]$icon
[1] "http://nominatim.openstreetmap.org/images/mapicons/poi_boundary_administrative.p.20.png"

I would like to extract the $lon and $lat, but when I do this I get the following error messages:

x$lon
NULL
x$lat
NULL

Does anybody have an idea what I do wrong and therefore do not get the expected result, which would look like:

x$lon
4.4631727
x$lat
51.9228958

Any suggestions? Thanks!

Was it helpful?

Solution

You need to access to the first list before:

> x[[1]]$lat
[1] "51.9228958"
> x[[1]]$lon
[1] "4.4631727"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top