0

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

TylerH
  • 20,799
  • 66
  • 75
  • 101
Karim Ghariani
  • 67
  • 1
  • 2
  • 8
  • Could you provide a reproducible example? If you just want to plot the polygon with colors, have a look at either `ggplot2` or (easier) `spplot`. – Paul Hiemstra Feb 26 '13 at 15:07
  • What I want is to convert a shapefile into a tiff without filling the polygons with colors. I want to convert only the edges. (The first image is my result using plot(rasterize(shape,raster)) the second image is what I want to have into my tiff (which is the same as my shapefile) – Karim Ghariani Feb 26 '13 at 20:56

2 Answers2

3

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

Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184
0

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.

Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
  • Thanks for your reply. poly_spatialpolygon is my SpatialPolygonsDataFrame? (the shapefile loaded with readOGR()?) And what about if I want to save it as a GeoTiff? (tif+tfw) Sorry, but I'm very new in R! Thank you in advance – Karim Ghariani Feb 26 '13 at 21:49
  • This solution with ggplot2 only delivers an image, not a georeferenced tiff. And your assumption about poly_spatialpolygon is correct. – Paul Hiemstra Feb 26 '13 at 22:35
  • So what can I do if I want a geotiff? – Karim Ghariani Feb 27 '13 at 08:28
  • The x is the x-coordinate, the y is the y-coordinate. This could be lat long, or some other projection. – Paul Hiemstra Feb 27 '13 at 09:19
  • It works! Thank You! My last problem is that I want to output a GeoTiff. Any Idea please? – Karim Ghariani Feb 27 '13 at 09:39
  • I didn't find any solution to my problem... Is it impossible to save the "rasterized" shapefile (with transparent areas) to a GeoTiff? – Karim Ghariani Feb 27 '13 at 22:14
  • @KarimGhariani Check my answer below. You need to use the `SpatialLines*` class to achieve what you want (namely that only polygon edges are rasterized to values, polygon interiors will be set to `NA`). You will get the results as a raster which can then be saved as a GeoTiff. – Simon O'Hanlon Mar 07 '13 at 12:37