terrarasterize

terra_rasterize Error: [rasterize] cannot create dataset


I am trying to use the rasterize function in the R package terra (v.1.6-7) to transfer population values associated with municipalities to a raster. I am doing this for two shapefiles, dipomun00gw.shp and municipiosPOP.shp. This is the code:

library(terra)

# Read shapefiles for 2000 and 2010
v_00 <- vect("dipomun00gw.shp")
v_10 <- vect("municipiosPOP.shp")

# create blank rasters using each year's shape file as extent
r_00 <- rast(v_00, res=.01)  
r_10 <- rast(v_10, res=.01)  

# Rasterize
x_00 <- rasterize(v_00, r_00, "POTO00")
x_10 <- rasterize(v_10, r_10, "POB_10")  
#Error: [rasterize] cannot create dataset

For some reason, v_00 rasterizes without any issues, but v_10 produces the an error.

I cannot identify what characteristic of the shapefile municipiosPOP.shp is causing this error.

Does anyone know what is going on?

Shapefiles can be accessed here.


Solution

  • The reason is that r_10 is too large (it has > 6.4e+16 cells!)

    r_10
    #class       : SpatRaster 
    #dimensions  : 203046581, 317517559, 1  (nrow, ncol, nlyr)
    #resolution  : 0.01, 0.01  (x, y)
    #extent      : 907821.8, 4082997, 319149.1, 2349615  (xmin, xmax, ymin, ymax)
    #coord. ref. : North_America_Lambert_Conformal_Conic 
    

    This is because you assigned a spatial resolution of 0.01 (that is ~1 cm), where you presumably assumed you were using 0.01 degrees (~1 km). If you first project v_10 to long/lat (like v_00) your code works.

    v_10 <- project(v_10, crs(v_00))