我找到了一篇不错的博客文章,详细介绍了它,Segmentation of spatial lines。基本上你将你的多线串转换成空间线,然后在博客中描述了很多很好的函数来发挥作用。
转换为空间线:
library(rgeos)
spatialLines <- readWKT(multiline)
功能一:创建线段
CreateSegment <- function(coords, from, to) {
distance <- 0
coordsOut <- c()
biggerThanFrom <- F
for (i in 1:(nrow(coords) - 1)) {
d <- sqrt((coords[i, 1] - coords[i + 1, 1])^2 + (coords[i, 2] - coords[i +
1, 2])^2)
distance <- distance + d
if (!biggerThanFrom && (distance > from)) {
w <- 1 - (distance - from)/d
x <- coords[i, 1] + w * (coords[i + 1, 1] - coords[i, 1])
y <- coords[i, 2] + w * (coords[i + 1, 2] - coords[i, 2])
coordsOut <- rbind(coordsOut, c(x, y))
biggerThanFrom <- T
}
if (biggerThanFrom) {
if (distance > to) {
w <- 1 - (distance - to)/d
x <- coords[i, 1] + w * (coords[i + 1, 1] - coords[i, 1])
y <- coords[i, 2] + w * (coords[i + 1, 2] - coords[i, 2])
coordsOut <- rbind(coordsOut, c(x, y))
break
}
coordsOut <- rbind(coordsOut, c(coords[i + 1, 1], coords[i + 1,
2]))
}
}
return(coordsOut)
}
功能 2:创建线段
CreateSegments <- function(coords, length = 0, n.parts = 0) {
stopifnot((length > 0 || n.parts > 0))
# calculate total length line
total_length <- 0
for (i in 1:(nrow(coords) - 1)) {
d <- sqrt((coords[i, 1] - coords[i + 1, 1])^2 + (coords[i, 2] - coords[i +
1, 2])^2)
total_length <- total_length + d
}
# calculate stationing of segments
if (length > 0) {
stationing <- c(seq(from = 0, to = total_length, by = length), total_length)
} else {
stationing <- c(seq(from = 0, to = total_length, length.out = n.parts),
total_length)
}
# calculate segments and store the in list
newlines <- list()
for (i in 1:(length(stationing) - 1)) {
newlines[[i]] <- CreateSegment(coords, stationing[i], stationing[i +
1])
}
return(newlines)
}
功能3:合并到最后一条线段
MergeLast <- function(lst) {
l <- length(lst)
lst[[l - 1]] <- rbind(lst[[l - 1]], lst[[l]])
lst <- lst[1:(l - 1)]
return(lst)
}
功能4:将它们放在一起,可以将空间线切割成n-1线或一定长度的线
SegmentSpatialLines <- function(sl, length = 0, n.parts = 0, merge.last = FALSE) {
stopifnot((length > 0 || n.parts > 0))
id <- 0
newlines <- list()
sl <- as(sl, "SpatialLines")
for (lines in sl@lines) {
for (line in lines@Lines) {
crds <- line@coords
# create segments
segments <- CreateSegments(coords = crds, length, n.parts)
if (merge.last && length(segments) > 1) {
# in case there is only one segment, merging would result into error
segments <- MergeLast(segments)
}
# transform segments to lineslist for SpatialLines object
for (segment in segments) {
newlines <- c(newlines, Lines(list(Line(unlist(segment))), ID = as.character(id)))
id <- id + 1
}
}
}
return(SpatialLines(newlines))
}
同样,所有功劳归于函数的创建者:Creator