Title: | Create a Spatial Raster from Plain Images |
---|---|
Description: | Create a spatial raster, as the ones provided by 'terra', from regular pictures. |
Authors: | Diego Hernangómez [aut, cre, cph]
|
Maintainer: | Diego Hernangómez <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.2.3 |
Built: | 2023-09-09 08:11:54 UTC |
Source: | https://github.com/dieghernan/rasterpic |
Geotags 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
)
x |
It could be
|
img |
An image to be geotagged. It can be a local file or an online file (e.g. "https://i.imgur.com/6yHmlwT.jpeg"). The following image extensions are accepted:
|
halign |
Horizontal alignment of |
valign |
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. Should the raster be masked to |
inverse |
Logical. It affects only if |
crs |
Character string describing a coordinate reference system.
This parameter would only affect if |
The function preserves the Coordinate Reference System of the x
object. 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 as sf::st_crs(25830)$wkt
or using
tidyterra::pull_crs()
. See Value and Notes on terra::crs()
.
A SpatRaster
object. See terra::rast()
.
sf::st_crs()
, sf::st_bbox()
, terra::crs()
.
library(sf)
library(terra)
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)
class(ex1)
plotRGB(ex1)
plot(x$geom, add = TRUE, col = NA, border = "white", lwd = 2)
# Expand
ex2 <- rasterpic_img(x, img, expand = 0.5)
plotRGB(ex2)
plot(x$geom, add = TRUE, col = NA, border = "white", lwd = 2)
# Align
ex3 <- rasterpic_img(x, img, halign = 0)
plotRGB(ex3)
plot(x$geom, add = TRUE, col = NA, border = "white", lwd = 2)
# Crop
ex4 <- rasterpic_img(x, img, crop = TRUE)
plotRGB(ex4)
plot(x$geom, add = TRUE, col = NA, border = "white", lwd = 2)
# Mask
ex5 <- rasterpic_img(x, img, mask = TRUE)
plotRGB(ex5)
plot(x$geom, add = TRUE, col = NA, border = "white", lwd = 2)
# Mask inverse
ex6 <- rasterpic_img(x, img, mask = TRUE, inverse = TRUE)
plotRGB(ex6)
plot(x$geom, add = TRUE, col = NA, border = "white", lwd = 2)
# Combine Mask inverse and crop
ex7 <- rasterpic_img(x, img, crop = TRUE, mask = TRUE, inverse = TRUE)
plotRGB(ex7)
plot(x$geom, add = TRUE, col = NA, border = "white", lwd = 2)