All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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 "RasterAttributes.h"
34 
36 {
37 }
38 
40 {
41 }
42 
43 bool te::rp::RasterAttributes::initialize(const AlgorithmInputParameters& inputParams) throw(te::rp::Exception)
44 {
45  return true;
46 }
47 
49 {
50  return true;
51 }
52 
53 bool te::rp::RasterAttributes::execute(AlgorithmOutputParameters& outputParams) throw(te::rp::Exception)
54 {
55  return true;
56 }
57 
58 void te::rp::RasterAttributes::reset() throw(te::rp::Exception)
59 {
60 }
61 
62 std::vector<std::complex<double> > te::rp::RasterAttributes::getValuesFromBand(const te::rst::Raster& raster, unsigned int band, const te::gm::Polygon& polygon)
63 {
64  assert(band < raster.getNumberOfBands());
65 
66  std::vector<std::complex<double> > values;
67 
68 // create iterators for band and polygon
71 
72  while (it != itend)
73  {
74 // using iterator
75  values.push_back((*it)[band]);
76 
77  ++it;
78  }
79 
80  return values;
81 }
82 
83 std::vector<std::vector<std::complex<double> > > te::rp::RasterAttributes::getValuesFromRaster(const te::rst::Raster& raster, const te::gm::Polygon& polygon, std::vector<unsigned int> bands)
84 {
85  assert(bands.size() > 0);
86  assert(bands.size() <= raster.getNumberOfBands());
87 
88  std::vector<std::vector<std::complex<double> > > allvalues;
89  std::vector<std::complex<double> > values;
90  std::complex<double> value;
91 
92 // create iterators for band and polygon
95 
96  while (it != itend)
97  {
98  values.clear();
99 
100  for (unsigned int i = 0; i < bands.size(); i++)
101  {
102  raster.getValue(it.getColumn(), it.getRow(), value, bands[i]);
103 
104  values.push_back(value);
105  }
106 
107  allvalues.push_back(values);
108 
109  ++it;
110  }
111 
112  return allvalues;
113 }
114 
115 std::complex<double> te::rp::RasterAttributes::getMean(const te::rst::Band& band, const te::gm::Polygon& polygon)
116 {
117  std::vector<unsigned int> b;
118 
119  b.push_back(band.getProperty()->m_idx);
120 
121  return getMeans(*band.getRaster(), polygon, b)[0];
122 }
123 
124 std::vector<std::complex<double> > te::rp::RasterAttributes::getMeans(const te::rst::Raster& raster, const te::gm::Polygon& polygon, std::vector<unsigned int> bands)
125 {
126  assert(bands.size() > 0);
127  assert(bands.size() <= raster.getNumberOfBands());
128 
129  std::vector<std::complex<double> > means;
130 
131  for (unsigned int i = 0; i < bands.size(); i++)
132  means.push_back(0.0);
133 
134  unsigned int nvalues = 0;
135 
136  std::complex<double> value;
137 
140 
141  while (it != itend)
142  {
143  for (unsigned int i = 0; i < bands.size(); i++)
144  {
145  raster.getValue(it.getColumn(), it.getRow(), value, bands[i]);
146 
147  means[i] += value;
148  }
149 
150  ++nvalues;
151 
152  ++it;
153  }
154 
155  for (unsigned int i = 0; i < means.size() && nvalues != 0; i++)
156  means[i] /= nvalues;
157 
158  return means;
159 }
160 
161 boost::numeric::ublas::matrix<double> te::rp::RasterAttributes::getCovarianceMatrix(const te::rst::Raster& raster, const te::gm::Polygon& polygon, std::vector<unsigned int> bands)
162 {
163  assert(bands.size() > 1);
164  assert(bands.size() <= raster.getNumberOfBands());
165 
166  unsigned int i;
167  unsigned int j;
168  unsigned int k;
169  unsigned int nbands = bands.size();
170 
171  std::vector<std::vector<std::complex<double> > > valuesperband = getValuesFromRaster(raster, polygon, bands);
172 
173  unsigned int nvalues = valuesperband.size();
174 
175  boost::numeric::ublas::matrix<double> covariance(nbands, nbands);
176 
177  std::vector<std::complex<double> > meansperband = getMeans(raster, polygon, bands);
178 
179  if (nvalues < 2)
180  {
181  for (i = 0; i < nbands; i++)
182  {
183  for (j = 0; j < nbands; j++)
184  covariance(i, j) = 0.0;
185  covariance(i, i) = 1000.0;
186  }
187 
188  return covariance;
189  }
190 
191  std::complex<double> sum;
192 
193  for (i = 0; i < nbands; i++)
194  for (j = 0; j < nbands; j++)
195  {
196  sum = std::complex<double> (0.0, 0.0);
197 
198  for (k = 0; k < nvalues; k++)
199  sum += (valuesperband[k][i] - meansperband[i]) * (valuesperband[k][j] - meansperband[j]);
200 
201  covariance(i, j) = sum.real() / (nvalues - 1);
202  }
203 
204  return covariance;
205 }
std::vector< std::complex< 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.
bool initialize(const AlgorithmInputParameters &inputParams)
Initialize the algorithm instance making it ready for execution.
std::complex< double > getMean(const te::rst::Band &band, const te::gm::Polygon &polygon)
Returns the mean value for the pixels of a band, inside the polygon.
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
unsigned int getRow() const
Returns the current row in iterator.
bool execute(AlgorithmOutputParameters &outputParams)
Executes the algorithm using the supplied parameters.
virtual std::size_t getNumberOfBands() const =0
Returns the number of bands (dimension of cells attribute values) in the raster.
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
static PolygonIterator begin(const te::rst::Raster *r, const te::gm::Polygon *p)
Returns an iterator referring to the first value of the band.
void reset()
Clear all internal allocated objects and reset the algorithm to its initial state.
A raster band description.
Definition: Band.h:63
boost::numeric::ublas::matrix< double > getCovarianceMatrix(const te::rst::Raster &raster, const te::gm::Polygon &polygon, std::vector< unsigned int > bands)
Returns the covariance matrix between raster bands, inside the polygon.
virtual Raster * getRaster() const =0
Returns the associated raster.
RasterAttributes()
Public constructor.
std::vector< std::vector< std::complex< 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.
Extraction of attributes from Raster, Bands, and Polygons.
This class implements the strategy to iterate with spatial restriction, the iteration occurs inside a...
unsigned int getColumn() const
Returns the current column in iterator.
std::size_t m_idx
The band index.
Definition: BandProperty.h:132
bool isInitialized() const
Returns true if the algorithm instance is initialized and ready for execution.
std::vector< std::complex< double > > getMeans(const te::rst::Raster &raster, const te::gm::Polygon &polygon, std::vector< unsigned int > bands)
Returns the mean value for the pixels of a band, inside the polygon.
Raster Processing algorithm output parameters base interface.
An abstract class for raster data strucutures.
Definition: Raster.h:70
BandProperty * getProperty()
Returns the band property.
Definition: Band.cpp:370
static PolygonIterator end(const te::rst::Raster *r, const te::gm::Polygon *p)
Returns an iterator referring to after the end of the iterator.
Raster Processing algorithm input parameters base interface.