[R-sig-Geo] Polygons from lines with open endings

Carson Farmer carson.farmer at gmail.com
Mon Apr 26 14:57:57 CEST 2010


> Since, I cannot manually close all endings, I would like to do find a way to create polygons from lines with open endings.
> Any suggestions ...?

If your lines are valid (i.e. correctly ordered pairs of coords), you
could probably just grab the first set of points in the list and add
that to the end... something like the following
(where line_layer is a SpatialLinesDataFrame):

> coords <- line_layer at lines[[1]]@Lines[[1]]@coords
> coords <- rbind(coords,coords[1,])
> line_layer at lines[[1]]@Lines[[1]]@coords <- coords

Note that it is simple to figure out how many features and geometries
(multipart features) to go through:

> length(line_layer at lines)
[1] 1032

> length(line_layer at lines[[1]]@Lines)
[1] 1

so then of course you can do this for all features in your line layer:

 tmp_lines <- line_layer at lines
 for (i in seq(length(tmp_lines))) {
     tmp_Lines <- tmp_lines[[i]]@Lines
     for (j in seq(length(tmp_Lines))) {
         coords <- tmp_Lines[[j]]@coords
         coords <- rbind(coords,coords[1,])
         line_layer at lines[[i]]@Lines[[j]]@coords <- coords
     }
}

There are probably faster ways to do this using apply etc. but this
works, and is relatively readable...

Hope that helps,

Carson

-- 
Carson J. Q. Farmer
ISSP Doctoral Fellow
National Centre for Geocomputation
National University of Ireland, Maynooth,
http://www.carsonfarmer.com/



More information about the R-sig-Geo mailing list