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) 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/Register.cpp
22  \brief Create a mosaic from a set of rasters.
23 */
24 
25 #include "Register.h"
26 #include "Macros.h"
27 #include "Functions.h"
28 #include "../geometry/GeometricTransformation.h"
29 #include "../geometry/GTFactory.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;
69  m_interpMethod = te::rst::NearestNeighbor;
70  m_noDataValue = 0;
71  m_geomTransfName = "Affine";
72  m_geomTransfPtr = 0;
73  m_outputBoundingBox.m_llx = m_outputBoundingBox.m_lly =
74  m_outputBoundingBox.m_urx = m_outputBoundingBox.m_ury = 0.0;
75  }
76 
78  const Register::InputParameters& params )
79  {
80  reset();
81 
82  m_inputRasterPtr = params.m_inputRasterPtr;
83  m_inputRasterBands = params.m_inputRasterBands;
84  m_tiePoints = params.m_tiePoints;
85  m_outputSRID = params.m_outputSRID;
86  m_outputResolutionX = params.m_outputResolutionX;
87  m_outputResolutionY = params.m_outputResolutionY;
88  m_interpMethod = params.m_interpMethod;
89  m_noDataValue = params.m_noDataValue;
90  m_geomTransfName = params.m_geomTransfName;
91  m_geomTransfPtr = params.m_geomTransfPtr;
92  m_outputBoundingBox = params.m_outputBoundingBox;
93 
94  return *this;
95  }
96 
98  {
99  return new InputParameters( *this );
100  }
101 
103  {
104  reset();
105  }
106 
108  {
109  reset();
110  operator=( other );
111  }
112 
114  {
115  reset();
116  }
117 
118  void Register::OutputParameters::reset() throw( te::rp::Exception )
119  {
120  m_rType.clear();
121  m_rInfo.clear();
122  m_outputRasterPtr.reset();
123  }
124 
126  const Register::OutputParameters& params )
127  {
128  reset();
129 
130  m_rType = params.m_rType;
131  m_rInfo = params.m_rInfo;
132 
133  return *this;
134  }
135 
137  {
138  return new OutputParameters( *this );
139  }
140 
142  {
143  reset();
144  }
145 
147  {
148  }
149 
151  throw( te::rp::Exception )
152  {
153  if( ! m_isInitialized ) return false;
154 
155  Register::OutputParameters* outParamsPtr = dynamic_cast<
156  Register::OutputParameters* >( &outputParams );
157  TERP_TRUE_OR_THROW( outParamsPtr, "Invalid paramters" );
158 
159  // instantiating the geometric transformation
160 
161  std::auto_ptr< te::gm::GeometricTransformation > transformationPtr;
162 
164  {
165  transformationPtr.reset( m_inputParameters.m_geomTransfPtr->clone() );
166  }
167  else
168  {
169  te::gm::GTParameters transfParams;
171 
172  transformationPtr.reset( te::gm::GTFactory::make( m_inputParameters.m_geomTransfName ) );
173  TERP_TRUE_OR_RETURN_FALSE( transformationPtr.get() != 0,
174  "Geometric transformation instatiation error" );
175  TERP_TRUE_OR_RETURN_FALSE( transformationPtr->initialize( transfParams ),
176  "Geometric transformation parameters calcule error" );
177  }
178 
179  // guessing the output raster bounding box coords
180 
181  double lowerLeftX = DBL_MAX;
182  double lowerLeftY = DBL_MAX;
183  double upperRightX = -1.0 * DBL_MAX;
184  double upperRightY = -1.0 * DBL_MAX;
185 
186  if(
188  ||
190  ||
192  ||
194  )
195  {
200  }
201  else
202  {
203  te::gm::LinearRing indexedDetailedExtent(te::gm::LineStringType, 0, 0);
205  *m_inputParameters.m_inputRasterPtr->getGrid(), indexedDetailedExtent ),
206  "Indexed extent creation error" );
207 
208  double mappedX = 0;
209  double mappedY = 0;
210 
211  for( std::size_t extentIdx = 0 ; extentIdx < indexedDetailedExtent.size() ;
212  ++extentIdx )
213  {
214  transformationPtr->directMap( indexedDetailedExtent.getX( extentIdx ),
215  indexedDetailedExtent.getY( extentIdx ), mappedX, mappedY );
216 
217  lowerLeftX = std::min( lowerLeftX, mappedX );
218  lowerLeftY = std::min( lowerLeftY, mappedY );
219  upperRightX = std::max( upperRightX, mappedX );
220  upperRightY = std::max( upperRightY, mappedY );
221  }
222  }
223 
224  // creating the output raster
225 
226  {
227  std::vector< te::rst::BandProperty* > bandsProperties;
228  for( unsigned int inputRasterBandsIdx = 0 ; inputRasterBandsIdx <
229  m_inputParameters.m_inputRasterBands.size() ; ++inputRasterBandsIdx )
230  {
231  bandsProperties.push_back( new te::rst::BandProperty(
233  m_inputParameters.m_inputRasterBands[ inputRasterBandsIdx ] )->getProperty() ) ) );
234  bandsProperties[ inputRasterBandsIdx ]->m_noDataValue = m_inputParameters.m_noDataValue;
235  }
236 
237  te::gm::Envelope* gridMbr = new te::gm::Envelope();
238  gridMbr->m_llx = lowerLeftX;
239  gridMbr->m_lly = lowerLeftY;
240  gridMbr->m_urx = upperRightX;
241  gridMbr->m_ury = upperRightY;
242 
243  outParamsPtr->m_outputRasterPtr.reset(
245  outParamsPtr->m_rType,
249  bandsProperties,
250  outParamsPtr->m_rInfo,
251  0,
252  0 ) );
253  TERP_TRUE_OR_RETURN_FALSE( outParamsPtr->m_outputRasterPtr.get(),
254  "Output raster creation error" );
255  }
256 
257  // Rendering the output raster
258 
259  te::rst::Raster& outputRaster = *outParamsPtr->m_outputRasterPtr;
260  const te::rst::Grid& outputGrid = *outputRaster.getGrid();
263  te::gm::GeometricTransformation& geomTransf = *transformationPtr;
264  const unsigned int nRows = outputRaster.getNumberOfRows();
265  const unsigned int nCols = outputRaster.getNumberOfColumns();
266  unsigned int outRow = 0;
267  unsigned int outCol = 0;
268  double outX = 0;
269  double outY = 0;
270  double inRow = 0;
271  double inCol = 0;
272  std::complex< double > cValue;
273 
274  for( unsigned int inputRasterBandsIdx = 0 ; inputRasterBandsIdx <
275  m_inputParameters.m_inputRasterBands.size() ; ++inputRasterBandsIdx )
276  {
277  const unsigned int inputBandIdx = m_inputParameters.m_inputRasterBands[ inputRasterBandsIdx ];
278  const double inputBandNoDataValue = m_inputParameters.m_inputRasterPtr->getBand( inputBandIdx )->getProperty()->m_noDataValue;
279  te::rst::Band& outputBand = *outputRaster.getBand( inputRasterBandsIdx );
280 
281  double outputBandRangeMin = 0;
282  double outputBandRangeMax = 0;
284  outputBandRangeMin, outputBandRangeMax );
285 
286  for( outRow = 0 ; outRow < nRows ; ++outRow )
287  {
288  for( outCol = 0 ; outCol < nCols ; ++outCol )
289  {
290  outputGrid.gridToGeo( (double)outCol, (double)outRow, outX, outY );
291 
292  geomTransf.inverseMap( outX, outY, inCol, inRow );
293 
294  interpInstance.getValue( inCol, inRow, cValue, inputBandIdx );
295 
296  if( cValue.real() == inputBandNoDataValue )
297  {
298  outputBand.setValue( outCol, outRow, m_inputParameters.m_noDataValue );
299  }
300  else
301  {
302  outputBand.setValue( outCol, outRow, std::max( outputBandRangeMin,
303  std::min( outputBandRangeMax, cValue.real() ) ) );
304  }
305  }
306  }
307  }
308 
309 
310  return true;
311  }
312 
313  void Register::reset() throw( te::rp::Exception )
314  {
316  m_isInitialized = false;
317  }
318 
320  throw( te::rp::Exception )
321  {
322  reset();
323 
324  Register::InputParameters const* inputParamsPtr = dynamic_cast<
325  Register::InputParameters const* >( &inputParams );
326  TERP_TRUE_OR_THROW( inputParamsPtr, "Invalid paramters pointer" );
327 
328  m_inputParameters = *inputParamsPtr;
329 
330  // Checking the input raster
331 
333  "Invalid m_inputRasterPtr" )
334 
337  "Invalid raster" );
338 
340  "Invalid raster bands number" );
341 
342  for( unsigned int inRasterBandsIdx = 0 ; inRasterBandsIdx <
343  m_inputParameters.m_inputRasterBands.size() ; ++inRasterBandsIdx )
344  {
346  m_inputParameters.m_inputRasterBands[ inRasterBandsIdx ] <
348  "Invalid raster bands" );
349  }
350 
351  // checking transformation related parameters
352 
354  {
356  "Invalid geometric transformation" );
357  }
358  else
359  {
360  std::auto_ptr< te::gm::GeometricTransformation > transformationPtr(
362  TERP_TRUE_OR_RETURN_FALSE( transformationPtr.get() != 0,
363  "Geometric transformation instatiation error" );
364 
366  transformationPtr->getMinRequiredTiePoints(),
367  "Invalid tie-points number" )
368  }
369 
370  // checking other parameters
371 
373  "Invalid m_outputSRID" )
374 
376  "Invalid m_outputResolutionX" )
377 
379  "Invalid m_outputResolutionY" )
380 
381  m_isInitialized = true;
382 
383  return true;
384  }
385 
387  {
388  return m_isInitialized;
389  }
390 
391 
392 
393  } // end namespace rp
394 } // end namespace te
395 
Register input parameters.
Definition: Register.h:56
Near neighborhood interpolation method.
Definition: Enums.h:95
const OutputParameters & operator=(const OutputParameters &params)
Definition: Register.cpp:125
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:334
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:150
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:136
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:55
AbstractParameters * clone() const
Create a clone copy of this instance.
Definition: Register.cpp:97
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:92
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
const double & getY(std::size_t i) const
It returns the n-th y coordinate value.
Definition: LineString.cpp:391
Raster Processing functions.
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
A LinearRing is a LineString that is both closed and simple.
Definition: LinearRing.h:53
bool initialize(const AlgorithmInputParameters &inputParams)
Initialize the algorithm instance making it ready for execution.
Definition: Register.cpp:319
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:386
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
const double & getX(std::size_t i) const
It returns the n-th x coordinate value.
Definition: LineString.cpp:385
void reset()
Clear all internal allocated objects and reset the algorithm to its initial state.
Definition: Register.cpp:313
BandProperty * getProperty()
Returns the band property.
Definition: Band.cpp:428
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:61
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.
bool GetIndexedDetailedExtent(const te::rst::Grid &grid, te::gm::LinearRing &indexedDetailedExtent)
Create a indexed (lines,columns) datailed extent from the given grid.
Definition: Functions.cpp:2298
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
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:118
#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:77
std::size_t size() const
It returns the number of points (vertexes) in the geometry.
Definition: LineString.h:262