# Averaging polylines from a number of lines

I have a number of polylines with different length, direction, and curvature. I want to create an average or ensemble of the polylines.

What I previously did in ArcGIS 10.1 is to convert the polylines to points in a cell size (25 km) that is quite larger than the study area (10). This way, I could "average" the points. Then I converted the raster cells back into lines. In a way they were "averaged" but the lines are very straight and formed small box-like in the intersection following the cell size.

I have read some solutions using R. I am interested to be provided with suggestions to do this in R.

Use the package in my github repository: https://gitlab.com/b-rowlingson/meanline

Here's an example usage. First make some data - it is a list of two-column matrices of x-y coordinates in the columns:

`````` > xylist = list(cbind(1:10, rnorm(10)), cbind(1:10, rnorm(10)), cbind(1:10, rnorm(10)))
``````

Then:

`````` > xyaverage = average_lines(xylist)
``````

Here's the lines and the average line generated by the above:

``````> plot(do.call(rbind,xylist), type="n")
> lapply(xylist,lines)
> lines(xyaverage, col="red")
``````

• Where in the sample code do I input the shapefile (polyline)? Thanks. – user2543 Apr 13 '18 at 15:46
• You don't, you create a list of line segments as matrices. If you need to know how to create a list of line segments as matrices from a shapefile, that is a separate question. Do you know how to read a shapefile into R? That's another question. Those questions may already have answers here, or elsewhere. – Spacedman Apr 13 '18 at 16:44
• I know how to read a shapefile into R by readOGR but the line segments as matrices, no. How do I do that? – user2543 Apr 13 '18 at 16:53
• Create a new question, give as much detail as possible about your data and maybe link to it. Refer to this question if it helps. You may even find the question has already been asked and the answer is on here already. – Spacedman Apr 13 '18 at 18:05