0

I am trying to make a RF-classification of aerial-imagery to specify tree-species. I extracted 3 bands and made a .csv file with columns:

band1  band2  band3  x  y  class

The band columns include the pixel-value of the point which I marked in ArcGIS and copied to the attribute-table. x/y include the coordinates of the points. class includes the class, such as forest, juniper...

The RF reads the .csv and classifies the bands it sees in the rasterstack. The classifier gives back an image with the classes.

So I mark my code and run it:

A classifier-window shows up, and freezes. 5 Seconds later the console shows an error:

 Error in eval(expr, envir, enclos) : object 'band1' not found

The problem is unclear. The code is right, but the compiler reacts as if there was a typo when I run the code. But as far as I can see, there is no typo. band1, like band2 and band3 are in the pointdata_sw_aug1.csv. It is the 1st column and has no space before or after it (" band1"). Therefore I uploaded the images. Since my rasters are in tif-format, I recently changed the code because at first is was written by Aaron for img-files:

rlist=list.files(getwd(), pattern="img$", full.names=TRUE) 

to

rlist=list.files(getwd(), pattern="tif$", full.names=TRUE)

The RandomForest-Package then opens a little "predict" window where it says that 31 steps are left. It takes 5 seconds till it stops with an error-message:

Error in eval(expr, envir, enclos) : object 'band1' not found

It doesn't go on.


This should work, but gives me an error:

require(sp)
require(rgdal)
require(raster)
require(randomForest)

# Set the working directory

setwd("D:/BA-Workspace/DOP_10/orthophotos_abcd/R/test_run_R")

# Create list of rasters

rlist=list.files(getwd(), pattern="tif$", full.names=TRUE) 

# Create raster-stack

rasters = stack(rlist)

# Read Raster Training-Table *.csv

intable = read.csv("D:/BA-Workspace/DOP_10/orthophotos_abcd/R/test_run_R/pointdata_sw_aug1.csv")

# Assign Training-points from Columns

myrf = randomForest(factor(class) ~ band1 + band2 + band3 , data = intable, ntree = 2500)

# Run RandomForest classifier

predict(rasters, myrf, filename="outFileName.img", type="response",
    index=1, na.rm=TRUE, progress="window", overwrite=TRUE) 

my .csv: enter image description here

enter image description here

As far as I can see there are no mistakes. Whats wrong here?

Error-message is this (copied from console):

require(sp)
require(rgdal)
require(raster)
require(randomForest)


 >setwd("D:/BA-Workspace/DOP_10/orthophotos_abcd/R/test_run_R")


> rlist=list.files(getwd(), pattern="tif$", full.names=TRUE) 
> 

> rasters = stack(rlist)
> 
> intable = read.csv("D:/BA-Workspace/DOP_10/orthophotos_abcd/R/pointdata_sw_aug1.csv")
> 
> myrf = randomForest(factor(class) ~ band1 + band2 + band3 , data = intable, ntree =     2500)
> 
> predict(rasters, myrf, filename="outFileName.img", type="response",
+         index=1, na.rm=TRUE, progress="window", overwrite=TRUE)
**Error in eval(expr, envir, enclos) : object 'band1' not found**
  • 2
    Try making sure the bands are separate in a single folder and named like so: band1.img, band2.img, band3.img. Then stack the bands in R. The band names in the script, csv and rasters need to be the same. – Aaron Aug 20 '14 at 23:54
  • My images have .tif format, but that's not the problem because the prediction starts. rasterstack is also created, but then, while the .csv is used to classify, something goes wrong and it stops. the band names are all the same, as I pointed out above. – steveomb Aug 21 '14 at 12:59
  • well, this question is still on hold. Is it not clear or did I make a mistake? thanks for the support so far. – steveomb Aug 21 '14 at 21:47
3

I think your problem is related to the layer names of your raster stack ('rasters'). Make sure these are the same as in your .csv. You can get the layer names with names(rasters) and set them with names(rasters) <- c("band1", "band2", "band3")

Hope this helps TimSalabim

| improve this answer | |
  • Hi Tim, Welcome to GIS@SE, this would have been better as a comment rather than an answer as it does not provide a specific solution to the question asked. Once you get the response form your suggestions, you may then be able to provide an answer to the question. Cheers. – Mark Cupitt Aug 22 '14 at 10:55
  • 1
    well. to be honest, it was the solution. I searched for typos and mistakes in the code, but forgot that my file-names have to fit, too... Thanks, Tim! – steveomb Aug 22 '14 at 11:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.