All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Register.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/Register.cpp
22  \brief Create a mosaic from a set of rasters.
23 */
24 
25 #include "Register.h"
26 #include "Macros.h"
27 #include "../geometry/GeometricTransformation.h"
28 #include "../geometry/GTFactory.h"
29 #include "../geometry/Envelope.h"
30 #include "../raster/RasterFactory.h"
31 #include "../raster/Band.h"
32 #include "../raster/BandProperty.h"
33 #include "../raster/Grid.h"
34 #include "../raster/Utils.h"
35 
36 #include <cfloat>
37 #include <memory>
38 
39 
40 namespace te
41 {
42  namespace rp
43  {
44 
46  {
47  reset();
48  }
49 
51  {
52  reset();
53  operator=( other );
54  }
55 
57  {
58  reset();
59  }
60 
61  void Register::InputParameters::reset() throw( te::rp::Exception )
62  {
63  m_inputRasterPtr = 0;
64  m_inputRasterBands.clear();
65  m_tiePoints.clear();
66  m_outputSRID = 0;
67  m_outputResolutionX = 1.0;
68  m_outputResolutionY = 1.0;
70  m_noDataValue = 0;
71  m_geomTransfName = "Affine";
72  }
73 
75  const Register::InputParameters& params )
76  {
77  reset();
78 
79  m_inputRasterPtr = params.m_inputRasterPtr;
80  m_inputRasterBands = params.m_inputRasterBands;
81  m_tiePoints = params.m_tiePoints;
82  m_outputSRID = params.m_outputSRID;
83  m_outputResolutionX = params.m_outputResolutionX;
84  m_outputResolutionY = params.m_outputResolutionY;
85  m_interpMethod = params.m_interpMethod;
86  m_noDataValue = params.m_noDataValue;
87  m_geomTransfName = params.m_geomTransfName;
88 
89  return *this;
90  }
91 
93  {
94  return new InputParameters( *this );
95  }
96 
98  {
99  reset();
100  }
101 
103  {
104  reset();
105  operator=( other );
106  }
107 
109  {
110  reset();
111  }
112 
113  void Register::OutputParameters::reset() throw( te::rp::Exception )
114  {
115  m_rType.clear();
116  m_rInfo.clear();
117  m_outputRasterPtr.reset();
118  }
119 
121  const Register::OutputParameters& params )
122  {
123  reset();
124 
125  m_rType = params.m_rType;
126  m_rInfo = params.m_rInfo;
127 
128  return *this;
129  }
130 
132  {
133  return new OutputParameters( *this );
134  }
135 
137  {
138  reset();
139  }
140 
142  {
143  }
144 
146  throw( te::rp::Exception )
147  {
148  if( ! m_isInitialized ) return false;
149 
150  Register::OutputParameters* outParamsPtr = dynamic_cast<
151  Register::OutputParameters* >( &outputParams );
152  TERP_TRUE_OR_THROW( outParamsPtr, "Invalid paramters" );
153 
154  // instantiating the geometric transformation
155 
156  std::auto_ptr< te::gm::GeometricTransformation > transformationPtr;
157 
158  {
159  te::gm::GTParameters transfParams;
161 
162  transformationPtr.reset( te::gm::GTFactory::make( m_inputParameters.m_geomTransfName ) );
163  TERP_TRUE_OR_RETURN_FALSE( transformationPtr.get() != 0,
164  "Geometric transformation instatiation error" );
165  TERP_TRUE_OR_RETURN_FALSE( transformationPtr->initialize( transfParams ),
166  "Geometric transformation parameters calcule error" );
167  }
168 
169  // guessing the output raster bounding box coords
170 
171  double lowerLeftX = DBL_MAX;
172  double lowerLeftY = DBL_MAX;
173  double upperRightX = -1.0 * DBL_MAX;
174  double upperRightY = -1.0 * DBL_MAX;
175 
176  {
177  double mappedX = 0;
178  double mappedY = 0;
179 
180  transformationPtr->directMap( -0.5, -0.5, mappedX, mappedY );
181  lowerLeftX = std::min( lowerLeftX, mappedX );
182  lowerLeftY = std::min( lowerLeftY, mappedY );
183  upperRightX = std::max( upperRightX, mappedX );
184  upperRightY = std::max( upperRightY, mappedY );
185 
186  transformationPtr->directMap(
188  -0.5, mappedX, mappedY );
189  lowerLeftX = std::min( lowerLeftX, mappedX );
190  lowerLeftY = std::min( lowerLeftY, mappedY );
191  upperRightX = std::max( upperRightX, mappedX );
192  upperRightY = std::max( upperRightY, mappedY );
193 
194  transformationPtr->directMap(
197  mappedX, mappedY );
198  lowerLeftX = std::min( lowerLeftX, mappedX );
199  lowerLeftY = std::min( lowerLeftY, mappedY );
200  upperRightX = std::max( upperRightX, mappedX );
201  upperRightY = std::max( upperRightY, mappedY );
202 
203  transformationPtr->directMap(
204  -0.5,
206  mappedX, mappedY );
207  lowerLeftX = std::min( lowerLeftX, mappedX );
208  lowerLeftY = std::min( lowerLeftY, mappedY );
209  upperRightX = std::max( upperRightX, mappedX );
210  upperRightY = std::max( upperRightY, mappedY );
211  }
212 
213  // creating the output raster
214 
215  {
216  std::vector< te::rst::BandProperty* > bandsProperties;
217  for( unsigned int inputRasterBandsIdx = 0 ; inputRasterBandsIdx <
218  m_inputParameters.m_inputRasterBands.size() ; ++inputRasterBandsIdx )
219  {
220  bandsProperties.push_back( new te::rst::BandProperty(
222  m_inputParameters.m_inputRasterBands[ inputRasterBandsIdx ] )->getProperty() ) ) );
223  bandsProperties[ inputRasterBandsIdx ]->m_noDataValue = m_inputParameters.m_noDataValue;
224  }
225 
226  te::gm::Envelope* gridMbr = new te::gm::Envelope();
227  gridMbr->m_llx = lowerLeftX;
228  gridMbr->m_lly = lowerLeftY;
229  gridMbr->m_urx = upperRightX;
230  gridMbr->m_ury = upperRightY;
231 
232  outParamsPtr->m_outputRasterPtr.reset(
234  outParamsPtr->m_rType,
238  bandsProperties,
239  outParamsPtr->m_rInfo,
240  0,
241  0 ) );
242  TERP_TRUE_OR_RETURN_FALSE( outParamsPtr->m_outputRasterPtr.get(),
243  "Output raster creation error" );
244  }
245 
246  // Rendering the output raster
247 
248  te::rst::Raster& outputRaster = *outParamsPtr->m_outputRasterPtr;
249  const te::rst::Grid& outputGrid = *outputRaster.getGrid();
252  te::gm::GeometricTransformation& geomTransf = *transformationPtr;
253  const unsigned int nRows = outputRaster.getNumberOfRows();
254  const unsigned int nCols = outputRaster.getNumberOfColumns();
255  unsigned int outRow = 0;
256  unsigned int outCol = 0;
257  double outX = 0;
258  double outY = 0;
259  double inRow = 0;
260  double inCol = 0;
261  std::complex< double > cValue;
262 
263  for( unsigned int inputRasterBandsIdx = 0 ; inputRasterBandsIdx <
264  m_inputParameters.m_inputRasterBands.size() ; ++inputRasterBandsIdx )
265  {
266  const unsigned int inputBandIdx = m_inputParameters.m_inputRasterBands[ inputRasterBandsIdx ];
267  const double inputBandNoDataValue = m_inputParameters.m_inputRasterPtr->getBand( inputBandIdx )->getProperty()->m_noDataValue;
268  te::rst::Band& outputBand = *outputRaster.getBand( inputRasterBandsIdx );
269 
270  double outputBandRangeMin = 0;
271  double outputBandRangeMax = 0;
273  outputBandRangeMin, outputBandRangeMax );
274 
275  for( outRow = 0 ; outRow < nRows ; ++outRow )
276  {
277  for( outCol = 0 ; outCol < nCols ; ++outCol )
278  {
279  outputGrid.gridToGeo( (double)outCol, (double)outRow, outX, outY );
280 
281  geomTransf.inverseMap( outX, outY, inCol, inRow );
282 
283  interpInstance.getValue( inCol, inRow, cValue, inputBandIdx );
284 
285  if( cValue.real() == inputBandNoDataValue )
286  {
287  outputBand.setValue( outCol, outRow, m_inputParameters.m_noDataValue );
288  }
289  else
290  {
291  outputBand.setValue( outCol, outRow, std::max( outputBandRangeMin,
292  std::min( outputBandRangeMax, cValue.real() ) ) );
293  }
294  }
295  }
296  }
297 
298 
299  return true;
300  }
301 
302  void Register::reset() throw( te::rp::Exception )
303  {
305  m_isInitialized = false;
306  }
307 
309  throw( te::rp::Exception )
310  {
311  reset();
312 
313  Register::InputParameters const* inputParamsPtr = dynamic_cast<
314  Register::InputParameters const* >( &inputParams );
315  TERP_TRUE_OR_THROW( inputParamsPtr, "Invalid paramters pointer" );
316 
317  m_inputParameters = *inputParamsPtr;
318 
319  // Checking the input raster
320 
322  "Invalid m_inputRasterPtr" )
323 
326  "Invalid raster" );
327 
329  "Invalid raster bands number" );
330 
331  for( unsigned int inRasterBandsIdx = 0 ; inRasterBandsIdx <
332  m_inputParameters.m_inputRasterBands.size() ; ++inRasterBandsIdx )
333  {
335  m_inputParameters.m_inputRasterBands[ inRasterBandsIdx ] <
337  "Invalid raster bands" );
338  }
339 
340  // checking transformation related parameters
341 
342  std::auto_ptr< te::gm::GeometricTransformation > transformationPtr(
344  TERP_TRUE_OR_RETURN_FALSE( transformationPtr.get() != 0,
345  "Geometric transformation instatiation error" );
346 
348  transformationPtr->getMinRequiredTiePoints(),
349  "Invalid tie-points number" )
350 
351  // checking other parameters
352 
354  "Invalid m_outputSRID" )
355 
357  "Invalid m_outputResolutionX" )
358 
360  "Invalid m_outputResolutionY" )
361 
362 
363  m_isInitialized = true;
364 
365  return true;
366  }
367 
369  {
370  return m_isInitialized;
371  }
372 
373 
374 
375  } // end namespace rp
376 } // end namespace te
377 
te::rst::Interpolator::Method m_interpMethod
The raster interpolator method (default:NearestNeighbor).
Definition: Register.h:70
std::string m_geomTransfName
The name of the geometric transformation used (see each te::gm::GTFactory inherited classes to find e...
Definition: Register.h:74
std::string m_rType
Output raster data source type (as described in te::raster::RasterFactory ).
Definition: Register.h:100
unsigned int getNumberOfRows() const
Returns the raster number of rows.
Definition: Raster.cpp:208
const InputParameters & operator=(const InputParameters &params)
Definition: Register.cpp:74
double m_noDataValue
The pixel value used where no raster data is avaliable (defaul:0).
Definition: Register.h:72
Grid * getGrid()
It returns the raster grid.
Definition: Raster.cpp:94
te::rst::Raster const * m_inputRasterPtr
Input raster.
Definition: Register.h:58
static Raster * make()
It creates and returns an empty raster with default raster driver.
const OutputParameters & operator=(const OutputParameters &params)
Definition: Register.cpp:120
void reset()
Clear all internal allocated objects and reset the algorithm to its initial state.
Definition: Register.cpp:302
void getValue(const double &c, const double &r, std::complex< double > &v, const std::size_t &b)
Get the interpolated value at specific band.
Definition: Interpolator.h:84
virtual std::size_t getNumberOfBands() const =0
Returns the number of bands (dimension of cells attribute values) in the raster.
It interpolates one pixel based on a selected algorithm. Methods currently available are Nearest Neig...
Definition: Interpolator.h:51
static GeometricTransformation * make(const std::string &factoryKey)
It creates an object with the appropriated factory.
std::vector< te::gm::GTParameters::TiePoint > m_tiePoints
Tie-points between each raster point (te::gm::GTParameters::TiePoint::first are raster lines/columns ...
Definition: Register.h:62
AbstractParameters * clone() const
Create a clone copy of this instance.
Definition: Register.cpp:131
#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
int m_type
The data type of the elements in the band.
Definition: BandProperty.h:133
Performs raster data registering into a SRS using a set of tie points.
Abstract parameters base interface.
std::vector< TiePoint > m_tiePoints
Tie points.
Definition: GTParameters.h:95
Register output parameters.
Definition: Register.h:96
A raster band description.
Definition: Band.h:63
A rectified grid is the spatial support for raster data.
Definition: Grid.h:55
double m_lly
Lower left corner y-coordinate.
Definition: Envelope.h:345
virtual void setValue(unsigned int c, unsigned int r, const double value)=0
Sets the cell attribute value.
void reset()
Clear all internal allocated resources and reset the parameters instance to its initial state...
Definition: Register.cpp:113
AbstractParameters * clone() const
Create a clone copy of this instance.
Definition: Register.cpp:92
double m_ury
Upper right corner y-coordinate.
Definition: Envelope.h:347
std::map< std::string, std::string > m_rInfo
The necessary information to create the output rasters (as described in te::raster::RasterFactory).
Definition: Register.h:102
Register input parameters.
Definition: Register.h:54
double m_urx
Upper right corner x-coordinate.
Definition: Envelope.h:346
double m_outputResolutionY
The output raster Y axis resolution (default:1).
Definition: Register.h:68
double m_outputResolutionX
The output raster X axis resolution (default:1).
Definition: Register.h:66
unsigned int getNumberOfColumns() const
Returns the raster number of columns.
Definition: Raster.cpp:213
const Algorithm & operator=(const Algorithm &)
Definition: Algorithm.cpp:43
A raster band description.
Definition: BandProperty.h:61
double m_noDataValue
Value to indicate elements where there is no data, default is std::numeric_limits&lt;double&gt;::max().
Definition: BandProperty.h:136
Register::InputParameters m_inputParameters
Input execution parameters.
Definition: Register.h:139
bool initialize(const AlgorithmInputParameters &inputParams)
Initialize the algorithm instance making it ready for execution.
Definition: Register.cpp:308
2D Geometric transformation base class.
void gridToGeo(const double &col, const double &row, double &x, double &y) const
Get the spatial location of a grid point.
Definition: Grid.cpp:302
#define TERP_TRUE_OR_THROW(value, message)
Checks if value is true and throws an exception if not.
Definition: Macros.h:149
Raster Processing algorithm output parameters base interface.
double m_llx
Lower left corner x-coordinate.
Definition: Envelope.h:344
An abstract class for raster data strucutures.
Definition: Raster.h:70
BandProperty * getProperty()
Returns the band property.
Definition: Band.cpp:370
virtual const Band * getBand(std::size_t i) const =0
Returns the raster i-th band.
bool isInitialized() const
Returns true if the algorithm instance is initialized and ready for execution.
Definition: Register.cpp:368
int m_outputSRID
The output raster SRID (default:0).
Definition: Register.h:64
2D Geometric transformation parameters.
Definition: GTParameters.h:50
std::vector< unsigned int > m_inputRasterBands
Bands to process from the input raster.
Definition: Register.h:60
bool execute(AlgorithmOutputParameters &outputParams)
Executes the algorithm using the supplied parameters.
Definition: Register.cpp:145
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
bool m_isInitialized
Tells if this instance is initialized.
Definition: Register.h:141
Near neighborhood interpolation method.
Definition: Interpolator.h:60
TERASTEREXPORT void GetDataTypeRanges(const int &dataType, double &min, double &max)
Return the values range of a given data type.
Definition: Utils.cpp:331
std::auto_ptr< te::rst::Raster > m_outputRasterPtr
The generated output registered raster.
Definition: Register.h:104
void reset()
Clear all internal allocated resources and reset the parameters instance to its initial state...
Definition: Register.cpp:61
te::common::AccessPolicy getAccessPolicy() const
Returns the raster access policy.
Definition: Raster.cpp:89
virtual void inverseMap(const GTParameters &params, const double &pt2X, const double &pt2Y, double &pt1X, double &pt1Y) const =0
Inverse mapping (from pt2 space into pt1 space).
Raster Processing algorithm input parameters base interface.