Raster layer not found

200 views
Skip to first unread message

Ricardo Martinez

unread,
Feb 20, 2022, 3:17:42 AM2/20/22
to SegOptim user group
After reading the post "Example optimisation fails", I have been trying to figure out why I get the message 

Error in .local(.Object, ...) :

Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer",  :
  Cannot create a RasterLayer object from this file. (file does not exist)


in the gaOptimizeSegmentationParams() function. It is as follows:

gaOptim <- gaOptimizeSegmentationParams(  rstFeatures = rstClassifFeatures, ## Type: stack
                                          trainData   = rstTrainData, # Type: S4 (loaded with function raster() )
                                          # // Segmentation parameters ---
                                          segmentMethod = "OTB_LSMS", 
                                          inputRstPath  = segRaster, # I am testing with Sentinel-2 , so this is the same stac as rstFeatures. 
                                          otbBinPath    = otbPath,  # Path to OTB exists
                                          verbose = FALSE,
                                          # // End segmentation parameters ---
                                          trainThresh   = 0.5,
                                          segmStatsFuns = c("mean","sd"),
                                          classificationMethod = "RF",
                                          classificationMethodParams = NULL,
                                          balanceTrainData = FALSE,
                                          balanceMethod = "ubOver",
                                          evalMethod = "10FCV",
                                          evalMetric = "Kappa",
                                          trainPerc = 0.6, #training proportion
                                          minTrainCases = 15,
                                          minCasesByClassTrain = 3,
                                          minCasesByClassTest = 1,
                                          minImgSegm = 4,
                                          lower = OTB_min_params,
                                          upper = OTB_max_params,
                                          popSize = 10,
                                          pcrossover = 0.9,
                                          pmutation = 0.1,
                                          maxiter = 5, # Number of iterations in a run
                                          run = 4, # number of times running all the iterations
                                          keepBest = TRUE,
                                          parallel = FALSE,
                                          bylayer = TRUE)


I have tried to change the file extension from .jp2 to .tif for Sentinel 2 images and still get the same message. Did anybody had the same problem? 

Thank you.

João F Gonçalves

unread,
Feb 20, 2022, 4:22:48 PM2/20/22
to segoptim-...@googlegroups.com

Hi Ricardo,

The code you submitted does not allow to check or reproduce the error. In any case, it seems that the file paths for Sentinel-2 images likely are not properly set which is generating this error. Recheck those and let me know if it works.


Cheers

João

- - -

--
You received this message because you are subscribed to the Google Groups "SegOptim user group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to segoptim-user-g...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/segoptim-user-group/bbf0b901-48a3-4d24-ba04-bb1df2ac1768n%40googlegroups.com.

Ricardo Martinez

unread,
Feb 22, 2022, 10:48:19 AM2/22/22
to SegOptim user group
Thank you  João . 

I am also using raster(file.choose()) and stack(file.choose()) and I am also having the same error. It must be something with my computer.


rstClassifFeatures <- stack(file.choose())#was: "C:/--/stack_ras.tif")

rstTrainData <- raster(file.choose())#was: "C:/---/training.tif")

inputRstPath  <- same as  rstClassifFeatures , because this is a test.

rstTrainData has the same extension and resolution as  rstClassifFeatures . The size is too large to send it.  rstClassifFeatures  is made of a stack of 3 Sentinel-2 bands. The stack is made with the function stack() to the jp2 files and then written on disk with the function writeRaster() , to tif.


João Gonçalves

unread,
Feb 22, 2022, 8:33:48 PM2/22/22
to SegOptim user group
Hi Ricardo,
Not sure if this is the problem but inputRstPath should be defined as a path string not a stack or raster object. Your previous message and comments on the code seem to suggest that. Do check that input.

?gaOptimizeSegmentationParams
(...)
inputRstPath        
The input raster dataset used to perform the image segmentation using OTB LSMS algorithm (typically a multi-layered raster dataset with segmentation features in each band).

Also, take into consideration that GA optimization can take quite a while to run and that OTB tends to use a lot of computing resources (especially RAM). For that reason usually, it's better to use a small representative subset scene to optimize image segmentation parameters (or perhaps better multiple small scenes and then take the average of each parameter).


Cheers
Reply all
Reply to author
Forward
0 new messages