ClassifierStrategy.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/rp/ClassifierStrategy.cpp
22 
23  \brief Raster classifier strategy base class.
24 */
25 
26 #include "ClassifierStrategy.h"
27 #include "Macros.h"
28 #include "../raster.h"
29 #include "Functions.h"
30 
32 {
33  reset();
34 }
35 
37 
39 {
40  reset();
41  operator=( other );
42 }
43 
45 {
46  reset();
47  return *this;
48 }
49 
51 {
52  m_inputRasterPtr = &raster;
53 }
54 
55 void te::rp::ClassifierStrategy::setInputRasterBands( const std::vector<unsigned int>& rasterBands )
56 {
57  m_inputRasterBands = rasterBands;
58 }
59 
60 std::unique_ptr< te::rst::Raster > te::rp::ClassifierStrategy::releaseOutputRaster()
61 {
62  return std::unique_ptr< te::rst::Raster >( m_outputRasterPtr.release() );
63 }
64 
65 void te::rp::ClassifierStrategy::setInputPolygons( const std::vector<te::gm::Polygon*>& polygonsPtrs )
66 {
67  m_inputPolygonsPtr = &polygonsPtrs;
68 }
69 
71 {
73 }
74 
76 {
77  m_enableRasterCache = enable;
78 }
79 
81 {
82  m_enableMultiThread = enable;
83 }
84 
85 void te::rp::ClassifierStrategy::getMetaData( std::map< std::string, std::string >& metadata ) const
86 {
87  metadata = m_metaData;
88 }
89 
91 {
92  m_createRasterPalette = enabled;
93 }
94 
95 void te::rp::ClassifierStrategy::setUserOutputPalette( std::vector< te::rst::BandProperty::ColorEntry >& userPalette )
96 {
97  m_userOutputPalette = userPalette;
98 }
99 
101 {
102  return m_errorMessage;
103 }
104 
106  const std::vector< int >& bandsDataTypes,
107  const std::vector< double >& noDataValues )
108 {
109  TERP_TRUE_OR_THROW( m_inputRasterPtr != nullptr, "Invalid input raster pointer" );
110  TERP_TRUE_OR_THROW( !bandsDataTypes.empty(), "Invalid bands data types" );
111  TERP_TRUE_OR_THROW( noDataValues.size() == bandsDataTypes.size(), "Invalid no-data values" );
112 
113  // Creating the output raster
114 
115  std::vector<te::rst::BandProperty*> bandsProperties;
116 
117  for(unsigned int bandsDataTypesIdx=0; bandsDataTypesIdx < bandsDataTypes.size();
118  bandsDataTypesIdx++)
119  {
123  newbprop->m_type = bandsDataTypes[bandsDataTypesIdx];
124  newbprop->m_noDataValue = noDataValues[ bandsDataTypesIdx ];
125  bandsProperties.push_back(newbprop);
126  }
127  // creating the output raster
128 
129  te::rst::Grid* newgrid = new te::rst::Grid(*(m_inputRasterPtr->getGrid()));
130  std::map< std::string, std::string > m_rInfo;
131 
132  try
133  {
134  m_outputRasterPtr.reset( te::rst::RasterFactory::make( "EXPANSIBLE", newgrid,
135  bandsProperties, std::map< std::string, std::string >(), nullptr, nullptr));
136  }
137  catch(...)
138  {
139  m_outputRasterPtr.reset();
140  }
141 
142  return ( m_outputRasterPtr.get() != nullptr );
143 }
144 
146 {
147  if( m_createRasterPalette && ( size > 0 ) )
148  {
149  if( m_outputRasterPtr.get() == nullptr ) return false;
150  if( m_outputRasterPtr->getNumberOfBands() < 1 ) return false;
151 
152  std::vector< te::rst::BandProperty::ColorEntry > palette;
153 
154  if( m_userOutputPalette.empty() )
155  {
156  te::rp::CreateFixedStepPalette( size, true, palette );
157  }
158  else
159  {
160  const std::size_t userOutputPaletteSize = m_userOutputPalette.size();
161  if( userOutputPaletteSize < size ) return false;
162 
163  for( std::size_t pIdx = 0 ; pIdx < userOutputPaletteSize ; ++pIdx )
164  {
165  palette.push_back( m_userOutputPalette[ pIdx ] );
166  }
167  }
168 
169  m_outputRasterPtr->getBand( 0 )->getProperty()->m_colorInterp =
171  m_outputRasterPtr->getBand( 0 )->getProperty()->m_paletteInterp =
173  m_outputRasterPtr->getBand( 0 )->getProperty()->m_palette =
174  palette;
175  }
176 
177  return true;
178 }
179 
180 void te::rp::ClassifierStrategy::setErrorMessage( const std::string& newErrorMessage )
181 {
182  m_errorMessage = newErrorMessage;
183 }
184 
186 {
187  m_inputRasterPtr = nullptr;
189  m_enableRasterCache = true;
190  m_enableMultiThread = true;
191  m_createRasterPalette = true;
192  m_outputRasterPtr.reset();
193  m_inputRasterBands.clear();
194  m_inputPolygonsPtr = nullptr;
195  m_metaData.clear();
196  m_userOutputPalette.clear();
197  m_errorMessage.clear();
198 }
Palette indexes color interpretation.
void enableOutputPalette(const bool enabled)
Enable (true) or disable (false) the creation of a paletted output raster.
const ClassifierStrategy & operator=(const ClassifierStrategy &rhs)
Assignment operator.
Index into a lookup table.
bool m_createRasterPalette
Enable (true) or disable (false) the creation of a paletted output raster.
bool m_enableRasterCache
Enable or disable the use a raster data cache.
A raster band description.
Definition: BandProperty.h:61
RGB indexed palette interpretation.
virtual void reset()
Reset to an initial state.
bool createOutputRaster(const std::vector< int > &bandsDataTypes, const std::vector< double > &noDataValues)
Create the output raster using the EXPANSIBLE driver.
void setInputRasterBands(const std::vector< unsigned int > &rasterBands)
Set the input raste bandsr.
void setInputPolygons(const std::vector< te::gm::Polygon * > &polygonsPtrs)
Set the input polygons.
int m_type
The data type of the elements in the band ( See te::dt namespace basic data types for reference )...
Definition: BandProperty.h:133
std::map< std::string, std::string > m_metaData
Strategy-dependent metadata.
double m_noDataValue
Value to indicate elements where there is no data, default is std::numeric_limits<double>::max().
Definition: BandProperty.h:136
void setUserOutputPalette(std::vector< te::rst::BandProperty::ColorEntry > &userPalette)
Set the output user palette.
ClassifierStrategy()
Default constructor.
std::vector< unsigned int > m_inputRasterBands
Input raster bands.
void setErrorMessage(const std::string &newErrorMessage)
Set the current error message.
An abstract class for raster data strucutures.
BandProperty * getProperty()
Returns the band property.
bool m_enableMultiThread
Enable or disable the use multipe threads.
virtual const Band * getBand(std::size_t i) const =0
Returns the raster i-th band.
const std::string & getErrorMessage() const
Return the current error message if there is any.
void enableProgressInterface(const bool &enable)
Enable / disable the progress interface.
Grid * getGrid()
It returns the raster grid.
std::unique_ptr< te::rst::Raster > m_outputRasterPtr
A pointer to the output raster.
void getMetaData(std::map< std::string, std::string > &metadata) const
Returns strategy-dependent metadata.
bool m_progressInterfaceEnabled
Progress interface status.
te::rst::Raster const * m_inputRasterPtr
A pointer to the input raster.
std::unique_ptr< te::rst::Raster > releaseOutputRaster()
Returns a pointer to the output raster or a void pointer if there is none.
Raster classifier strategy base class.
static Raster * make()
It creates and returns an empty raster with default raster driver.
void CreateFixedStepPalette(const unsigned int paletteSize, const bool randomize, std::vector< te::rst::BandProperty::ColorEntry > &palette)
Create a fixed step sequential color palette.
Raster classifier strategy base class.
Raster Processing functions.
std::vector< te::rst::BandProperty::ColorEntry > m_userOutputPalette
User output raster palette (it must be large enough to accomodate all classifyier generated classes o...
void setInputRaster(const te::rst::Raster &raster)
Set the input raster.
A rectified grid is the spatial support for raster data.
Definition: raster/Grid.h:68
std::vector< te::gm::Polygon * > const * m_inputPolygonsPtr
Input polygons.
ColorInterp m_colorInterp
The color interpretation.
Definition: BandProperty.h:140
void enableMultiThread(const bool &enable)
Enable / disable the use of multiple threads.
bool setOutputRasterPalette(const unsigned int size)
Create and set the output raster palette folowing the current internal settings.
void enableRasterCache(const bool &enable)
Enable / disable the use of raster data cache.
#define TERP_TRUE_OR_THROW(value, message)
Checks if value is true and throws an exception if not.
Definition: Macros.h:150
std::string m_errorMessage
Current error message.
virtual ~ClassifierStrategy()
Virtual destructor.