Andy
Andy

Reputation: 21

Parameter error in tmaptools package read osm when using example data

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.

Upvotes: 2

Views: 724

Answers (3)

Marcos
Marcos

Reputation: 474

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

Upvotes: 1

Delphine
Delphine

Reputation: 31

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")

Upvotes: 2

Jindra Lacko
Jindra Lacko

Reputation: 8719

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

Upvotes: 0

Related Questions