[R-sig-Geo] How to project a SpatExtent object?

Tim Elrick @bo @end|ng |rom e|r|ck@de
Tue Jul 6 17:47:10 CEST 2021


Hi Mike,

Thanks. Your code didn't take into account that I was interested in 
SpatExtent instead of SpatRaster. So, I modified the code:

library(terra)
f <- system.file("ex/elev.tif", package="terra", mustWork = TRUE)
f
r <- rast(f)

e <- ext(c(6.0, 6.2, 49.7, 49.8))
plot(r, ext = e)

r2 <- project(r, "+proj=utm +zone=31")
r2
# the following is the line that re-projects the SpatExtent
e2 <- ext(project(rast(e, crs = crs(r)), "+proj=utm +zone=31"))
plot(r2, ext = e2)

 From the example it is difficult to judge if it worked out (as I am not 
too familiar with the DEM of the example). When I try to apply it my 
case (reprojecting a SpatExtent from UTM zone 18N to WGS84), this code 
doesn't work. The resulting extent will select the whole raster image 
instead of the previously defined extent. Due to the large file sizes, I 
cannot offer a reproducible example based on my data unfortunately. I 
will look into it further and try to understand, why it doesn't work.

Thanks anyway,
Tim

On 2021-07-06 02:00, Michael Sumner wrote:
I think you can project the empty rast() object, using the same idiom as

{raster} to create an empty version:

   f <- system.file("ex/elev.tif", package="terra", mustWork = TRUE)
   r <- rast(f)
terra::project(terra::rast(r), "+proj=laea")

that seems analogous to what raster::projectExtent does.

HTH, Mike



On Tue, Jul 6, 2021 at 6:14 AM Tim Elrick <abo using elrick.de
<mailto:abo using elrick.de>> wrote:

     Hi all,

     Does anyone know how to (re-)project a SpatExtent object from the terra
     package?

     project() only works on SpatVector or SpatRaster, but vect() doesn't
     work on a SpatExtent either to convert a SpatExtent to a SpatVector.

     Thanks,

     Tim

     _______________________________________________
     R-sig-Geo mailing list
     R-sig-Geo using r-project.org <mailto:R-sig-Geo using r-project.org>
     https://stat.ethz.ch/mailman/listinfo/r-sig-geo
     <https://stat.ethz.ch/mailman/listinfo/r-sig-geo>



-- 
Michael Sumner
Software and Database Engineer
Australian Antarctic Division
Hobart, Australia
e-mail: mdsumner using gmail.com <mailto:mdsumner using gmail.com>



More information about the R-sig-Geo mailing list