Search code examples
rshapefileedgesrasterize

Rasterize only the edges of a SpatialPolygonsDataFrame


I've imported a shapefile using readOGR (from package 'rgdal'), and obtained a SpatialPolygonsDataFrame. When I use the 'rasterize' function (of package 'raster') I obtain this

http://img15.hostingpics.net/pics/427269plot.png

But I want to rasterize only the edges, so I can obtain a GeoTiff that looks like this

http://img15.hostingpics.net/pics/270288Rplot.png


Solution

  • You can do this using raster and sp, using the SpatialLines object type. Try this example and substitue spdf with your imported shapefile name:

    spdf <- readShapePoly(system.file("shapes/sids.shp", package="maptools")[1]) # Read in your datafile. You can use readOGR or readShapePoly, it doesn't really matter.
    sldf <- as( spdf , "SpatialLinesDataFrame") # Create a lines object. This gives you the borders of the polygons
    r <- raster( nrow = 180 , ncols = 360 , ext = extent(spdf) ) # Create a template raster file which will form the mask you will rasterzie to (so if you want a more precise 
    r <- rasterize( sldf , r ) # Depending on the resolution of your target raster and the complexity of your shapefile this may take a few seconds or a few minutes to run
    

    You can save the raster file as you wish.

    plot( spdf )
    plot( r )
    

    enter image description here enter image description here