R/get_gcp_residuals.R
get_gcp_residuals.RdThis function refits the polynomial transformation that GDAL uses in
georeference_img() (pixel coordinates to longitude/latitude) and returns,
for each Ground Control Point (GCP), the ordinary residuals and the
leave-one-out (LOO) residuals. Ordinary residuals measure how well the
transformation fits the GCPs used to estimate it. On the other hand, LOO
residuals are obtained by predicting each GCP from a transformation fitted to
all the other GCPs, and give a more "honest" measure of the error expected
for points of the map that were not used as GCPs. The leverage of each GCP is
also returned, which is a measure of how much influence each GCP has on the
fitted transformation. Leverage values range from 0 to 1, with higher values
indicating that the GCP has more influence on the fitted transformation.
get_gcp_residuals(
gcp,
transform_method = c("auto", "poly_1", "poly_2", "poly_3")
)A data frame containing the Ground Control Points (GCPs), with
columns id, x, y, longitude and latitude (the same format used by
georeference_img()).
A character string specifying the polynomial used for
the transformation. Options are "poly_1" (first order polynomial), "poly_2"
(second order polynomial), "poly_3" (third order polynomial) or "auto" (the
default), which mimics the choice made by GDAL in georeference_img(): a
first order polynomial if fewer than 6 GCPs are available, and a second
order polynomial otherwise.
A data frame with one row per GCP, containing the original columns and:
fitted_lon, fitted_lat: the position predicted by the transformation.
res_lon, res_lat: the ordinary residuals, in degrees.
res_km: the distance between observed and fitted position, in km.
leverage: the leverage of each GCP. Values close to 1 indicate GCPs
that strongly determine the transformation (typically points at the edges
of the map, or isolated points).
loo_lon, loo_lat: the leave-one-out residuals, in degrees.
loo_km: the distance between observed and leave-one-out predicted
position, in km.
The data frame has the attributes order (the order of the polynomial).
The transformation is a polynomial in pixel coordinates, fitted by ordinary
least square separately for longitude and latitude, with all terms up to the
specific order (3, 6, 10 coefficients for first, second and third order
polynomials). This forward (pixel to lon/lat) transformation, is the same
applied by GDAL in georeference_img() and get_pts_coords(). LOO residuals
are computed without refitting as \(e_i / (1 - h_{ii})\), where \(e_i\)
is the ordinary residual and \(h_{ii}\) is the leverage of the i-th GCP.
Residuals are given in degrees (observed minus fitted) and as the great-circle distance (in km, on a sphere of radius 6371.0088 km) between the observed and the fitted position of each GCP. Note that, because the polynomial is fitted in degrees, residuals in longitude are not comparable across latitudes, whereas distances in km are.
gcp_df <- readRDS(system.file(
"extdata/europe_gcp_georef.RDS",
package = "crstools"
))
res <- get_gcp_residuals(gcp_df, transform_method = "poly_1")
res
#> id x y longitude latitude fitted_lon fitted_lat res_lon
#> 1 1 830.3113 576.5869 -9.071636 37.16461 -14.37127 36.92099 5.299632
#> 2 2 2399.7464 783.3988 18.132085 40.06647 22.68395 40.24030 -4.551865
#> 3 3 2886.9187 2617.8971 41.207327 67.11767 37.55940 67.03625 3.647923
#> 4 4 414.0564 2537.4702 -24.627151 65.64215 -20.35858 65.37835 -4.268566
#> 5 5 836.4781 1645.1151 -10.472369 51.87063 -12.19129 52.47395 1.718923
#> 6 6 1992.7416 561.2675 10.907249 37.11543 12.75330 36.92711 -1.846047
#> res_lat res_km leverage loo_lon loo_lat loo_km
#> 1 0.24361635 471.0829 0.4488880 9.616253 0.4420451 855.6425
#> 2 -0.17383656 387.2971 0.3982387 -7.564236 -0.2888796 642.9361
#> 3 0.08142377 158.2274 0.8331247 21.860169 0.4879317 951.2944
#> 4 0.26380279 198.8889 0.6693948 -12.911370 0.7979389 606.6181
#> 5 -0.60331713 135.0555 0.2772730 2.378385 -0.8347788 186.4964
#> 6 0.18831078 165.2206 0.3730808 -2.944634 0.3003749 263.7281
# mean error (km)
mean(res$res_km)
#> [1] 252.6287
# root mean square error (km)
sqrt(mean(res$res_km^2))
#> [1] 283.4457
# leave-one-out root mean square error (km), no GCPs with leverage 1 (NA)
sqrt(mean(res$loo_km^2, na.rm = TRUE))
#> [1] 648.4303