# \donttest{
## Prepare file paths
td <- tempdir()
in_tif <- file.path(td, "tahoe.tif")
gcp_tif <- file.path(td, "tahoe_gcp.tif")
out_tif <- file.path(td, "tahoe_warped.tif")
## Set up some ground control points, then warp
file.copy(system.file("extdata/tahoe.tif", package = "gdalUtilities"),
in_tif)
## Four numbers: column, row, x-coord, y-coord
gcp <- matrix(c(100, 300, -119.93226, 39.28977, ## A
0, 300, -119.93281, 39.28977, ## B
100, 400, -119.93226, 39.28922, ## C
0, 400, -119.93281, 39.28922, ## lower-left
400, 0, -119.93067, 39.29136, ## upper-right
400, 400, -119.93062, 39.28922, ## lower-right
0, 0, -119.93281, 39.29141), ## upper-left
ncol = 4, byrow = TRUE)
## Add ground control points. (For some reason, this drops CRS, so
## it needs to be explicitly given via `a_srs` argument.)
gdal_translate(in_tif, gcp_tif, gcp = gcp, a_srs = "EPSG:4326")
gdalwarp(gcp_tif, out_tif, r = "bilinear")
## Check that it worked
if(require(terra)) {
op <- par(mfcol = c(1, 2))
r1 <- plot(rast(in_tif), main = "Original raster")
r2 <- plot(rast(out_tif), main = "Warped raster")
par(op) ## Reset preexisting parameters
}
# }
Run the code above in your browser using DataLab