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) 2001-2009 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 "../geometry.h"
28 #include "../raster/Band.h"
29 #include "../raster/BandIterator.h"
30 #include "../raster/Grid.h"
31 #include "../raster/PositionIterator.h"
32 #include "../raster/Raster.h"
33 #include "../statistics.h"
34 #include "RasterAttributes.h"
35 
37 {
38 }
39 
41 {
42 }
43 
44 bool te::rp::RasterAttributes::initialize(const AlgorithmInputParameters& inputParams) throw(te::rp::Exception)
45 {
46  return true;
47 }
48 
50 {
51  return true;
52 }
53 
54 bool te::rp::RasterAttributes::execute(AlgorithmOutputParameters& outputParams) throw(te::rp::Exception)
55 {
56  return true;
57 }
58 
59 void te::rp::RasterAttributes::reset() throw(te::rp::Exception)
60 {
61 }
62 
63 std::vector<std::complex<double> > te::rp::RasterAttributes::getComplexValuesFromBand(const te::rst::Raster& raster, unsigned int band, const te::gm::Polygon& polygon)
64 {
65  std::vector<double> dvalues = getValuesFromBand(raster, band, polygon);
66  std::vector<std::complex<double> > values;
67 
68  for (unsigned int i = 0; i < dvalues.size(); i++)
69  values.push_back(dvalues[i]);
70 
71  return values;
72 }
73 
74 std::vector<double> te::rp::RasterAttributes::getValuesFromBand(const te::rst::Raster& raster, unsigned int band, const te::gm::Polygon& polygon)
75 {
76  assert(band < raster.getNumberOfBands());
77 
78  std::vector<double> values;
79 
80 // create iterators for band and polygon
83 
84  while (it != itend)
85  {
86 // using iterator
87  values.push_back((*it)[band]);
88 
89  ++it;
90  }
91 
92  return values;
93 }
94 
95 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)
96 {
97  std::vector<std::vector<double> > dallvalues = getValuesFromRaster(raster, polygon, bands);
98  std::vector<std::complex<double> > values;
99  std::vector<std::vector<std::complex<double> > > allvalues;
100 
101  for (unsigned int i = 0; i < dallvalues.size(); i++)
102  {
103  values.clear();
104  for (unsigned int j = 0; j < dallvalues[i].size(); j++)
105  values.push_back(dallvalues[i][j]);
106  allvalues.push_back(values);
107  }
108 
109  return allvalues;
110 }
111 
112 std::vector<std::vector<double> > te::rp::RasterAttributes::getValuesFromRaster(const te::rst::Raster& raster, const te::gm::Polygon& polygon, std::vector<unsigned int> bands)
113 {
114  assert(bands.size() > 0);
115  assert(bands.size() <= raster.getNumberOfBands());
116 
117  std::vector<std::vector<double> > allvalues;
118  double value;
119 
120 // create iterators for band and polygon
123 
124  for (unsigned int i = 0; i < bands.size(); i++)
125  allvalues.push_back(std::vector<double> ());
126  while (it != itend)
127  {
128  for (unsigned int i = 0; i < bands.size(); i++)
129  {
130  raster.getValue(it.getColumn(), it.getRow(), value, bands[i]);
131  allvalues[i].push_back(value);
132  }
133 
134  ++it;
135  }
136 
137  return allvalues;
138 }
139 
141 {
142  assert(pixels.size() > 0);
143 
146 
147  return summary;
148 }
149 
150 boost::numeric::ublas::matrix<double> te::rp::RasterAttributes::getCovarianceMatrix(const std::vector<std::vector<double> >& vpixels, const std::vector<double>& vmeans)
151 {
152  for (unsigned int i = 0; i < vpixels.size(); i++)
153  assert(vpixels[i].size() > 0);
154  for (unsigned int i = 1; i < vpixels.size(); i++)
155  assert(vpixels[0].size() == vpixels[i].size());
156  assert(vpixels.size() == vmeans.size());
157 
158  unsigned int i;
159  unsigned int j;
160  unsigned int k;
161  unsigned int nbands = vpixels.size();
162  unsigned int nvalues = vpixels[0].size();
163 
164  boost::numeric::ublas::matrix<double> covariance(nbands, nbands);
165 
166 // with few values, the covariance is default
167  if (nvalues < 2)
168  {
169  for (i = 0; i < nbands; i++)
170  {
171  for (j = 0; j < nbands; j++)
172  covariance(i, j) = 0.0;
173  covariance(i, i) = 1000.0;
174  }
175 
176  return covariance;
177  }
178 
179 // compute covariance matrix based on values and means
180  std::complex<double> sum;
181  for (i = 0; i < nbands; i++)
182  for (j = 0; j < nbands; j++)
183  {
184  sum = std::complex<double> (0.0, 0.0);
185 
186  for (k = 0; k < nvalues; k++)
187  sum += (vpixels[i][k] - vmeans[i]) * (vpixels[j][k] - vmeans[j]);
188 
189  covariance(i, j) = sum.real() / (nvalues - 1);
190  }
191 
192  return covariance;
193 }
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.
This class implements the strategy to iterate with spatial restriction, the iteration occurs inside a...
Extraction of attributes from Raster, Bands, and Polygons.
Raster Processing algorithm output parameters base interface.
unsigned int getRow() const
Returns the current row in iterator.
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.
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
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.
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.
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.
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.
Raster Processing algorithm input parameters base interface.
unsigned int getColumn() const
Returns the current column in iterator.