2

Getting error with read_osm from the tmaptools package in R. Want to generate a static map with background layer.

Error is present even when using the example NLD_muni data from tmap package

library(tmap)
library(tmaptools)
library(OpenStreetMap)
tmap_mode("plot")
data(NLD_muni)
test <- tmaptools::read_osm(NLD_muni, type = "esri", zoom = NULL)

Error

Error in FUN(X[[i]], ...) : Sorry, parameter type `NA' is ambiguous or not supported.

Expected to load a basemap

Could use tmap_mode("view") and created interactive plot but ideally can make a static plot.

Andy
  • 21
  • 3
  • 1
    Can you show what is `NLD_mundi`? – Humpelstielzchen Aug 08 '19 at 08:34
  • NLD_muni is the example sf data provided in tmap. I've updated the code section to show how to load it. NLD_muni: NLD_muni Simple feature collection with 403 features and 14 fields geometry type: MULTIPOLYGON dimension: XY bbox: xmin: 13565.4 ymin: 306846.9 xmax: 277992.8 ymax: 619292 epsg (SRID): 28992 proj4string: +proj=sterea +lat_0=52.15616055555555 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000 +ellps=bessel +towgs84=565.2369,50.0087,465.658,-0.406857,0.350733,-1.87035,4.0812 +units=m +no_defs – Andy Aug 08 '19 at 09:15

3 Answers3

2

I encountered the same problem as you, the examples from the help files don't work anymore for read_osm depending on the help file (this since I upgraded to Windows 10 and reinstalled R - I suspect it is related).

I have found another example on the web which is working for me, and from which you can hopefully start again.

# load Netherlands shape
data(NLD_muni)

# read OSM raster data
osm_NLD <- read_osm(bb(NLD_muni, ext=1.1, projection ="longlat"))

# plot with regular tmap functions
tm_shape(osm_NLD) +
    tm_raster() +
    tm_shape(NLD_muni) +
    tm_polygons("population", convert2density=TRUE, style="kmeans", alpha=.7, palette="Purples")
Delphine
  • 31
  • 5
  • Including the basemap in a static map is a powerful tool. This code was working in an earlier version of tmaptools and is not working in the current version 2.3-1 -- I've opened a GitHub issue https://github.com/mtennekes/tmaptools/issues/19 – ZRoss Jan 17 '20 at 19:08
  • I agree, thanks for opening an issue! I had a whole script working perfectly before the update which I had to modify, so there has clearly been a change – Delphine Jan 18 '20 at 20:49
1

In order to generate a static base map in tmap using read_osm, the x argument object needs to be in the WGS84 projection (EPSG 4326).

library(tmap)
library(tmaptools)
library(tidyverse)

tmap_mode("plot")

data(NLD_muni)

# This does not work:
NLD_bm <- tmaptools::read_osm(NLD_muni, type = "esri")

# The problem is that NLD_muni is in the projected coordinate system for Netherlands,
# but read_osm wants WGS84
sf::st_crs(NLD_muni) # check the coordinate reference system

# This does work
NLD_bm <- NLD_muni %>% 
  sf::st_transform(., crs = 4326) %>% # transform NLD_muni to WGS84 projection
  sf::st_bbox(.) %>% # extract bounding box of transformed layer
  tmaptools::read_osm(., type = "esri") # pass bounding box to read_osm

# Now you can map them
tm_shape(NLD_bm) + tm_rgb() + tm_shape(NLD_muni) + tm_borders(col = "red")

Here is what it looks like: static map with OSM basemap

Marcos
  • 444
  • 4
  • 9
0

Two comments:

1) it is not doable to show a basemap with a static {tmap} map; the basemap functionality comes from {leaflet} package, and is therefore available only in view mode.

If you absolutely positively require a basemap in a static map (which is a valid use case) consider using a combination of {ggplot2} and {ggmap}.

2) your loading of NLD_muni is needlesly complex; consider this code instead:

library(tmap)

data(NLD_muni)

tmap_mode("view")

tm_shape(NLD_muni) + tm_polygons(col = "population")

enter image description here

Jindra Lacko
  • 7,814
  • 3
  • 22
  • 44