ITK Error: shapefile Field not Find

Hello Everyone,
I have just started to use OTB in python notebooks, trying to write an example supervised classifier,
I have a TIF file and the shapefile with field "CODE’ as the classifying label.
(I have controlled the shapefile and it actually contains the mentioned field and I can access features and fields and everything seems fine.)
But running the following code returns error:

T_Img_C= otbApplication.Registry.CreateApplication(“TrainImagesClassifier”)
T_Img_C.SetParameterString(“io.il”, ‘20160607T31TCJROI20m.tif’)
T_Img_C.SetParameterString(“io.vd”, ‘training.shp’)
T_Img_C.SetParameterString(“io.out”, ‘PP_1st_model.rf’)
#T_Img_C.UpdateParameters()
T_Img_C.SetParameterString(“sample.vfn”,‘CODE’)
T_Img_C.SetParameterString(“classifier”, ‘rf’)

saying that itk::ERROR: ListViewParameter(0x2011370): Cannot find CODE

I have searched on internet and for the same error someone had suggested adding the line :

T_Img_C.UpdateParameters()

where I have commented in my code, yet it does not work out in my case.

It seems that I can not address that field in my shapefile to be considered as classifying label.

(As I understood if I leave sample.vfn out, by default it will be set as ‘Class’, would it be a work-around to AlterField and rename ‘CODE’ to ‘Class’ :thinking: )
Thank you for any help or ideas
Panteha

This line is definitively needed for the list of fields to be available.

Can you open your shapefile in qgis and check that it contains a field named CODE with an integer type ?

Hello Julien
Practically I am using

[quote=“yannick, post:2, topic:109”]
the dataset here : https://www.orfeo-toolbox.org/packages/WorkshopData/

[quote=“yannick, post:2, topic:109”]
classification/references/training/training.shp
[/quote]https://gitlab.orfeo-toolbox.org/orfeotoolbox/otb-data.git

I have opened the shapefile in QGIS and it has the integer field “CODE”,
I would be grateful if you could address what might have been wrongly set in my code.
Best
PP

Hello Panteha,

sorry for the late reply,

first of all you do need to call the UpdateParameters()

there is two things you should change in your code :

  • This application expects a list of training images and a list of training vectors as input. You can use only one image and one vector, but you still have to give lists to the application (arrays in Python), i.e. :
T_Img_C.SetParameterStringList('io.il', ['20160607T31TCJROI20m']) # Note the brackets
T_Img_C.SetParameterStringList('io.vd', ['training.shp']) 
  • give the field name in lowercase characters, i.e. :
T_Img_C.SetParameterString('sample.vfn','code')

This is not intuitive and I’m not sure why it is like this, but this change made it work for me.

Hope this helps,

Cédric

Hi Cédric and Panteha,

I was also trying to test the code, because I had exactly the same problem a few days ago !
It seems that you need to use SetParameterStringList, because the application needs a list of parameters (in the GUI, you’ll see that the widget is different from the other widgets that need only a single string value).

And in my example, I have to use SetParameterStringList both for the image, the training (or validation) set, and the code.

And the UpdateParameters() is needed, in order that ITK opens the file to check consistency between the field name you gave and the shapefile content.

Best regards,

Yannick

1 Like

Yesss!
changing the field name to lower-case characters solved the issue!
I had already made it run in command line mode and did my very first modelling using OTB :slight_smile:
Stay tuned for upcoming questions :-)))
Thank you very much
Panteha