I am trying to follow best practice and reproject my SpatVector into the projection for a SpatRaster, but the resulting reprojected object doesn't function properly. I have designed a reproducible dataset below. I am operating under R 4.4.2.
#Libraries:
library(terra)
#Make a raster file from scratch, with projection EPSG:4326:
t <- terra::rast(crs = "EPSG:4326", xmin = -124.7722, xmax = -67.0648, ymin = 25.0631,
ymax = 49.396, nrows = 585, ncols = 1386)
#Make a SpatVector from scratch, with EPSG:4269:
e <- ext(-87.24353, -77.06105, 32.76981, 39.46601)
p <- as.polygons(e, crs = "EPSG:4269")
#Making a new vector object by reprojecting the spatvector with the projection of the spatraster:
sca_proj <- terra::project(p, terra::crs(t))
This produces warnings:
Warning messages:
1: In class(object) <- "environment" :
Setting class(x) to "environment" sets attribute to NULL; result will no longer be an S4 object
2: PROJ: Cannot open https://cdn.proj.org/us_noaa_alhpgn.tif: schannel: CertGetCertificateChain trust error CERT_TRUST_IS_UNTRUSTED_ROOT (GDAL error 1)
3: PROJ: Cannot open https://cdn.proj.org/us_noaa_inhpgn.tif: schannel: CertGetCertificateChain trust error CERT_TRUST_IS_UNTRUSTED_ROOT (GDAL error 1)
4: PROJ: Cannot open https://cdn.proj.org/us_noaa_MD.tif: schannel: CertGetCertificateChain trust error CERT_TRUST_IS_UNTRUSTED_ROOT (GDAL error 1)
Plotting demonstrates that it is no longer a properly structured polygon:
plot(sca_proj)
Error in x@ptr$linesNA() : negative length vectors are not allowed
What am I doing wrong here?
This works for me
library(terra)
#terra 1.8.5
r <- terra::rast(crs = "EPSG:4326", xmin = -124.7722, xmax = -67.0648, ymin = 25.0631, ymax = 49.396, nrows = 585, ncols = 1386)
p <- ext(-87.24353, -77.06105, 32.76981, 39.46601) |> as.polygons(crs = "EPSG:4269")
pr <- terra::project(p, r)
This involves a transformation from the WGS84 to the NAD83 datum (even though these are essentially equivalent):
crs(r, proj=T)
#[1] "+proj=longlat +datum=WGS84 +no_defs"
crs(p, proj=T)
#[1] "+proj=longlat +datum=NAD83 +no_defs"
To be able to do a datum transformation, terra needs to (GDAL) download parameter files. That is not working for you, probably because of a firewall or some such security feature. This leads to these warnings
PROJ: Cannot open https://cdn.proj.org/us_noaa_alhpgn.tif: schannel: CertGetCertificateChain trust error CERT_TRUST_IS_UNTRUSTED_ROOT (GDAL error 1)
And the projection cannot be done.
I do not know how to solve that for your computer/network. But given that these two datums are very similar (differences up to 1 m) perhaps you can use the polygons without projecting.