26 #include "../raster/Band.h" 27 #include "../raster/BandProperty.h" 28 #include "../raster/Grid.h" 29 #include "../raster/RasterFactory.h" 59 const std::vector< unsigned int >& rasterBands,
61 const unsigned int maskRasterBand,
62 const unsigned int rasterTargetAreaLineStart,
63 const unsigned int rasterTargetAreaColStart,
64 const unsigned int rasterTargetAreaWidth,
65 const unsigned int rasterTargetAreaHeight,
66 const double desiredRescaleFactorX,
67 const double desiredRescaleFactorY,
69 const unsigned char maxMemPercentUsage,
70 std::vector< boost::shared_ptr< FloatsMatrix > >& loadedRasterData,
72 double& achievedRescaleFactorX,
73 double& achievedRescaleFactorY )
77 const unsigned int rescaledNLines = (
unsigned int)(
78 ((
double)rasterTargetAreaHeight) * desiredRescaleFactorY );
79 const unsigned int rescaledNCols = (
unsigned int)(
80 ((
double)rasterTargetAreaWidth) * desiredRescaleFactorX );
82 achievedRescaleFactorX = ((double)rescaledNCols) / ((double)rasterTargetAreaWidth);
83 achievedRescaleFactorY = ((double)rescaledNLines) / ((double)rasterTargetAreaHeight);
86 unsigned char maxMemPercentUsagePerMatrix =
MAX( 1u, maxMemPercentUsage /
88 ( maskRasterPtr ? 1 : 0 )
90 ((
unsigned int)rasterBands.size()) )
93 loadedRasterData.resize( rasterBands.size() );
95 for(
unsigned int rasterBandsIdx = 0 ; rasterBandsIdx < rasterBands.size() ;
98 loadedRasterData[ rasterBandsIdx ].reset(
new FloatsMatrix );
101 maxMemPercentUsagePerMatrix ),
102 "Cannot allocate image 1 matrix" );
103 maxMemPercentUsagePerMatrix *= 2;
109 rescaledNLines, rescaledNCols,
111 "Cannot allocate image 1 mask matrix" );
120 assert( inMaskRasterBand );
122 unsigned char* outMaskLinePtr =
nullptr;
123 unsigned int outLine = 0;
124 unsigned int outCol = 0;
125 unsigned int inLine = 0;
126 unsigned int inCol = 0;
129 for( outLine = 0 ; outLine < rescaledNLines ; ++outLine )
131 inLine = (
unsigned int)( ( ( (
double)outLine ) /
132 desiredRescaleFactorY ) + ( (double)rasterTargetAreaLineStart ) );
134 outMaskLinePtr = loadedMaskRasterData[ outLine ];
136 for( outCol = 0 ; outCol < rescaledNCols ; ++outCol )
138 inCol = (
unsigned int)( ( ( (
double)outCol ) /
139 desiredRescaleFactorX ) + ( (double)rasterTargetAreaColStart ) );
141 inMaskRasterBand->
getValue( inCol, inLine, value );
144 outMaskLinePtr[ outCol ] = 0;
146 outMaskLinePtr[ outCol ] = 255;
153 const double rasterTargetAreaLineStartDouble = (double)
154 rasterTargetAreaLineStart;
155 const double rasterTargetAreaColStartDouble = (double)
156 rasterTargetAreaColStart;
158 float* floatLinePtr =
nullptr;
159 double* doubleLinePtr =
nullptr;
160 unsigned int outLine = 0;
161 unsigned int outCol = 0;
164 std::complex< double > interpolatedValue;
165 unsigned int bandIdx = 0;
173 "Cannot allocate auxiliar matrix" );
175 for(
unsigned int rasterBandsIdx = 0 ; rasterBandsIdx < rasterBands.size() ;
178 bandIdx= rasterBands[ rasterBandsIdx ];
180 bandMax = -1.0 * DBL_MAX;
182 for( outLine = 0 ; outLine < rescaledNLines ; ++outLine )
184 inLine = ( ( (double)outLine ) / desiredRescaleFactorY ) +
185 rasterTargetAreaLineStartDouble;
187 doubleLinePtr = auxBandData[ outLine ];
189 for( outCol = 0 ; outCol < rescaledNCols ; ++outCol )
191 inCol = ( ( (double)outCol ) / desiredRescaleFactorX ) +
192 rasterTargetAreaColStartDouble;
194 interpInstance.
getValue( inCol, inLine, interpolatedValue,
197 doubleLinePtr[ outCol ] = interpolatedValue.real();
199 if( interpolatedValue.real() > bandMax ) bandMax = interpolatedValue.real();
200 if( interpolatedValue.real() < bandMin ) bandMin = interpolatedValue.real();
204 if( bandMin == bandMax )
207 gain = ( 1.0 / ( bandMax - bandMin ) );
209 for( outLine = 0 ; outLine < rescaledNLines ; ++outLine )
211 doubleLinePtr = auxBandData[ outLine ];
212 floatLinePtr = loadedRasterData[ rasterBandsIdx ]->operator[]( outLine );
214 for( outCol = 0 ; outCol < rescaledNCols ; ++outCol )
216 floatLinePtr[ outCol ] = (float)( ( doubleLinePtr[ outCol ] - bandMin ) * gain );
228 const std::string& tifFileName )
230 std::map<std::string, std::string> rInfo;
231 rInfo[
"URI"] = tifFileName +
".tif";
233 std::vector<te::rst::BandProperty*> bandsProperties;
236 bandsProperties[0]->m_noDataValue = 0;
245 std::unique_ptr< te::rst::Raster > outputRasterPtr(
249 unsigned int line = 0;
250 unsigned int col = 0;
253 double rasterDataMin = DBL_MAX;
254 double rasterDataMax = (-1.0) * DBL_MAX;
258 for( col = 0 ; col <
nCols ; ++
col )
260 value = (double)rasterData[ line ][ col ];
262 if( rasterDataMin > value )
263 rasterDataMin = value;
264 if( rasterDataMax < value )
265 rasterDataMax = value;
268 if( rasterDataMax == rasterDataMin )
275 for( col = 0 ; col <
nCols ; ++
col )
277 value = (double)rasterData[ line ][ col ];
278 value = ( value - rasterDataMin ) / ( rasterDataMax - rasterDataMin );
280 value = std::max( 0.0, value );
281 value = std::min( 255.0, value );
283 outputRasterPtr->setValue( col, line, value, 0 );
284 outputRasterPtr->setValue( col, line, value, 1 );
285 outputRasterPtr->setValue( col, line, value, 2 );
288 InterestPointsSetT::const_iterator itB = interestPoints.begin();
289 InterestPointsSetT::const_iterator itE = interestPoints.end();
293 outputRasterPtr->setValue( itB->m_x, itB->m_y, 0, 0 );
294 outputRasterPtr->setValue( itB->m_x, itB->m_y, 255, 1 );
295 outputRasterPtr->setValue( itB->m_x, itB->m_y, 0, 2 );
303 const std::string& fileNameBeginning )
305 const unsigned int tifLinesNumber = (
unsigned int)std::sqrt( (
double)
309 double const* featureLinePtr =
nullptr;
311 InterestPointsSetT::const_iterator iIt = interestPoints.begin();
313 for(
unsigned int featuresIdx = 0 ; featuresIdx < features.
getLinesNumber() ;
316 featureLinePtr = features[ featuresIdx ];
318 std::vector<te::rst::BandProperty*> bandsProperties;
321 bandsProperties[0]->m_noDataValue = 0;
323 std::map<std::string, std::string> rInfo;
324 rInfo[
"URI"] = fileNameBeginning +
"_" + boost::lexical_cast< std::string >( iIt->m_x )
325 +
"_" + boost::lexical_cast< std::string >( iIt->m_y ) +
".tif";
328 tifLinesNumber,
nullptr, -1 );
330 std::unique_ptr< te::rst::Raster > outputRasterPtr(
334 unsigned int line = 0;
335 unsigned int col = 0;
341 for( col = 0 ; col < featuresColsNumber ; ++
col )
343 if( min > featureLinePtr[ col ] ) min = featureLinePtr[
col ];
344 if( max < featureLinePtr[ col ] ) max = featureLinePtr[
col ];
347 gain = 255.0 / ( max - min );
349 for( line = 0 ; line < tifLinesNumber ; ++
line )
350 for( col = 0 ; col < tifLinesNumber ; ++
col )
352 value = featureLinePtr[ ( line * tifLinesNumber ) + col ];
355 value =
MIN( 255.0, value );
356 value =
MAX( 0.0, value );
358 outputRasterPtr->setValue( col, line, value, 0 );
369 InterestPointsSetT::const_iterator it1 = interestPoints.begin();
370 InterestPointsSetT::const_iterator it2;
371 const InterestPointsSetT::const_iterator itE = interestPoints.end();
380 if( ( it1->m_x == it2->m_x ) && ( it1->m_y == it2->m_y ) )
virtual ~TiePointsLocatorStrategy()
This file contains include headers for the memory data source of TerraLib.
A raster band description.
It interpolates one pixel based on a selected algorithm. Methods currently available are Nearest Neig...
TiePointsLocatorStrategy & operator=(const TiePointsLocatorStrategy &)
#define MIN(a, b)
Macro that returns min between two values.
Red channel color interpretation.
void getValue(const double &c, const double &r, std::complex< double > &v, const std::size_t &b)
Get the interpolated value at specific band.
InterpolationMethod
Allowed interpolation methods.
Tie-Pointsr locator strategy.
#define MAX(a, b)
Macro that returns max between two values.
std::string m_errorMessage
Current error message.
#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...
static void createTifFromMatrix(const FloatsMatrix &rasterData, const InterestPointsSetT &interestPoints, const std::string &tifFileName)
Moravec interest points locator.
unsigned int unsigned int nCols
An abstract class for raster data strucutures.
unsigned int getColumnsNumber() const
The number of current matrix columns.
virtual void reset()
Clear all internal allocated resources and go back to the initial not-initialized state...
std::multiset< InterestPointT > InterestPointsSetT
TiePointsLocatorStrategy()
A raster band description.
virtual const Band * getBand(std::size_t i) const =0
Returns the raster i-th band.
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.
Tie-points locator strategy.
static void features2Tiff(const DoublesMatrix &features, const InterestPointsSetT &interestPoints, const std::string &fileNameBeginning)
Save the generated features to tif files.
const std::string & getErrorMessage() const
Return the current error message if there is any.
A generic template matrix.
static Raster * make()
It creates and returns an empty raster with default raster driver.
void setErrorMessage(const std::string &newErrorMessage)
Set the current error message.
Blue channel color interpretation.
A rectified grid is the spatial support for raster data.
Green channel color interpretation.
unsigned int getLinesNumber() const
The number of current matrix lines.
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.
virtual void getValue(unsigned int c, unsigned int r, double &value) const =0
Returns the cell attribute value.