Crop a SpatRaster
object based on user-defined selection using an
interactive map or plot.
Usage
mosaic_crop(
mosaic,
r = 3,
g = 2,
b = 1,
re = 4,
nir = 5,
shapefile = NULL,
buffer = 0,
show = c("rgb", "index"),
index = "R",
max_pixels = 5e+05,
downsample = NULL,
...
)
Arguments
- mosaic
A mosaic of class
SpatRaster
, generally imported withmosaic_input()
.- r, g, b, re, nir
The red, green, blue, red-edge, and near-infrared bands of the image, respectively. By default, the function assumes a BGR as input (b = 1, g = 2, r = 3). If a multispectral image is provided up to seven bands can be used to compute built-in indexes. There are no limitation of band numbers if the index is computed using the band name.
- shapefile
An optional
SpatVector
, that can be created withshapefile_input()
.- buffer
A buffering factor to be used when a shapefile is used to crop the mosaic.
- show
The display option for the map view. Options are "rgb" for RGB view and "index" for index view.
- index
The index to use for the index view. Defaults to "B".
- max_pixels
Maximum number of pixels to render in the map or plot (default: 500000).
- downsample
Downsampling factor to reduce the number of pixels (default: NULL). In this case, if the number of pixels in the image (width x height) is greater than
max_pixels
a downsampling factor will be automatically chosen so that the number of plotted pixels approximates themax_pixels
.- ...
Additional arguments passed to
mosaic_view()
.
Details
This function uses the mosaic_view
function to display an
interactive map or plot of the mosaic raster, allowing users to draw a
rectangle to select the cropping area. The selected area is then cropped
from the input mosaic and returned as a new SpatRaster
object. If
shapefile
is declared, the mosaic will be cropped to the extent of
shapefile
.
Examples
if (interactive() && requireNamespace("EBImage")) {
library(pliman)
# Load a raster showing the elevation of Luxembourg
mosaic <- mosaic_input(system.file("ex/elev.tif", package="terra"))
# Generate an interactive map using 'mapview' (works only in an interactive section)
cropped <- mosaic_crop(mosaic)
mosaic_view(cropped)
}
#> Warning: The current raster is in the lat/lon coordinate system, which may result in processing errors when trying to segment individuals in the `mosaic_analyze()` function. It is highly suggested to reproject the raster using mosaic_project() with EPSG:32632
#> class : SpatRaster
#> dimensions : 90, 95, 1 (nrow, ncol, nlyr)
#> resolution : 0.008333333, 0.008333333 (x, y)
#> extent : 5.741667, 6.533333, 49.44167, 50.19167 (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326)
#> source : elev.tif
#> name : elevation
#> min value : 141
#> max value : 547
#> Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.3.1; sf_use_s2() is TRUE
#>
#> Attaching package: ‘sf’
#> The following object is masked from ‘package:pliman’:
#>
#> %>%
#>
#> Listening on http://127.0.0.1:6534
#> Error in st_bbox.default(x): no st_bbox method available for object of class NULL