Question

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

Was it helpful?

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

OTHER TIPS

In general, to get this polygon data plotted in ggplot2:

library(ggplot2)
# Convert the SpatialPolygons object to a data.frame, which ggplot2 needs
poly_data_frame = fortify(poly_spatialpolygon)
ggplot(poly_data_frame, aes(x = x, y = y)) + geom_polygon(fill = "transparent")
ggsave("poly_plot.png")

now you end up with a polygon plot in the PNG file without any colors inside the polygons.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top