0

I have a raster layer that has values as factors, and an attribute table. I would like to convert this to a SpatialPixelsDataFrame however when I try:

library(sp)
library(sf)
library(rgdal)
library(raster)
library(FedData)
library(rasterVis)

create_factor_rast <- function(base_rast,Spatial_polygon,fieldname) {
  nam <- unique(Spatial_polygon[[fieldname]])
  nam_df <- data.frame(ID = 1:length(nam), nam = nam)
  idfieldname = paste0(fieldname,"ID")
  Spatial_polygon[[idfieldname]] <- nam_df$ID[match(Spatial_polygon[[fieldname]],nam_df$nam)]
  new_raster  <- rasterize(x=Spatial_polygon, y=base_rast, field = idfieldname)
  new_raster  <- ratify(new_raster)
  # # Create levels
  rat <- levels(new_raster)[[1]]
  rat$names <- nam_df$nam
  rat$IDs <- nam_df$ID
  levels(new_raster) <- rat
  new_raster
}

spft <- "+proj=lcc +lat_1=40 +lat_2=43 +lat_0=39.83333333333334 +lon_0=-100 +x_0=500000.0000000002 +y_0=0 +datum=NAD83 +units=us-ft +no_defs"

SSURGO.dl <- FedData::get_ssurgo(template=c('NE043'), label = "Tau",raw.dir = "./SSURGO_r/RAW/SSURGO",
                    extraction.dir = paste0("./SSURGO_R/EXTRACTIONS/", "soils","/SSURGO"))

SSURGO.dl_shp<-SSURGO.dl$spatial

unlink('SSURGO_r',recursive=TRUE)

# extract the muaggatt table
muaggatt<-SSURGO.dl[["tabular"]][["muaggatt"]]

# get only the fields needed, must have mukey to do the join
muaggatt  <- muaggatt[c("mukey","hydgrpdcd","drclassdcd")]
SSURGO_merge <- merge(SSURGO.dl_shp,muaggatt,by.x = "MUKEY", by.y="mukey")

# Convert merged SSURGO polygon to Stateplane feet
SSURGO_merge    <- sf::st_transform(SSURGO_merge,crs(spft))
SSURGO_merge    <- as(SSURGO_merge,"Spatial")

grid_cell_size  <- 100   # ~30 meters


shp_extent      <- extent(SSURGO_merge)
r               <- raster(SSURGO_merge,resolution = grid_cell_size,crs=crs(SSURGO_merge))

r.drclassdcd  <- create_factor_rast(r, SSURGO_merge,'drclassdcd')

rasterVis::levelplot(r.drclassdcd)

r.drclassdcd_sp <- as(r.drclassdcd,"SpatialPixelsDataFrame")

The last line gives the following error:

Error in h(simpleError(msg, call)) : error in evaluating the argument 'x' in selecting a method for function 'as.factor': undefined columns selected

Is there a way to get a pixel dataframe from the raster layer with factors converted back to the characters in the attribute field?

  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Oct 30 '20 at 04:02
  • In other words, instead of your own data, show a simple example raster with an attribute table that you can take from the `raster` help files, and then show what you tried to create a SPDF from that. – Robert Hijmans Oct 30 '20 at 18:59
  • I'm having trouble finding an example that has isfactor set to TRUE, must are numeric values (which I understand is how most rasters data are stored) , however I'm looking to store numeric values which are identified in an attribute table, then convert this to a SpatialPixelsDataFrame for use in another package. – user14546579 Nov 03 '20 at 16:09

0 Answers0