All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
RasterAttributes.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/rp/RasterAttributes.cpp
22 
23  \brief Extraction of attributes from Raster, Bands, and Polygons.
24 */
25 
26 // TerraLib
27 #include "../common/progress/TaskProgress.h"
28 #include "../geometry.h"
29 #include "../raster/Band.h"
30 #include "../raster/BandIterator.h"
31 #include "../raster/Grid.h"
32 #include "../raster/PositionIterator.h"
33 #include "../raster/Raster.h"
34 #include "../raster/Utils.h"
35 #include "../statistics.h"
36 #include "RasterAttributes.h"
37 
39 {
40 }
41 
43 {
44 }
45 
46 bool te::rp::RasterAttributes::initialize(const AlgorithmInputParameters& inputParams) throw(te::rp::Exception)
47 {
48  return true;
49 }
50 
52 {
53  return true;
54 }
55 
56 bool te::rp::RasterAttributes::execute(AlgorithmOutputParameters& outputParams) throw(te::rp::Exception)
57 {
58  return true;
59 }
60 
61 void te::rp::RasterAttributes::reset() throw(te::rp::Exception)
62 {
63 }
64 
65 std::vector<std::complex<double> > te::rp::RasterAttributes::getComplexValuesFromBand(const te::rst::Raster& raster, unsigned int band, const te::gm::Polygon& polygon)
66 {
67  std::vector<double> dvalues = getValuesFromBand(raster, band, polygon);
68  std::vector<std::complex<double> > values;
69 
70  for (unsigned int i = 0; i < dvalues.size(); i++)
71  values.push_back(dvalues[i]);
72 
73  return values;
74 }
75 
76 std::vector<double> te::rp::RasterAttributes::getValuesFromBand(const te::rst::Raster& raster, unsigned int band, const te::gm::Polygon& polygon)
77 {
78  assert(band < raster.getNumberOfBands());
79 
80  std::vector<double> values;
81 
82 // create iterators for band and polygon
85 
86  while (it != itend)
87  {
88 // using iterator
89  values.push_back((*it)[band]);
90 
91  ++it;
92  }
93 
94  return values;
95 }
96 
97 std::vector<std::vector<std::complex<double> > > te::rp::RasterAttributes::getComplexValuesFromRaster(const te::rst::Raster& raster, const te::gm::Polygon& polygon, std::vector<unsigned int> bands)
98 {
99  std::vector<std::vector<double> > dallvalues = getValuesFromRaster(raster, polygon, bands);
100  std::vector<std::complex<double> > values;
101  std::vector<std::vector<std::complex<double> > > allvalues;
102 
103  for (unsigned int i = 0; i < dallvalues.size(); i++)
104  {
105  values.clear();
106  for (unsigned int j = 0; j < dallvalues[i].size(); j++)
107  values.push_back(dallvalues[i][j]);
108  allvalues.push_back(values);
109  }
110 
111  return allvalues;
112 }
113 
114 std::vector<std::vector<double> > te::rp::RasterAttributes::getValuesFromRaster(const te::rst::Raster& raster, const te::gm::Polygon& polygon, std::vector<unsigned int> bands)
115 {
116  assert(bands.size() > 0);
117  assert(bands.size() <= raster.getNumberOfBands());
118 
119  std::vector<std::vector<double> > allvalues;
120  double value;
121 
122 // create iterators for band and polygon
125 
126  for (unsigned int i = 0; i < bands.size(); i++)
127  allvalues.push_back(std::vector<double> ());
128  while (it != itend)
129  {
130  for (unsigned int i = 0; i < bands.size(); i++)
131  {
132  raster.getValue(it.getColumn(), it.getRow(), value, bands[i]);
133  allvalues[i].push_back(value);
134  }
135 
136  ++it;
137  }
138 
139  return allvalues;
140 }
141 
143 {
144  assert(pixels.size() > 0);
145 
148 
149  return summary;
150 }
151 
152 boost::numeric::ublas::matrix<double> te::rp::RasterAttributes::getCovarianceMatrix(const std::vector<std::vector<double> >& vpixels, const std::vector<double>& vmeans)
153 {
154  for (unsigned int i = 0; i < vpixels.size(); i++)
155  assert(vpixels[i].size() > 0);
156  for (unsigned int i = 1; i < vpixels.size(); i++)
157  assert(vpixels[0].size() == vpixels[i].size());
158  assert(vpixels.size() == vmeans.size());
159 
160  unsigned int i;
161  unsigned int j;
162  unsigned int k;
163  unsigned int nbands = vpixels.size();
164  unsigned int nvalues = vpixels[0].size();
165 
166  boost::numeric::ublas::matrix<double> covariance(nbands, nbands);
167 
168 // with few values, the covariance is default
169  if (nvalues < 2)
170  {
171  for (i = 0; i < nbands; i++)
172  {
173  for (j = 0; j < nbands; j++)
174  covariance(i, j) = 0.0;
175  covariance(i, i) = 1000.0;
176  }
177 
178  return covariance;
179  }
180 
181 // compute covariance matrix based on values and means
182  te::common::TaskProgress taskProgress("Computing covariance matrix", te::common::TaskProgress::UNDEFINED, nbands * nbands);
183  std::complex<double> sum;
184  for (i = 0; i < nbands; i++)
185  for (j = 0; j < nbands; j++)
186  {
187  taskProgress.pulse();
188 
189  sum = std::complex<double> (0.0, 0.0);
190 
191  for (k = 0; k < nvalues; k++)
192  sum += (vpixels[i][k] - vmeans[i]) * (vpixels[j][k] - vmeans[j]);
193 
194  covariance(i, j) = sum.real() / (nvalues - 1);
195  }
196 
197  return covariance;
198 }
199 
200 boost::numeric::ublas::matrix<double> te::rp::RasterAttributes::getGLCM(const te::rst::Raster& rin, unsigned int band, int dx, int dy)
201 {
202  assert(band < rin.getNumberOfBands());
203 
204  double minPixel;
205  double maxPixel;
206  te::rst::GetDataTypeRanges(rin.getBandDataType(band), minPixel, maxPixel);
207 
208  boost::numeric::ublas::matrix<double> glcm (maxPixel + 1, maxPixel + 1);
209  glcm.clear();
210  double pixel;
211  double neighborPixel;
212  double noDataValue = rin.getBand(band)->getProperty()->m_noDataValue;
213  double N = 0.0;
214 
215 // defining limits for iteration
216  int row_start = 0;
217  int row_end = rin.getNumberOfRows();
218  int column_start = 0;
219  int column_end = rin.getNumberOfColumns();
220 
221  if (dy > 0)
222  row_end -= dy;
223  else
224  row_start -= dy;
225 
226  if (dx > 0)
227  column_end -= dx;
228  else
229  column_start -= dx;
230 
231 // computing GLCM
232  te::common::TaskProgress taskProgress("Computing the GLCM", te::common::TaskProgress::UNDEFINED, (row_end - row_start) * (column_end - column_start));
233  for (int r = row_start; r < row_end; r++)
234  {
235  for (int c = column_start; c < column_end; c++)
236  {
237  taskProgress.pulse();
238 
239 // get central pixel
240  rin.getValue(c, r, pixel, band);
241  if (pixel == noDataValue)
242  continue;
243 
244 // get neighbor pixel
245  rin.getValue(c + dx, r + dy, neighborPixel, band);
246  if (neighborPixel == noDataValue)
247  continue;
248 
249 // update GLCM matrix
250  glcm(pixel, neighborPixel) = glcm(pixel, neighborPixel) + 1;
251  N++;
252  }
253  }
254 
255  if (N > 0.0)
256  {
257  for (unsigned int i = 0; i < glcm.size1(); i++)
258  for (unsigned int j = 0; j < glcm.size2(); j++)
259  glcm(i, j) = glcm(i, j) / N;
260  }
261 
262  return glcm;
263 }
264 
265 boost::numeric::ublas::matrix<double> te::rp::RasterAttributes::getGLCM(const te::rst::Raster& rin, unsigned int band, int dx, int dy, const te::gm::Polygon& polygon)
266 {
267  // create raster crop with polygon
268  std::map<std::string, std::string> rcropinfo;
269  rcropinfo["FORCE_MEM_DRIVER"] = "TRUE";
270  te::rst::RasterPtr rcrop = te::rst::CropRaster(rin, polygon, rcropinfo, "MEM");
271 
272  // call previous method using crop
273  return getGLCM(*rcrop.get(), band, dx, dy);
274 }
275 
276 te::rp::Texture te::rp::RasterAttributes::getGLCMMetrics(boost::numeric::ublas::matrix<double> glcm)
277 {
278  te::rp::Texture metrics;
279 
280  unsigned int i;
281  unsigned int j;
282  double di;
283  double dj;
284  double di_minus_dj;
285  double square_di_minus_dj;
286  te::common::TaskProgress taskProgress("Computing GLCM Metrics", te::common::TaskProgress::UNDEFINED, glcm.size1() * glcm.size2());
287  for (i = 0, di = 0.0; i < glcm.size1(); i++, di++)
288  {
289  for (j = 0, dj = 0.0; j < glcm.size2(); j++, dj++)
290  {
291  taskProgress.pulse();
292 
293  di_minus_dj = (di - dj);
294  square_di_minus_dj = di_minus_dj * di_minus_dj;
295 
296  metrics.m_contrast += glcm(i, j) * square_di_minus_dj;
297  metrics.m_dissimilarity += glcm(i, j) * std::abs(di_minus_dj);
298  metrics.m_energy += glcm(i, j) * glcm(i, j);
299  if (glcm(i, j) > 0)
300  metrics.m_entropy += glcm(i, j) * (-1.0 * log(glcm(i, j)));
301  metrics.m_homogeneity += glcm(i, j) / (1 + square_di_minus_dj);
302  }
303  }
304 
305  if (metrics.m_energy > 0)
306  metrics.m_energy = std::sqrt(metrics.m_energy);
307 
308  return metrics;
309 }
TERASTEREXPORT te::rst::RasterPtr CropRaster(const te::rst::Raster &rin, const te::gm::Polygon &pin, const std::map< std::string, std::string > &rinfo, const std::string &rType=std::string("GDAL"))
Creates a raster crop using a polygon delimiter.
Definition: Utils.cpp:431
TESTATEXPORT void GetNumericStatisticalSummary(std::vector< double > &values, te::stat::NumericStatisticalSummary &ss, double nullVal)
A structure to hold the set of statistics from a set of numerical values.
boost::numeric::ublas::matrix< double > getCovarianceMatrix(const std::vector< std::vector< double > > &vpixels, const std::vector< double > &vmeans)
Returns the covariance matrix between vectors of pixel values.
TERASTEREXPORT void GetDataTypeRanges(const int &dataType, double &min, double &max)
Return the values range of a given data type.
Definition: Utils.cpp:334
unsigned int getNumberOfColumns() const
Returns the raster number of columns.
Definition: Raster.cpp:213
virtual const Band * getBand(std::size_t i) const =0
Returns the raster i-th band.
This class implements the strategy to iterate with spatial restriction, the iteration occurs inside a...
Extraction of attributes from Raster, Bands, and Polygons.
This class can be used to inform the progress of a task.
Definition: TaskProgress.h:53
Raster Processing algorithm output parameters base interface.
unsigned int getRow() const
Returns the current row in iterator.
boost::numeric::ublas::matrix< double > getGLCM(const te::rst::Raster &rin, unsigned int band, int dx, int dy)
Computes the Gray-Level CoOccurrence Matrix (GLCM) from a raster band.
double m_noDataValue
Value to indicate elements where there is no data, default is std::numeric_limits::max().
Definition: BandProperty.h:136
double m_energy
GLCM metric Energy (the square root of Angular Second Moment) ${{i,j=0}^{N-1}P_{i,j}^2}$.
Definition: Texture.h:77
void reset()
Clear all internal allocated objects and reset the algorithm to its initial state.
bool isInitialized() const
Returns true if the algorithm instance is initialized and ready for execution.
double m_homogeneity
GLCM metric Homogeneity (also called Inverse Difference Moment) ${i,j=0}^{N-1}{P_{i,j}}{1+(i-j)^2}$.
Definition: Texture.h:79
boost::shared_ptr< Raster > RasterPtr
Definition: Raster.h:685
static PolygonIterator end(const te::rst::Raster *r, const te::gm::Polygon *p)
Returns an iterator referring to after the end of the iterator.
An abstract class for raster data strucutures.
Definition: Raster.h:71
unsigned int getNumberOfRows() const
Returns the raster number of rows.
Definition: Raster.cpp:208
std::vector< std::complex< double > > getComplexValuesFromBand(const te::rst::Raster &raster, unsigned int band, const te::gm::Polygon &polygon)
Returns the pixel values (real and imag) for the band, inside the polygon.
BandProperty * getProperty()
Returns the band property.
Definition: Band.cpp:428
std::vector< std::vector< double > > getValuesFromRaster(const te::rst::Raster &raster, const te::gm::Polygon &polygon, std::vector< unsigned int > bands)
Returns the pixel values for all the bands in raster, inside the polygon.
virtual std::size_t getNumberOfBands() const =0
Returns the number of bands (dimension of cells attribute values) in the raster.
double m_dissimilarity
GLCM metric Dissimilarity ${i,j=0}^{N-1}P_{i,j}|i-j|$.
Definition: Texture.h:76
te::rp::Texture getGLCMMetrics(boost::numeric::ublas::matrix< double > glcm)
Compute texture metrics from GLCM matrix.
void pulse()
Calls setCurrentStep() function using getCurrentStep() + 1.
bool execute(AlgorithmOutputParameters &outputParams)
Executes the algorithm using the supplied parameters.
te::stat::NumericStatisticalSummary getStatistics(std::vector< double > &pixels)
Returns several statistics from a set of pixels.
virtual void getValue(unsigned int c, unsigned int r, double &value, std::size_t b=0) const
Returns the attribute value of a band of a cell.
Definition: Raster.cpp:228
RasterAttributes()
Public constructor.
double m_entropy
GLCM metric Entropy ${i,j=0}^{N-1}P_{i,j}(-{P_{i,j}})$.
Definition: Texture.h:78
double m_contrast
GLCM metric Contrast (also called Sum of Squares Variance) ${i,j=0}^{N-1}P_{i,j}(i-j)^2$.
Definition: Texture.h:75
std::vector< double > getValuesFromBand(const te::rst::Raster &raster, unsigned int band, const te::gm::Polygon &polygon)
Returns the pixel values for the band, inside the polygon.
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
static PolygonIterator begin(const te::rst::Raster *r, const te::gm::Polygon *p)
Returns an iterator referring to the first value of the band.
std::vector< std::vector< std::complex< double > > > getComplexValuesFromRaster(const te::rst::Raster &raster, const te::gm::Polygon &polygon, std::vector< unsigned int > bands)
Returns the pixel values (real and imag) for all the bands in raster, inside the polygon.
bool initialize(const AlgorithmInputParameters &inputParams)
Initialize the algorithm instance making it ready for execution.
A structure to hold the set of GLCM metrics.
Definition: Texture.h:44
Raster Processing algorithm input parameters base interface.
unsigned int getColumn() const
Returns the current column in iterator.
virtual int getBandDataType(std::size_t i) const =0
Returns the data type in a particular band (or dimension).