On the line of segments, find the point, which is x units along the line

Assume a segment string of class SpatialLines length len . This line starts in the upper left corner.

 library(sp) x <- structure(list(x = c(-7.23437435517476, 6.35937810318614, -5.86718660792582, 7.96094089282062), y = c(7.08139459814975, 6.8633712983227, -7.61337581019376, -6.2180266913006)), .Names = c("x", "y")) xline <- SpatialLines(list(Lines(Line(x), ID = 1))) #len <- LineLength(as.matrix(data.frame(x))) len <- LineLength(as.matrix(data.frame(coordinates(xline)))) plot(0,0, xlim = c(-10, 10), ylim = c(-10, 10), type = "n") lines(xline) 

find point on a line at desired distance from start

I would like to find a point on this line that is at a distance findme from the beginning of the line. For example, if I were looking for a point with 10 units along the line from the beginning, I would get a point near the node between the first and second segments. Your contribution to a more reliable solution is most welcome.

I tried to find it using spsample (see below), but this method is (also) unreliable and does not work for points in the second half of the line.

 # very approximate method, not very suitable findme <- 11 # 11, 12 and 13 give same result segs <- 1/(findme/xline.length) xsam <- spsample(x = xline, n = segs, type = "regular", offset = 0) points(xsam) 
+4
source share
2 answers

The following steps will help you find the coordinates.

General line information:

 library(np) coord <- coordinates(xline)[[1]][[1]] nLines <- nrow(coord) - 1 #lengths <- sapply(seq_len(nLines), function(x) LineLength(coord[c(x, x + 1), ])) lengths <- LineLength(coord, sum = FALSE) 

Find new coordinates:

 findme <- 11 # the distance of the new coordinates distances <- cumsum(lengths) - findme # distances from the nodes segment <- which(distances >= 0)[1] # the segment of interest distToNode <- distances[segment] ratio <- distToNode / lengths[segment] segCoord <- coord[c(segment, segment + 1), ] newCoord <- (1 - ratio) * segCoord[2 , ] + ratio * segCoord[1 , ] 

Plot:

 points(newCoord[1], newCoord[2]) 

enter image description here

+4
source

Could you:

  • Define Segment Order
  • Determine the length of each segment
  • Use order and length to determine which segment the point is on.
  • Subtract the beginning of the segment from the length of the order * (for example, if you know that you are on the third segment, you subtract the length of the point at which the third and second segments meet (this is just the sum of segments 1 and 2) from the beginning of the entire line). So, if you tried to find a point in 7 units from the beginning of the line, and the sum of segments 1 and 2 was 5, you would have 2 left.
  • Then simply use this method to deal with the only remaining segment to determine that the point is 2 units below that segment.

I apologize if this is not a complete solution. But perhaps this will give you an approach that is allowed.

Hope this helps ...

0
source

Source: https://habr.com/ru/post/1445816/


All Articles