Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

terra::viewshed error: source and target filename cannot be the same? #1100

Closed
kamistick opened this issue Apr 6, 2023 · 1 comment
Closed

Comments

@kamistick
Copy link

When running terra::viewshed() I am receiving the error: Error : [viewshed] source and target filename cannot be the same. However, I am only specifying a target filename. When wrapping terra::viewshed in try() an output is written to file and my processing continues. Without the try() the output is still generated but processing stops.

Is this an erroneous error? Some code reproducing the error:


## libraries
library(terra)
library(sf)

## version info 
# R version 4.2.3 (2023-03-15 ucrt)
# terra_1.7-18                     

## make a raster and fill with random values
ras <- rast(nrows = 100, ncols = 100, xmin = 0, xmax = 100, ymin = 0, ymax = 100, crs = "epsg:5070")
set.seed(0)
values(ras) <- runif(ncell(ras))

# make a point (just for plotting purposes)
xy <- st_point(c(50,50))
v <- vect(xy)
crs(v) <- "epsg:5070"

# plot random raster and point
plot(ras)
points(v)

Screen Shot 2023-04-06 at 2 22 58 PM

# define directory where the viewshed will be saved
of <- paste0(main_dir, "terra_test\\viewsheds\\rewritten_vs_test.tif")

# run viewshed
try(viewshed(x = ras, loc = c(50,50), observer = 1.7, target = 0, curvcoef = 0.85714, output = "yes/no", filename = of, overwrite = TRUE))

# read output back in as a rast and plot
vs_ras <- rast(of)
plot(vs_ras)
points(v)

Screen Shot 2023-04-06 at 2 25 09 PM

The resulting viewshed, despite the error, seems reasonable (and I have generated many using real, less easily reproducible data that look reasonable as well).

@rhijmans
Copy link
Member

rhijmans commented Apr 6, 2023

Thanks, I now get:

library(terra)
# terra 1.7.23
ras <- rast(nrows = 100, ncols = 100, xmin = 0, xmax = 100, ymin = 0, ymax = 100, crs = "epsg:5070")
set.seed(0)
values(ras) <- runif(ncell(ras))
v <- vect(cbind(50,50), crs="epsg:5070")

vs <- viewshed(ras, loc=c(50,50), observer=1.7, filename="test.tif", overwrite=TRUE)
vs
#class       : SpatRaster 
#dimensions  : 100, 100, 1  (nrow, ncol, nlyr)
#resolution  : 1, 1  (x, y)
#extent      : 0, 100, 0, 100  (xmin, xmax, ymin, ymax)
#coord. ref. : NAD83 / Conus Albers (EPSG:5070) 
#source      : test.tif 
#name        : viewshed 
#min value   :    FALSE 
#max value   :     TRUE 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants