CloudDetection.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/clouddetection/CloudDetection.cpp
22 
23  \brief Implementation of the cloud detection.
24 */
25 
26 // Terralib
27 #include "../raster.h"
28 #include "../rp/Filter.h"
29 #include "CloudDetection.h"
30 
32 
34 
35 te::rst::Raster* te::rp::CloudDetection::executeCloudDetection(te::rst::Raster* cloudRaster, te::rst::Raster* shadowRaster, int cloudBand, int shadowBand, double minCloud, double maxCloud,
36  double minShadow, double maxShadow, std::vector<te::rp::Filter::InputParameters::FilterType> vecFilter, std::vector<int> vecIt)
37 {
38  //cloud limiar
39  te::rst::Raster* limiarCloudRaster = generateCloudRaster(cloudRaster, cloudBand, minCloud, maxCloud);
40 
41  //shadow limiar
42  te::rst::Raster* limiarShadowRaster = generateShadowRaster(shadowRaster, shadowBand, minShadow, maxShadow);
43 
44  //Sum of cloud limiar and shadow limiar
45  te::rst::Raster* raster = generateSumRasters(limiarCloudRaster, limiarShadowRaster);
46 
47  //Apply morphological filters
48  for (unsigned int i = 0; i < vecFilter.size(); i++)
49  {
50  {
51  //run filter
52  te::rp::Filter algorithmInstance;
53 
54  te::rp::Filter::InputParameters algoInputParams;
55  algoInputParams.m_iterationsNumber = static_cast<unsigned int>(vecIt[i]);
56  algoInputParams.m_enableProgress = true;
57  algoInputParams.m_filterType = vecFilter[i];
58  algoInputParams.m_inRasterPtr = raster;
59  algoInputParams.m_inRasterBands.push_back(0);
60 
61  std::map< std::string, std::string > dummyRInfo;
62 
63  te::rp::Filter::OutputParameters algoOutputParams;
64  algoOutputParams.m_rInfo = dummyRInfo;
65  algoOutputParams.m_rType = "MEM";
66 
67  if (algorithmInstance.initialize(algoInputParams))
68  {
69  if (!algorithmInstance.execute(algoOutputParams))
70  {
71  return nullptr;
72  }
73  }
74 
75  delete raster;
76 
77  raster = algoOutputParams.m_outputRasterPtr.release();
78  }
79  }
80 
81  //adjust dummy value
82  std::size_t nBands = raster->getNumberOfBands();
83 
84  for (std::size_t t = 0; t < nBands; ++t)
85  {
86  te::rst::BandProperty* bProp = raster->getBand(t)->getProperty();
87  bProp->m_noDataValue = 255.;
88  }
89 
90  delete limiarCloudRaster;
91  delete limiarShadowRaster;
92 
93  return raster;
94 }
95 
96 te::rst::Raster* te::rp::CloudDetection::generateCloudRaster(te::rst::Raster* raster, int cloudBand, double minCloud, double maxCloud)
97 {
98  //load to memory
99  std::vector<te::rst::BandProperty*> bprops;
100 
102  bp->m_noDataValue = 255.;
103 
104  bprops.push_back(bp);
105 
106  std::map< std::string, std::string > dummyRInfo;
107 
108  te::rst::Raster* rasterMem = te::rst::RasterFactory::make("MEM", new te::rst::Grid(*(raster->getGrid())), bprops, dummyRInfo, nullptr, nullptr);
109 
110  const te::rst::Band& bin = *raster->getBand(static_cast<size_t>(cloudBand));
111  const unsigned int nRows = raster->getNumberOfRows();
112  const unsigned int nCols = raster->getNumberOfColumns();
113 
114  for (unsigned int row = 0; row < nRows; ++row)
115  {
116  for (unsigned int col = 0; col < nCols; ++col)
117  {
118  double val;
119  bin.getValue(col, row, val);
120 
121  if (val >= minCloud && val <= maxCloud)
122  rasterMem->setValue(col, row, 0.);
123  else
124  rasterMem->setValue(col, row, 255.);
125  }
126  }
127 
128  return rasterMem;
129 }
130 
131 te::rst::Raster* te::rp::CloudDetection::generateShadowRaster(te::rst::Raster* raster, int shadowBand, double minShadow, double maxShadow)
132 {
133  //load to memory
134  std::vector<te::rst::BandProperty*> bprops;
135 
137  bp->m_noDataValue = 255.;
138 
139  bprops.push_back(bp);
140 
141  std::map< std::string, std::string > dummyRInfo;
142 
143  te::rst::Raster* rasterMem = te::rst::RasterFactory::make("MEM", new te::rst::Grid(*(raster->getGrid())), bprops, dummyRInfo, nullptr, nullptr);
144 
145  const te::rst::Band& bin = *raster->getBand(static_cast<size_t>(shadowBand));
146  const unsigned int nRows = raster->getNumberOfRows();
147  const unsigned int nCols = raster->getNumberOfColumns();
148 
149  for (unsigned int row = 0; row < nRows; ++row)
150  {
151  for (unsigned int col = 0; col < nCols; ++col)
152  {
153  double val;
154  bin.getValue(col, row, val);
155 
156  if (val >= minShadow && val <= maxShadow)
157  rasterMem->setValue(col, row, 0.);
158  else
159  rasterMem->setValue(col, row, 255.);
160  }
161  }
162 
163  return rasterMem;
164 }
165 
167 {
168  //load to memory
169  std::vector<te::rst::BandProperty*> bprops;
170 
172 
173  bprops.push_back(bp);
174 
175  std::map< std::string, std::string > dummyRInfo;
176 
177  te::rst::Raster* rasterMem = te::rst::RasterFactory::make("MEM", new te::rst::Grid(*(cloudRaster->getGrid())), bprops, dummyRInfo, nullptr, nullptr);
178 
179  const te::rst::Band& cloudBand = *cloudRaster->getBand(0);
180  const te::rst::Band& shadowBand = *shadowRaster->getBand(0);
181  const unsigned int nRows = cloudRaster->getNumberOfRows();
182  const unsigned int nCols = cloudRaster->getNumberOfColumns();
183 
184  for (unsigned int row = 0; row < nRows; ++row)
185  {
186  for (unsigned int col = 0; col < nCols; ++col)
187  {
188  double cloudValue;
189  cloudBand.getValue(col, row, cloudValue);
190 
191  if (cloudValue == 255.)
192  {
193  double shadowValue;
194  shadowBand.getValue(col, row, shadowValue);
195 
196  rasterMem->setValue(col, row, shadowValue);
197  }
198  else
199  {
200  rasterMem->setValue(col, row, cloudValue);
201  }
202  }
203  }
204 
205  return rasterMem;
206 }
virtual void setValue(unsigned int c, unsigned int r, const double value, std::size_t b=0)
Sets the attribute value in a band of a cell.
bool m_enableProgress
Enable/Disable the progress interface (default:false).
Definition: rp/Filter.h:85
A raster band description.
Definition: BandProperty.h:61
unsigned int getNumberOfColumns() const
Returns the raster number of columns.
bool execute(AlgorithmOutputParameters &outputParams) _NOEXCEPT_OP(false)
Executes the algorithm using the supplied parameters.
std::string m_rType
Output raster data source type (as described in te::raster::RasterFactory ).
Definition: rp/Filter.h:113
te::rst::Raster * generateSumRasters(te::rst::Raster *cloudRaster, te::rst::Raster *shadowRaster)
double m_noDataValue
Value to indicate elements where there is no data, default is std::numeric_limits<double>::max().
Definition: BandProperty.h:136
std::vector< unsigned int > m_inRasterBands
Bands to be used from the input raster 1.
Definition: rp/Filter.h:77
te::rst::Raster * executeCloudDetection(te::rst::Raster *cloudRaster, te::rst::Raster *shadowRaster, int cloudBand, int shadowBand, double minCloud, double maxCloud, double minShadow, double maxShadow, std::vector< te::rp::Filter::InputParameters::FilterType > vecFilter, std::vector< int > vecIt)
This method is used to run cloud detection operation.
std::map< std::string, std::string > m_rInfo
The necessary information to create the raster (as described in te::raster::RasterFactory).
Definition: rp/Filter.h:115
unsigned int unsigned int nCols
unsigned int m_iterationsNumber
The number of iterations to perform (default:1).
Definition: rp/Filter.h:79
An abstract class for raster data strucutures.
unsigned int getNumberOfRows() const
Returns the raster number of rows.
virtual std::size_t getNumberOfBands() const =0
Returns the number of bands (dimension of cells attribute values) in the raster.
BandProperty * getProperty()
Returns the band property.
A raster band description.
virtual const Band * getBand(std::size_t i) const =0
Returns the raster i-th band.
Grid * getGrid()
It returns the raster grid.
A series of well-known filtering algorithms for images, linear and non-linear..
Definition: rp/Filter.h:47
te::rst::Raster * generateCloudRaster(te::rst::Raster *raster, int cloudBand, double minCloud, double maxCloud)
Filter input parameters.
Definition: rp/Filter.h:55
static Raster * make()
It creates and returns an empty raster with default raster driver.
FilterType m_filterType
The edge filter type.
Definition: rp/Filter.h:73
te::rst::Raster const * m_inRasterPtr
Input raster.
Definition: rp/Filter.h:75
bool initialize(const AlgorithmInputParameters &inputParams) _NOEXCEPT_OP(false)
Initialize the algorithm instance making it ready for execution.
Filter output parameters.
Definition: rp/Filter.h:109
A rectified grid is the spatial support for raster data.
Definition: raster/Grid.h:68
te::rst::Raster * generateShadowRaster(te::rst::Raster *raster, int shadowBand, double minShadow, double maxShadow)
unsigned int col
std::unique_ptr< te::rst::Raster > m_outputRasterPtr
A pointer the ge generated output raster (label image).
Definition: rp/Filter.h:117
virtual void getValue(unsigned int c, unsigned int r, double &value) const =0
Returns the cell attribute value.