28 #include "../raster/Interpolator.h"
29 #include "../raster/Enums.h"
30 #include "../raster/RasterFactory.h"
31 #include "../raster/Grid.h"
32 #include "../raster/Band.h"
33 #include "../raster/BandProperty.h"
34 #include "../raster/PositionIterator.h"
35 #include "../raster/Utils.h"
36 #include "../memory/CachedRaster.h"
37 #include "../geometry/Envelope.h"
38 #include "../geometry/GTFactory.h"
39 #include "../geometry/Polygon.h"
40 #include "../geometry/LinearRing.h"
41 #include "../geometry/MultiPolygon.h"
42 #include "../srs/Converter.h"
43 #include "../common/progress/TaskProgress.h"
45 #include <boost/shared_ptr.hpp>
46 #include <boost/scoped_array.hpp>
76 m_feederRasterPtr = 0;
77 m_inputRastersBands.clear();
79 m_geomTransfName =
"Affine";
82 m_forceInputNoDataValue =
false;
84 m_autoEqualize =
true;
85 m_useRasterCache =
true;
86 m_enableProgress =
false;
87 m_enableMultiThread =
true;
136 m_outputRasterPtr.reset();
165 throw( te::rp::Exception )
175 std::auto_ptr< te::common::TaskProgress > progressPtr;
182 progressPtr->setMessage(
"Mosaic" );
187 double mosaicXResolution = 0.0;
188 double mosaicYResolution = 0.0;
189 double mosaicLLX = DBL_MAX;
190 double mosaicLLY = DBL_MAX;
191 double mosaicURX = -1.0 * DBL_MAX;
192 double mosaicURY = -1.0 * DBL_MAX;
194 std::vector< double > mosaicBandsRangeMin;
195 std::vector< double > mosaicBandsRangeMax;
197 std::vector< te::gm::Polygon > rastersBBoxes;
200 std::vector< boost::shared_ptr< te::gm::GeometricTransformation > >
201 eachRasterPixelToFirstRasterPixelGeomTransfms;
206 std::vector< te::rst::Grid > rastersGrids;
211 unsigned int inputRasterIdx = 0;
213 boost::shared_ptr< te::gm::GeometricTransformation > auxTransPtr;
227 rastersGrids.push_back( (*inputRasterPtr->
getGrid()) );
231 if( inputRasterIdx == 0 )
249 auxLinearRingPtr->
setPoint( 0, mosaicLLX, mosaicURY );
250 auxLinearRingPtr->
setPoint( 1, mosaicURX, mosaicURY );
251 auxLinearRingPtr->
setPoint( 2, mosaicURX, mosaicLLY );
252 auxLinearRingPtr->
setPoint( 3, mosaicLLX, mosaicLLY );
253 auxLinearRingPtr->
setPoint( 4, mosaicLLX, mosaicURY );
254 auxPolygon.
push_back( auxLinearRingPtr );
255 auxPolygon.
setSRID( mosaicSRID );
256 rastersBBoxes.push_back( auxPolygon );
262 if( ( inputRasterIdx == 1 ) ||
273 const std::vector< te::gm::GTParameters::TiePoint >& inputTPs =
275 const unsigned int inputTPsSize = inputTPs.size();
277 (*eachRasterPixelToFirstRasterPixelGeomTransfms[ inputRasterIdx - 2 ].get());
279 for(
unsigned int inputTPsIdx = 0 ; inputTPsIdx < inputTPsSize ;
282 auxTP.second = inputTPs[ inputTPsIdx ].second;
283 lastTransf.
inverseMap( inputTPs[ inputTPsIdx ].first, auxTP.first );
291 "Geometric transformation instatiation error" );
293 "Geometric transformation parameters calcule error" );
294 eachRasterPixelToFirstRasterPixelGeomTransfms.push_back( auxTransPtr );
308 auxTransPtr->inverseMap( urCoord2, urCoord1 );
309 auxTransPtr->inverseMap( llCoord2, llCoord1 );
313 rastersGrids[ 0 ].gridToGeo( urCoord1.
x, urCoord1.
y, urCoord2.
x,
315 rastersGrids[ 0 ].gridToGeo( llCoord1.
x, llCoord1.
y, llCoord2.
x,
320 mosaicLLX = std::min( mosaicLLX, urCoord2.
x );
321 mosaicLLX = std::min( mosaicLLX, llCoord2.
x );
323 mosaicLLY = std::min( mosaicLLY, urCoord2.
y );
324 mosaicLLY = std::min( mosaicLLY, llCoord2.
y );
326 mosaicURX = std::max( mosaicURX, urCoord2.
x );
327 mosaicURX = std::max( mosaicURX, llCoord2.
x );
329 mosaicURY = std::max( mosaicURY, urCoord2.
y );
330 mosaicURY = std::max( mosaicURY, llCoord2.
y );
336 auxLinearRingPtr->
setPoint( 0, llCoord2.
x, urCoord2.
y );
337 auxLinearRingPtr->
setPoint( 1, urCoord2.
x, urCoord2.
y );
338 auxLinearRingPtr->
setPoint( 2, urCoord2.
x, llCoord2.
y );
339 auxLinearRingPtr->
setPoint( 3, llCoord2.
x, llCoord2.
y );
340 auxLinearRingPtr->
setPoint( 4, llCoord2.
x, urCoord2.
y );
341 auxPolygon.
push_back( auxLinearRingPtr );
342 auxPolygon.
setSRID( mosaicSRID );
343 rastersBBoxes.push_back( auxPolygon );
348 for( std::vector< unsigned int >::size_type inputRastersBandsIdx = 0 ;
349 inputRastersBandsIdx <
351 ++inputRastersBandsIdx )
353 const unsigned int& currBand =
369 progressPtr->pulse();
370 if( ! progressPtr->isActive() )
return false;
378 mosaicBandsRangeMin.resize(
380 mosaicBandsRangeMax.resize(
383 std::vector< te::rst::BandProperty* > bandsProperties;
384 for( std::vector< unsigned int >::size_type bandIdx = 0 ; bandIdx <
392 mosaicBandsRangeMin[ bandIdx ],
393 mosaicBandsRangeMax[ bandIdx ]);
398 mosaicURY ), mosaicSRID );
409 "Output raster creation error" );
414 std::auto_ptr< te::mem::CachedRaster > cachedOutputRasterInstancePtr;
421 outputRasterPtr = cachedOutputRasterInstancePtr.get();
426 progressPtr->pulse();
427 if( ! progressPtr->isActive() )
return false;
434 std::vector< boost::shared_ptr< te::gm::GeometricTransformation > >
435 eachRasterPixelToMosaicRasterPixelGeomTransfms;
438 const double firstRasterColOffset = std::abs( rastersBBoxes[ 0 ].getMBR()->m_llx -
441 const double firstRasterLinOffset = std::abs( rastersBBoxes[ 0 ].getMBR()->m_ury -
451 const double prevRasterColOffset = std::abs( rastersBBoxes[ tiePointsIdx ].getMBR()->m_llx -
454 const double prevRasterLinOffset = std::abs( rastersBBoxes[ tiePointsIdx ].getMBR()->m_ury -
458 for(
unsigned int tpIdx = 0 ; tpIdx < transfParams.
m_tiePoints.size() ;
463 transfParams.
m_tiePoints[ tpIdx ].first.x += firstRasterColOffset;
464 transfParams.
m_tiePoints[ tpIdx ].first.y += firstRasterLinOffset;
468 transfParams.
m_tiePoints[ tpIdx ].first.x += prevRasterColOffset;
469 transfParams.
m_tiePoints[ tpIdx ].first.y += prevRasterLinOffset;
473 boost::shared_ptr< te::gm::GeometricTransformation > auxTransPtr(
476 "Geometric transformation instatiation error" );
478 "Geometric transformation parameters calcule error" );
479 eachRasterPixelToMosaicRasterPixelGeomTransfms.push_back( auxTransPtr );
485 progressPtr->pulse();
486 if( ! progressPtr->isActive() )
return false;
495 unsigned int col = 0;
496 unsigned int row = 0;
497 unsigned int bandIdx = 0;
499 for( bandIdx = 0 ; bandIdx < nBands ; ++bandIdx )
503 for( row = 0 ; row < nRows ; ++row )
505 for( col = 0 ; col < nCols ; ++col )
515 progressPtr->pulse();
516 if( ! progressPtr->isActive() )
return false;
522 std::vector< double > mosaicTargetMeans( outputRasterPtr->
getNumberOfBands(), 0 );
523 std::vector< double > mosaicTargetVariances( outputRasterPtr->
getNumberOfBands(), 0 );
532 double inXStartGeo = 0;
533 double inYStartGeo = 0;
535 double outRowStartDouble = 0;
536 double outColStartDouble = 0;
538 outColStartDouble, outRowStartDouble );
540 const unsigned int outRowStart = (
unsigned int)std::max( 0.0, outRowStartDouble );
541 const unsigned int outColStart = (
unsigned int)std::max( 0.0, outColStartDouble );
542 const unsigned int outRowsBound = std::min( outRowStart +
545 const unsigned int outColsBound = std::min( outColStart +
549 const unsigned int nBands = (
unsigned int)
551 unsigned int outCol = 0;
552 unsigned int outRow = 0;
555 double bandNoDataValue = -1.0 * DBL_MAX;
556 std::complex< double > pixelCValue = 0;
559 unsigned int inputBandIdx = 0;
561 for(
unsigned int inputRastersBandsIdx = 0 ; inputRastersBandsIdx <
562 nBands ; ++inputRastersBandsIdx )
565 inputRastersBandsIdx ] ;
570 (*outputRasterPtr->
getBand( inputRastersBandsIdx ));
571 unsigned int validPixelsNumber = 0;
573 double& mean = mosaicTargetMeans[ inputRastersBandsIdx ];
576 for( outRow = outRowStart ; outRow < outRowsBound ; ++outRow )
578 inRow = ((double)outRow) - outRowStartDouble;
580 for( outCol = outColStart ; outCol < outColsBound ; ++outCol )
582 inCol = ((double)outCol) - outColStartDouble;
584 interpInstance.
getValue( inCol, inRow, pixelCValue, inputBandIdx );
586 if( pixelCValue.real() != bandNoDataValue )
588 outBand.
setValue( outCol, outRow, pixelCValue );
589 mean += pixelCValue.real();
595 mean /= ( (double)validPixelsNumber );
601 double& variance = mosaicTargetVariances[ inputRastersBandsIdx ];
604 double pixelValue = 0;
606 for( outRow = outRowStart ; outRow < outRowsBound ; ++outRow )
608 for( outCol = outColStart ; outCol < outColsBound ; ++outCol )
610 outBand.
getValue( outCol, outRow, pixelValue );
614 variance += ( ( pixelValue - mean ) * ( pixelValue -
615 mean ) ) / ( (
double)validPixelsNumber );
625 progressPtr->pulse();
626 if( ! progressPtr->isActive() )
return false;
631 std::auto_ptr< te::gm::MultiPolygon > mosaicBBoxesUnionPtr(
633 rastersBBoxes[ 0 ].getSRID(), 0 ) );
635 mosaicBBoxesUnionPtr->add( (
te::gm::Polygon*)( rastersBBoxes[ 0 ].clone() ) );
639 std::vector< unsigned int > outputRasterBands;
640 std::vector< double > dummyRasterOffsets;
641 std::vector< double > dummyRasterScales;
642 for(
unsigned int outputRasterBandsIdx = 0 ; outputRasterBandsIdx <
645 outputRasterBands.push_back( outputRasterBandsIdx );
646 dummyRasterOffsets.push_back( 0.0 );
647 dummyRasterScales.push_back( 1.0 );
663 std::auto_ptr< te::mem::CachedRaster > cachedInputRasterPtr;
667 *nonCachedInputRasterPtr, 25, 0 ) );
668 inputRasterPtr = cachedInputRasterPtr.get();
673 std::vector< double > currentRasterBandsOffsets;
674 std::vector< double > currentRasterBandsScales;
678 double currentRasterVariance = 0;
679 double currentRasterMean = 0;
681 for(
unsigned int inputRastersBandsIdx = 0 ; inputRastersBandsIdx <
683 ++inputRastersBandsIdx )
686 inputRastersBandsIdx ];
692 currentRasterVariance );
694 currentRasterBandsScales.push_back( std::sqrt( mosaicTargetVariances[ inputRastersBandsIdx ] /
695 currentRasterVariance ) );
696 currentRasterBandsOffsets.push_back( mosaicTargetMeans[ inputRastersBandsIdx ] -
697 ( currentRasterBandsScales[ inputRastersBandsIdx ] * currentRasterMean ) );
702 currentRasterBandsOffsets = dummyRasterOffsets;
703 currentRasterBandsScales = dummyRasterScales;
722 currentRasterBandsOffsets,
723 currentRasterBandsScales,
724 mosaicBBoxesUnionPtr.get(),
726 *( eachRasterPixelToMosaicRasterPixelGeomTransfms[ inputRasterIdx - 1 ] ),
729 "Blender initiazing error" );
732 "Error blending images" );
740 std::auto_ptr< te::gm::Geometry > unionMultiPolPtr;
741 unionMultiPolPtr.reset( mosaicBBoxesUnionPtr->Union(
742 &( rastersBBoxes[ inputRasterIdx ] ) ) );
744 unionMultiPolPtr->setSRID( mosaicBBoxesUnionPtr->getSRID() );
753 unionMultiPolPtr->getSRID(), 0 );
755 mosaicBBoxesUnionPtr.reset( mPolPtr );
765 cachedInputRasterPtr.reset();
771 progressPtr->pulse();
772 if( ! progressPtr->isActive() )
return false;
786 throw( te::rp::Exception )
799 "Invalid m_feederRasterPtr" )
803 "Invalid number of rasters" )
812 for( std::vector< std::vector< unsigned int > >::size_type
813 inputRastersBandsIdx = 0 ; inputRastersBandsIdx <
817 inputRastersBandsIdx ].size() > 0,
"Invalid bands number" );
821 0 ].size(),
"Bands number mismatch" );
842 const bool& forceNoDataValue,
843 const double& noDataValue,
844 double& mean,
double& variance )
849 double internalNoDataValue = 0;
850 if( forceNoDataValue )
851 internalNoDataValue = noDataValue;
860 double pixelsNumber = 0;
862 unsigned int col = 0;
863 unsigned int line = 0;
865 for( line = 0 ; line < nLines ; ++line )
866 for( col = 0 ; col < nCols ; ++col )
870 if( value != internalNoDataValue )
877 if( pixelsNumber != 0.0 )
879 mean /= pixelsNumber;
881 for( line = 0 ; line < nLines ; ++line )
882 for( col = 0 ; col < nCols ; ++col )
886 if( value != internalNoDataValue )
888 variance += ( ( value - mean ) * ( value - mean ) ) / pixelsNumber;
virtual unsigned int getObjsCount() const =0
Return the total number of feeder objects.
unsigned int getNumberOfRows() const
Returns the grid number of rows.
void push_back(Curve *ring)
It adds the curve to the curve polygon.
MultiPolygon is a MultiSurface whose elements are Polygons.
std::map< std::string, std::string > m_rInfo
The necessary information to create the output rasters (as described in te::raster::RasterFactory).
const OutputParameters & operator=(const OutputParameters ¶ms)
std::vector< TiePoint > m_tiePoints
Tie points.
TERASTEREXPORT void GetDataTypeRanges(const int &dataType, double &min, double &max)
Return the values range of a given data type.
Index into a lookup table.
virtual void getValue(unsigned int c, unsigned int r, double &value) const =0
Returns the cell attribute value.
void reset()
Clear all internal allocated objects and reset the algorithm to its initial state.
void setSRID(int srid)
It sets the Spatial Reference System ID of the geometry and all its parts if it is a GeometryCollecti...
bool initialize(const AlgorithmInputParameters &inputParams)
Initialize the algorithm instance making it ready for execution.
A raster band description.
Blended pixel value calculation for two overlaped rasters.
int getSRID() const
Returns the grid spatial reference system identifier.
unsigned int getNumberOfColumns() const
Returns the raster number of columns.
int m_nblocksx
The number of blocks in x.
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...
int m_nblocksy
The number of blocks in y.
AbstractParameters * clone() const
Create a clone copy of this instance.
This class can be used to inform the progress of a task.
Raster Processing algorithm output parameters base interface.
double m_urx
Upper right corner x-coordinate.
static void calcBandStatistics(const te::rst::Band &band, const bool &forceNoDataValue, const double &noDataValue, double &mean, double &variance)
Raster band statistics calcule.
An utility struct for representing 2D coordinates.
void getValue(const double &c, const double &r, std::complex< double > &v, const std::size_t &b)
Get the interpolated value at specific band.
double m_noDataValue
Value to indicate elements where there is no data, default is std::numeric_limits::max().
bool m_isInitialized
Tells if this instance is initialized.
std::pair< Coord2D, Coord2D > TiePoint
Tie point type definition.
void geoToGrid(const double &x, const double &y, double &col, double &row) const
Get the grid point associated to a spatial location.
bool blendIntoRaster1()
Execute blending of the given input rasters and write the result into raster1.
double getResolutionY() const
Returns the grid vertical (y-axis) resolution.
#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...
const Algorithm & operator=(const Algorithm &)
te::common::AccessPolicy getAccessPolicy() const
Returns the raster access policy.
A LinearRing is a LineString that is both closed and simple.
double m_llx
Lower left corner x-coordinate.
bool execute(AlgorithmOutputParameters &outputParams)
Executes the algorithm using the supplied parameters.
void setPoint(std::size_t i, const double &x, const double &y)
It sets the value of the specified point.
virtual void reset()=0
Reset the feeder to the first position (subsequent accesses will start from the first sequence obejct...
An Envelope defines a 2D rectangular region.
An abstract class for raster data strucutures.
unsigned int getNumberOfRows() const
Returns the raster number of rows.
BandProperty * getProperty()
Returns the band property.
int m_blkw
Block width (pixels).
virtual std::size_t getNumberOfBands() const =0
Returns the number of bands (dimension of cells attribute values) in the raster.
unsigned int getNumberOfColumns() const
Returns the grid number of columns.
double getResolutionX() const
Returns the grid horizontal (x-axis) resolution.
A RAM cache adaptor to an external existent raster that must always be avaliable. ...
std::string m_rType
Output raster data source type (as described in te::raster::RasterFactory ).
A raster band description.
Grid * getGrid()
It returns the raster grid.
static GeometricTransformation * make(const std::string &factoryKey)
It creates an object with the appropriated factory.
double m_lly
Lower left corner y-coordinate.
virtual void setValue(unsigned int c, unsigned int r, const double value)=0
Sets the cell attribute value.
A Converter is responsible for the conversion of coordinates between different Coordinate Systems (CS...
Abstract parameters base interface.
#define TERP_LOG_AND_RETURN_FALSE(message)
Logs a warning message will and return false.
bool isInitialized() const
Returns true if the algorithm instance is initialized and ready for execution.
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
double m_ury
Upper right corner y-coordinate.
void add(Geometry *g)
It adds the geometry into the collection.
static Raster * make()
It creates and returns an empty raster with default raster driver.
Mosaic output parameters.
#define TERP_DEBUG_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...
te::gm::Envelope * getExtent()
Returns the geographic extension of the grid.
void clear()
It deletes all the rings of the CurvePolygon and clear it.
void gridToGeo(const double &col, const double &row, double &x, double &y) const
Get the spatial location of a grid point.
std::auto_ptr< te::rst::Raster > m_outputRasterPtr
The generated output mosaic raster.
void reset()
Clear all internal allocated resources and reset the parameters instance to its initial state...
2D Geometric transformation parameters.
Create a mosaic from a set of rasters using tie-points.
TiePointsMosaic::InputParameters m_inputParameters
Input execution parameters.
virtual bool moveNext()=0
Advances to the next sequence obeject.
int m_blkh
Block height (pixels).
Near neighborhood interpolation method.
virtual te::rst::Raster const * getCurrentObj() const =0
Return the current sequence object.
A rectified grid is the spatial support for raster data.
virtual unsigned int getCurrentOffset() const =0
Return the index of the current object.
#define TERP_TRUE_OR_THROW(value, message)
Checks if value is true and throws an exception if not.
bool initialize(te::rst::Raster &raster1, const std::vector< unsigned int > &raster1Bands, const te::rst::Raster &raster2, const std::vector< unsigned int > &raster2Bands, const BlendMethod &blendMethod, const te::rst::Interpolator::Method &interpMethod1, const te::rst::Interpolator::Method &interpMethod2, const double &noDataValue, const bool forceInputNoDataValue, const std::vector< double > &pixelOffsets1, const std::vector< double > &pixelScales1, const std::vector< double > &pixelOffsets2, const std::vector< double > &pixelScales2, te::gm::MultiPolygon const *const r1ValidDataDelimiterPtr, te::gm::MultiPolygon const *const r2ValidDataDelimiterPtr, const te::gm::GeometricTransformation &geomTransformation, const unsigned int threadsNumber, const bool enableProgressInterface)
Inititate the blender instance.