All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 {
52 }
53 
55 {
56  reset();
57 
58  m_dummyParameter = rhs.m_dummyParameter;
59 
60  return *this;
61 }
62 
64 {
65  m_dummyParameter = 0;
66 }
67 
69 {
71 }
72 
74 {
75  m_isInitialized = false;
76 }
77 
79 {
80 }
81 
82 bool te::rp::ClassifierDummyStrategy::initialize(te::rp::StrategyParameters const* const strategyParams) throw(te::rp::Exception)
83 {
84  m_isInitialized = false;
85 
86  te::rp::ClassifierDummyStrategy::Parameters const* paramsPtr = dynamic_cast<te::rp::ClassifierDummyStrategy::Parameters const*>(strategyParams);
87 
88  if(!paramsPtr)
89  return false;
90 
91  m_parameters = *(paramsPtr);
92 
93  TERP_TRUE_OR_RETURN_FALSE(m_parameters.m_dummyParameter > 1, TE_TR("The value of dummy must be at least 2."))
94 
95  m_isInitialized = true;
96 
97  return true;
98 }
99 
100 bool te::rp::ClassifierDummyStrategy::execute(const te::rst::Raster& inputRaster, const std::vector<unsigned int>& inputRasterBands,
101  const std::vector<te::gm::Polygon*>& inputPolygons, te::rst::Raster& outputRaster,
102  const unsigned int outputRasterBand, const bool enableProgressInterface) throw(te::rp::Exception)
103 {
104  TERP_TRUE_OR_RETURN_FALSE(m_isInitialized, TE_TR("Instance not initialized"))
105 
106  unsigned int c;
107  unsigned int r;
108  unsigned int pattern;
109  te::gm::Point* point;
110 
111 // check if OBIA method is employed (raster + polygons)
112  if(inputPolygons.size() > 0)
113  {
114 // iterate over geometries
115  for (unsigned i = 0; i < inputPolygons.size(); i++)
116  {
117  te::gm::Polygon* polygon = inputPolygons[i];
118 
119  te::gm::Coord2D ll = polygon->getMBR()->getLowerLeft();
120  te::gm::Coord2D ur = polygon->getMBR()->getUpperRight();
121 
122  te::gm::Coord2D startGridCoord = outputRaster.getGrid()->geoToGrid(ll.x, ur.y);
123  te::gm::Coord2D endGridCoord = outputRaster.getGrid()->geoToGrid(ur.x, ll.y);
124 
125  pattern = rand() % 5 + 1;
126 
127  for(r = (unsigned) startGridCoord.y; r < endGridCoord.y; r++)
128  for(c = (unsigned) startGridCoord.x; c < endGridCoord.x; c++)
129  {
130  te::gm::Coord2D geoCoord = outputRaster.getGrid()->gridToGeo(c, r);
131  point = new te::gm::Point(geoCoord.x, geoCoord.y);
132 
133  if (polygon->intersects(point))
134  outputRaster.setValue(c, r, pattern, outputRasterBand);
135 
136  delete point;
137  }
138  }
139  }
140  else
141  {
142  const unsigned int nrows = outputRaster.getNumberOfRows();
143  const unsigned int ncols = outputRaster.getNumberOfColumns();
144 
145  for(r = 0; r < nrows; r++)
146  {
147  for(c = 0; c < ncols; c++)
148  {
149 // generate random classification with 5 classes
150  pattern = rand() % 5 + 1;
151 
152  outputRaster.setValue(c, r, pattern, outputRasterBand);
153  }
154  }
155  }
156 
157  return true;
158 }
159 
161  : te::rp::ClassifierStrategyFactory("dummy")
162 {
163 }
164 
166 {
167 }
168 
170 {
171  return new te::rp::ClassifierDummyStrategy();
172 }
bool initialize(StrategyParameters const *const strategyParams)
Initialize the classification strategy.
double y
y-coordinate.
Definition: Coord2D.h:114
virtual bool intersects(const Geometry *const rhs) const
It returns true if the geometry object spatially intersects rhs geometry.
Definition: Geometry.cpp:254
double x
x-coordinate.
Definition: Coord2D.h:113
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:347
AbstractParameters * clone() const
Create a clone copy of this instance.
Raster Processing functions.
#define TERP_TRUE_OR_RETURN_FALSE(value, message)
Checks if value is true. For false values a warning message will be logged and a return of context wi...
Definition: Macros.h:183
Raster classifier strategy factory base class.
const Parameters & operator=(const Parameters &params)
A point with x and y coordinate values.
Definition: Point.h:50
An abstract class for raster data strucutures.
Definition: Raster.h:71
Raster strategy parameters base class.
bool m_isInitialized
True if this instance is initialized.
te::rp::ClassifierStrategy * build()
Concrete factories (derived from this one) must implement this method in order to create objects...
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.
Definition: Envelope.cpp:46
Coord2D getLowerLeft() const
It returns the lower left coordinate of the envelope.
Definition: Envelope.cpp:41
Dummy strategy (just for testing purposes).
const Envelope * getMBR() const
It returns the minimum bounding rectangle for the geometry in an internal representation.
Definition: Geometry.cpp:104
bool execute(const te::rst::Raster &inputRaster, const std::vector< unsigned int > &inputRasterBands, const std::vector< te::gm::Polygon * > &inputPolygons, te::rst::Raster &outputRaster, const unsigned int outputRasterBand, const bool enableProgressInterface)
Executes the classification strategy.
Dummy strategy (just for testing purposes).