[R-sig-Geo] split SLDF with attributes

marta azores martazores at gmail.com
Wed Mar 1 15:15:27 CET 2017


*Hi Melanie,*

*Sorry for the delayed response. Your loop made exactly what I want it.
Thanks Mel, it's really helpful.*
*Now, I'm trying to manipulated the function last.merge to join my segments
by length. My aim is dividing the transects by 10km (n segments). However,
If the last segment is shorter than 5km, I have two options: *


*option 1)W*e can distribute the remainder among the n segments
(segments>10km).
raw-lines: 10+10+2
output-1:   11+11


*option 2)*  We can add 1 segment(n+1), and divided by segments smaller
than 10km.
raw-lines: 10+10+7
process: 27/3
output-2:     9+9+9

*I think my main aim is still far away. Then I decided to start with
something simple, the function **SegmentSpatialLines split the segments *
*effectively* *in the 10km and merge the last segment with the penultimate.
**I thought it would be easy define a cut-off to decide if the shorter
segments can be merge, but I was wrong.*

*1) First,* the transects were divided by length(10km) into segments.
segments:
10+10+6+10+3

*2) Second*, the length of transects is not a multiple of given
length(10km), if last segment is shorter we can merge it with penultimate
segment, if we wish(merge.last=T).
10+16+13

*3) Third*, I would like merge the sorter last segment with the penultimate
segment, only if the length is <5km.
segments:
10+10+null+13

*#I try to include a cut-off with these modifications:*
#*A)*      if ((merge.last && length(segments) <5000))#5km cut-off
##But the output of A was the same as if ((merge.last && length(segments)
>1)) and merge.last=F
#
I thought may be the length(segments) here is only to give you the nrow and
not the length of the segments. And I decided to use other ways to
calculate the length. The funcion gLenght of the rgeos package, it's not
possible because the segments in the function SegmentSpatialLines are in a
list. I also projected the segments on the loop before the last.merge, but
without success.  Then I try an other option with the total_lenght.
#*B)*
##change segmentspatilines function
SegmentSpatialLines <- function(sl, length = 0, n.parts = 0, merge.last =
F) {
 stopifnot((length > 0 || n.parts > 0))
 id <- 0
 newlines <- list()
 sl <- as(sl, "SpatialLines")
 for (lines in sl at lines) {
   for (line in lines at Lines) {
     crds <- line at coords
     # create segments
     segments <- CreateSegments(coords = crds, length, n.parts)
     # calculate total length line
       total_length <- 0
     for (i in 1:(nrow(crds) - 1)) {
       d <- sqrt((crds[i, 1] - crds[i + 1, 1])^2 + (crds[i, 2] - crds[i +
1, 2])^2)
       total_length <- total_length + d
     }

     if ((merge.last && length(segments) >1) &
(total_length(segments)<5000))

       {
       # 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))
}




##But the output of B was the same as if ((merge.last && length(segments)
>1)) and merge.last=F

Thanks in advance,

Marta

>
>>
>

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list