All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TiePointsLocatorStrategy.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/TiePointsLocatorStrategy.cpp
22  \brief Tie-Pointsr locator strategy.
23 */
24 
26 #include "../raster/Band.h"
27 #include "../raster/BandProperty.h"
28 #include "../raster/Grid.h"
29 #include "../raster/RasterFactory.h"
30 
31 #include <memory.h>
32 
33 namespace te
34 {
35  namespace rp
36  {
38  {
39  }
40 
42  {
43  }
44 
46  {
47  }
48 
50  {
51  return *this;
52  }
53 
55  te::rst::Raster const* rasterPtr,
56  const std::vector< unsigned int >& rasterBands,
57  te::rst::Raster const* maskRasterPtr,
58  const unsigned int maskRasterBand,
59  const unsigned int rasterTargetAreaLineStart,
60  const unsigned int rasterTargetAreaColStart,
61  const unsigned int rasterTargetAreaWidth,
62  const unsigned int rasterTargetAreaHeight,
63  const double desiredRescaleFactorX,
64  const double desiredRescaleFactorY,
65  const te::rst::Interpolator::Method rasterInterpMethod,
66  const unsigned char maxMemPercentUsage,
67  std::vector< boost::shared_ptr< FloatsMatrix > >& loadedRasterData,
68  UCharsMatrix& loadedMaskRasterData,
69  double& achievedRescaleFactorX,
70  double& achievedRescaleFactorY )
71  {
72  // Allocating the output matrixes
73 
74  const unsigned int rescaledNLines = (unsigned int)(
75  ((double)rasterTargetAreaHeight) * desiredRescaleFactorY );
76  const unsigned int rescaledNCols = (unsigned int)(
77  ((double)rasterTargetAreaWidth) * desiredRescaleFactorX );
78 
79  achievedRescaleFactorX = ((double)rescaledNCols) / ((double)rasterTargetAreaWidth);
80  achievedRescaleFactorY = ((double)rescaledNLines) / ((double)rasterTargetAreaHeight);
81 
82  {
83  unsigned char maxMemPercentUsagePerMatrix = MAX( 1u, maxMemPercentUsage /
84  (
85  ( maskRasterPtr ? 1 : 0 )
86  +
87  ((unsigned int)rasterBands.size()) )
88  );
89 
90  loadedRasterData.resize( rasterBands.size() );
91 
92  for( unsigned int rasterBandsIdx = 0 ; rasterBandsIdx < rasterBands.size() ;
93  ++rasterBandsIdx )
94  {
95  loadedRasterData[ rasterBandsIdx ].reset( new FloatsMatrix );
96  TERP_TRUE_OR_RETURN_FALSE( loadedRasterData[ rasterBandsIdx ]->reset(
97  rescaledNLines, rescaledNCols, FloatsMatrix::AutoMemPol,
98  maxMemPercentUsagePerMatrix ),
99  "Cannot allocate image 1 matrix" );
100  maxMemPercentUsagePerMatrix *= 2;
101  }
102 
103  if( maskRasterPtr )
104  {
105  TERP_TRUE_OR_RETURN_FALSE( loadedMaskRasterData.reset(
106  rescaledNLines, rescaledNCols,
107  UCharsMatrix::AutoMemPol, maxMemPercentUsagePerMatrix ),
108  "Cannot allocate image 1 mask matrix" );
109  }
110  }
111 
112  // loading mask data
113 
114  if( maskRasterPtr )
115  {
116  te::rst::Band const* inMaskRasterBand = maskRasterPtr->getBand( maskRasterBand );
117  assert( inMaskRasterBand );
118 
119  unsigned char* outMaskLinePtr = 0;
120  unsigned int outLine = 0;
121  unsigned int outCol = 0;
122  unsigned int inLine = 0;
123  unsigned int inCol = 0;
124  double value = 0;
125 
126  for( outLine = 0 ; outLine < rescaledNLines ; ++outLine )
127  {
128  inLine = (unsigned int)( ( ( (double)outLine ) /
129  desiredRescaleFactorY ) + ( (double)rasterTargetAreaLineStart ) );
130 
131  outMaskLinePtr = loadedMaskRasterData[ outLine ];
132 
133  for( outCol = 0 ; outCol < rescaledNCols ; ++outCol )
134  {
135  inCol = (unsigned int)( ( ( (double)outCol ) /
136  desiredRescaleFactorX ) + ( (double)rasterTargetAreaColStart ) );
137 
138  inMaskRasterBand->getValue( inCol, inLine, value );
139 
140  if( value == 0 )
141  outMaskLinePtr[ outCol ] = 0;
142  else
143  outMaskLinePtr[ outCol ] = 255;
144  }
145  }
146  }
147 
148  // loading raster data
149  {
150  const double rasterTargetAreaLineStartDouble = (double)
151  rasterTargetAreaLineStart;
152  const double rasterTargetAreaColStartDouble = (double)
153  rasterTargetAreaColStart;
154  te::rst::Interpolator interpInstance( rasterPtr, rasterInterpMethod );
155  float* floatLinePtr = 0;
156  double* doubleLinePtr = 0;
157  unsigned int outLine = 0;
158  unsigned int outCol = 0;
159  double inLine = 0;
160  double inCol = 0;
161  std::complex< double > interpolatedValue;
162  unsigned int bandIdx = 0;
163  double bandMin = 0;
164  double bandMax = 0;
165  double gain = 0;
166 
167  DoublesMatrix auxBandData;
168  TERP_TRUE_OR_RETURN_FALSE( auxBandData.reset(
169  rescaledNLines, rescaledNCols, DoublesMatrix::AutoMemPol, 40 ),
170  "Cannot allocate auxiliar matrix" );
171 
172  for( unsigned int rasterBandsIdx = 0 ; rasterBandsIdx < rasterBands.size() ;
173  ++rasterBandsIdx )
174  {
175  bandIdx= rasterBands[ rasterBandsIdx ];
176  bandMin = DBL_MAX;
177  bandMax = -1.0 * DBL_MAX;
178 
179  for( outLine = 0 ; outLine < rescaledNLines ; ++outLine )
180  {
181  inLine = ( ( (double)outLine ) / desiredRescaleFactorY ) +
182  rasterTargetAreaLineStartDouble;
183 
184  doubleLinePtr = auxBandData[ outLine ];
185 
186  for( outCol = 0 ; outCol < rescaledNCols ; ++outCol )
187  {
188  inCol = ( ( (double)outCol ) / desiredRescaleFactorX ) +
189  rasterTargetAreaColStartDouble;
190 
191  interpInstance.getValue( inCol, inLine, interpolatedValue,
192  bandIdx );
193 
194  doubleLinePtr[ outCol ] = interpolatedValue.real();
195 
196  if( interpolatedValue.real() > bandMax ) bandMax = interpolatedValue.real();
197  if( interpolatedValue.real() < bandMin ) bandMin = interpolatedValue.real();
198  }
199  }
200 
201  if( bandMin == bandMax )
202  gain = 0.0;
203  else
204  gain = ( 1.0 / ( bandMax - bandMin ) );
205 
206  for( outLine = 0 ; outLine < rescaledNLines ; ++outLine )
207  {
208  doubleLinePtr = auxBandData[ outLine ];
209  floatLinePtr = loadedRasterData[ rasterBandsIdx ]->operator[]( outLine );
210 
211  for( outCol = 0 ; outCol < rescaledNCols ; ++outCol )
212  {
213  floatLinePtr[ outCol ] = (float)( ( doubleLinePtr[ outCol ] - bandMin ) * gain );
214  }
215  }
216  }
217  }
218 
219  return true;
220  }
221 
223  const FloatsMatrix& rasterData,
224  const InterestPointsSetT& interestPoints,
225  const std::string& tifFileName )
226  {
227  std::map<std::string, std::string> rInfo;
228  rInfo["URI"] = tifFileName + ".tif";
229 
230  std::vector<te::rst::BandProperty*> bandsProperties;
231  bandsProperties.push_back(new te::rst::BandProperty( 0, te::dt::UCHAR_TYPE, "" ));
232  bandsProperties[0]->m_colorInterp = te::rst::RedCInt;
233  bandsProperties[0]->m_noDataValue = 0;
234  bandsProperties.push_back(new te::rst::BandProperty( *bandsProperties[0] ));
235  bandsProperties[1]->m_colorInterp = te::rst::GreenCInt;
236  bandsProperties.push_back(new te::rst::BandProperty( *bandsProperties[0] ));
237  bandsProperties[2]->m_colorInterp = te::rst::BlueCInt;
238 
239  te::rst::Grid* newgrid = new te::rst::Grid( rasterData.getColumnsNumber(),
240  rasterData.getLinesNumber(), 0, -1 );
241 
242  std::auto_ptr< te::rst::Raster > outputRasterPtr(
243  te::rst::RasterFactory::make( "GDAL", newgrid, bandsProperties, rInfo, 0, 0));
244  TERP_TRUE_OR_THROW( outputRasterPtr.get(), "Output raster creation error");
245 
246  unsigned int line = 0;
247  unsigned int col = 0;
248  const unsigned int nLines = rasterData.getLinesNumber();
249  const unsigned int nCols = rasterData.getColumnsNumber();
250  double rasterDataMin = DBL_MAX;
251  double rasterDataMax = (-1.0) * DBL_MAX;
252  double value = 0;
253 
254  for( line = 0 ; line < nLines ; ++line )
255  for( col = 0 ; col < nCols ; ++col )
256  {
257  value = (double)rasterData[ line ][ col ];
258 
259  if( rasterDataMin > value )
260  rasterDataMin = value;
261  if( rasterDataMax < value )
262  rasterDataMax = value;
263  }
264 
265  if( rasterDataMax == rasterDataMin )
266  {
267  rasterDataMin = 0.0;
268  rasterDataMax = 1.0;
269  }
270 
271  for( line = 0 ; line < nLines ; ++line )
272  for( col = 0 ; col < nCols ; ++col )
273  {
274  value = (double)rasterData[ line ][ col ];
275  value = ( value - rasterDataMin ) / ( rasterDataMax - rasterDataMin );
276  value *= 255.0;
277  value = std::max( 0.0, value );
278  value = std::min( 255.0, value );
279 
280  outputRasterPtr->setValue( col, line, value, 0 );
281  outputRasterPtr->setValue( col, line, value, 1 );
282  outputRasterPtr->setValue( col, line, value, 2 );
283  }
284 
285  InterestPointsSetT::const_iterator itB = interestPoints.begin();
286  InterestPointsSetT::const_iterator itE = interestPoints.end();
287 
288  while( itB != itE )
289  {
290  outputRasterPtr->setValue( itB->m_x, itB->m_y, 0, 0 );
291  outputRasterPtr->setValue( itB->m_x, itB->m_y, 255, 1 );
292  outputRasterPtr->setValue( itB->m_x, itB->m_y, 0, 2 );
293 
294  ++itB;
295  }
296  }
297 
299  const InterestPointsSetT& interestPoints,
300  const std::string& fileNameBeginning )
301  {
302  const unsigned int tifLinesNumber = (unsigned int)std::sqrt( (double)
303  features.getColumnsNumber() );
304  const unsigned int featuresColsNumber = features.getColumnsNumber();
305 
306  double const* featureLinePtr = 0;
307 
308  InterestPointsSetT::const_iterator iIt = interestPoints.begin();
309 
310  for( unsigned int featuresIdx = 0 ; featuresIdx < features.getLinesNumber() ;
311  ++featuresIdx )
312  {
313  featureLinePtr = features[ featuresIdx ];
314 
315  std::vector<te::rst::BandProperty*> bandsProperties;
316  bandsProperties.push_back(new te::rst::BandProperty( 0, te::dt::UCHAR_TYPE, "" ));
317  bandsProperties[0]->m_colorInterp = te::rst::RedCInt;
318  bandsProperties[0]->m_noDataValue = 0;
319 
320  std::map<std::string, std::string> rInfo;
321  rInfo["URI"] = fileNameBeginning + "_" + boost::lexical_cast< std::string >( iIt->m_x )
322  + "_" + boost::lexical_cast< std::string >( iIt->m_y ) + ".tif";
323 
324  te::rst::Grid* newgrid = new te::rst::Grid( tifLinesNumber,
325  tifLinesNumber, 0, -1 );
326 
327  std::auto_ptr< te::rst::Raster > outputRasterPtr(
328  te::rst::RasterFactory::make( "GDAL", newgrid, bandsProperties, rInfo, 0, 0));
329  TERP_TRUE_OR_THROW( outputRasterPtr.get(), "Output raster creation error");
330 
331  unsigned int line = 0;
332  unsigned int col = 0;
333  double value = 0;
334  double min = 0;
335  double max = 0;
336  double gain = 1.0;
337 
338  for( col = 0 ; col < featuresColsNumber ; ++col )
339  {
340  if( min > featureLinePtr[ col ] ) min = featureLinePtr[ col ];
341  if( max < featureLinePtr[ col ] ) max = featureLinePtr[ col ];
342  }
343 
344  gain = 255.0 / ( max - min );
345 
346  for( line = 0 ; line < tifLinesNumber ; ++line )
347  for( col = 0 ; col < tifLinesNumber ; ++col )
348  {
349  value = featureLinePtr[ ( line * tifLinesNumber ) + col ];
350  value *= gain;
351  value -= min;
352  value = MIN( 255.0, value );
353  value = MAX( 0.0, value );
354 
355  outputRasterPtr->setValue( col, line, value, 0 );
356  }
357 
358  ++iIt;
359  }
360  }
361 
362 
364  const InterestPointsSetT& interestPoints, double& x, double& y )
365  {
366  InterestPointsSetT::const_iterator it1 = interestPoints.begin();
367  InterestPointsSetT::const_iterator it2;
368  const InterestPointsSetT::const_iterator itE = interestPoints.end();
369 
370  while( it1 != itE )
371  {
372  it2 = it1;
373  ++it2;
374 
375  while( it2 != itE )
376  {
377  if( ( it1->m_x == it2->m_x ) && ( it1->m_y == it2->m_y ) )
378  {
379  x = it1->m_x;
380  y = it1->m_y;
381  return false;
382  }
383 
384  ++it2;
385  }
386 
387  ++it1;
388  }
389 
390  return true;
391  }
392 
393  } // end namespace rp
394 } // end namespace te
395 
This file contains include headers for the memory data source of TerraLib.
virtual void getValue(unsigned int c, unsigned int r, double &value) const =0
Returns the cell attribute value.
A raster band description.
Definition: BandProperty.h:61
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
#define MIN(a, b)
Macro that returns min between two values.
Red channel color interpretation.
Definition: Enums.h:59
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
InterpolationMethod
Allowed interpolation methods.
Definition: Enums.h:92
Tie-Pointsr locator strategy.
#define MAX(a, b)
Macro that returns max between two values.
#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
static void createTifFromMatrix(const FloatsMatrix &rasterData, const InterestPointsSetT &interestPoints, const std::string &tifFileName)
Moravec interest points locator.
const TiePointsLocatorStrategy & operator=(const TiePointsLocatorStrategy &)
virtual void reset()=0
Clear all internal allocated resources and go back to the initial not-initialized state...
An abstract class for raster data strucutures.
Definition: Raster.h:71
unsigned int getColumnsNumber() const
The number of current matrix columns.
Definition: Matrix.h:678
std::multiset< InterestPointT > InterestPointsSetT
A raster band description.
Definition: Band.h:63
static bool loadRasterData(te::rst::Raster const *rasterPtr, const std::vector< unsigned int > &rasterBands, te::rst::Raster const *maskRasterPtr, const unsigned int maskRasterBand, const unsigned int rasterTargetAreaLineStart, const unsigned int rasterTargetAreaColStart, const unsigned int rasterTargetAreaWidth, const unsigned int rasterTargetAreaHeight, const double desiredRescaleFactorX, const double desiredRescaleFactorY, const te::rst::Interpolator::Method rasterInterpMethod, const unsigned char maxMemPercentUsage, std::vector< boost::shared_ptr< FloatsMatrix > > &loadedRasterData, UCharsMatrix &loadedMaskRasterData, double &achievedRescaleFactorX, double &achievedRescaleFactorY)
Load rasters data (normalized between 0 and 1).
void reset()
Reset (clear) the active instance data.
Definition: Matrix.h:480
Tie-points locator strategy.
static void features2Tiff(const DoublesMatrix &features, const InterestPointsSetT &interestPoints, const std::string &fileNameBeginning)
Save the generated features to tif files.
A generic template matrix.
Definition: Matrix.h:51
static Raster * make()
It creates and returns an empty raster with default raster driver.
Blue channel color interpretation.
Definition: Enums.h:61
A rectified grid is the spatial support for raster data.
Definition: Grid.h:68
Green channel color interpretation.
Definition: Enums.h:60
unsigned int getLinesNumber() const
The number of current matrix lines.
Definition: Matrix.h:671
static bool checkForDuplicatedInterestPoints(const InterestPointsSetT &interestPoints, double &x, double &y)
Check for duplicated interest points.
#define TERP_TRUE_OR_THROW(value, message)
Checks if value is true and throws an exception if not.
Definition: Macros.h:149