Commit 16f778d3 authored by Victor Poughon's avatar Victor Poughon

Merge branch '1842-update-parameter' into 'develop'

Resolve "UpdateParameters has to be called manually from the python API"

Closes #1842

See merge request !530
parents 9bfd5471 374b859f
Pipeline #2102 passed with stages
in 72 minutes and 1 second
PROJCS["RGF93_Lambert_93",GEOGCS["GCS_RGF93",DATUM["D_RGF_1993",SPHEROID["GRS_1980",6378137,298.257222101]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]],PROJECTION["Lambert_Conformal_Conic"],PARAMETER["standard_parallel_1",49],PARAMETER["standard_parallel_2",44],PARAMETER["latitude_of_origin",46.5],PARAMETER["central_meridian",3],PARAMETER["false_easting",700000],PARAMETER["false_northing",6600000],UNIT["Meter",1]]
\ No newline at end of file
......@@ -83,9 +83,12 @@ def GetApplicationExamplePythonSnippet(app,idx,expand = False, inputpath="",outp
paramtype = app.GetParameterType(param)
paramrole = app.GetParameterRole(param)
if paramtype == ParameterType_ListView:
break # TODO
if app.GetListViewSingleSelectionMode(param):
output += "\t" + appname + ".SetParameterString("+EncloseString(param)+", "+EncloseString(value)+")"
else:
output += "\t" + appname + ".SetParameterStringList("+EncloseString(param)+", "+EncloseString(value)+")"
if paramtype == ParameterType_Group:
break # TODO
pass
if paramtype == ParameterType_Choice:
#app.SetParameterString(param,value)
output+= "\t" + appname + ".SetParameterString(" + EncloseString(param) + "," + EncloseString(value) + ")"
......
......@@ -389,43 +389,10 @@ happens, this documentation will report the OTB version that fixes the issue.
Calling UpdateParameters()
^^^^^^^^^^^^^^^^^^^^^^^^^^
These wrappers are made as a mirror of the C++ API, so there is a function
``UpdateParameters()``. Its role is to update parameters that depend on others.
It is called at least once at the beginning of ``Execute()``.
In command line and GUI launchers, this functions gets called each time a
parameter of the application is modified. In Python, this mechanism is not
automated: there are cases where you may have to call it yourself.
Let's take an example with the application ``PolygonClassStatictics``. In this
application, the choices available in the parameter ``field`` depend on the list
of fields actually present in the vector file ``vec``. If you try to set the
parameters ``vec`` and ``field``, you will get an error:
.. code-block:: python
import otbApplication as otb
app = otb.Registry.CreateApplication("PolygonClassStatistics")
app.SetParameterString("vec","../../src/OTB-Data/Input/Classification/variousVectors.sqlite")
app.SetParameterString("field", "label")
::
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/gpasero/Projet_OTB/build/OTB/lib/otb/python/otbApplication.py", line 897, in SetParameterString
def SetParameterString(self, *args): return _otbApplication.Application_SetParameterString(self, *args)
RuntimeError: Exception thrown in otbApplication Application_SetParameterString: /home/gpasero/Projet_OTB/src/OTB/Modules/Wrappers/ApplicationEngine/src/otbWrapperListViewParameter.cxx:141:
itk::ERROR: ListViewParameter(0x149da10): Cannot find label
The error says that the choice ``label`` is not recognized, because ``UpdateParameters()``
was not called after setting the vector file. The solution is to call it before
setting the ``field`` parameter:
.. code-block:: python
app.UpdateParameters()
app.SetParameterString("field", "label")
``UpdateParameters()`` is available to the Python API. But in normal use, it
does not need to be called manually. From OTB 7.0.0 and later, it is called
automatically after each call to ``SetParameter*()`` methods. With previous versions
of OTB you may need to call it after setting a parameter.
No metadata in NumPy arrays
^^^^^^^^^^^^^^^^^^^^^^^^^^^
......
......@@ -203,6 +203,35 @@ public:
#endif
#if SWIGPYTHON
// We want all SetParameterXXX functions to call UpdateParameters automaticaly
// so that using it is not required from the Python API
// for more discussion about this see gitlab issue #1842
%pythonappend Application::SetParameterInt %{
self.UpdateParameters()
%}
%pythonappend Application::SetParameterFloat %{
self.UpdateParameters()
%}
%pythonappend Application::SetParameterString %{
self.UpdateParameters()
%}
%pythonappend Application::SetParameterStringList %{
self.UpdateParameters()
%}
%pythonappend Application::SetParameterOutputImagePixelType %{
self.UpdateParameters()
%}
#endif
class Application: public itkObject
{
public:
......@@ -511,7 +540,6 @@ private:
void operator =(const Application&);
};
DECLARE_REF_COUNT_CLASS( Application )
......
......@@ -131,7 +131,7 @@ add_test( NAME pyTvNewStyleParameters
COMMAND ${TEST_DRIVER} Execute
${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/PythonTestDriver.py
PythonNewStyleParametersTest
${OTB_DATA_ROOT}/Input/poupees.tif
${OTB_DATA_ROOT}/Input/sensor_stereo_left.tif
${TEMP}/pyTvNewStyleParametersTest.tif
${OTB_DATA_ROOT}/Input/apTvUtSmoothingTest_OutXML.xml)
......@@ -160,3 +160,12 @@ add_test( NAME pyTvParametersDict
${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/PythonTestDriver.py
PythonParametersDict
${OTB_DATA_ROOT}/Input/poupees.tif)
add_test( NAME pyTvNoUpdateParameter
COMMAND ${TEST_DRIVER} Execute
${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/PythonTestDriver.py
PythonNoUpdateParameter
${OTB_DATA_ROOT}/Input/poupees.tif
${OTB_DATA_ROOT}/Input/training/training.shp
)
......@@ -67,8 +67,8 @@ def test(otb, argv):
cm_assert(app.MAP, 'epsg')
# 6 - int type 2nd level sub parameters of choice parameter set
app.MAP.EPSG.CODE = 32768
cm_assert(32768, app.GetParameterInt('map.epsg.code'))
app.MAP.EPSG.CODE = 2154
cm_assert(2154, app.GetParameterInt('map.epsg.code'))
# 7 - another choice with sub parameters set
app.MAP = 'utm'
......
# -*- coding: utf-8 -*-
#
# Copyright (C) 2005-2019 Centre National d'Etudes Spatiales (CNES)
#
# This file is part of Orfeo Toolbox
#
# https://www.orfeo-toolbox.org/
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
def test(otb, argv):
"""
The purpose of this test is to check that we don't need UpdateParameter()
from the PythonAPI, especially in the case of ListView parameters
See gitlab issue #1842
"""
app = otb.Registry.CreateApplication("TrainImagesClassifier")
app.SetParameterStringList("io.il", [argv[1]])
app.SetParameterStringList("io.vd",[argv[2]])
app.SetParameterStringList("sample.vfn",["CODE"])
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment