ClassifierDummyStrategy.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/ClassifierDummyStrategy.cpp
22 
23  \brief Dummy strategy (just for testing purposes).
24 */
25 
26 // TerraLib
27 #include "../geometry/Coord2D.h"
28 #include "../geometry/Envelope.h"
29 #include "../geometry/Point.h"
30 #include "../geometry/Polygon.h"
31 #include "../raster/Grid.h"
33 #include "Functions.h"
34 #include "Macros.h"
35 
36 // STL
37 #include <iostream>
38 #include <stdlib.h>
39 
40 namespace
41 {
42  static te::rp::ClassifierDummyStrategyFactory classifierDummyStrategyFactoryInstance;
43 }
44 
46 {
47  reset();
48 }
49 
51 
53 {
54  reset();
55 
57 
58  return *this;
59 }
60 
62 {
63  m_dummyParameter = 0;
64 }
65 
67 {
69 }
70 
72 {
73  m_isInitialized = false;
74 }
75 
77 
78 bool te::rp::ClassifierDummyStrategy::initialize(te::rp::ClassifierStrategyParameters const* const strategyParams) throw(te::rp::Exception)
79 {
80  m_isInitialized = false;
81 
82  te::rp::ClassifierDummyStrategy::Parameters const* paramsPtr = dynamic_cast<te::rp::ClassifierDummyStrategy::Parameters const*>(strategyParams);
83 
84  if(!paramsPtr)
85  return false;
86 
87  m_parameters = *(paramsPtr);
88 
89  TERP_INSTANCE_TRUE_OR_RETURN_FALSE(m_parameters.m_dummyParameter > 1, TE_TR("The value of dummy must be at least 2."))
90 
91  m_isInitialized = true;
92 
93  return true;
94 }
95 
97 {
98  TERP_INSTANCE_TRUE_OR_RETURN_FALSE(m_isInitialized, TE_TR("Instance not initialized"))
99 
100  // Creating the output raster
101 
102  {
103  std::vector< int > dt;
104  dt.push_back( te::dt::UINT32_TYPE );
105 
106  std::vector< double > noDataValues;
107  noDataValues.push_back( 0 );
108 
110  "Output raster creation error" );
111 
112  // no-data fill
113 
114  const double outNoDataValue = m_outputRasterPtr->getBand( 0 )->getProperty()->m_noDataValue;
115  const unsigned int nCols = m_outputRasterPtr->getNumberOfColumns();
116  const unsigned int nRows = m_outputRasterPtr->getNumberOfRows();
117  unsigned int col = 0;
118  te::rst::Band& outBand = *m_outputRasterPtr->getBand( 0 );
119 
120  for( unsigned int row = 0 ; row < nRows ; ++row )
121  {
122  for( col = 0 ; col < nCols ; ++col )
123  {
124  outBand.setValue( col, row, outNoDataValue );
125  }
126  }
127  }
128 
129 // check if OBIA method is employed (raster + polygons)
130 
131  unsigned int c;
132  unsigned int r;
133  unsigned int pattern;
134  te::gm::Point* point;
135  unsigned int outputRasterPaletteSize = 0;
136 
137  if(
138  ( m_inputPolygonsPtr != nullptr )
139  &&
140  ( m_inputPolygonsPtr->size() > 0)
141  )
142  {
143 // iterate over geometries
144  for (unsigned i = 0; i < m_inputPolygonsPtr->size(); i++)
145  {
146  te::gm::Polygon* polygon = m_inputPolygonsPtr->operator[]( i );
147 
148  te::gm::Coord2D ll = polygon->getMBR()->getLowerLeft();
149  te::gm::Coord2D ur = polygon->getMBR()->getUpperRight();
150 
151  te::gm::Coord2D startGridCoord = m_outputRasterPtr->getGrid()->geoToGrid(ll.x, ur.y);
152  te::gm::Coord2D endGridCoord = m_outputRasterPtr->getGrid()->geoToGrid(ur.x, ll.y);
153 
154  pattern = rand() % 5 + 1;
155 
156  for(r = (unsigned) startGridCoord.y; r < endGridCoord.y; r++)
157  for(c = (unsigned) startGridCoord.x; c < endGridCoord.x; c++)
158  {
159  te::gm::Coord2D geoCoord = m_outputRasterPtr->getGrid()->gridToGeo(c, r);
160  point = new te::gm::Point(geoCoord.x, geoCoord.y);
161 
162  if (polygon->intersects(point))
163  m_outputRasterPtr->setValue(c, r, pattern, 0);
164 
165  delete point;
166  }
167 
168  outputRasterPaletteSize = std::max( outputRasterPaletteSize, pattern );
169  }
170  }
171  else
172  {
173  const unsigned int nrows = m_outputRasterPtr->getNumberOfRows();
174  const unsigned int ncols = m_outputRasterPtr->getNumberOfColumns();
175 
176  for(r = 0; r < nrows; r++)
177  {
178  for(c = 0; c < ncols; c++)
179  {
180 // generate random classification with 5 classes
181  pattern = rand() % 5 + 1;
182 
183  m_outputRasterPtr->setValue(c, r, pattern, 0);
184 
185  outputRasterPaletteSize = std::max( outputRasterPaletteSize, pattern );
186  }
187  }
188  }
189 
190  // Configure the output raster palette
191 
192  ++outputRasterPaletteSize;
193 
195  "Output raster palette creation error" );
196 
197  return true;
198 }
199 
201  : te::rp::ClassifierStrategyFactory("dummy")
202 {
203 }
204 
206  default;
207 
209 {
210  return new te::rp::ClassifierDummyStrategy();
211 }
bool initialize(ClassifierStrategyParameters const *const strategyParams)
Initialize the classification strategy.
double y
y-coordinate.
Definition: Coord2D.h:114
double x
x-coordinate.
Definition: Coord2D.h:113
Base exception class for plugin module.
bool createOutputRaster(const std::vector< int > &bandsDataTypes, const std::vector< double > &noDataValues)
Create the output raster using the EXPANSIBLE driver.
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
virtual bool intersects(const Geometry *const rhs) const _NOEXCEPT_OP(false)
It returns true if the geometry object spatially intersects rhs geometry.
AbstractParameters * clone() const
Create a clone copy of this instance.
Raster classifier strategy factory base class.
unsigned int unsigned int nCols
const Parameters & operator=(const Parameters &params)
A point with x and y coordinate values.
Definition: Point.h:50
const Envelope * getMBR() const _NOEXCEPT_OP(true)
It returns the minimum bounding rectangle for the geometry in an internal representation.
URI C++ Library.
Definition: Attributes.h:37
static te::dt::TimeDuration dt(20, 30, 50, 11)
bool execute()
Executes the classification strategy.
A raster band description.
bool m_isInitialized
True if this instance is initialized.
virtual void setValue(unsigned int c, unsigned int r, const double value)=0
Sets the cell attribute value.
te::rp::ClassifierStrategy * build()
Concrete factories (derived from this one) must implement this method in order to create objects...
std::unique_ptr< te::rst::Raster > m_outputRasterPtr
A pointer to the output raster.
Abstract parameters base interface.
void reset()
Clear all internal allocated resources and reset the parameters instance to its initial state...
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
Raster classifier strategy base class.
unsigned int m_dummyParameter
A dummy parameter.
ClassifierDummyStrategy::Parameters m_parameters
Internal execution parameters.
Raster dummy Classifier strategy factory.
Coord2D getUpperRight() const
It returns the upper right coordinate of the envelope.
Raster Processing functions.
Coord2D getLowerLeft() const
It returns the lower left coordinate of the envelope.
std::vector< te::gm::Polygon * > const * m_inputPolygonsPtr
Input polygons.
#define TERP_INSTANCE_TRUE_OR_RETURN_FALSE(value, message)
Checks if value is true. For false values a warning message will be logged, the current instance erro...
Definition: Macros.h:200
bool setOutputRasterPalette(const unsigned int size)
Create and set the output raster palette folowing the current internal settings.
Dummy strategy (just for testing purposes).
unsigned int col
Dummy strategy (just for testing purposes).