fasterize error when converting sf to raster

I am beginner to R and am using it to prep some data for species distribution modelling using MaxEnt.

I've gone back and forth between using the MaxEnt package in R and the GUI. I'm trying the latter right now as I can't seem to figure out the MaxEnt package as well. I think regardless of which way I use, ensuring my data is all raster data in the same projection is my first hurdle. I am running on this 10+ different caribou ranges, or else I would've just used ArcGIS to prep all the data (which I'm still debating considering how long its taken me.... :sweat_smile:)

Some of my data is vector data, and I'm trying to convert my disparate environmental data into clean, usable raster data that can be fed into a MaxEnt species distribution model based on the tutorial provided here.

My code to convert the vector to raster data is below. Its more or less copied from the linked tutorials with a bit of modifications because I wasn't understanding the assign(paste0) errors but where I'm really stumped is with the "fasterize". I've tried both "fasterize" and "rasterize" and the error that's coming up looks like a type error to me.

avi <- st_read('./Maxent/EnvironmentalVariables_AB/AVI_Query_BorealPlain.shp')

assign(paste0('./Maxent/EnvironmentalVariables_AB/AVI_Query_BorealPlain.shp', "_raw"), 
       
  sf::st_read(dsn='./Maxent/EnvironmentalVariables_AB', layer='/AVI_Query_BorealPlain.shp'))

# convert new spatial vector data to raster

require(raster)

# set up raster dataset to fill in the data im about to rasterize

hold.raster <- raster()

extent(hold.raster) <- extent(avi)

res(hold.raster) <- 20

      # create raster on "SP1 attribute 

assign(paste0("avi", 1))

fasterize(avi, hold.raster, field = "SP1")


Error in fasterize(avi, hold.raster, field = "SP1") : 
  Not compatible with requested type: [type=character; target=double].
> rasterize(avi, hold.raster, field = "SP1")

Error in p$rasterize(nrow(x), ncol(x), as.vector(extent(x)), values, background) : 
  Not compatible with requested type: [type=character; target=double].

I'm really quite stumped on this...not sure how I can modify my vector file to convert it to a raster. Also any tips regarding MaxEnt or data prep for MaxEnt would be deeply appreciated as well. Thank you.

1 Like