Identify a linear feature on a raster map and return a linear shape object using R

后端 未结 4 592
慢半拍i
慢半拍i 2021-01-31 11:22

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.

<
4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-31 12:00

    Thanks to @klewis at gis.stackexchange.com for linking to this elegant algorithm for finding the centre line (in response to a related question I asked there).

    The process requires finding the coordinates on the edge of a polygon describing the linear feature and performing a Voronoi tessellation of those points. The coordinates of the Voronoi tiles that fall within the polygon of the linear feature fall on the centre line. Turn these points into a line.

    Voronoi tessellation is done really efficiently in R using the deldir package, and intersections of polygons and points with the rgeos package.

    ## Find points on boundary of rPoly (see question)
    rPolyPts <-  coordinates(as(as(rPoly, "SpatialLinesDataFrame"),
                    "SpatialPointsDataFrame"))
    
    ## Perform Voronoi tessellation of those points and extract coordinates of tiles
    library(deldir)
    rVoronoi <- tile.list(deldir(rPolyPts[, 1], rPolyPts[,2]))
    rVoronoiPts <- SpatialPoints(do.call(rbind, 
                     lapply(rVoronoi, function(x) cbind(x$x, x$y))))
    
    ## Find the points on the Voronoi tiles that fall inside 
    ## the linear feature polygon
    ## N.B. That the width parameter may need to be adjusted if coordinate
    ## system is fractional (i.e. if longlat), but must be negative, and less
    ## than the dimension of a cell on the original raster.
    library(rgeos)
    rLinePts <- gIntersection(gBuffer(rPoly, width=-1), rVoronoiPts)
    
    ## Create SpatialLines object
    rLine <- SpatialLines(list(Lines(Line(rLinePts), ID="1")))
    

    The resulting SpatialLines object: SpatialLines object describing linear feature from a raster

提交回复
热议问题