Skip to content
Snippets Groups Projects
Commit f40179bb authored by Luc Hermitte's avatar Luc Hermitte
Browse files

ENH: Inject `nodata` automatically in image metadata

parent a50cc19d
No related branches found
No related tags found
1 merge request!3Resolve "Automatically inject NODATA metadata in generated images"
......@@ -23,7 +23,7 @@
#include "otbRepack.h"
#include "otbPositionHelpers.h"
#include "otbWrapperApplication.h"
#include "otbApplicationWithNoData.h"
#include "otbWrapperApplicationFactory.h"
#include "otbFunctorImageFilter.h"
#include "otbDEMHandler.h"
......@@ -184,7 +184,7 @@ namespace Wrapper
* \ingroup App???
* \ingroup SARCalibrationExtended
*/
class ExtractNormalVector : public Application
class ExtractNormalVector : public ApplicationWithNoData
{
public:
/** Standard class typedefs. */
......@@ -226,10 +226,7 @@ private:
AddParameter(ParameterType_OutputImage, "out", "Image of normal vectors");
SetParameterDescription("out", "Image of normal vectors");
AddParameter(ParameterType_Float, "nodata", "NoData value");
SetParameterDescription("nodata", "DEM empty cells are filled with this value (optional -32768 by default)");
SetDefaultParameterFloat("nodata", -32768);
MandatoryOff("nodata");
DoInit_NoData();
AddRAMParameter();
......@@ -249,6 +246,7 @@ private:
auto const nodata = GetParameterFloat("nodata");
// std::cout << "nodata => " << nodata << std::endl;
AddNodataInMetadata(nodata);
auto const spacing = inputImage->GetSignedSpacing();
FloatType const x_spacing = spacing[0];
......
......@@ -19,7 +19,7 @@
*/
#include "otbSARComputeLocalIncidenceAngle.h"
#include "otbWrapperApplication.h"
#include "otbApplicationWithNoData.h"
#include "otbWrapperApplicationFactory.h"
// #define DOUBLES_EVERYWHERE
......@@ -47,7 +47,7 @@ namespace Wrapper
* \ingroup App???
* \ingroup SARCalibrationExtended
*/
class SARComputeLocalIncidenceAngle : public Application
class SARComputeLocalIncidenceAngle : public ApplicationWithNoData
{
public:
/** Standard class typedefs. */
......@@ -109,6 +109,7 @@ private:
#endif
SetParameterDescription(k_out_sin, "Output image with abs(sin LIA)");
DoInit_NoData();
AddParameter(ParameterType_Float, "nodata", "NoData value");
SetParameterDescription("nodata", "DEM empty cells are filled with this value (optional -32768 by default)");
SetDefaultParameterFloat("nodata", -32768);
......@@ -139,6 +140,8 @@ private:
// filter->ShallComputeLIASign(true);
auto const nodata = GetParameterFloat("nodata");
AddNodataInMetadata(nodata, k_out_lia);
AddNodataInMetadata(nodata, k_out_sin);
filter->SetNoData(nodata);
SetParameterOutputImage(k_out_sin, filter->GetSinOutputImage());
......
/*
* Copyright (C) 2005-2022 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.
*/
#ifndef otbApplicationWithNoData_h
#define otbApplicationWithNoData_h
#include "otbWrapperApplication.h"
namespace otb
{
namespace Wrapper
{
/** Helper parent class for application that need to inject `&nodata=` in
* metadata.
*
* This class provides two services:
* - `DoInit_NoData()` meant to be called from `DoInit()` specialisation.
* - `AddNodataInMetadata()` meant to be called from `DoExecute()`
* specialisation.
*
* \author Luc Hermitte
* \copyright CS Group
* \ingroup App???
* \ingroup SARCalibrationExtended
*/
class ApplicationWithNoData : public Application
{
protected:
/**
* Registers a `-nodata` parameter.
*/
void DoInit_NoData()
{
AddParameter(ParameterType_Float, "nodata", "NoData value");
SetParameterDescription("nodata", "DEM empty cells are filled with this value (optional -32768 by default)");
SetDefaultParameterFloat("nodata", -32768);
MandatoryOff("nodata");
}
/**
* Appends `&nodata={value}` at the end of the extended filename to force
* nodata to be added in metadata.
*/
template <typename FloatType>
void AddNodataInMetadata(FloatType nodata, std::string const& out_param = "out")
{
std::string origin_FileName = GetParameterString(out_param);
std::ostringstream oss;
oss << origin_FileName;
// Check if FileName is extended (with the ? caracter)
// If not extended then override the FileName
auto const extension_start = origin_FileName.find('?');
if (extension_start == std::string::npos && !origin_FileName.empty())
oss << '?';
else
{
auto const nodata_start = origin_FileName.find("&nodata=");
if (nodata_start > extension_start)
{
// Let's trust the end-user. Even if the value doesn't match
otbLogMacro(Warning, <<"Trusting the nodata value required in extended filename. User specified " << nodata << " value won't be propagated in image metadata");
return ;
}
}
oss << "&nodata="<<nodata;
// Set the new FileName with extended options
SetParameterString(out_param, oss.str());
}
};
} //end namespace Wrapper
} //end namespace otb
#endif // otbApplicationWithNoData_h
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment