Get a RasterBrick from a .png image cut using the shape of a spatial object. The .png file could be either a local file or extracted from a given url.

getPngLayer(
  x,
  pngpath,
  align = "center",
  margin = 0,
  crop = FALSE,
  mask = TRUE,
  inverse = FALSE,
  dwmode = "curl",
  ...
)

Arguments

x

an sf object, a simple feature collection (POLYGON or MULTIPOLYGON) or a tile (see getTiles).

pngpath

local path or url of a .png file.

align

set how the .png file should be fitted within x. Possible values are 'left','right','top', 'bottom' or 'center'.

margin

inner margin, zooms out the .png over x. If 0 then .png is completely zoomed over x.

crop

TRUE if results should be cropped to the specified x extent.

mask

TRUE if the result should be masked to x.

inverse

logical. If FALSE, overlapped areas of x on pngpath are extracted, otherwise non-overlapping areas are returned. See mask.

dwmode

Set the download mode. It could be 'base' for download.file or 'curl' for curl_download.

...

additional arguments for downloading the file. See download.file or curl_download.

Value

A RasterBrick object is returned.

Details

The effect of align would differ depending of the aspect ratio of x and pngpath. To obtain a fitted tile from pngpath given that x is the tile to fit, set margin = 0 , crop = TRUE.

Note

The accuracy of the final plot would depend on the quality of the .png file, the scale of x and the resolution setup of the graphic device. Exporting to svg is highly recommended.

See also

Author

dieghernan, https://github.com/dieghernan/

Examples

library(sf)
mtq <- st_read(system.file("gpkg/mtq.gpkg", package = "cartography"))
#> Reading layer `mtq' from data source 
#>   `/tmp/RtmpmpfIrO/temp_libpath18ee15f22a9e/cartography/gpkg/mtq.gpkg' 
#>   using driver `GPKG'
#> Simple feature collection with 34 features and 7 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: 690574 ymin: 1592536 xmax: 735940.2 ymax: 1645660
#> Projected CRS: WGS 84 / UTM zone 20N
#Local file
dirpng <- system.file("img/LogoMartinique.png", package = "cartography")
mask <- getPngLayer(mtq, dirpng)

if (FALSE) {
#Remote file
urlpng <- "https://i.imgur.com/gePiDvB.png"
masksea <- getPngLayer(mtq, urlpng, mode = "wb", inverse = TRUE)
}