Archives par mot-clé : cartography

The maptiles package

To create maps from tiles, maptiles downloads, composes and displays tiles from a large number of providers (e.g. OpenStreetMap, Stamen, Esri, CARTO, or Thunderforest).

Installation

You can install the released version of maptiles from CRAN with:

install.packages("maptiles")

You can install the development version of maptiles from GitHub with:

# install.packages("remotes")
remotes::install_github("riatelab/maptiles")

Note: maptiles uses terra which requires a recent version of GDAL (>= 3.0.4).

Demo

This is a basic example which shows you how to dowload and display OpenStreetMap tiles over North Carolina:

library(sf)
library(maptiles)
# import North Carolina counties
nc <- st_read(system.file("shape/nc.shp", package="sf"), 
              quiet = TRUE)
# dowload tiles and compose raster (SpatRaster)
nc_osm <- get_tiles(nc, crop = TRUE)
# display map
plot_tiles(nc_osm)
# add Norh Carolina counties
plot(st_geometry(nc), col = NA, add = TRUE)
# add credit
mtext(text = get_credit("OpenStreetMap"), 
      side = 1, line = -1, adj = 1, cex = .9, 
      font = 3)

maptiles gives access to a lot of tiles servers, but it is possible to add others. The following example demonstrates the setting of a map tiles server and how to cache the original tiles for future use:

# define the query
fullserver <- paste(
  "https://server.arcgisonline.com/ArcGIS/rest/services",
  "Specialty/DeLorme_World_Base_Map/MapServer",
  "tile/{z}/{y}/{x}.jpg",
  sep = "/"
)
# define the tile server parameter
esri <-  list(
  src = 'esri',
  q = fullserver,
  sub = NA,
  cit = 'Tiles: Esri; Copyright: 2012 DeLorme'
)
# dowload tiles and compose raster (SpatRaster)
nc_esri <- get_tiles(x = nc, provider = esri, crop = TRUE, 
                     cachedir = tempdir(), verbose = TRUE)
# display map
plot_tiles(nc_esri)
# display credits
mtext(text = esri$cit, side = 1, line = -1, 
      adj = 1, cex = .9, font = 3)

The following figure shows mini maps for most of the tiles providers available:

Attribution of map tiles

All maps available through maptiles are offered freely by various providers. The only counterpart from the user is to properly display an attribution text on the maps. get_credit() displays a short credit text to add on each map using the downloaded tiles.

Background

Most of maptilescode comes from getTiles() and tilesLayer() functions in cartography. It appears useful to me to have a package focused on the download and display of map tiles only. On the technical side, it uses terra instead of raster for managing raster data.

Alternatives

There are many alternative packages that pursue the same objective as maptiles. Some focus on a specific map tiles provider (e.g. mapbox, google, OpenStreetMap) or on a specific graphics device (ggplot2). The goal of maptiles is to be flexible enough to allow the use of different providers and to have a minimal number of robust and modern dependencies. However, depending on the use case, one of following packages may better suit your needs:

Note

Not to be confused with tilemaps, that “implements an algorithm for generating maps, known as tile maps, in which each region is represented by a single tile of the same shape and size.”

New version of cartography

A new version of the cartography package (v2.4.0) has arrived on CRAN.

New Features

  • waffleLayer() plots a « waffle map ». This kind of representation allows to plot several quantities on the same map.
  • ghostLayer() is a short function that plot an invisible layer with the extent of a spatial object. This function is useful to initiate a map with a specific extent.
library(sf)
library(cartography)
mtq <- st_read(system.file("gpkg/mtq.gpkg",
package="cartography")) target <- mtq[30, ] ghostLayer(target) plot(st_geometry(mtq), col = "gold2", add = TRUE)

These 3 functions are well described by their creator, Diego Hernangómez, on his blog.

Enhancements

getTiles() gets map tiles from various tile servers.

  •  The function has gained caching capacities (through cachedir and forceDownload arguments). Users can now cache tiles in a folder and reuse them across R sessions.
  • More than 50 tile servers have been included and other can be added by users.
  •  It is possible to use Thunderforest tiles with an API key.
click here to see the figure in a better resolution

The barscale() function plots a scale bar on a map, a new unit argument has been added to plot the scale bar in meters, kilometers or miles.

Under the Hood

cartography does not use rosm anymore to import map tiles. The package is working great but its maintainer, Dewey Dunnington, suggests that it will not evolve much and that other package will appear to replace it. Thus we have decided to use slippymath and internal code to download tiles. A (positive) side effect of this replacement is a decrease in the indirect dependancies of cartography.

cartography appears to be robust to the changes introduced by the last sf version (v0.9). This version takes into account the new representation of coordinate reference systems proposed by GDAL and PROJ. More information on this here.

How to interactively position legends and layout elements on a map with cartography

In cartography several functions have a pos or legend.pos argument. These arguments can take the following values: « topleft », « top », « topright », « right », « bottomright », « bottom », « bottomleft », « bottomleftextra », « left » or a vector of two coordinates in map units (c(x, y)).

The posibility to use a vector of coordinates is useful for placing an element at a precise location: Continuer la lecture de How to interactively position legends and layout elements on a map with cartography