2

I am having issues converting this spatialpolygondataframe to a raster. When I do ther conversion, the raster has NA as its values. As shown below:

 DL3
[1]
class       : SpatialPolygonsDataFrame 
features    : 126 
extent      : -15.04001, 46.1036, 3.759985, 31.71804  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +towgs84=0,0,0 +ellps=WGS84 
variables   : 1
names       :  LFRP 
min values  :    14 
max values  : 335.2 

This is how I rasterize it:

##TO CONVERT TO RASTER
FunR<-function(r){
ext<-raster(extent(r))
crs(ext)<-crs(r)
D<-rasterize(r,ext,field=1,update=T)
D}

DL4<-lapply(DL3,FunR)
DL4
[1]
class       : RasterLayer 
dimensions  : 45, 40, 1800  (nrow, ncol, ncell)
resolution  : 1.52859, 0.6212901  (x, y)
extent      : -15.04001, 46.1036, 3.759985, 31.71804  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +towgs84=0,0,0 +ellps=WGS84 
data source : in memory
names       : layer 
values      : NA, NA  (min, max)

What can I be doing wrongly? I need help with a method to ensure the values in the dataframe reflect in the raster, please.

Joke O.
  • 515
  • 6
  • 29
  • Make the example more reproducible for people who might want to help you: Not all SO users have `x` or `FRP` on their computers! Give at least a thinned version of whatever your very first command is reading in.. – shekeine Jul 09 '15 at 19:45
  • Hi Shekeine, the first line was pasted in error The "rasterize" codes work well from spatialpointsdatafram but not for spatialpolygonsdataframes. that's my challenge. – Joke O. Jul 10 '15 at 00:34
  • You should provide a sample of your data that other SO users can use to recreate your problem. RobertH below has done this but his data does not reproduce your problem: simply because its not the same data as the one you are working with. – shekeine Jul 10 '15 at 08:14

1 Answers1

3

The principle works, as illustrated below.

library(raster)
SPP <- shapefile(system.file("external/lux.shp", package="raster"))
r <- raster(SPP, ncol=40, nrow=45)    
SPP2 <- rasterize(SPP, r, "ID_1")

As this does not work for you, I assume that your polygons are very small relative to the raster cell size such that none of the cells is covered. Can you try with much smaller grid cells? If indeed the polygons are that small, it might make more sense to use their centroids (coordinates(SPP)) and rasterize these (as points).

Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63
  • Hi Robert, thanks for responding. I will post the full codes. They original data were actually point data with coordinates and area. Is there another way to incorporate the area and point coordinates and then write a raster, please? – Joke O. Jul 10 '15 at 11:27
  • Thanks, @RobertH. Based on your recommendation, I created really tiny grid cells and it worked: (r <- raster(SPP, ncol=1000, nrow=1000)) – Joke O. Jul 10 '15 at 12:23
  • If you have point data, you can directly rasterize the points. Or you can perhaps first make proximity polygons and rasterize those: `library(dismo); pols <- voronoi(points)` – Robert Hijmans Jul 10 '15 at 17:57