terra icon indicating copy to clipboard operation
terra copied to clipboard

distance and boundary functions run slower

Open fRIdanwismer opened this issue 3 years ago • 4 comments

Hi, was there updates to these functions? They seem to run considerably slower compared to older version of terra. Thanks!

fRIdanwismer avatar Jun 17 '21 16:06 fRIdanwismer

Not on purpose, but I can look into it. Do you have an example where you observed this, or can you show(x) where x is a SpatRaster? Is this a SpatRaster with a longlat crs?

rhijmans avatar Jun 19 '21 18:06 rhijmans

Thanks of the response Robert. Attached is the .tif I use in the following code chunk to generate distance to edge:

library(terra)
library(raster)

# Input binary raster
cb <-rast("./cb.tif")

# Reclassify
m <- c(0,NA, 1,1)
rclmat <- matrix(m, ncol=2, byrow=TRUE)
na_1 <- terra::classify(cb,rclmat)

# Aggregate 30m to 150m cell size
cb150m_terra <- terra::aggregate(na_1,5, fun='max')
cb150m_raster <- raster(cb150m_terra)

# Terra::Distance
start.time <- Sys.time()
d2cb <- terra::distance(cb150m_terra)
end.time <- Sys.time()
time.taken <- end.time - start.time
time.taken
# Time difference of 2.53582 mins

# Raster::Distance
start.time <- Sys.time()
d2cb <- terra::distance(cb150m_raster)
end.time <- Sys.time()
time.taken <- end.time - start.time
time.taken
# Time difference of 2.002817 mins

I appreciate you looking into this. Terra is a great package and I have begun converting my code base to replacing raster with it.

Dan

fRIdanwismer avatar Jun 21 '21 14:06 fRIdanwismer

Can you upload the file to github (or a file share and provide a link?). Attaching by email does not work (and github generally works much if you avoid email responses)

rhijmans avatar Jun 22 '21 18:06 rhijmans

For sure. It is a .tif however and it won't let me upload. Here is a link you can download though: https://portal.onefilesync.ca/shares/file/b2gLr1ctxKJ/

fRIdanwismer avatar Jun 22 '21 21:06 fRIdanwismer

I have (at long last) been working on speeding these things up, but I no longer have the file to compare with your benchmark,

rhijmans avatar Oct 13 '22 01:10 rhijmans

I am closing this as I do not have the file for comparison. But it would be great if you could share it again.

rhijmans avatar Oct 14 '22 22:10 rhijmans