Dear List, Please I have been running a certain the codes below to read my image, shapeffile to enable me classify for cover with Random forest. I have gotten to the point of extracting the raster values and the raster information. However I keep getting errors. though I have tried different combination and other codes like readOGR for importing the training polygon. I will be glad if you could be of help as I am new to using R.
#import clip image of study area TestImg3 <- brick("C:\\Users\\hp\\Desktop\\Data collection\\Nkandla Images\\Landsat\\2019\\08052019\\Corrected\\New Folder\\Image08052019_Clip.tif") #Assign name of bands names(TestImg3) <- c(paste0("B", 1:7, coll=""), "B9") plotRGB(TestImg3, r=4, g=3, b=2, stretch ="lin") #Import training shapefile sample <- shapefile("C:/Users/hp/Desktop/Data collection/Nkandla Images/Landsat/2019/08052019/Corrected/Train08052019_2.shp") responseCol <- "class" #(I have tried options of changing "class" to "classname" as reflect actaul name assigned in ArcMap) # Overlap the sample polygons on the image (combine the class information with extracted values). pixels = data.frame(matrix(vector(), nrow = 0, ncol = length(names(img)) + 1)) for (i in 1:length(unique(sample[[responseCol]]))){ category <- unique(sample[[responseCol]])[i] categorymap <- sample[sample[[responseCol]] == category,] dataSet <- extract(img, categorymap) dataSet <- dataSet[!unlist(lapply(dataSet, is.null))] if(is(sample, "SpatialPointsDataFrame")){ dataSet <- cbind(dataSet, class = as.numeric(category)) pixeles <- rbind(pixeles, dataSet) } if(is(sample, "SpatialPolygonsDataFrame")){ dataSet <- lapply(dataSet, function(x){cbind(x, class = as.numeric(rep(category, nrow(x))))}) df <- do.call("rbind", dataSet) pixels <- rbind(pixeles, df) } } THIS IS THE ERROR I GET FROM RUNNING THE ABOVE CODES > for (i in 1:length(unique(samples[[responseCol]]))){ + category <- unique(samples[[responseCol]])[i] + categorymap <- samples[samples[[responseCol]] == category,] + dataSet <- extract(img, categorymap) + + if(is(sample, "SpatialPointsDataFrame")){ + dataSet <- cbind(dataSet, class = as.numeric(category)) + pixeles <- rbind(pixeles, dataSet) + } + if(is(sample, "SpatialPolygonsDataFrame")){ + dataSet <- lapply(dataSet, function(x){cbind(x, class = as.numeric(rep(category, + nrow(x))))}) + df <- do.call("rbind", dataSet) + pixels <- rbind(pixeles, df) + } + + } Error in y[i, ] : cannot get a slot ("Polygons") from an object of type "NULL" Please help me out. Thank you. Best regards, Enoch -- *Enoch Gyamfi - Ampadu* *Geography & Environmental Sciences* *College of Agriculture, Engineering & Science* *University of KwaZulu-Natal, Westville Campus* *Private Bag X54001* *Durban, South Africa **– 4000**.* *Phone: +27 835 828255* *email: egamp...@gmail.com <egamp...@gmail.com>* *skype: enoch.ampadu* *The highest evidence of nobility is self-control*. *A simple act of kindness creates an endless ripple*. [[alternative HTML version deleted]] _______________________________________________ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo