gpt4 book ai didi

r - 如何将折线形状文件分割为等长的较小段?

转载 作者:行者123 更新时间:2023-12-02 15:46:24 25 4
gpt4 key购买 nike

我有一个折线形状文件,它代表城市道路网的一部分。我的 shapefile 包含多个线路/街道段(在我的例子中为 58)。通过使用 R-cran,我想进一步将折线段划分为具有相同长度(例如 10 m)的较小部分。

提供一个想法:

当我将折线形状文件导入 R 并创建一个数据框时,它看起来像:

# Import the polyline shapefile into a SpatialLinesDataFrame object:
# library(sp)
sp.roads <- readOGR(dsn="/Users/mgv/Documents/project",layer="roads_sr_corr")

# Create a dataframe from the SpatialLinesDataFrame
# library(stplanr)
linedf <- line2df(sp.roads)
str(linedf)
> str(linedf)
'data.frame': 58 obs. of 4 variables:
$ fx: num 13.39991 13.40138 13.40606 13.40232 13.40177 ...
$ fy: num 42.35066 42.35412 42.35599 42.34514 42.34534 ...
$ tx: num 13.40150 13.40119 13.40591 13.40246 13.40182 ...
$ ty: num 42.35026 42.35386 42.35602 42.34530 42.34525 ...

其中 (fx, fy, tx, ty) 分别是点 (x,y)_from 和 (x,y)_to 的经度和纬度,界定每个线段(此处为 5 个)。

这个想法是获得一个更密集的折线形状文件,我可以将其用作“某种网格”进行空间分析,以将沿道路收集的地理引用数据点与每个路段相关联。

非常感谢您的帮助以及解决此问题的任何建议。

最佳答案

以下函数将空间线对象的每条线划分为 split_length 长度加上剩余线段的段。它使用线段的向量表示法,创建一个单位长度和要分割的线方向的向量u,可以将其相乘以创建由许多线段组成的更长的线(herehere引用文献我用过)。

