Saving AKDEs to different projections

89 views
Skip to first unread message

Michael Taylor

unread,
Mar 9, 2022, 9:07:31 PM3/9/22
to ctmm R user group
Hi Chris,

Apologies if I have missed a description of how to do this correctly, but I am having some trouble exporting AKDEs as rasters to a different CRS.

Having calculated a list of AKDEs and am trying to export shapefiles of the home range extent and rasters of the AKDE CDF values both in the flat projection used in CTMM and to a WGS 84 CRS.

Exporting without reprojecting is easy, as is the re-projected shapefile, however, when exporting the rasters I cannot just assign a new proj4string. If I try this the reprojected raster is 100s of km from where it should be. 

The only way I have managed to export in the new CRS correctly is to use another raster layer as a reference (see script below). This raster is much larger (it covers the full study area, and results in the AKDE being upsized to this raster's size (all the blank space comes out with no data). This both takes a lot of time and creates much larger files. Equally, if I load the non-reprojected layers into QGIS and then reproject the raster is nowhere near the correct location (though the standard two-point projection looks to be in the perfect position). I can also get the correct results if I calculate the AKDEs from the start in the WGS84 projection, but I would prefer to avoid this.

Do you know of a way to speed up this process or an alternative approach that I may have missed?

Cheers,

Mike


#  Export as WGS84
export.proj <- "+proj=utm +zone=52 +south +datum=WGS84 +units=m +no_defs"
dataset <-  test

#Shapefile
for(i in 1:length(AKDE)){
  SP.AKDE<-SpatialPolygonsDataFrame.UD(AKDE[[i]],level.UD=c(0.95,0.5),level=0.95)
  SP.AKDE.PROJ<-spTransform(SP.AKDE, CRS(export.proj))
 rgdal::writeOGR(SP.AKDE.PROJ,dsn=getwd(),layer=paste(list(names(AKDE[i])),dataset,"UD_WGS84_tpp",sep="_"), driver="ESRI Shapefile")


# Raster
AKDE.EX <- list()
AKDE.EX.proj <- list()
for(i in 1:length(AKDE)){
 
  AKDE.EX[[i]] <- raster(AKDE[[i]])
  # projection(AKDE.EX[[i]]) <- export.proj # when loaded into QGIS layers created this way are nowhere near the correct location
  AKDE.EX.proj[[i]] <- raster::projectRaster(AKDE.EX[[i]], WGS84)
  writeRaster(AKDE.EX.proj[[i]], filename= paste(list(names(AKDE[i])),dataset,"CDF_WGS84_tpp.tif",sep="_"),DF ="CDF", overwrite=TRUE)
}

Christen Fleming

unread,
Mar 9, 2022, 10:44:24 PM3/9/22
to ctmm R user group
Hi Mike,

raster::projection()<-  just assigns the projection string, but it doesn't actually re-project the data to have that projection.
raster::projectRaster() sounds like what you want, but I think you also need to fiddle with raster::projectExtent() and then fix the resolution of that to what you want to make a suitable to argument for projectRaster(). With that, you should be able to calculate a re-projected UD that is only slightly larger than the original.

Best,
Chris
Reply all
Reply to author
Forward
0 new messages