otbWrapperElevationParametersHandler.cxx 6.46 KB
Newer Older
1
/*
2
 * Copyright (C) 2005-2020 Centre National d'Etudes Spatiales (CNES)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * 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.
 */
20
21

#include "otbWrapperElevationParametersHandler.h"
22
#include "otbDEMHandler.h"
23

24
25
26
27
28
29

namespace otb
{
namespace Wrapper
{

30
void ElevationParametersHandler::AddElevationParameters(Application::Pointer app, const std::string& key)
31
{
32
  app->AddParameter(ParameterType_Group, key, "Elevation management");
33
  app->SetParameterDescription(key,
34
                               "This group of parameters allows managing elevation values.");
35
36
37

  // DEM directory
  std::ostringstream oss;
38
  oss << key << ".dem";
39
40
  app->AddParameter(ParameterType_Directory, oss.str(), "DEM directory");
  app->SetParameterDescription(oss.str(),
41
                               "This parameter allows selecting a directory containing Digital Elevation Model files. Note that this directory should contain "
42
                               "only DEM files. Unexpected behaviour might occurs if other images are found in this directory. Input DEM tiles should be in a raster format supported by GDAL.");
43
44
  app->MandatoryOff(oss.str());

45
  std::string demDirFromConfig = otb::ConfigurationManager::GetDEMDirectory();
46

47
48
  if (demDirFromConfig != "")
  {
49
    app->SetParameterString(oss.str(), demDirFromConfig);
50
    app->EnableParameter(oss.str());
51
  }
52
  else
53
  {
54
    app->DisableParameter(oss.str());
55
  }
56

57
  // Geoid file
58
  oss.str("");
59
  oss << key << ".geoid";
60
  app->AddParameter(ParameterType_InputFilename, oss.str(), "Geoid File");
61
62
63
64
65
66
  app->SetParameterDescription(oss.str(),
                               "Use a geoid grid to get the height "
                               "above the ellipsoid in case there is no DEM available, no coverage for "
                               "some points or pixels with no_data in the DEM tiles. A version of the "
                               "geoid can be found on the OTB website"
                               "(https://gitlab.orfeo-toolbox.org/orfeotoolbox/otb-data/blob/master/Input/DEM/egm96.grd).");
67
  app->MandatoryOff(oss.str());
68

69
  std::string geoidFromConfig = otb::ConfigurationManager::GetGeoidFile();
70

71
72
  if (geoidFromConfig != "")
  {
73
    app->SetParameterString(oss.str(), geoidFromConfig);
74
    app->EnableParameter(oss.str());
75
  }
76
  else
77
  {
78
    app->DisableParameter(oss.str());
79
  }
80

81
82
  // Average elevation
  oss.str("");
83
  oss << key << ".default";
84
  app->AddParameter(ParameterType_Float, oss.str(), "Default elevation");
85
86
87
88
  app->SetParameterDescription(oss.str(),
                               "This parameter allows setting the default height above ellipsoid when there is no DEM available, no coverage for some points "
                               "or pixels with no_data in the DEM tiles, and no geoid file has been set. This is also used by some application as an average "
                               "elevation value.");
OTB Bot's avatar
STYLE    
OTB Bot committed
89
  app->SetDefaultParameterFloat(oss.str(), 0.);
90

91
92
93
94
95
96
  // TODO : not implemented yet
  //   // Tiff image
  //   oss << ".tiff";
  //   app->AddChoice(oss.str(), "Tiff file");
  //   app->AddParameter(ParameterType_InputImage, oss.str(), "Tiff file");
  //   app->SetParameterDescription(oss.str(),"Tiff file used to get elevation for each location in the image");
97
98
}

99
void ElevationParametersHandler::SetupDEMHandlerFromElevationParameters(const Application::Pointer app, const std::string& key)
100
{
Cédric Traizet's avatar
Cédric Traizet committed
101
102
  auto & demHandler = otb::DEMHandler::GetInstance();

103
  // Set default elevation
Cédric Traizet's avatar
Cédric Traizet committed
104
  demHandler.SetDefaultHeightAboveEllipsoid(GetDefaultElevation(app, key));
105

106

107
  std::ostringstream oss;
108
  oss << "Elevation management: setting default height above ellipsoid to " << GetDefaultElevation(app, key) << " meters" << std::endl;
109
110
111
112

  app->GetLogger()->Info(oss.str());

  // Set geoid if available
113
114
  if (IsGeoidUsed(app, key))
  {
115
    oss.str("");
116
    oss << "Elevation management: using geoid file (" << GetGeoidFile(app, key) << ")" << std::endl;
Cédric Traizet's avatar
Cédric Traizet committed
117
    demHandler.OpenGeoidFile(GetGeoidFile(app, key));
118
    
119
    app->GetLogger()->Info(oss.str());
120
  }
121
122

  // Set DEM directory if available
123
124
125
  if (IsDEMUsed(app, key))
  {
    std::string demDirectory = GetDEMDirectory(app, key);
Cédric Traizet's avatar
Cédric Traizet committed
126
    if (demHandler.IsValidDEMDirectory(demDirectory.c_str()))
127
    {
128
      oss.str("");
129
      oss << "Elevation management: using DEM directory (" << demDirectory << ")" << std::endl;
Cédric Traizet's avatar
Cédric Traizet committed
130
      demHandler.OpenDEMDirectory(demDirectory);
131
132
      app->GetLogger()->Info(oss.str());
    }
133
    else
134
135
    {
      oss.str("");
136
      oss << "DEM directory : " << demDirectory << " is not a valid DEM directory";
137
      app->GetLogger()->Warning(oss.str());
138
    }
139
  }
140
141
142
143
144
145
}

/**
 *
 * Get the Average elevation value
 */
146
float ElevationParametersHandler::GetDefaultElevation(const Application::Pointer app, const std::string& key)
147
148
{
  std::ostringstream oss;
149
  oss << key << ".default";
150
151
152
153
154
155
  return app->GetParameterFloat(oss.str());
}

/**
 * Get the Geoid file
 */
156
const std::string ElevationParametersHandler::GetGeoidFile(const Application::Pointer app, const std::string& key)
157
158
{
  std::ostringstream oss;
159
  oss << key << ".geoid";
160
  if (IsGeoidUsed(app, key))
161
  {
162
    return app->GetParameterString(oss.str());
163
  }
164

165
  return "";
166
167
168
}

/**
169
 * Is Geoid used
170
 */
171
bool ElevationParametersHandler::IsGeoidUsed(const Application::Pointer app, const std::string& key)
172
{
173
  std::ostringstream geoidKey;
174
  geoidKey << key << ".geoid";
175

176
  return app->IsParameterEnabled(geoidKey.str()) && app->HasValue(geoidKey.str());
177
178
}

179
180
181
182
/**
 *
 * Is Geoid used
 */
183
bool ElevationParametersHandler::IsDEMUsed(const Application::Pointer app, const std::string& key)
184
{
185
  std::ostringstream demKey;
186
  demKey << key << ".dem";
187

188
  return app->IsParameterEnabled(demKey.str()) && app->HasValue(demKey.str());
189
190
}

191

192
const std::string ElevationParametersHandler::GetDEMDirectory(const Application::Pointer app, const std::string& key)
193
{
194
  std::ostringstream oss;
195
  oss << key << ".dem";
196
  if (IsDEMUsed(app, key))
197
  {
198
    return app->GetParameterString(oss.str());
199
  }
200

201
  return "";
202
203
}

204

205
206
} // End namespace Wrapper
} // End namespace otb