SplitLines = function(spatial_line,
split_length = 20,
return.dataframe = F,
plot.results = F) {
# The function splits each line of the spatial line object into segments of a given length
# spatial_line: a spatial line object
# split_length: the length of the segments to split the lines into, in units of the SpatialLine object
# return.dataframe: if true it returns the segments in the form of a dataframe, otherwise in a SpatialLine object
# plot.results:
require(sp)
#### Define support functions ####
# SpatialLines2df extracts start and end point coordinates of each segment of a SpatialLine object
# spatial_line: an object class SpatialLinesDataFrame of the package sp
SpatialLines2df = function(spatial_line) {
df = data.frame(
id = character(),
mline_id = character(),
segment_id = character(),
fx = numeric(),
fy = numeric(),
tx = numeric(),
ty = numeric(),
stringsAsFactors = FALSE
)
for (i in 1:length(spatial_line)) {
coords = spatial_line@lines[[i]]@Lines[[1]]@coords # For each line takes the coords of the vertex
row_nums = 1:(nrow(coords) - 1)
mline_id = formatC(i, width = 9, flag = '0') # Creates id for the line
segment_id = formatC(row_nums, width = 3, flag = '0') # Creates id for each single segment belonging to the line
id = paste0(mline_id, '_', segment_id) # Creates a composite id
for (j in row_nums) {
# For each segment stores ids and coordinates
df[nrow(df) + 1, ] = c(id[j],
mline_id,
segment_id[j],
coords[j, 1],
coords[j, 2],
coords[j + 1, 1],
coords[j + 1, 2])
}
}
row.names(df) = NULL
df$fx = as.numeric(df$fx)
df$fy = as.numeric(df$fy)
df$tx = as.numeric(df$tx)
df$ty = as.numeric(df$ty)
return(df)
}

# linedf2SpatialLines converts a dataframe of IDs and coordinates into a spatial line
# linedf: a data.frame with columns as:
# id = generic ids of the lines,
# fx = coordinates x of the first point of the line
# fy = coordinates y of the first point of the line
# tx = coordinates x of the second point of the line
# tx = coordinates y of the second point of the line

require(sp)
linedf2SpatialLines = function(linedf) {
sl = list()
for (i in 1:nrow(linedf)) {
c1 = cbind(rbind(linedf$fx[i], linedf$tx[i]),
rbind(linedf$fy[i], linedf$ty[i]))
l1 = Line(c1)
sl[[i]] = Lines(list(l1), ID = linedf$id[i])
}
SL = SpatialLines(sl)
return(SL)
}


#### Split the lines ####
# Convert the input SpatialLine object into a dataframe and create an empty output dataframe
linedf = SpatialLines2df(spatial_line)
df = data.frame(
id = character(),
fx = numeric(),
fy = numeric(),
tx = numeric(),
ty = numeric(),
stringsAsFactors = FALSE
)


for (i in 1:nrow(linedf)) {
# For each line of the dataframe, corresponding to a single line of the spatial object
# skips if length is less then split_length
v_seg = linedf[i, ]
seg_length = sqrt((v_seg$fx - v_seg$tx) ^ 2 + (v_seg$fy - v_seg$ty) ^
2) # segment length
if (seg_length <= split_length) {
df[nrow(df) + 1,] = c(paste0(v_seg$id, '_', '0000'),
v_seg$fx,
v_seg$fy,
v_seg$tx,
v_seg$ty)
next()
}
# Create a vector of direction as the line and unit length
# vector v corresponding to the line
v = c(v_seg$tx - v_seg$fx,
v_seg$ty - v_seg$fy)
# vector of direction v and unit length
u = c(v[1] / sqrt(v[1] ^ 2 + v[2] ^ 2), v[2] / sqrt(v[1] ^ 2 + v[2] ^ 2))
# Calculates how many segment the line is split into and the leftover
num_seg = floor(seg_length / split_length)
seg_left = seg_length - (num_seg * split_length)

# Add to the output dataframe each segment plus the leftover
for (i in 0:(num_seg - 1)) {
# Add num_seg segments
df[nrow(df) + 1,] = c(
paste0(v_seg$id, '_', formatC(i, width = 4, flag = '0')),
v_seg$fx + u[1] * split_length * i,
v_seg$fy + u[2] * split_length * i,
v_seg$fx + u[1] * split_length * (i + 1),
v_seg$fy + u[2] * split_length * (i + 1)
)
}
df[nrow(df) + 1,] = c(
paste0(v_seg$id, '_', formatC(
num_seg, width = 4, flag = '0'
)),
# Add leftover segment
v_seg$fx + u[1] * split_length * num_seg,
v_seg$fy + u[2] * split_length * num_seg,
v_seg$tx,
v_seg$ty
)

}

#### Visualise the results to check ####
if (plot.results) {
plot(spatial_line)
coords = cbind(as.numeric(df$fx), as.numeric(df$fy))
coords = rbind(coords, as.numeric(df$tx[nrow(df)]), as.numeric(df$ty)[nrow(df)])
sp_points = SpatialPoints(coords)
plot(sp_points, col = 'red', add = T)
}

#### Output ####
df$fx = as.numeric(df$fx)
df$fy = as.numeric(df$fy)
df$tx = as.numeric(df$tx)
df$ty = as.numeric(df$ty)
if (return.dataframe) {
return(df)
} # Return a dataframe
sl = linedf2SpatialLines(df)
return(sl) # Return a SpatialLine object
}

您可以使用以下命令测试该功能:

Sl = SpatialLines(list(Lines(list(Line(cbind(c(1,2,3, 4),c(3,2,2,4)))), ID="a")))
plot(Sl)
Sl_split = SplitLines(Sl, split_length = 0.1, return.dataframe = FALSE, plot.results = TRUE)

我相信这个函数可以写得更加简洁和高效。我在 git repository 中创建了一个包,以防有人愿意贡献。

关于r - 如何将折线形状文件分割为等长的较小段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38700246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com