View source: R/rasterpic_img.R
| rasterpic_img | R Documentation |
SpatRasterGeotags an image based on the coordinates of a given spatial object.
rasterpic_img(
x,
img,
halign = 0.5,
valign = 0.5,
expand = 0,
crop = FALSE,
mask = FALSE,
inverse = FALSE,
crs = NULL
)
x |
R object that may be: |
img |
An image to be geotagged. It can be a local file or an online
file (e.g.
|
halign, valign |
Horizontal and vertical alignment of
|
expand |
An expansion factor of the bounding box of |
crop |
Logical. Should the raster be cropped to the (expanded) bounding
box of |
mask |
Logical, applicable only if |
inverse |
Logical. It affects only if |
crs |
Character string describing a coordinate reference system.
This parameter would only affect if |
vignette("rasterpic", package = "rasterpic") explains with examples the
effect of parameters halign, valign, expand, crop and mask.
The function preserves the Coordinate Reference System of x if applicable.
For optimal results do not use geographic coordinates
(longitude/latitude).
crs can be in a WKT format, as a "authority:number" code such as
"EPSG:4326", or a PROJ-string format such as "+proj=utm +zone=12". It can
be also retrieved with:
sf::st_crs(25830)$wkt.
terra::crs().
tidyterra::pull_crs().
See Value and Notes on terra::crs().
A SpatRaster object (see terra::rast()) where each layer corresponds to
a color channel of img:
If img has at least 3 channels (e.g. layers), the result would have
an additional property setting the layers 1 to 3 as the Red, Green and Blue
channels.
If img already has a definition or RGB values (this may be the case for
tiff/tif files) the result would keep that channel definition.
From sf:
sf::st_crs().
sf::st_bbox().
vignette("sf1", package = "sf") to understand how sf organizes
R objects.
From terra:
terra::vect(), terra::rast() and terra::ext().
terra::mask().
terra::crs().
terra::RGB().
For plotting:
terra::plot() and terra::plotRGB().
tidyterra::autoplot.SpatRaster().
tidyterra::geom_spatraster_rgb().
library(sf)
library(terra)
library(ggplot2)
library(tidyterra)
x_path <- system.file("gpkg/UK.gpkg", package = "rasterpic")
x <- st_read(x_path, quiet = TRUE)
img <- system.file("img/vertical.png", package = "rasterpic")
# Default config
ex1 <- rasterpic_img(x, img)
ex1
autoplot(ex1) +
geom_sf(data = x, fill = NA, color = "white", linewidth = .5)
# Expand
ex2 <- rasterpic_img(x, img, expand = 0.5)
autoplot(ex2) +
geom_sf(data = x, fill = NA, color = "white", linewidth = .5)
# Align
ex3 <- rasterpic_img(x, img, halign = 0)
autoplot(ex3) +
geom_sf(data = x, fill = NA, color = "white", linewidth = .5)
labs(title = "Align")
# Crop
ex4 <- rasterpic_img(x, img, crop = TRUE)
autoplot(ex4) +
geom_sf(data = x, fill = NA, color = "white", linewidth = .5) +
labs(title = "Crop")
# Mask
ex5 <- rasterpic_img(x, img, mask = TRUE)
autoplot(ex5) +
geom_sf(data = x, fill = NA, color = "white", linewidth = .5) +
labs(title = "Mask")
# Mask inverse
ex6 <- rasterpic_img(x, img, mask = TRUE, inverse = TRUE)
autoplot(ex6) +
geom_sf(data = x, fill = NA, color = "white", linewidth = .5) +
labs(title = "Mask Inverse")
# Combine Mask inverse and crop
ex7 <- rasterpic_img(x, img, crop = TRUE, mask = TRUE, inverse = TRUE)
autoplot(ex7) +
geom_sf(data = x, fill = NA, color = "white", linewidth = .5) +
labs(title = "Combine")
# RGB channels ------
plot(ex1)
ex_rgb <- ex1
has.RGB(ex_rgb)
RGB(ex_rgb)
# Modify RGB channels
RGB(ex_rgb) <- c(2, 3, 1)
RGB(ex_rgb)
plot(ex_rgb)
# Remove RGB channels
RGB(ex_rgb) <- NULL
has.RGB(ex_rgb)
RGB(ex_rgb)
# Note the difference with terra::plot
plot(ex_rgb)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.