I have got an excel sheet with X and Y coordinates, actually Y is constant, since it is a straight line. I want a Shapefile with as many features as distance between points. I only get one line from the whole transect by using Linestring, but I need every segment, I tried with Multilinestring, but sincerely, I'm completely a beginner and after 2 days trying I couldn't get it.


x<- c(P$x)
y<- c(P$y)
coord2<- cbind(x,y)
L1<- st_linestring(coord2)
plot(L1, axes = TRUE)
geometria7 <- st_sfc(L1)
plot(geometria7, axes = TRUE)
st_write(geometria7, "proof.shp")

raw dat

  • So just to be clear for your sample 9 points you want to get 9x8/2 = 36 line segments?
    – Spacedman
    Sep 20, 2018 at 16:32
  • the values are the length of the canopy of each specie, in 15 transects of 50 meters each, so, it could be thousands segments, I need all of them features in one Shapefile.
    – MIA
    Sep 21, 2018 at 11:14
  • what "values"? I don't see any values, only points. You've shown us 9 data points, and if that was all your data, would you expect to get 36 line segments?
    – Spacedman
    Sep 21, 2018 at 11:56

1 Answer 1


First make some random points for testing - its probably hard to tell whats going in with points in a straight line, so I'll make some random points:

pts = cbind(runif(6),runif(6))

next work out which points we need to combine to generate (in this case) the 6x5/2 = 15 features using the handy combn function:

> combo = combn(nrow(pts),2)
> combo
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
[1,]    1    1    1    1    1    2    2    2    2     3     3     3     4     4
[2,]    2    3    4    5    6    3    4    5    6     4     5     6     5     6
[1,]     5
[2,]     6

next we iterate over the columns of that matrix, taking corresponding rows from the points, and making an st_linestring object - the lapply gives us a list which we turn into a simple features column:

slines = do.call(

So we should see six points, each with five connecting lines:

> plot(slines)
> points(pts,pch=19,col="red")

and make a shapefile:

> st_write(slines, "/tmp/foobar.shp")
Writing layer `foobar' to data source `/tmp/foobar.shp' using driver `ESRI Shapefile'
features:       15
fields:         0
geometry type:  Line String
  • Super interesting. but unfortunately this creates too many combinations following this method. As I have already achieved the points and a single line, therefore I have been able to use ArcMap tool "split line at point". it would be possible to do it with an R sprip?
    – MIA
    Sep 21, 2018 at 12:20
  • So if your complete data set is 9 points, you want 8 features, each one a line segment connecting point N with point N+1?
    – Spacedman
    Sep 21, 2018 at 13:08
  • yes, exactly that. Actually my complete data set is 56 points, I would need 55 line features. Thanks!!!
    – MIA
    Sep 21, 2018 at 13:24
  • ah, you said "as many features as distance between points", and between N points there are N*(N-1)/2 distances, so I though you wanted lines between every pair of points. I'll redo.
    – Spacedman
    Sep 21, 2018 at 13:30
  • no, it's not exactly like that. What I did in my field work was to write down the length of each specie in a 50 meters straight transect. I also have the length of each segment (could be better or the coordinates?. So I need to transform this data to Shapefile, and then,put it on a multispectral image. I mean if I wrote down 100 specie lengths, I'll will have 100 lengths or 101 points. Thank you very much :)
    – MIA
    Sep 24, 2018 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.