All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ClassifierDummyStrategy.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2001-2009 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, TR_RP("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, TR_RP("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 }
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
Raster strategy parameters base class.
Dummy strategy (just for testing purposes).
Raster Processing functions.
virtual bool intersects(const Geometry *const rhs) const
It returns true if the geometry object spatially intersects rhs geometry.
Definition: Geometry.cpp:243
double y
y-coordinate.
Definition: Coord2D.h:87
ClassifierDummyStrategy::Parameters m_parameters
Internal execution parameters.
void reset()
Clear all internal allocated resources and reset the parameters instance to its initial state...
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.
#define TR_RP(message)
It marks a string in order to get translated. This is a special mark used in the Raster Processing mo...
Definition: Config.h:58
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
Coord2D getUpperRight() const
It returns the upper right coordinate of the envelope.
Definition: Envelope.cpp:43
#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
unsigned int m_dummyParameter
A dummy parameter.
Abstract parameters base interface.
A point with x and y coordinate values.
Definition: Point.h:50
Raster classifier strategy factory base class.
te::rp::ClassifierStrategy * build()
Concrete factories (derived from this one) must implement this method in order to create objects...
bool initialize(StrategyParameters const *const strategyParams)
Initialize the classification strategy.
AbstractParameters * clone() const
Create a clone copy of this instance.
const Parameters & operator=(const Parameters &params)
Dummy strategy (just for testing purposes).
An abstract class for raster data strucutures.
Definition: Raster.h:70
const Envelope * getMBR() const
It returns the minimum bounding rectangle for the geometry in an internal representation.
Definition: Geometry.cpp:103
double x
x-coordinate.
Definition: Coord2D.h:86
Raster classifier strategy base class.
bool m_isInitialized
True if this instance is initialized.
Raster dummy Classifier strategy factory.
Coord2D getLowerLeft() const
It returns the lower left coordinate of the envelope.
Definition: Envelope.cpp:38