Calculate swath-profile values perpendicular to a straight baseline.
The distance between samples and the number of samples can be
specified, see arguments k and dist. Values of the swath-profile are
extracted from a given raster file, see argument raster. CRS of raster
and points have to be the same.
Source
The algorithm is a modified version of "swathR" by Vincent Haburaj (https://github.com/jjvhab/swathR).
Arguments
- profile
either a
sfobject or a matrix(ncol=2, nrow=2) with x and y coordinates of beginning and end point of the baseline; each point in one row- column 1
x coordinates (or longitudes)
- column 2
y coordinates (latitudes)
- raster
Raster file (
"SpatRaster"object as loaded byterra::rast())- k
integer. number of lines on each side of the baseline
- dist
numeric. distance between lines
- crs
character. coordinate reference system. Both the
rasterand theprofileare transformed into this CRS. Uses the CRS ofrasterby default.- method
character. method for extraction of raw data, see
terra::extract(): default value:"bilinear"
Value
list.
swathmatrix. Statistics of the raster measured along the lines
datalist of numeric vector containing the data extracted from the raster along each line
linesswath lines as
"sf"objects
Examples
# Create a random raster
r <- terra::rast(ncol = 10, nrow = 10, xmin = -150, xmax = -80, ymin = 20, ymax = 60, crs = "WGS84")
terra::values(r) <- runif(terra::ncell(r))
# Create a random profile
profile <- data.frame(lon = c(-140, -90), lat = c(55, 25)) |>
sf::st_as_sf(coords = c("lon", "lat"), crs = "WGS84")
swath_profile(profile, r, k = 2, dist = 1)
#> $swath
#> distance mean median std.dev. min max quantile(25)
#> [1,] -2 0.5188714 0.5354328 0.1226090 0.2986234 0.7434251 0.4610370
#> [2,] -1 0.5193847 0.5047430 0.1157125 0.2986234 0.7434251 0.4610370
#> [3,] 0 0.4861382 0.4843019 0.1315223 0.2613481 0.7434251 0.4053778
#> [4,] 1 0.4729833 0.4900782 0.1175709 0.2613481 0.6733179 0.3944666
#> [5,] 2 0.4590701 0.4745807 0.1073198 0.2613481 0.6733179 0.4035579
#> quantile(75)
#> [1,] 0.5807059
#> [2,] 0.5776294
#> [3,] 0.5411026
#> [4,] 0.5409959
#> [5,] 0.5320362
#>
#> $data
#> $data$lyr.1
#> [1] 0.5367092 0.4990050 0.4435484 0.3726443 0.3382311 0.5542827 0.4785257
#> [8] 0.2986234 0.6733179 0.6009761 0.5354328 0.6431701 0.5604357 0.5047430
#> [15] 0.7434251
#>
#> $data$lyr.1
#> [1] 0.5367092 0.4990050 0.4435484 0.3726443 0.4900782 0.5542827 0.4785257
#> [8] 0.2986234 0.6733179 0.6009761 0.5354328 0.6431701 0.4162889 0.5047430
#> [15] 0.7434251
#>
#> $data$lyr.1
#> [1] 0.5367092 0.4745807 0.4435484 0.3726443 0.4900782 0.5542827 0.4785257
#> [8] 0.2986234 0.6733179 0.3514926 0.5354328 0.6431701 0.4162889 0.5047430
#> [15] 0.7434251 0.2613481
#>
#> $data$lyr.1
#> [1] 0.4745807 0.4435484 0.3726443 0.4900782 0.5542827 0.5465590 0.2986234
#> [8] 0.6733179 0.3514926 0.5354328 0.6431701 0.4162889 0.5047430 0.5286397
#> [15] 0.2613481
#>
#> $data$lyr.1
#> [1] 0.4745807 0.4435484 0.4067186 0.4900782 0.5542827 0.5465590 0.2986234
#> [8] 0.6733179 0.3514926 0.5354328 0.4003972 0.4162889 0.5047430 0.5286397
#> [15] 0.2613481
#>
#>
#> $lines
#> Simple feature collection with 5 features and 0 fields
#> Geometry type: LINESTRING
#> Dimension: XY
#> Bounding box: xmin: -141.029 ymin: 23.28501 xmax: -88.97101 ymax: 56.71499
#> Geodetic CRS: WGS 84
#> geometry
#> 1 LINESTRING (-138.971 56.714...
#> 2 LINESTRING (-139.4855 55.85...
#> 3 LINESTRING (-140 55, -90 25)
#> 4 LINESTRING (-140.5145 54.14...
#> 5 LINESTRING (-141.029 53.285...
#>