R/georeference_img.R
georeference_img.RdThis function warps a given image using a set of Ground Control Points (GCPs)
to create a georeferenced version of the image. The function uses GDAL tools
to first translate the image into a georeferenced TIFF format and then
applies a warp operation to reproject the image into a spatial reference
system (EPSG:4326). The warped image is saved as a new file with the suffix
_warp.tif.
georeference_img(
image_obj,
gcp,
output_path = NULL,
transform_method = c("auto", "poly_1", "poly_2", "poly_3", "tps")
)A character string specifying the file path to the input image (JPEG format). The function reads this image and applies the GCPs for georeferencing.
A data frame containing the Ground Control Points (GCPs). This
dataframe can be produced with the choose_gcp() and find_gcp_coords()
functions. This data frame should have the following columns:
id: An identifier for each GCP (numeric).
x: The x-coordinate of the GCP (in pixel space).
y: The y-coordinate of the GCP (in pixel space).
longitude: The longitude of the GCP (georeferenced).
latitude: The latitude of the GCP (georeferenced).
A character string giving the path (without extension)
used to name the output files: .tif is appended to it for the image with
the GCPs, and _warp.tif for the warped image. If NULL (the default), the
path of the input image without its extension is used if image_obj is a
file path, and "map" (in the working directory) if it is an array.
A character string specifying the transformation method to be used for warping the image. Options are "poly_1" (first order polynomial), "poly_2" (second order polynomial), "poly_3" (third order polynomial), "tps" (thin plate spline), or "auto" (the default, allowing GDAL to choose of a polynomial of the appropriate order based on the number of available GCP. Polynomials are best for standard maps directly captured from a publication (a first or second order polynomial is often sufficient), tps allows for scanning artefacts, but it is badly affected by any incorrect GCP.
A character string representing the path to the newly created warped
TIFF image file (_warp.tif). This file contains the georeferenced image.
if (FALSE) { # rlang::is_interactive()
# get the path to an example image included in the package
img_path <- system.file("extdata/europe_map.jpeg",
package = "crstools"
)
# load a set of GCPs (or we could create them using the choose_gcp()
# and find_gcp_coords() functions)
gcp_df <- readRDS(system.file(
"extdata/europe_gcp_georef.RDS",
package = "crstools"
))
# Assuming you have a set of GCPs in gcp_df and an image file "image.jpg"
warped_img <- georeference_img(
image_obj = img_path, gcp = gcp_df,
output_path = tempfile(pattern = "georef_img_")
)
}