OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimHillshadeTool.cpp
Go to the documentation of this file.
1 //**************************************************************************************************
2 //
3 // OSSIM Open Source Geospatial Data Processing Library
4 // See top level LICENSE.txt file for license information
5 //
6 //**************************************************************************************************
7 
13 #include <ossim/base/ossimIrect.h>
16 #include <ossim/base/ossimNotify.h>
18 #include <ossim/base/ossimRefPtr.h>
23 #include <ossim/base/ossimTrace.h>
25 
46 #include <ossim/init/ossimInit.h>
47 
54 
57 #include <cmath>
58 #include <sstream>
59 #include <string>
60 
61 static ossimTrace traceDebug("ossimHillshadeUtil:debug");
62 
63 static const std::string COLOR_BLUE_KW = "color_blue";
64 static const std::string COLOR_GREEN_KW = "color_green";
65 static const std::string COLOR_RED_KW = "color_red";
66 static const std::string COLOR_SOURCE_KW = "color_source";
67 
69  "Computes shaded representation of input elevation surface with specified lighting parameters.";
70 
72 {
74 }
75 
76 // Private/hidden from use.
78 {
79 }
80 
81 // Private/hidden from use.
83 {
84  return *this;
85 }
86 
88 {
89  clear();
90 }
91 
93 {
94  // Permit base class to pull out common options first.
96  return false;
97  if (m_helpRequested)
98  return true;
99 
100  std::string tempString1;
101  ossimArgumentParser::ossimParameter stringParam1(tempString1);
102  std::string tempString2;
103  ossimArgumentParser::ossimParameter stringParam2(tempString2);
104  std::string tempString3;
105  ossimArgumentParser::ossimParameter stringParam3(tempString3);
106  std::string tempString4;
107  ossimArgumentParser::ossimParameter stringParam4(tempString4);
108  std::string tempString5;
109  ossimArgumentParser::ossimParameter stringParam5(tempString5);
110  std::string tempString6;
111  ossimArgumentParser::ossimParameter stringParam6(tempString6);
112  double tempDouble1;
113  ossimArgumentParser::ossimParameter doubleParam1(tempDouble1);
114  double tempDouble2;
115  ossimArgumentParser::ossimParameter doubleParam2(tempDouble2);
116 
117  // Extract optional arguments and stuff them in a keyword list.
118  if( ap.read("--azimuth", stringParam1) )
119  {
120  m_kwl.addPair( std::string(ossimKeywordNames::AZIMUTH_ANGLE_KW), tempString1 );
121  }
122 
123  if( ap.read("--color", stringParam1, stringParam2, stringParam3) )
124  {
125  m_kwl.addPair( COLOR_RED_KW, tempString1 );
126  m_kwl.addPair( COLOR_GREEN_KW, tempString2 );
127  m_kwl.addPair( COLOR_BLUE_KW, tempString3 );
128  }
129 
130  int color_source_idx = 0;
131  while( ap.read("--color-source", stringParam1) )
132  {
133  ossimString key = COLOR_SOURCE_KW;
134  key += ossimString::toString(color_source_idx++);
135  key += ".";
137  m_kwl.addPair(key.string(), tempString1 );
138  }
139 
140  if ( ap.read("--elevation", stringParam1) )
141  {
142  m_kwl.addPair( std::string(ossimKeywordNames::ELEVATION_ANGLE_KW), tempString1 );
143  }
144 
146  return true;
147 }
148 
149 
151 {
152  // Need a mosaic of DEM over the AOI as an image mosaic:
154  m_procChain->add(demMosaic.get());
155 
156  // Set up the normal source.
158  normSource->setTrackScaleFlag(true);
159  m_procChain->add( normSource.get() );
160 
161  // Set the smoothness factor.
162  ossim_float64 gain = 1.0;
163  normSource->setSmoothnessFactor(gain);
164 
165  // Create the bump shade.
167  m_procChain->add(bumpShade.get());
168 
169  // Set the azimuth angle.
170  ossim_float64 azimuthAngle = 180;
172  if ( lookup.size() )
173  {
174  ossim_float64 f = lookup.toFloat64();
175  if ( (f >= 0) && (f <= 360) )
176  {
177  azimuthAngle = f;
178  }
179  }
180  bumpShade->setAzimuthAngle(azimuthAngle);
181 
182  // Set the elevation angle.
183  ossim_float64 elevationAngle = 45.0;
185  if ( lookup.size() )
186  {
187  ossim_float64 f = lookup.toFloat64();
188  if ( (f >= 0.0) && (f <= 90) )
189  {
190  elevationAngle = f;
191  }
192  }
193  bumpShade->setElevationAngle(elevationAngle);
194 
195 
196  // Color can be added via color image source:
197  if (!m_imgLayers.empty())
198  {
199  // A color source image (or list) is provided. Add them as input to bump shade:
201  bumpShade->connectMyInputTo(1, colorSource.get());
202  }
203  else
204  {
205  // Default colors are grey:
206  ossim_uint8 r = 0xff;
207  ossim_uint8 g = 0xff;
208  ossim_uint8 b = 0xff;
209  lookup = m_kwl.findKey( COLOR_RED_KW );
210  if ( lookup.size() )
211  r = lookup.toUInt8();
212  lookup = m_kwl.findKey( COLOR_GREEN_KW );
213  if ( lookup.size() )
214  g = lookup.toUInt8();
215  lookup = m_kwl.findKey( COLOR_BLUE_KW );
216  if ( lookup.size() )
217  b = lookup.toUInt8();
218  bumpShade->setRgbColorSource(r, g, b);
219  }
220 }
221 
223 {
224  // Add global usage options.
226 
227  // Set app name.
228  ossimString appName = ap.getApplicationName();
230  ossimString usageString = appName;
231  usageString += " hillshade [option]... [input-option]... <input-file(s)> <output-file>\nNote at least one input is required either from one of the input options, e.g. --input-dem <my-dem.hgt> or adding to command line in front of the output file in which case the code will try to ascertain what type of input it is.\n\nAvailable traces:\n-T \"ossimChipperUtil:debug\" - General debug trace to standard out.\n-T \"ossimChipperUtil:log\" - Writes a log file to output-file.log.\n-T \"ossimChipperUtil:options\" - Writes the options to output-file-options.kwl.";
232  au->setCommandLineUsage(usageString);
233 
234  // Add arguments.
235  au->addCommandLineOption("--azimuth", "<azimuth>\nLight source azimuth angle for bump shade.\nRange: 0 to 360, Default = 180.0");
236  au->addCommandLineOption("--color","<r> <g> <b>\nSet the red, green and blue color values to be used with hillshade.\nRange 0 to 255, Defualt r=255, g=255, b=255");
237  au->addCommandLineOption("--color-source","<file>\nSpecifies the image file to use as a color source instead of a fixed RGB value.");
238  au->addCommandLineOption("--elevation", "<elevation>\nhillshade option - Light source elevation angle for bumb shade.\nRange: 0 to 90, Default = 45.0");
239 
240  // Base class has its own:
242 
243  ostringstream description;
244  description << DESCRIPTION << "\n\nNOTES:\n"
245  << "1) Never use same base name in the same directory! Example is you have a Chicago.tif\n"
246  << " and you want a Chicago.jp2, output Chicago.jp2 to its own directory.\n"
247  << "\nExample command to Hill shade: Hill shade two DEMs, output to a geotiff.\n"
248  << appName << " --color 255 255 255 --azimuth 270 --elevation 45 --exaggeration 2.0 N37W123.hgt N38W123.hgt outputs/hillshade.tif\n"
249  << "\n// Above command where all options are in a keyword list:\n"
250  << appName << " --options r39-options.kwl\n"
251  << std::endl;
252 
253  au->setDescription(description.str());
254 
255 }
256 
virtual bool initialize(ossimArgumentParser &ap)
Initial method to be ran prior to execute.
std::string getApplicationName() const
return the application name, as specified by argv[0]
void addCommandLineOption(const ossimString &option, const ossimString &explanation)
This uses a derivative of Blinn&#39;s bump function to compute a 3-D looking image.
std::basic_ostringstream< char > ostringstream
Class for char output memory streams.
Definition: ossimIosFwd.h:35
static const char * FILE_KW
virtual void setUsage(ossimArgumentParser &ap)
Initializes the aurgument parser with expected parameters and options.
const std::string & findKey(const std::string &key) const
Find methods that take std::string(s).
bool m_helpRequested
Definition: ossimTool.h:150
ossim_uint8 toUInt8() const
static const char * DESCRIPTION
Used by ossimUtilityFactory.
void addOptions(ossimArgumentParser &parser)
Definition: ossimInit.cpp:100
bool read(const std::string &str)
search for an occurance of a string in the argument list, on sucess remove that occurance from the li...
static ossimString toString(bool aValue)
Numeric to string methods.
virtual void initProcessingChain()
Derived classes initialize their custom chains here.
ossimKeywordlist m_kwl
Definition: ossimTool.h:148
ossimRefPtr< ossimImageSource > combineLayers(std::vector< ossimRefPtr< ossimSingleImageChain > > &layers) const
When multiple input sources are present, this method instantiates a combiner and adds inputs...
void addPair(const std::string &key, const std::string &value, bool overwrite=true)
ossimHillshadeTool()
default constructor
void setCommandLineUsage(const ossimString &explanation)
ossimApplicationUsage * getApplicationUsage()
virtual ~ossimHillshadeTool()
virtual destructor
ossimRefPtr< ossimImageChain > m_procChain
virtual void setElevationAngle(double angle)
double ossim_float64
virtual void setUsage(ossimArgumentParser &ap)
Initializes the aurgument parser with expected parameters and options.
virtual void clear()
Disconnects and clears the dem and image layers.
std::string::size_type size() const
Definition: ossimString.h:405
void processRemainingArgs(ossimArgumentParser &ap)
Intended to be called after derived class has picked off its own options from the parser...
ossimRefPtr< ossimImageSource > mosaicDemSources()
Some utilities need to work on DEMs as images.
void setRgbColorSource(ossim_uint8 r, ossim_uint8 g, ossim_uint8 b)
Set the red, green and blue color source values.
ossim_float64 toFloat64() const
std::vector< ossimRefPtr< ossimSingleImageChain > > m_imgLayers
virtual void setAzimuthAngle(double angle)
virtual bool add(ossimConnectableObject *source)
Will return true or false if an image source was added to the chain.
virtual ossim_int32 connectMyInputTo(ossimConnectableObject *inputObject, bool makeOutputConnection=true, bool createEventFlag=true)
Will try to connect this objects input to the passed in object.
static const char * AZIMUTH_ANGLE_KW
void setDescription(const ossimString &desc)
const ossimHillshadeTool & operator=(const ossimHillshadeTool &rhs)
Hidden from use assignment operator.
static ossimInit * instance()
Definition: ossimInit.cpp:89
static const char * ELEVATION_ANGLE_KW
virtual bool initialize(ossimArgumentParser &ap)
Initial method to be ran prior to execute.
void setExpandEnvVarsFlag(bool flag)
unsigned char ossim_uint8
const std::string & string() const
Definition: ossimString.h:414