All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 "Raster.h"
30 #include "RasterSummary.h"
31 #include "RasterSummaryManager.h"
32 
33 // STL
34 #include <complex>
35 
36 std::string getConnInfoStr(const te::rst::Raster* raster);
37 
39 {
40  std::string connInfoStr = getConnInfoStr(raster);
41 
42  if(m_rasterSummaries.find(connInfoStr) != m_rasterSummaries.end())
43  m_rasterSummaries.erase(connInfoStr);
44 
45  if(!connInfoStr.empty())
46  m_rasterSummaries.insert(std::map<std::string, RasterSummary*>::value_type(connInfoStr, summary));
47 }
48 
50 {
51  std::string connInfoStr = getConnInfoStr(raster);
52 
53  std::map<std::string, RasterSummary*>::const_iterator it = m_rasterSummaries.find(connInfoStr);
54 
55  if (it == m_rasterSummaries.end())
56  return 0;
57 
58  return it->second;
59 }
60 
62 {
63  std::string connInfoStr = getConnInfoStr(raster);
64 
65  m_rasterSummaries.erase(connInfoStr);
66 }
67 
68 const te::rst::RasterSummary* te::rst::RasterSummaryManager::get(const Raster* raster, const SummaryTypes types, bool readall)
69 {
70  std::string connInfoStr = getConnInfoStr(raster);
71 
72  std::map<std::string, RasterSummary*>::const_iterator it = m_rasterSummaries.find(connInfoStr);
73 
74  te::rst::RasterSummary* rs = 0;
75 
76  if(it == m_rasterSummaries.end())
77  {
78  rs = new te::rst::RasterSummary(raster->getNumberOfBands());
79 
80  for (std::size_t i = 0; i < raster->getNumberOfBands(); i++)
81  rs->push_back(new te::rst::BandSummary());
82 
83  add(raster, rs);
84  }
85  else
86  rs = it->second;
87 
88  for (std::size_t b = 0; b < raster->getNumberOfBands(); b++)
89  {
90  te::rst::BandSummary& bs = (*rs)[b];
91 
92  if (types & te::rst::SUMMARY_R_HISTOGRAM && bs.m_histogramR == 0)
93  {
94  bs.m_histogramR = new std::map<double, unsigned>(raster->getBand(b)->getHistogramR());
95 
96  std::map<double, unsigned>::iterator it = bs.m_histogramR->begin();
97 
98  if (!bs.m_minVal)
99  bs.m_minVal = new std::complex<double>(it->first, 0.0);
100  else
101  bs.m_minVal = new std::complex<double>(it->first, bs.m_minVal->imag());
102 
103  it = bs.m_histogramR->end();
104 
105  if (!bs.m_maxVal)
106  bs.m_maxVal = new std::complex<double>((--it)->first, 0.0);
107  else
108  bs.m_maxVal = new std::complex<double>((--it)->first, bs.m_maxVal->imag());
109  }
110 
111  if (types & te::rst::SUMMARY_I_HISTOGRAM && bs.m_histogramI == 0)
112  {
113  bs.m_histogramI = new std::map<double, unsigned>(raster->getBand(b)->getHistogramI());
114 
115  std::map<double, unsigned>::iterator it = bs.m_histogramI->begin();
116 
117  if (!bs.m_minVal)
118  bs.m_minVal = new std::complex<double>(0.0, it->first);
119  else
120  bs.m_minVal = new std::complex<double>(bs.m_minVal->real(), it->first);
121 
122  it = bs.m_histogramI->end();
123 
124  if (!bs.m_maxVal)
125  bs.m_maxVal = new std::complex<double>(0.0, (--it)->first);
126  else
127  bs.m_maxVal = new std::complex<double>(bs.m_maxVal->real(), (--it)->first);
128  }
129 
130  if (types & te::rst::SUMMARY_MIN && bs.m_minVal == 0)
131  bs.m_minVal = new std::complex<double>(raster->getBand(b)->getMinValue(readall));
132 
133  if (types & te::rst::SUMMARY_MAX && bs.m_maxVal == 0)
134  bs.m_maxVal = new std::complex<double>(raster->getBand(b)->getMaxValue(readall));
135 
136  if (types & te::rst::SUMMARY_STD && bs.m_stdVal == 0)
137  bs.m_stdVal = new std::complex<double>(raster->getBand(b)->getStdValue());
138 
139  if (types & te::rst::SUMMARY_MEAN && bs.m_meanVal == 0)
140  bs.m_meanVal = new std::complex<double>(raster->getBand(b)->getMeanValue());
141  }
142 
143  return rs;
144 }
145 
147 {
148  te::common::FreeContents(m_rasterSummaries);
149 
150  m_rasterSummaries.clear();
151 }
152 
154 {
155 }
156 
157 std::string getConnInfoStr(const te::rst::Raster* raster)
158 {
159  std::string connInfoStr = "";
160 
161  std::map<std::string, std::string> connInfo = raster->getInfo();
162 
163  std::map<std::string, std::string>::iterator it = connInfo.begin();
164 
165  while(it != connInfo.end())
166  {
167  connInfoStr += it->first;
168  connInfoStr += "=";
169  connInfoStr += it->second;
170  connInfoStr += ";";
171 
172  ++it;
173  }
174 
175  return connInfoStr;
176 }
177 
Calculate the histogram for the imaginary part.
Definition: Enums.h:45
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
virtual std::map< std::string, std::string > getInfo() const =0
It returns additional information about the raster.
std::complex< double > * m_minVal
The minimum occurring values (real and imaginary), default is std::numeric_limits::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).
virtual const Band * getBand(std::size_t i) const =0
Returns the raster i-th band.
std::complex< double > * m_stdVal
The standard deviation of the occurring values (real and imaginary), default is 1.0.
Definition: BandSummary.h:80
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...
Definition: Band.cpp:260
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)
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...
Definition: Band.cpp:334
std::complex< double > * m_maxVal
The maximum occurring values (real and imaginary), default is std::numeric_limits::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.
Definition: Band.cpp:139
const RasterSummary * find(const Raster *raster) const
It searches for a raster summary.
An abstract class for raster data strucutures.
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...
Definition: Band.cpp:196
An abstract class for raster data strucutures.
Definition: Raster.h:71
Calculate the standard deviation value.
Definition: Enums.h:42
SummaryTypes
Types for the BandSummary.
Definition: Enums.h:38
virtual std::size_t getNumberOfBands() const =0
Returns the number of bands (dimension of cells attribute values) in the raster.
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
It gives access to values in one band (dimension) of a raster.
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.
Definition: Band.cpp:82
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. ...
Definition: Band.cpp:231
Calculate the min value.
Definition: Enums.h:40
Calculate the max value.
Definition: Enums.h:41
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.
Definition: Enums.h:43
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.
Definition: Enums.h:44