![](/img/trans.png)
[英]How can I delineate multiple watersheds in R using the streamstats package?
[英]How can I extract the watershed shapefile from the streamstats package in R?
我有許多分水嶺(n = 116),我想提取 shapefile 以使用 R package streamstats 。 我了解它如何適用於單個分水嶺,但我希望有一個可以適用於我所有分水嶺的代碼,而不是單獨執行每個分水嶺。 我開發了一個類似於這個 SO question的代碼。 但是,我一直遇到我認為是 memory 限制問題,這導致代碼在編寫 shapefile 時拋出以下錯誤消息: ogrListLayers 中的錯誤(dsn = dsn):無法打開數據源。
這是 memory 限制問題嗎? 為什么代碼在編寫 shapefile 時遇到問題?
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")
}
使用tryCatch
處理運行時錯誤
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)
}
將文件寫入目錄
for(i in seq_along(water_shed)){
writeShapefile(watershed = water_shed[[i]],
layer = water_shed[[i]],
dir = "shapefile_tmp",
what = "boundary")
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.