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