I would like to identify linear features, such as roads and rivers, on raster maps and convert them to a linear spatial object (SpatialLines
class) using R.
The raster
and sp
packages can be used to convert features from rasters to polygon vector objects (SpatialPolygons
class). rasterToPolygons()
will extract cells of a certain value from a raster and return a polygon object. The product can be simplified using the dissolve=TRUE
option, which calls routines in the rgeos
package to do this.
This all works just fine, but I would prefer it to be a SpatialLines
object. How can I do this?
Consider this example:
## Produce a sinuous linear feature on a raster as an example
library(raster)
r <- raster(nrow=400, ncol=400, xmn=0, ymn=0, xmx=400, ymx=400)
r[] <- NA
x <-seq(1, 100, by=0.01)
r[cellFromRowCol(r, round((sin(0.2*x) + cos(0.06*x)+2)*100), round(x*4))] <- 1
## Quick trick to make it three cells wide
r[edge(r, type="outer")] <- 1
## Plot
plot(r, legend=FALSE, axes=FALSE)
## Convert linear feature to a SpatialPolygons object
library(rgeos)
rPoly <- rasterToPolygons(r, fun=function(x) x==1, dissolve=TRUE)
plot(rPoly)
Would the best approach be to find a centre line through the polygon?
Or is there existing code available to do this?
EDIT: Thanks to @mdsumner for pointing out that this is called skeletonization.