Skip to content

Instantly share code, notes, and snippets.

@francisbarton
Created February 20, 2020 16:47
Show Gist options
  • Save francisbarton/3028d3f73e67091c40647345f0077552 to your computer and use it in GitHub Desktop.
Save francisbarton/3028d3f73e67091c40647345f0077552 to your computer and use it in GitHub Desktop.
nominatim problem
<!-- language-all: lang-r -->
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(sf)
#> Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
library(raster)
#> Loading required package: sp
library(tmap)
# get OSM search results for Ashfield district (UK)
ashfield <- nominatim::osm_search_spatial("Ashfield", limit = 1, key = "KtacQGOTApeFbfDhKaq5cGk8T16V4ioP")
class(ashfield)
#> [1] "list"
# extract SPDF from list
ashfield <- ashfield[[1]]
class(ashfield)
#> [1] "SpatialPolygonsDataFrame"
#> attr(,"package")
#> [1] "sp"
raster::projection(ashfield)
#> [1] NA
# set projection
tmap::qtm(ashfield, projection = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
#> Warning: The shape ashfield is invalid. See sf::st_is_valid
#> Warning: Currect projection of shape ashfield unknown. Long-lat (WGS84) is
#> assumed.
![](https://i.imgur.com/AjzVwrP.png)
sf::st_is_valid(ashfield, reason = TRUE)
#> Error in UseMethod("st_geometry"): no applicable method for 'st_geometry' applied to an object of class "c('SpatialPolygonsDataFrame', 'SpatialPolygons', 'Spatial', 'SpatialVector', 'SpatialPolygonsNULL')"
raster::projection(ashfield) <- "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
tmap::qtm(ashfield)
#> Warning: The shape ashfield is invalid. See sf::st_is_valid
![](https://i.imgur.com/Rh0El4Q.png)
# Convert to an SF object and try again
ashfield_sf <- sf::st_as_sf(ashfield)
class(ashfield_sf)
#> [1] "sf" "data.frame"
tmap::qtm(ashfield_sf, projection = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
#> Warning: The shape ashfield_sf is invalid. See sf::st_is_valid
![](https://i.imgur.com/zzpeIbu.png)
sf::st_is_valid(ashfield_sf, reason = TRUE)
#> [1] "Self-intersection[53.0709899483331 53.0709561246412]"
sf::st_transform(ashfield_sf, crs = 4326)
#> Simple feature collection with 1 feature and 15 fields
#> geometry type: POLYGON
#> dimension: XY
#> bbox: xmin: -1.344593 ymin: -1.344593 xmax: 53.17143 ymax: 53.17142
#> epsg (SRID): 4326
#> proj4string: +proj=longlat +datum=WGS84 +no_defs
#> place_id
#> 186877616 186877616
#> licence
#> 186877616 Data © OpenStreetMap contributors, ODbL 1.0. https://www.openstreetmap.org/copyright
#> osm_type osm_id lat lon
#> 186877616 relation 154043 53.08977 -1.251877
#> display_name
#> 186877616 Ashfield, Nottinghamshire, East Midlands, England, United Kingdom
#> class type importance
#> 186877616 boundary administrative 0.2116014
#> icon
#> 186877616 http://ip-10-98-162-13.mq-us-east-1.ec2.aolcloud.net/nominatim/images/mapicons/poi_boundary_administrative.p.20.png
#> bbox_left bbox_top bbox_right bbox_bottom
#> 186877616 53.0080617 53.1714343 -1.3445928 -1.1642542
#> geometry
#> 186877616 POLYGON ((-1.344593 -1.3444...
tmap::qtm(ashfield_sf)
#> Warning: The shape ashfield_sf is invalid. See sf::st_is_valid
![](https://i.imgur.com/WtHEwYl.png)
<sup>Created on 2020-02-20 by the [reprex package](https://reprex.tidyverse.org) (v0.3.0)</sup>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment