1
votes

I have two shapefiles of type LINESTRING.

One is a list of 9 lines between Object A and Object B.

The other is a list of 18 lines between Object B and Object C.

For linestrings that share the same Object B coordinates, I want to find the distance between the corresponding Object A coordinates and Object C coordinates.

For example;

Object B to Object C linestrings: LINESTRING(21 15, 40.5 10.5) LINESTRING(23 15,40.5 10.5)

Object B to Object A linestrings: LINESTRING(53 11, 40.5 10.5)

Then I would like to calculate the distance between the coordinates (21, 15) and (53,11) and calculate the distance between (23, 15) and (53,11)

To reproduce my LINESTRING shapefiles, you can run the following code:

Object B to Object A

ObjectBtoA <-structure(list(structure(c(53, 40.5, 11, 10.5), .Dim = c(2L, 
2L), class = c("XY", "LINESTRING", "sfg")), structure(c(53, 41.5, 
11, 10.5), .Dim = c(2L, 2L), class = c("XY", "LINESTRING", "sfg"
)), structure(c(53, 42.5, 11, 10.5), .Dim = c(2L, 2L), class = c("XY", 
"LINESTRING", "sfg")), structure(c(53, 40.5, 11.5, 11.5), .Dim = c(2L, 
2L), class = c("XY", "LINESTRING", "sfg")), structure(c(53, 41.5, 
11.5, 11.5), .Dim = c(2L, 2L), class = c("XY", "LINESTRING", 
"sfg")), structure(c(53, 42.5, 11.5, 11.5), .Dim = c(2L, 2L), class = c("XY", 
"LINESTRING", "sfg")), structure(c(53, 40.5, 12.5, 12.5), .Dim = c(2L, 
2L), class = c("XY", "LINESTRING", "sfg")), structure(c(53, 41.5, 
12.5, 12.5), .Dim = c(2L, 2L), class = c("XY", "LINESTRING", 
"sfg")), structure(c(53, 42.5, 12.5, 12.5), .Dim = c(2L, 2L), class = c("XY", 
"LINESTRING", "sfg"))), n_empty = 0L, precision = 0, crs = structure(list(
input = "EPSG:4326", wkt = "GEOGCS[\"WGS 84\",\n    DATUM[\"WGS_1984\",\n        
SPHEROID[\"WGS 84\",6378137,298.257223563,\n            AUTHORITY[\"EPSG\",\"7030\"]],\n        
AUTHORITY[\"EPSG\",\"6326\"]],\n    PRIMEM[\"Greenwich\",0,\n        
AUTHORITY[\"EPSG\",\"8901\"]],\n    UNIT[\"degree\",0.0174532925199433,\n        
AUTHORITY[\"EPSG\",\"9122\"]],\n    AUTHORITY[\"EPSG\",\"4326\"]]"), class = "crs"), class = 
c("sfc_LINESTRING", 
"sfc"), bbox = structure(c(xmin = 40.5, ymin = 10.5, xmax = 53, 
ymax = 12.5), class = "bbox"))

Object B to Object C:

structure(list(structure(c(21, 40.5, 15, 10.5), .Dim = c(2L, 
2L), class = c("XY", "LINESTRING", "sfg")), structure(c(21, 41.5, 
15, 10.5), .Dim = c(2L, 2L), class = c("XY", "LINESTRING", "sfg"
)), structure(c(21, 42.5, 15, 10.5), .Dim = c(2L, 2L), class = c("XY", 
"LINESTRING", "sfg")), structure(c(21, 40.5, 15, 11.5), .Dim = c(2L, 
2L), class = c("XY", "LINESTRING", "sfg")), structure(c(21, 41.5, 
15, 11.5), .Dim = c(2L, 2L), class = c("XY", "LINESTRING", "sfg"
 )), structure(c(21, 42.5, 15, 11.5), .Dim = c(2L, 2L), class = c("XY", "LINESTRING", 
"sfg")), structure(c(21, 40.5, 15, 12.5), .Dim = c(2L, 
2L), class = c("XY", "LINESTRING", "sfg")), structure(c(21, 41.5, 
15, 12.5), .Dim = c(2L, 2L), class = c("XY", "LINESTRING", "sfg"
)), structure(c(21, 42.5, 15, 12.5), .Dim = c(2L, 2L), class = c("XY", 
"LINESTRING", "sfg")), structure(c(23, 40.5, 15, 10.5), .Dim = c(2L, 
2L), class = c("XY", "LINESTRING", "sfg")), structure(c(23, 41.5, 
15, 10.5), .Dim = c(2L, 2L), class = c("XY", "LINESTRING", "sfg"
)), structure(c(23, 42.5, 15, 10.5), .Dim = c(2L, 2L), class = c("XY", 
"LINESTRING", "sfg")), structure(c(23, 40.5, 15, 11.5), .Dim = c(2L, 
2L), class = c("XY", "LINESTRING", "sfg")), structure(c(23, 41.5, 
15, 11.5), .Dim = c(2L, 2L), class = c("XY", "LINESTRING", "sfg"
)), structure(c(23, 42.5, 15, 11.5), .Dim = c(2L, 2L), class = c("XY", 
"LINESTRING", "sfg")), structure(c(23, 40.5, 15, 12.5), .Dim = c(2L, 
2L), class = c("XY", "LINESTRING", "sfg")), structure(c(23, 41.5, 
15, 12.5), .Dim = c(2L, 2L), class = c("XY", "LINESTRING", "sfg"
)), structure(c(23, 42.5, 15, 12.5), .Dim = c(2L, 2L), class = c("XY", 
"LINESTRING", "sfg"))), n_empty = 0L, precision = 0, crs = structure(list(
input = "EPSG:4326", wkt = "GEOGCS[\"WGS 84\",\n    DATUM[\"WGS_1984\",\n        
SPHEROID[\"WGS 84\",6378137,298.257223563,\n            AUTHORITY[\"EPSG\",\"7030\"]],\n        
AUTHORITY[\"EPSG\",\"6326\"]],\n    PRIMEM[\"Greenwich\",0,\n        
AUTHORITY[\"EPSG\",\"8901\"]],\n    UNIT[\"degree\",0.0174532925199433,\n        
AUTHORITY[\"EPSG\",\"9122\"]],\n    AUTHORITY[\"EPSG\",\"4326\"]]"), class = "crs"), class = 
c("sfc_LINESTRING", 
"sfc"), bbox = structure(c(xmin = 21, ymin = 10.5, xmax = 42.5, 
ymax = 15), class = "bbox"))
1

1 Answers

0
votes

Since this question lingered for a few days without any answers, I decided to take a look at it.

This is actually an interesting question. To make this answer simpler, rather than using the many lines from your data, I answered this question using just a few linestrings created from some points.

Regarding the question: "For linestrings that share the same Object B coordinates, I want to find the distance between the corresponding Object A coordinates and Object C coordinates."

Create the points and lines:

# Create the lines
x1 <- c(2,3,4)    # str line 1
y1 <- c (3,3,3)
y2 <- c (4,4,4)

x2 <- c(x1 - 5)  # str line 2
x3 <- c(x1-2)    # str line 3
ya <- c(3,4,5)    # diagonal line

l1 <- matrix(data = c(x1, y1), nrow = 3, ncol = 2)
l2 <- matrix(data = c(x2, y1), nrow = 3, ncol = 2)
l3 <- matrix(data = c(x3, y1), nrow = 3, ncol = 2)
l4 <- matrix(data = c(x1, ya), nrow = 3, ncol = 2)

Create the simple feature linestring (sfc) objects:

l1 <- st_linestring(l1, dim = "XY")
l2 <- st_linestring(l2, dim = "XY")
l3 <- st_linestring(l3, dim = "XY")
l4 <- st_linestring(l4, dim = "XY")


g1 <- c(l1, l3)                     # new var with multiple linestrings
g2 <- c(l1, l3, l4)                  # new var with multiple linestrings

Plot the new variables:

par(mfrow=c(1,2))    # set the plotting area into a 1*2 array
plot(g1, axes = T)       # plot  horizontal line
plot(g2, axes = T)        # plot horizontal + diag line

Determine the length of the linestrings:

st_length(g1)
st_length(g2)

    4
    6.82842712474619

So this answer shows when two or more linestrings are assigned to a variable and when these linestrings share a common coordinate, the resulting length is the same as joining the strings. It does not matter whether the line is straight or not. The variable, g2, shares a common point with three linestrings. The value that was returned, 6.82, can be proofed. Just find the length of the hypotenuse using the Pythagorean theorem and add that value to the length of the two other horizontal linestring segments: ( 2 + 2 + 2.828)

The plots can be viewed at the link below

[![ plots1 ]