Correct format for features in VectorClassifier Python script

Hi, in this post there’s a response about the correct format for the field names of the training features.
I try to do it in Python (also copied the QGIS output) and it simply doesn’t work:
ex.

'feat':'\meanB1 meanB2 meanB3\'
'feat':'\"meanB1 meanB2 meanB3\"'

The documentation says it is a list parameter, but also if imported as it doesn’t work.
What’s the catch?
Alen

Hi,

From Python, there may be a trick:

appVector.IN = "myInput.shp"
appVector.UpdateParameters()
appVector.FEAT = ['meanB1', 'meanB2', 'meanB3']

Does it work for you?

No. I tried with

[‘yy’, …], [“yy”, …], [\"yy…]

and all of those without the comma, with a space… Nada.

The “funny” thing is that i can make it work in QGIS as:

import processing
import glob
import gdal
import os

folder_vector_in = '.............'
vector_file_format = '.shp'
search_pattern = os.path.join(folder_vector_in, '*' + vector_file_format)
vector_files_in = glob.glob(search_pattern)
folder_out = '...............'

for vector in vector_files_in:
name_ext = os.path.split(vector)[1]
name = os.path.splitext(name_ext)[0]
fout = os.path.join(folder_out, name + vector_file_format)

parameters = {
    'in':vector,
    'instat':'',
    'model':'...................classifier.model',
    'cfield':'nap',
    'feat':'\"meanB1 meanB2 meanB3\"',
    'confmap':False,
    'out':fout}        
    
processing.run("otb:VectorClassifier", parameters)

Hi Alen,

Can you provide us the full piece of code you wrote to configure and launch the application from yout Python script ?
In the code snippet below, it seems that you are using the command line interface from a Python script.
The Python API is a better way to launch OTB applications (and benefit from many functionalities).

Yannick

Hi Yannic, this is the general code, with variations of feat

import otbApplication
import glob
import os

app = otbApplication.Registry.CreateApplication("VectorClassifier")
vector_in = '/home/alen/Documents/Stavbe_2019/test_geopackage/test_iz_shp.gpkg'
model_in = '/home/alen/Documents/Stavbe_2019/klasifikator/klasifikator.model'

parametri = {
    'in': vector_in,
    'model': model_in,
    'feat': %__as_described__%,
    'cfield': 'label'}

for key, value in parametri.items():
    app.SetParameterString(key, value)

app.ExecuteAndWriteOutput()

Hi Alen,

The parameter “feat” expects a list of values ; therefore you should use :

app.SetParameterStringList('feat',['value1', 'value2', 'value3'])

I think it will solve your problem.

Best regards

1 Like

Hi Alen,
For me both solutions (from @yannick and from @gpasero) work.
The parameter cannot be set with SetParameterString but with SetParameterStringList or just by its key app.FEAT but overall you need to update your application with app.UpdateParameters(). This will update the parameter list (feat) and then allow you to choose among the field you have.
Parameter List are tricky to use in OTB…

Antoine

Dear @yannick and @Antoine, I can confirm this makes it work.
Thank you very much and have a nice day,

Alen