RasterSummaryManager.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/raster/RasterSummaryManager.cpp
22 
23  \brief A singleton for keeping raster summaries (most statistics).
24 */
25 
26 // TerraLib
27 #include "../common/STLUtils.h"
28 #include "Band.h"
29 #include "BandProperty.h"
30 #include "Raster.h"
31 #include "RasterSummary.h"
32 #include "RasterSummaryManager.h"
33 
34 // STL
35 #include <complex>
36 
37 std::string getConnInfoStr(const te::rst::Raster* raster);
38 
40 {
41  std::string connInfoStr = getConnInfoStr(raster);
42 
43  if(m_rasterSummaries.find(connInfoStr) != m_rasterSummaries.end())
44  m_rasterSummaries.erase(connInfoStr);
45 
46  if(!connInfoStr.empty())
47  m_rasterSummaries.insert(std::map<std::string, RasterSummary*>::value_type(connInfoStr, summary));
48 }
49 
51 {
52  std::string connInfoStr = getConnInfoStr(raster);
53 
54  std::map<std::string, RasterSummary*>::const_iterator it = m_rasterSummaries.find(connInfoStr);
55 
56  if (it == m_rasterSummaries.end())
57  return nullptr;
58 
59  return it->second;
60 }
61 
63 {
64  std::string connInfoStr = getConnInfoStr(raster);
65 
66  m_rasterSummaries.erase(connInfoStr);
67 }
68 
69 const te::rst::RasterSummary* te::rst::RasterSummaryManager::get(const Raster* raster, const SummaryTypes types, bool readall)
70 {
71  std::string connInfoStr = getConnInfoStr(raster);
72 
73  std::map<std::string, RasterSummary*>::const_iterator it = m_rasterSummaries.find(connInfoStr);
74 
75  te::rst::RasterSummary* rs = nullptr;
76 
77  if(it == m_rasterSummaries.end())
78  {
79  rs = new te::rst::RasterSummary(raster->getNumberOfBands());
80 
81  for (std::size_t i = 0; i < raster->getNumberOfBands(); i++)
82  rs->push_back(new te::rst::BandSummary());
83 
84  add(raster, rs);
85  }
86  else
87  rs = it->second;
88 
89  for (std::size_t b = 0; b < raster->getNumberOfBands(); b++)
90  {
91  const int inputDataType = raster->getBand( b )->getProperty()->getType();
92 
93  const bool isRealValuesBand = ( inputDataType == te::dt::FLOAT_TYPE ) ||
94  ( inputDataType == te::dt::DOUBLE_TYPE );
95 
96  const unsigned int histoSize = isRealValuesBand ? m_realValuesRasterHistSize : 0;
97 
98  te::rst::BandSummary& bs = (*rs)[b];
99 
100  if (types & te::rst::SUMMARY_R_HISTOGRAM && bs.m_histogramR == nullptr)
101  {
102  bs.m_histogramR = new std::map<double, unsigned>(raster->getBand(b)->getHistogramR(
103  0, 0, 0, 0, histoSize ) );
104 
105  std::map<double, unsigned>::iterator it = bs.m_histogramR->begin();
106 
107  if (!bs.m_minVal)
108  bs.m_minVal = new std::complex<double>(it->first, 0.0);
109  else
110  bs.m_minVal = new std::complex<double>(it->first, bs.m_minVal->imag());
111 
112  it = bs.m_histogramR->end();
113 
114  if (!bs.m_maxVal)
115  bs.m_maxVal = new std::complex<double>((--it)->first, 0.0);
116  else
117  bs.m_maxVal = new std::complex<double>((--it)->first, bs.m_maxVal->imag());
118  }
119 
120  if (types & te::rst::SUMMARY_I_HISTOGRAM && bs.m_histogramI == nullptr)
121  {
122  bs.m_histogramI = new std::map<double, unsigned>(raster->getBand(b)->getHistogramI(
123  0, 0, 0, 0, histoSize ) );
124 
125  std::map<double, unsigned>::iterator it = bs.m_histogramI->begin();
126 
127  if (!bs.m_minVal)
128  bs.m_minVal = new std::complex<double>(0.0, it->first);
129  else
130  bs.m_minVal = new std::complex<double>(bs.m_minVal->real(), it->first);
131 
132  it = bs.m_histogramI->end();
133 
134  if (!bs.m_maxVal)
135  bs.m_maxVal = new std::complex<double>(0.0, (--it)->first);
136  else
137  bs.m_maxVal = new std::complex<double>(bs.m_maxVal->real(), (--it)->first);
138  }
139 
140  if (types & te::rst::SUMMARY_MIN)
141  {
142  if(bs.m_minVal == nullptr)
143  bs.m_minVal = new std::complex<double>(raster->getBand(b)->getMinValue(readall));
144  }
145 
146  if (types & te::rst::SUMMARY_MAX)
147  {
148  if (bs.m_maxVal == nullptr)
149  bs.m_maxVal = new std::complex<double>(raster->getBand(b)->getMaxValue(readall));
150  }
151 
152  if (types & te::rst::SUMMARY_STD)
153  {
154  if (bs.m_stdVal == nullptr)
155  bs.m_stdVal = new std::complex<double>(raster->getBand(b)->getStdValue());
156  }
157 
158  if (types & te::rst::SUMMARY_MEAN)
159  {
160  if (bs.m_meanVal == nullptr)
161  bs.m_meanVal = new std::complex<double>(raster->getBand(b)->getMeanValue());
162  }
163  }
164 
165  return rs;
166 }
167 
169 {
171 }
172 
174 {
175  m_realValuesRasterHistSize = newSize;
176 }
177 
179 {
181 
182  m_rasterSummaries.clear();
183 }
184 
187 {
188 }
189 
190 std::string getConnInfoStr(const te::rst::Raster* raster)
191 {
192  std::string connInfoStr;
193 
194  std::map<std::string, std::string> connInfo = raster->getInfo();
195 
196  std::map<std::string, std::string>::iterator it = connInfo.begin();
197 
198  while(it != connInfo.end())
199  {
200  connInfoStr += it->first;
201  connInfoStr += "=";
202  connInfoStr += it->second;
203  connInfoStr += ";";
204 
205  ++it;
206  }
207 
208  return connInfoStr;
209 }
210 
Calculate the histogram for the imaginary part.
std::map< double, unsigned int > * m_histogramR
The histogram (a map of occurring values and frequency) of the real part of a band.
Definition: BandSummary.h:82
It gives access to values in one band (dimension) of a raster.
It describes one band (or dimension) of a raster.
std::complex< double > * m_minVal
The minimum occurring values (real and imaginary), default is std::numeric_limits<double>::min().
Definition: BandSummary.h:78
std::map< std::string, RasterSummary * > m_rasterSummaries
A map of rasters conn info and their respective summaries.
A singleton for keeping raster summaries (most statistics).
std::complex< double > * m_stdVal
The standard deviation of the occurring values (real and imaginary), default is 1.0.
Definition: BandSummary.h:80
const unsigned int getRealValuesRasterHistSize() const
Returns the current real values raster histogram size.
An abstract class for raster data strucutures.
virtual std::map< double, unsigned > getHistogramR(unsigned int rs=0, unsigned int cs=0, unsigned int rf=0, unsigned int cf=0, unsigned int b=0) const
It computes and returns the histogram occurring values (real part) in a window of the band...
std::map< double, unsigned int > * m_histogramI
The histogram (a map of occurring values and frequency) of the imaginary part of a band...
Definition: BandSummary.h:83
void add(const Raster *raster, RasterSummary *summary)
Adds a new entry in the summary manager.
std::string getConnInfoStr(const te::rst::Raster *raster)
int b
Definition: TsRtree.cpp:32
virtual std::map< std::string, std::string > getInfo() const =0
It returns additional information about the raster.
virtual std::map< double, unsigned > getHistogramI(unsigned int rs=0, unsigned int cs=0, unsigned int rf=0, unsigned int cf=0, unsigned int b=0) const
It computes and returns the histogram occurring values (imaginary part) in a window of the band...
std::complex< double > * m_maxVal
The maximum occurring values (real and imaginary), default is std::numeric_limits<double>::max().
Definition: BandSummary.h:79
virtual std::complex< double > getMaxValue(bool readall=false, unsigned int rs=0, unsigned int cs=0, unsigned int rf=0, unsigned int cf=0) const
It computes and returns the maximum occurring value in a window of the band.
const RasterSummary * find(const Raster *raster) const
It searches for a raster summary.
virtual std::complex< double > getStdValue(unsigned int rs=0, unsigned int cs=0, unsigned int rf=0, unsigned int cf=0) const
It computes and returns the standard deviation of the occurring values in a window of the band...
An abstract class for raster data strucutures.
virtual std::size_t getNumberOfBands() const =0
Returns the number of bands (dimension of cells attribute values) in the raster.
Calculate the standard deviation value.
BandProperty * getProperty()
Returns the band property.
SummaryTypes
Types for the BandSummary.
RasterSummary is just a typedef of a boost::ptr_vector.
A summary of a raster band (most statistics).
Definition: BandSummary.h:47
boost::ptr_vector< BandSummary > RasterSummary
RasterSummary is just a typedef of a boost::ptr_vector.
Definition: RasterSummary.h:44
unsigned int m_realValuesRasterHistSize
Real values raster histogram size (default:255).
virtual const Band * getBand(std::size_t i) const =0
Returns the raster i-th band.
const RasterSummary * get(const Raster *raster, const SummaryTypes st, bool readall=false)
It searches for a raster summary. If not found it creates the summary and returns it...
virtual std::complex< double > getMinValue(bool readall=false, unsigned int rs=0, unsigned int cs=0, unsigned int rf=0, unsigned int cf=0) const
It computes and returns the minimum occurring value in a window of the band.
virtual std::complex< double > getMeanValue(unsigned int rs=0, unsigned int cs=0, unsigned int rf=0, unsigned int cf=0) const
It computes and returns the mean of the occurring values in a window of the band. ...
int getType() const
It returns the data type of the elements in the band.
Definition: BandProperty.h:113
void setRealValuesRasterHistSize(const unsigned int newSize)
Set the real values raster histogram size.
Calculate the min value.
Calculate the max value.
void remove(const Raster *raster)
Removes the summary from the specified raster.
void FreeContents(boost::unordered_map< K, V * > &m)
This function can be applied to a map of pointers. It will delete each pointer in the map...
Definition: BoostUtils.h:55
Calculate the mean value.
std::complex< double > * m_meanVal
The mean of the occurring values (real and imaginary), default is 0.0.
Definition: BandSummary.h:81
Calculate the histogram for the real part.