Shadow.cpp
Go to the documentation of this file.
1 /*!
2 \file terralib/mnt/core/Shadow.cpp
3 
4 \brief This file contains a class to shadow image generation.
5 
6 */
7 
8 #include "Shadow.h"
9 #include "Utils.h"
10 
11 //terralib
12 #include "../../common/progress/TaskProgress.h"
13 #include "../../core/translator/Translator.h"
14 #include "../../dataaccess/utils/Utils.h"
15 #include "../../geometry/GeometryProperty.h"
16 #include "../../raster/BandProperty.h"
17 #include "../../raster/Grid.h"
18 #include "../../raster/RasterFactory.h"
19 #include "../../raster/RasterProperty.h"
20 #include "../../srs/SpatialReferenceSystemManager.h"
21 
23  :m_out(nullptr),
24  m_srid(0),
25  m_azimuth(0.),
26  m_elevation(0.),
27  m_relief(0.),
28  m_vmin(0),
29  m_vmax(0),
30  m_minval(0),
31  m_maxval(0),
32  m_dummy(0),
33  m_outputWidth(0),
34  m_outputHeight(0),
35  m_resxo(0),
36  m_resyo(0)
37 {
38 }
39 
41 {
42  if (m_out)
43  delete m_out;
44 }
45 
47  std::string inDsetName,
48  const te::da::DataSetTypePtr& inDsetType)
49 {
50  m_inDsrc = inDsrc;
51  m_inDsetName = inDsetName;
52  m_inDsetType = inDsetType;
53 }
54 
55 void te::mnt::Shadow::setOutput(std::map<std::string, std::string> &dsinfo, std::string outRstDSType)
56 {
57  m_outDsinfo = dsinfo;
58  m_outRstDSType = outRstDSType;
59 }
60 
61 void te::mnt::Shadow::setParams(double azimuth, double elevation, double relief,
62  double vmin, double vmax, double minval, double maxval, double dummy,
63  unsigned int outputWidth, unsigned int outputHeight, double resxo, double resyo)
64 {
65  m_azimuth = azimuth;
66  m_elevation = elevation;
67  m_relief = relief;
68  m_vmin = vmin;
69  m_vmax = vmax;
70  m_minval = minval;
71  m_maxval = maxval;
72  m_dummy = dummy;
73  m_outputWidth = outputWidth;
74  m_outputHeight = outputHeight;
75  m_resxo = resxo;
76  m_resyo = resyo;
77 }
78 
80 {
81  //get input raster
82  if (!m_inDsetType.get()->hasRaster())
83  throw te::common::Exception(TE_TR("Layer isn't Regular Grid."));
84 
86  inDset = m_inDsrc->getDataSet(m_inDsetName);
87  std::unique_ptr<te::rst::Raster> raster(inDset->getRaster(rasterProp->getName()));
88  if (raster->getNumberOfBands() > 1)
89  throw te::common::Exception(TE_TR("Layer isn't Regular Grid."));
90 
91  m_out = GenerateImage(raster.get());
92 
93  return true;
94 }
95 
96 bool te::mnt::Shadow::calcLocalGradient(te::rst::Raster* inputRaster, int, int, double vmin, double vmax, double& dx, double& dy, double *val)
97 {
98  dx = 0;
99  dy = 0;
100 
101  double factor = 1;
102 
103  double deltaX = inputRaster->getResolutionX() * factor;
104  double deltaY = inputRaster->getResolutionY() * factor;
105 
106  if (((val[1] <= vmax) && (val[1] >= vmin)) && ((val[3] <= vmax) && (val[3] >= vmin)) &&
107  ((val[6] <= vmax) && (val[6] >= vmin)) && ((val[4] <= vmax) && (val[4] >= vmin)))
108  {
109  if (((val[0] <= vmax) && (val[0] >= vmin)) && ((val[2] <= vmax) && (val[2] >= vmin)) &&
110  ((val[5] <= vmax) && (val[5] >= vmin)) && ((val[7] <= vmax) && (val[7] >= vmin)))
111  {//Calculate dzdx and dzdy derivative values with neighbourhood 8
112  dx = ((val[7] + (2 * val[4]) + val[2]) - (val[5] + (2 * val[3]) + val[0])) / (8 * deltaX);
113  dy = ((val[7] + (2 * val[6]) + val[5]) - (val[2] + (2 * val[1]) + val[0])) / (8 * deltaY);
114  }
115  else
116  {//Calculate dzdx and dzdy derivative values with the neighbourhood 4
117  dx = (val[4] - val[3]) / (2 * deltaX);
118  dy = (val[6] - val[1]) / (2 * deltaY);
119  }
120  }
121  else
122  {//neighborhood 4 using extreme neighbors
123  if (((val[0] <= vmax) && (val[0] >= vmin)) && ((val[2] <= vmax) && (val[2] >= vmin)) &&
124  ((val[5] <= vmax) && (val[5] >= vmin)) && ((val[7] <= vmax) && (val[7] >= vmin)))
125  {
126  dx = ((val[7] + val[2]) - (val[5] + val[0])) / (4 * deltaX);
127  dy = ((val[7] + val[5]) - (val[2] + val[0])) / (4 * deltaY);
128  }
129  else
130  return false;
131  }
132 
133  return true;
134 }
135 
136 
137 void te::mnt::Shadow::getRasterElementLine(te::rst::Raster* inputRaster, int line, std::vector< std::complex<double> > &val)
138 {
139  int ncols = inputRaster->getNumberOfColumns();
140  std::vector< std::complex<double> > aux;
141  for (int col = 0; col < ncols; col++)
142  {
143  inputRaster->getValues(col, line, aux);
144  val[col] = aux[0];
145  }
146 }
147 
149 {
150  int X1 = static_cast<int>(raster->getExtent()->getLowerLeftX());
151  int Y2 = static_cast<int>(raster->getExtent()->getUpperRightY());
152  te::gm::Coord2D ulc(X1, Y2);
154  std::vector<te::rst::BandProperty*> bands;
155  bands.push_back(new te::rst::BandProperty(0, te::dt::DOUBLE_TYPE, "Shadow Image"));
156  bands[0]->m_nblocksx = 1;
157  bands[0]->m_nblocksy = (int)m_outputHeight;
158  bands[0]->m_blkw = (int)m_outputWidth;
159  bands[0]->m_blkh = 1;
160  bands[0]->m_colorInterp = te::rst::GrayIdxCInt;
161 
162  int nlines = raster->getNumberOfRows();
163  int ncols = raster->getNumberOfColumns();
164 
165  double pi = 3.1415927;
166  double teta = (90. - m_azimuth) * (pi / 180.);
167  double phi = (m_elevation * pi) / (double)180;
168  double exRelief = m_relief;
169 
170  //Set coefficients of the ilumination
171  double cx = cos(teta) * cos(phi);
172  double cy = sin(teta) * cos(phi);
173  double cz = sin(phi);
174 
175  double ambi = m_minval + ((m_maxval - m_minval) * 0.2);
176  double difu = m_maxval - ((m_maxval - m_minval) * 0.2);
177 
178  // te::common::TaskProgress task("Generating shadow image...", te::common::TaskProgress::UNDEFINED, (int)(m_outputHeight*m_outputWidth));
179 
180  // create raster
182  int nrows_out = static_cast<int>(out->getNumberOfRows());
183  int ncols_out = static_cast<int>(out->getNumberOfColumns());
184  for (int i = 0; i < nrows_out; i++)
185  for (int j = 0; j < ncols_out; j++)
186  out->setValue(j,i, m_dummy);
187 
188  std::vector < std::complex<double> > val1(ncols);
189  std::vector < std::complex<double> > val2(ncols);
190  std::vector < std::complex<double> > val3(ncols);
191 
192  for (int line = 0; line < nlines - 2; line++)
193  {
194  //if (!task.isActive())
195  // throw te::common::Exception(TE_TR("Canceled by user"));
196  //task.pulse();
197 
198  if (line == 0)
199  {
200  getRasterElementLine(raster, line, val1);
201  getRasterElementLine(raster, line + 1, val2);
202  getRasterElementLine(raster, line + 2, val3);
203  }
204  else
205  {
206  val1.swap(val2);
207  val2.swap(val3);
208  getRasterElementLine(raster, line + 2, val3);
209  }
210 
211  for (int col = 1; col < ncols - 1; col++)
212  {
213  double val[8];
214  val[0] = val1[col - 1].real();
215  val[1] = val1[col].real();
216  val[2] = val1[col + 1].real();
217  val[3] = val2[col - 1].real();
218  val[4] = val2[col + 1].real();
219  val[5] = val3[col - 1].real();
220  val[6] = val3[col].real();
221  val[7] = val3[col + 1].real();
222  double val0 = val2[col].real();
223 
224  if ((val0 >= m_vmin) && (val0 <= m_vmax))
225  {
226  double dzdx, dzdy;
227  if (calcLocalGradient(raster, line, col, m_vmin, m_vmax, dzdx, dzdy, val))
228  {
229  dzdx = (dzdx * exRelief);
230  dzdy = (dzdy * exRelief);
231 
232  double d = sqrt((dzdx * dzdx) + (dzdy * dzdy) + 1);
233  double costeta = (-(dzdx * cx) - (dzdy * cy) + cz) / d;
234 
235  if (costeta < 0)
236  costeta = 0;
237 
238  double value = ambi + difu * costeta;
239  out->setValue(col, line, value);
240  }
241  else
242  out->setValue(col, line, 0);
243  }
244  else
245  out->setValue(col, line, m_dummy);
246  } //for (int col = 1; col < ncols - 1; col++)
247  } //for (int line = 0; line < nlines; line++)
248 
249  val1.clear();
250  val2.clear();
251  val3.clear();
252 
253  return out;
254 }
virtual void setValue(unsigned int c, unsigned int r, const double value, std::size_t b=0)
Sets the attribute value in a band of a cell.
te::gm::Envelope * getExtent()
Returns the geographic extension of the raster data.
TEDATAACCESSEXPORT te::rst::RasterProperty * GetFirstRasterProperty(const DataSetType *dt)
This file contains a class to shadow image generation. Adapted from SPRING.
double m_relief
Definition: Shadow.h:68
boost::shared_ptr< DataSetType > DataSetTypePtr
Definition: DataSetType.h:653
Index into a lookup table.
double m_resxo
Definition: Shadow.h:77
bool calcLocalGradient(te::rst::Raster *inputRaster, int line, int col, double vmin, double vmax, double &dx, double &dy, double *val)
Definition: Shadow.cpp:96
std::map< std::string, std::string > m_outDsinfo
Definition: Shadow.h:60
te::da::DataSourcePtr m_inDsrc
Definition: Shadow.h:55
boost::shared_ptr< DataSource > DataSourcePtr
static te::dt::Date dx(2010, 12, 31)
A raster band description.
Definition: BandProperty.h:61
unsigned int getNumberOfColumns() const
Returns the raster number of columns.
virtual void getValues(unsigned int c, unsigned int r, std::vector< double > &values) const
Returns the imaginary attribute values in all complex bands of a cell.
double m_azimuth
Definition: Shadow.h:66
void setInput(te::da::DataSourcePtr inDsrc, std::string inDsetName, const te::da::DataSetTypePtr &inDsetType)
Definition: Shadow.cpp:46
double m_maxval
Definition: Shadow.h:71
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
double m_resyo
Definition: Shadow.h:77
const double & getUpperRightY() const
It returns a constant refernce to the x coordinate of the upper right corner.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
Raster property.
unsigned int line
std::string m_outRstDSType
Definition: Shadow.h:61
An abstract class for raster data strucutures.
int m_srid
Attribute with spatial reference information.
Definition: Shadow.h:64
unsigned int getNumberOfRows() const
Returns the raster number of rows.
double m_minval
Definition: Shadow.h:70
static te::dt::DateTime d(2010, 8, 9, 15, 58, 39)
double getResolutionX() const
Returns the raster horizontal (x-axis) resolution.
list bands
Definition: compose.py:2
void setParams(double azimuth, double elevation, double relief, double dummy, double vmin, double vmax, double minval, double maxval, unsigned int outputWidth, unsigned int outputHeight, double resxo, double resyo)
Definition: Shadow.cpp:61
double m_dummy
Definition: Shadow.h:73
Utility functions for the data access module.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
double m_vmin
Definition: Shadow.h:69
std::unique_ptr< te::da::DataSet > inDset
Definition: Shadow.h:58
double m_elevation
Definition: Shadow.h:67
te::rst::Raster * GenerateImage(te::rst::Raster *raster)
Definition: Shadow.cpp:148
void setOutput(std::map< std::string, std::string > &dsinfo, std::string outRstDSType)
Definition: Shadow.cpp:55
std::string m_inDsetName
Definition: Shadow.h:56
bool run()
Definition: Shadow.cpp:79
unsigned int m_outputWidth
Definition: Shadow.h:74
double m_vmax
Definition: Shadow.h:69
static Raster * make()
It creates and returns an empty raster with default raster driver.
const double & getLowerLeftX() const
It returns a constant reference to the x coordinate of the lower left corner.
double getResolutionY() const
Returns the raster vertical (y-axis) resolution.
void getRasterElementLine(te::rst::Raster *inputRaster, int line, std::vector< std::complex< double > > &val)
Definition: Shadow.cpp:137
te::da::DataSetTypePtr m_inDsetType
Definition: Shadow.h:57
A rectified grid is the spatial support for raster data.
Definition: raster/Grid.h:68
te::rst::Raster * m_out
Definition: Shadow.h:62
unsigned int col
unsigned int m_outputHeight
Definition: Shadow.h:75
const std::string & getName() const
It returns the property name.
Definition: Property.h:127