0

I have many watersheds (n = 116) that I would like to extract the shapefiles for using the R package streamstats. I understand how it works for a single watershed but I would like to have a code that can work for all my watersheds, rather than doing each individually. I have developed a code similar to this SO question. However, I keep running into what I think is a memory limit issue which causes the code to throw the following error message when it comes time to write the shapefiles: Error in ogrListLayers(dsn = dsn) : Cannot open data source.

Is this a memory limit issue? Why is the code having trouble writing the shapefiles?

devtools::install_github("markwh/streamstats")
library(streamstats)
library(dplyr)
library(purrr)
library(tidyr)

setTimeout(9999)

### Example data, seems like a lot but needed to demonstrate issue

dat1 <- data.frame(matrix(ncol = 3, nrow = 5))
x <- c("state","lat","long")
colnames(dat1) <- x
dat1$state <- c("VA","NC","NC","SC","SC")
dat1$lat <- c(38.06957, 36.39778, 36.52250, 33.83295, 33.64908)
dat1$long <- c(-79.89700, -79.19667, -78.99750, -79.04365, -79.09347)

water_shed <- list()

### Hits the StreamStats API (https://streamstats.usgs.gov/docs/streamstatsservices/#/)
### and delineates the watersheds. This can handle all the 100+ watersheds

for(i in 1:nrow(dat1)){
  water_shed[[i]] <- 
    delineateWatershed(xlocation = dat1$long[i], ylocation = dat1$lat[i], crs = 4326,
                       includeparameters = "false",
                       includeflowtypes = "false",
                       includefeatures = "true",
                       rcode = dat1$state[i])
}

### Writes the shapefiles into my directory
### This is when the error message gets thrown

for(i in 1:length(water_shed)){
  writeShapefile(watershed = water_shed[[i]],
                 layer = water_shed[[i]],
                 dir = ".",
                 what = "boundary")
  
}
tassones
  • 891
  • 5
  • 18
  • I'm not sure why but if I broke into smaller chunks some of the shapefiles would write without error. If that did not fix the issue, I opened up another R script, only uploaded the site(s) that I was having an immediate issue with and it would work. Again, not sure why this helped but it did. – tassones Dec 15 '21 at 21:25

1 Answers1

0

Use tryCatch for runtime errors

for(i in 1:nrow(dat1)){
    water_shed[[i]] <- 
      tryCatch({delineateWatershed(xlocation = dat1$long[i], 
              ylocation = dat1$lat[i], crs = 4326,
                         includeparameters = "false",
                         includeflowtypes = "false",
                         includefeatures = "true",
                         rcode = dat1$state[i])},
                         
                         error = function(e) e)
                         
                         
  }

Writing the file to a directory

for(i in seq_along(water_shed)){
   writeShapefile(watershed = water_shed[[i]],
                  layer = water_shed[[i]],
                  dir = "shapefile_tmp",
                  what = "boundary")
  
 }

-checking the output directory enter image description here

akrun
  • 874,273
  • 37
  • 540
  • 662