I'm trying to convert a shapefile into a raster within R. My approach is to read in the raster as follows:
library(rgdal) # Loads SP package by default
demo <- readOGR('F:/data/', 'shapefile') # Creates a SpatialPolygonsDataFrame class (sp)
This works fine, and I can plot it. However it is a large shapefile and I want to convert it into a raster. I've tried the following:
r <- raster(ncol=180, nrow=180)
Demo_ras = rasterize(r, demo, 'pop') # pop is an integer here
Error message:
Error in function (classes, fdef, mtable) :
unable to find an inherited method for function "rasterize", for signature "RasterLayer", "SpatialPolygonsDataFrame"
I'm slightly confused which is the correct order of the arguments. I've also tried this:
Demo_ras = rasterize(demo, r, 'pop')
Which results in the follow error:
Error in .polygonsToRaster(x, y, ...) :
polygon and raster have no overlapping areas
While it makes sense that a raster cannot be generated for an area that does not cover the SpatialPolygonsDataFrame
, I'm not sure what information I need to specify so that the raster should contain the SpatialPolygonsDataFrame
area.