在R分割折线与折线

时间:2017-05-04 08:50:02

标签: r sp maptools

我有一条折线我想分成几段。并创建一个新的折线。 折线需要分割的位置是与另一条折线相交的位置。

library(sp)
library(maptools)
library(rgeos)

dir_shp     <- paste("g:/.../Stackoverflow_example/")
naam_shp    <- paste("traject_A")

traject_A   <- readShapeLines(paste(dir_shp, layer=naam_shp, sep=""))
Split_loc   <- readShapeLines(paste(dir_shp,"Cross_section" , sep=""))
InterS1     <- gIntersection(traject_A, Split_loc)

shapefile traject_A如下所示:

> traject_A
An object of class "SpatialLinesDataFrame"
Slot "data":
  Id
0  0

Slot "lines":
[[1]]
An object of class "Lines"
Slot "Lines":
[[1]]
An object of class "Line"
Slot "coords":
          [,1]     [,2]
 [1,] 62706.30 382326.9
 [2,] 63055.55 382049.6
 [3,] 63265.10 382070.7
 [4,] 63483.12 382045.3
 [5,] 63648.22 381797.7
 [6,] 63950.90 381816.7
 [7,] 64158.34 382000.9
 [8,] 64245.12 381886.6
 [9,] 64253.59 381770.2

Slot "ID":
[1] "0"

Slot "bbox":
       min       max
x  62706.3  64253.59
y 381770.2 382326.85

Slot "proj4string":
CRS arguments: NA 

InterS1给出了线相交的位置。

> InterS1
SpatialPoints:
         x        y
1 62806.00 382247.7
1 62966.51 382120.3
1 63180.03 382062.1
1 63371.13 382058.4
1 63563.55 381924.7
1 63697.28 381800.8
1 63950.98 381816.8
1 64193.49 381954.6
1 64249.98 381819.8
Coordinate Reference System (CRS) arguments: NA

下一个关键是我迷失的地方。

如何使用这些点将我的折线traject_A分割为新的shapefile。

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

从其他地方转贴。只要回想一下,您就可以了解将多边形转换为折线的信息。

library(sf)
A <- st_as_sfc("LINESTRING(458.1 768.23, 455.3 413.29, 522.3 325.77, 664.8 282.01, 726.3 121.56)")
B <- st_as_sfc("MULTIPOLYGON(((402.2 893.03, 664.8 800.65, 611.7 666.13, 368.7 623.99, 215.1 692.06, 402.2 893.03)), ((703.9 366.29, 821.2 244.73, 796.1  25.93, 500.0 137.76, 703.9 366.29)))")

## Convert the MULTIPOLYGON to a MULTILINESTRING object
BB <- st_cast(B, "MULTILINESTRING", group_or_split=FALSE)

## Break LINESTRING A into segments by using:
## - st_intersection() to find points at which lines features intersect
## - st_buffer() to convert points to tiny polygons with some width
## - st_difference() to break line up into sections not overlapping tiny polygons
C <- st_difference(A, st_buffer(st_intersection(A,BB), dist=1e-12))