Notice how the bbox section of the geometry is incorrect (below): the bbox_left number (a longitude) is using the latitude number for my area, and the bbox_bottom number (a latitude) is using a longitude number for my area. Hence the polygon plotted stretches from west Africa to Russia!
Here's a second reprex showing the result of the equivalent "direct" query to the API for comparison:
I have not managed to get the result of this to be a plottable/mappable spatial object though (would appreciate some advice on this by the way!)
library(nominatim)
#> Data (c) OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright
#> Nominatim Usage Policy: http://wiki.openstreetmap.org/wiki/Nominatim_usage_policy
#> MapQuest Nominatim Terms of Use: http://info.mapquest.com/terms-of-use/
library(jsonlite)
library(purrr)
#>
#> Attaching package: 'purrr'
#> The following object is masked from 'package:jsonlite':
#>
#> flatten
# try with direct query
ashfield_fromjson <- jsonlite::fromJSON("https://open.mapquestapi.com/nominatim/v1/search.php?key=KtacQGOTApeFbfDhKaq5cGk8T16V4ioP&format=json&q=ashfield&limit=1&polygon_geojson=1")
class(ashfield_fromjson)
#> [1] "data.frame"
# ^^^ I don't know how to turn this data frame into a plottable/tmappable spatial object.
# I have tried various things to try to use the geometry data in this result
# as a spatial object but I have not yet found the right way to do this
ashfield_coords <- ashfield_fromjson %>%
pluck("geojson", "coordinates", 1) %>%
matrix(., ncol = 2, dimnames = list(NULL, c("lon", "lat")))
# compare to the result from osm_search_spatial above
head(ashfield_coords)
#> lon lat
#> [1,] -1.344593 53.06326
#> [2,] -1.344409 53.06299
#> [3,] -1.344221 53.06276
#> [4,] -1.344161 53.06242
#> [5,] -1.344115 53.06231
#> [6,] -1.344068 53.06225
I asked a question about this on Stack Overflow but haven't got to the bottom of it.
I want to use nominatim to return a valid spatial object that I can plot as a polygon.
Here's a reprex of my problem with
osm_search_spatial
:Created on 2020-02-26 by the reprex package (v0.3.0)
Notice how the bbox section of the geometry is incorrect (below): the bbox_left number (a longitude) is using the latitude number for my area, and the bbox_bottom number (a latitude) is using a longitude number for my area. Hence the polygon plotted stretches from west Africa to Russia!
Here's a second reprex showing the result of the equivalent "direct" query to the API for comparison: I have not managed to get the result of this to be a plottable/mappable spatial object though (would appreciate some advice on this by the way!)
Created on 2020-02-26 by the reprex package (v0.3.0)