“如何修复”;trimriver错误-无剩余线段“;使用Riverdist时?
我一直在寻找解决方案,以计算采样点和河口/河口之间的距离,沿着河道线。看起来“Riverdist”可以解决我所有的问题,但我从一开始就遇到了麻烦。我在国家层面(南非)工作,但由于河流系统相当大和复杂,我一直在尝试首先使用单一集水区 我所做的:“如何修复”;trimriver错误-无剩余线段“;使用Riverdist时?,r,qgis,R,Qgis,我一直在寻找解决方案,以计算采样点和河口/河口之间的距离,沿着河道线。看起来“Riverdist”可以解决我所有的问题,但我从一开始就遇到了麻烦。我在国家层面(南非)工作,但由于河流系统相当大和复杂,我一直在尝试首先使用单一集水区 我所做的: 我使river shp文件尽可能简单,并删除了所有不必要的支流 使用的投影CR 尝试使用“Line2网络”加载它 >库(riverdist) >limpopo我不熟悉riverdist包或line2network()函数,但是,可以稍微修改line2n
- 我使river shp文件尽可能简单,并删除了所有不必要的支流
- 使用的投影CR
- 尝试使用“Line2网络”加载它
>库(riverdist)
>limpopo我不熟悉riverdist
包或line2network()
函数,但是,可以稍微修改line2network()
使其在这种情况下工作。函数中有一行导致问题
# Create your custom function to read the file.
# Examine the line2network function and modify the lines that cause an issue with your specific shape file
my.custom.line2network = function (path = ".", layer, tolerance = 100, reproject = NULL, supplyprojection = NULL) {
sp <- suppressWarnings(rgdal::readOGR(dsn = path, layer = layer, verbose = F))
if (class(sp) != "SpatialLinesDataFrame")
stop("Specified shapefile is not a linear feature.")
if (is.na(sp@proj4string@projargs) & !is.null(supplyprojection))
sp@proj4string@projargs <- supplyprojection
if (is.na(sp@proj4string@projargs))
stop("Shapefile projection information is missing. Use supplyprojection= to specify a Proj.4 projection to use. If the input shapefile is in WGS84 geographic (long-lat) coordinates, this will be +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 (in double-quotes). If so, it must also be reprojected using reproject=.")
proj4 <- strsplit(sp@proj4string@projargs, split = " ")
projected <- sp::is.projected(sp)
if (is.null(reproject) & !projected)
stop("Distances can only be computed from a projected coordinate system. Use reproject= to specify a Proj.4 projection to use.")
if (!is.null(reproject)) {
sp <- sp::spTransform(sp, sp::CRS(reproject))
proj4 <- strsplit(sp@proj4string@projargs, split = " ")
}
units <- "unknown"
for (i in 1:length(proj4[[1]])) {
if (proj4[[1]][i] != "") {
proj4arg <- strsplit(proj4[[1]][i], split = "=")
if (proj4arg[[1]][1] == "+units") {
units <- proj4arg[[1]][2]
cat("\n", "Units:", proj4arg[[1]][2], "\n")
}
}
}
if (length(sp@lines) > 1) {
sp_line <- NA
sp_seg <- NA
lines <- list()
j <- 1
for (i in 1:length(sp@lines)) {
for (k in 1:length(sp@lines[i][[1]]@Lines)) {
lines[[j]] <- sp@lines[i][[1]]@Lines[[k]]@coords
sp_line[j] <- i
sp_seg[j] <- k
j <- j + 1
}
}
}
if (length(sp@lines) == 1) {
lines <- sp@lines[1][[1]]@Lines
length <- length(lines)
lines.new <- list()
for (i in 1:length) {
lines.new[[i]] <- lines[[i]]@coords
}
lines <- lines.new
sp_line <- rep(1, length)
sp_seg <- 1:length
}
length <- length(lines)
rivID <- 1:length
lineID <- data.frame(rivID, sp_line, sp_seg)
connections <- calculateconnections(lines = lines, tolerance = tolerance)
if (any(connections %in% 5:6))
braided <- TRUE
lengths <- rep(NA, length)
for (i in 1:length) {
lengths[i] <- pdisttot(lines[[i]])
}
names <- rep(NA, length)
mouth.seg <- NA
mouth.vert <- NA
mouth <- list(mouth.seg, mouth.vert)
names(mouth) <- c("mouth.seg", "mouth.vert")
sequenced <- FALSE
braided <- NA
cumuldist <- list()
for (i in 1:length) {
xy <- lines[[i]]
n <- dim(xy)[1]
cumuldist[[i]] <- c(0, cumsum(sqrt(((xy[1:(n - 1), 1] -
xy[2:n, 1])^2) + ((xy[1:(n - 1), 2] - xy[2:n, 2])^2))))
}
out.names <- c("sp", "lineID", "lines", "connections", "lengths",
"names", "mouth", "sequenced", "tolerance", "units",
"braided", "cumuldist")
out <- list(sp, lineID, lines, connections, lengths, names,
mouth, sequenced, tolerance, units, braided, cumuldist)
names(out) <- out.names
class(out) <- "rivernetwork"
length1 <- length(out$lengths)
suppressMessages(out <- removeduplicates(out))
length2 <- length(out$lengths)
if (length2 < length1)
cat("\n", "Removed", length1 - length2, "duplicate segments.", "\n")
# THIS LINE CAUSES ISSUES COMENT IT OUT
# THIS LINE CAUSES ISSUES COMENT IT OUT
# suppressMessages(out <- removemicrosegs(out))
length3 <- length(out$lengths)
if (length3 < length2)
cat("\n", "Removed", length2 - length3, "segments with lengths shorter than the connectivity tolerance.", "\n")
return(out)
}
#创建自定义函数以读取文件。
#检查line2network函数并修改导致特定形状文件出现问题的线
my.custom.line2network=function(路径=“.”,图层,容差=100,重投影=NULL,供应投影=NULL){
sp我不熟悉riverdist
包或line2network()
函数,但是,可以稍微修改line2network()
使其在这种情况下工作。函数中有一行导致问题
# Create your custom function to read the file.
# Examine the line2network function and modify the lines that cause an issue with your specific shape file
my.custom.line2network = function (path = ".", layer, tolerance = 100, reproject = NULL, supplyprojection = NULL) {
sp <- suppressWarnings(rgdal::readOGR(dsn = path, layer = layer, verbose = F))
if (class(sp) != "SpatialLinesDataFrame")
stop("Specified shapefile is not a linear feature.")
if (is.na(sp@proj4string@projargs) & !is.null(supplyprojection))
sp@proj4string@projargs <- supplyprojection
if (is.na(sp@proj4string@projargs))
stop("Shapefile projection information is missing. Use supplyprojection= to specify a Proj.4 projection to use. If the input shapefile is in WGS84 geographic (long-lat) coordinates, this will be +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 (in double-quotes). If so, it must also be reprojected using reproject=.")
proj4 <- strsplit(sp@proj4string@projargs, split = " ")
projected <- sp::is.projected(sp)
if (is.null(reproject) & !projected)
stop("Distances can only be computed from a projected coordinate system. Use reproject= to specify a Proj.4 projection to use.")
if (!is.null(reproject)) {
sp <- sp::spTransform(sp, sp::CRS(reproject))
proj4 <- strsplit(sp@proj4string@projargs, split = " ")
}
units <- "unknown"
for (i in 1:length(proj4[[1]])) {
if (proj4[[1]][i] != "") {
proj4arg <- strsplit(proj4[[1]][i], split = "=")
if (proj4arg[[1]][1] == "+units") {
units <- proj4arg[[1]][2]
cat("\n", "Units:", proj4arg[[1]][2], "\n")
}
}
}
if (length(sp@lines) > 1) {
sp_line <- NA
sp_seg <- NA
lines <- list()
j <- 1
for (i in 1:length(sp@lines)) {
for (k in 1:length(sp@lines[i][[1]]@Lines)) {
lines[[j]] <- sp@lines[i][[1]]@Lines[[k]]@coords
sp_line[j] <- i
sp_seg[j] <- k
j <- j + 1
}
}
}
if (length(sp@lines) == 1) {
lines <- sp@lines[1][[1]]@Lines
length <- length(lines)
lines.new <- list()
for (i in 1:length) {
lines.new[[i]] <- lines[[i]]@coords
}
lines <- lines.new
sp_line <- rep(1, length)
sp_seg <- 1:length
}
length <- length(lines)
rivID <- 1:length
lineID <- data.frame(rivID, sp_line, sp_seg)
connections <- calculateconnections(lines = lines, tolerance = tolerance)
if (any(connections %in% 5:6))
braided <- TRUE
lengths <- rep(NA, length)
for (i in 1:length) {
lengths[i] <- pdisttot(lines[[i]])
}
names <- rep(NA, length)
mouth.seg <- NA
mouth.vert <- NA
mouth <- list(mouth.seg, mouth.vert)
names(mouth) <- c("mouth.seg", "mouth.vert")
sequenced <- FALSE
braided <- NA
cumuldist <- list()
for (i in 1:length) {
xy <- lines[[i]]
n <- dim(xy)[1]
cumuldist[[i]] <- c(0, cumsum(sqrt(((xy[1:(n - 1), 1] -
xy[2:n, 1])^2) + ((xy[1:(n - 1), 2] - xy[2:n, 2])^2))))
}
out.names <- c("sp", "lineID", "lines", "connections", "lengths",
"names", "mouth", "sequenced", "tolerance", "units",
"braided", "cumuldist")
out <- list(sp, lineID, lines, connections, lengths, names,
mouth, sequenced, tolerance, units, braided, cumuldist)
names(out) <- out.names
class(out) <- "rivernetwork"
length1 <- length(out$lengths)
suppressMessages(out <- removeduplicates(out))
length2 <- length(out$lengths)
if (length2 < length1)
cat("\n", "Removed", length1 - length2, "duplicate segments.", "\n")
# THIS LINE CAUSES ISSUES COMENT IT OUT
# THIS LINE CAUSES ISSUES COMENT IT OUT
# suppressMessages(out <- removemicrosegs(out))
length3 <- length(out$lengths)
if (length3 < length2)
cat("\n", "Removed", length2 - length3, "segments with lengths shorter than the connectivity tolerance.", "\n")
return(out)
}
#创建自定义函数以读取文件。
#检查line2network函数并修改导致特定形状文件出现问题的线
my.custom.line2network=function(路径=“.”,图层,容差=100,重投影=NULL,供应投影=NULL){
sp问题已解决。使用投影CRS保存我的river shp时出错。投影正确后,问题不再出现。问题已解决。使用投影CRS保存我的river shp时出错。投影正确后,问题不再出现。是否可能请提供一个链接,我们可以在其中下载Limpopo.shp
文件,以便我们可以尝试复制您的结果。这将有助于尝试找出解决方案。添加到原始帖子,谢谢!您是否能够使用我提供的答案中的代码获得所需的结果?不幸的是,没有。我编辑了原始帖子(不确定我是否应该发布一个答案?)。我认为我的shp文件投影是个问题。当文件加载到GIS上时,它是不正确的。是否可以提供一个链接,我们可以下载Limpopo.shp
文件,以便我们可以尝试复制您的结果?这将有助于找到解决方案。添加到原始帖子中,谢谢!您是否能够o使用我提供的答案中的代码获得所需的结果?不幸的是,没有。我编辑了原始帖子(不确定我是否应该发布答案?)。但我认为我的shp文件投影是个问题。当文件加载到GIS上时,它不正确。
# Create your custom function to read the file.
# Examine the line2network function and modify the lines that cause an issue with your specific shape file
my.custom.line2network = function (path = ".", layer, tolerance = 100, reproject = NULL, supplyprojection = NULL) {
sp <- suppressWarnings(rgdal::readOGR(dsn = path, layer = layer, verbose = F))
if (class(sp) != "SpatialLinesDataFrame")
stop("Specified shapefile is not a linear feature.")
if (is.na(sp@proj4string@projargs) & !is.null(supplyprojection))
sp@proj4string@projargs <- supplyprojection
if (is.na(sp@proj4string@projargs))
stop("Shapefile projection information is missing. Use supplyprojection= to specify a Proj.4 projection to use. If the input shapefile is in WGS84 geographic (long-lat) coordinates, this will be +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 (in double-quotes). If so, it must also be reprojected using reproject=.")
proj4 <- strsplit(sp@proj4string@projargs, split = " ")
projected <- sp::is.projected(sp)
if (is.null(reproject) & !projected)
stop("Distances can only be computed from a projected coordinate system. Use reproject= to specify a Proj.4 projection to use.")
if (!is.null(reproject)) {
sp <- sp::spTransform(sp, sp::CRS(reproject))
proj4 <- strsplit(sp@proj4string@projargs, split = " ")
}
units <- "unknown"
for (i in 1:length(proj4[[1]])) {
if (proj4[[1]][i] != "") {
proj4arg <- strsplit(proj4[[1]][i], split = "=")
if (proj4arg[[1]][1] == "+units") {
units <- proj4arg[[1]][2]
cat("\n", "Units:", proj4arg[[1]][2], "\n")
}
}
}
if (length(sp@lines) > 1) {
sp_line <- NA
sp_seg <- NA
lines <- list()
j <- 1
for (i in 1:length(sp@lines)) {
for (k in 1:length(sp@lines[i][[1]]@Lines)) {
lines[[j]] <- sp@lines[i][[1]]@Lines[[k]]@coords
sp_line[j] <- i
sp_seg[j] <- k
j <- j + 1
}
}
}
if (length(sp@lines) == 1) {
lines <- sp@lines[1][[1]]@Lines
length <- length(lines)
lines.new <- list()
for (i in 1:length) {
lines.new[[i]] <- lines[[i]]@coords
}
lines <- lines.new
sp_line <- rep(1, length)
sp_seg <- 1:length
}
length <- length(lines)
rivID <- 1:length
lineID <- data.frame(rivID, sp_line, sp_seg)
connections <- calculateconnections(lines = lines, tolerance = tolerance)
if (any(connections %in% 5:6))
braided <- TRUE
lengths <- rep(NA, length)
for (i in 1:length) {
lengths[i] <- pdisttot(lines[[i]])
}
names <- rep(NA, length)
mouth.seg <- NA
mouth.vert <- NA
mouth <- list(mouth.seg, mouth.vert)
names(mouth) <- c("mouth.seg", "mouth.vert")
sequenced <- FALSE
braided <- NA
cumuldist <- list()
for (i in 1:length) {
xy <- lines[[i]]
n <- dim(xy)[1]
cumuldist[[i]] <- c(0, cumsum(sqrt(((xy[1:(n - 1), 1] -
xy[2:n, 1])^2) + ((xy[1:(n - 1), 2] - xy[2:n, 2])^2))))
}
out.names <- c("sp", "lineID", "lines", "connections", "lengths",
"names", "mouth", "sequenced", "tolerance", "units",
"braided", "cumuldist")
out <- list(sp, lineID, lines, connections, lengths, names,
mouth, sequenced, tolerance, units, braided, cumuldist)
names(out) <- out.names
class(out) <- "rivernetwork"
length1 <- length(out$lengths)
suppressMessages(out <- removeduplicates(out))
length2 <- length(out$lengths)
if (length2 < length1)
cat("\n", "Removed", length1 - length2, "duplicate segments.", "\n")
# THIS LINE CAUSES ISSUES COMENT IT OUT
# THIS LINE CAUSES ISSUES COMENT IT OUT
# suppressMessages(out <- removemicrosegs(out))
length3 <- length(out$lengths)
if (length3 < length2)
cat("\n", "Removed", length2 - length3, "segments with lengths shorter than the connectivity tolerance.", "\n")
return(out)
}
limpopo <- my.custom.line2network(path = "/Volumes/Shadowfax/Distribution/simple rivers/Limpopo.shp",
layer = "Limpopo",
tolerance = 100,
reproject = NULL,
supplyprojection = NULL)