29 #include "../raster/Interpolator.h" 30 #include "../raster/Enums.h" 31 #include "../raster/RasterFactory.h" 32 #include "../raster/Grid.h" 33 #include "../raster/Band.h" 34 #include "../raster/BandProperty.h" 35 #include "../raster/PositionIterator.h" 36 #include "../raster/Utils.h" 37 #include "../memory/CachedRaster.h" 38 #include "../geometry/Envelope.h" 39 #include "../geometry/GTFactory.h" 40 #include "../geometry/Polygon.h" 41 #include "../geometry/LinearRing.h" 42 #include "../geometry/MultiPolygon.h" 43 #include "../srs/Converter.h" 44 #include "../common/progress/TaskProgress.h" 45 #include "../common/MathUtils.h" 47 #include <boost/shared_array.hpp> 48 #include <boost/filesystem.hpp> 49 #include <boost/lexical_cast.hpp> 136 m_outputRasterPtr.reset();
146 m_outputRasterPtr.reset();;
176 std::unique_ptr< te::common::TaskProgress > progressPtr;
183 progressPtr->setMessage(
"Mosaic" );
188 double mosaicXResolution = 0.0;
189 double mosaicYResolution = 0.0;
190 double mosaicLLX = DBL_MAX;
191 double mosaicLLY = DBL_MAX;
192 double mosaicURX = -1.0 * DBL_MAX;
193 double mosaicURY = -1.0 * DBL_MAX;
196 std::vector< te::gm::Polygon > rastersBBoxes;
200 unsigned int inputRasterIdx = 0;
201 std::unique_ptr< te::gm::Polygon > rasterExtentPolPtr;
213 for( std::vector< unsigned int >::size_type inputRastersBandsIdx = 0 ;
214 inputRastersBandsIdx <
216 ++inputRastersBandsIdx )
218 const unsigned int& currBand =
227 if( inputRasterIdx == 0 )
232 mosaicBaseBandProperties = *inputRasterPtr->
getBand(
241 rasterExtentPolPtr->transform( mosaicSRID );
244 rastersBBoxes.push_back( *rasterExtentPolPtr );
248 mosaicLLX = std::min( mosaicLLX, rasterExtentPolPtr->getMBR()->getLowerLeftX() );
249 mosaicLLY = std::min( mosaicLLY, rasterExtentPolPtr->getMBR()->getLowerLeftY() );
251 mosaicURX = std::max( mosaicURX, rasterExtentPolPtr->getMBR()->getUpperRightX() );
252 mosaicURY = std::max( mosaicURY, rasterExtentPolPtr->getMBR()->getUpperRightY() );
262 progressPtr->pulse();
263 if( ! progressPtr->isActive() )
return false;
269 std::vector< double > mosaicBandsRangeMin;
270 std::vector< double > mosaicBandsRangeMax;
273 mosaicBandsRangeMin.resize(
275 mosaicBandsRangeMax.resize(
278 std::vector< te::rst::BandProperty* > bandsProperties;
279 for( std::vector< unsigned int >::size_type bandIdx = 0 ; bandIdx <
287 mosaicBandsRangeMin[ bandIdx ],
288 mosaicBandsRangeMax[ bandIdx ]);
293 mosaicURY ), mosaicSRID );
304 "Output raster creation error" );
309 std::unique_ptr< te::mem::CachedRaster > outputCachedRasterInstancePtr;
316 outputRasterPtr = outputCachedRasterInstancePtr.get();
322 const unsigned int nBands =
static_cast<unsigned int>(outputRasterPtr->
getNumberOfBands());
325 unsigned int col = 0;
326 unsigned int row = 0;
327 unsigned int bandIdx = 0;
329 for( bandIdx = 0 ; bandIdx < nBands ; ++bandIdx )
333 for( row = 0 ; row < nRows ; ++row )
335 for( col = 0 ; col <
nCols ; ++
col )
345 progressPtr->pulse();
346 if( ! progressPtr->isActive() )
return false;
352 std::vector< double > mosaicTargetMeans( outputRasterPtr->
getNumberOfBands(), 0 );
353 std::vector< double > mosaicTargetVariances( outputRasterPtr->
getNumberOfBands(), 0 );
362 double inXStartGeo = 0;
363 double inYStartGeo = 0;
366 double outFirstRowDouble = 0;
367 double outFirstColDouble = 0;
369 outFirstColDouble, outFirstRowDouble );
371 const double outRowsBoundDouble = outFirstRowDouble +
373 const double outColsBoundDouble = outFirstColDouble +
376 const unsigned int outFirstRow =
static_cast<unsigned int>(std::max( 0u,
377 te::common::Round< double, unsigned int >( outFirstRowDouble ) ) );
378 const unsigned int outFirstCol =
static_cast<unsigned int>(std::max( 0u,
379 te::common::Round< double, unsigned int >( outFirstColDouble ) ) );
381 const unsigned int outRowsBound =
static_cast<unsigned int>(std::min(
382 te::common::Round< double, unsigned int >( outRowsBoundDouble ),
384 const unsigned int outColsBound =
static_cast<unsigned int>(std::min(
385 te::common::Round< double, unsigned int >( outColsBoundDouble ),
389 unsigned int outCol = 0;
390 unsigned int outRow = 0;
393 double bandNoDataValue = -1.0 * DBL_MAX;
394 std::complex< double > pixelCValue = 0;
397 unsigned int inputBandIdx = 0;
399 for(
unsigned int inputRastersBandsIdx = 0 ; inputRastersBandsIdx <
400 nBands ; ++inputRastersBandsIdx )
403 inputRastersBandsIdx ] ;
408 (*outputRasterPtr->
getBand( inputRastersBandsIdx ));
409 unsigned long int validPixelsNumber = 0;
413 for( outRow = outFirstRow ; outRow < outRowsBound ; ++outRow )
415 inRow = (
static_cast<double>(outRow)) - outFirstRowDouble;
417 for( outCol = outFirstCol ; outCol < outColsBound ; ++outCol )
419 inCol = (
static_cast<double>(outCol)) - outFirstColDouble;
421 interpInstance.
getValue( inCol, inRow, pixelCValue, inputBandIdx );
423 if( pixelCValue.real() != bandNoDataValue )
425 outBand.
setValue( outCol, outRow, pixelCValue );
426 mean += pixelCValue.real();
436 mean /= (
static_cast<double>(validPixelsNumber) );
437 mosaicTargetMeans[ inputRastersBandsIdx ] = mean;
439 double& variance = mosaicTargetVariances[ inputRastersBandsIdx ];
444 for( outRow = outFirstRow ; outRow < outRowsBound ; ++outRow )
446 for( outCol = outFirstCol ; outCol < outColsBound ; ++outCol )
448 outBand.
getValue( outCol, outRow, pixelValue );
452 variance += ( ( pixelValue - mean ) * ( pixelValue -
453 mean ) ) / ( static_cast<double>(validPixelsNumber) );
463 "Rasters bounding boxes number mismatch" );
467 progressPtr->pulse();
468 if( ! progressPtr->isActive() )
return false;
473 std::unique_ptr< te::gm::MultiPolygon > mosaicBBoxesUnionPtr(
475 outputRasterPtr->
getSRID(), nullptr ) );
476 mosaicBBoxesUnionPtr->add( static_cast<te::gm::Polygon*>(rastersBBoxes[ 0 ].
clone()) );
487 std::vector< unsigned int > outputRasterBands;
488 std::vector< double > dummyRasterOffsets;
489 std::vector< double > dummyRasterScales;
490 for(
unsigned int outputRasterBandsIdx = 0 ; outputRasterBandsIdx <
493 outputRasterBands.push_back( outputRasterBandsIdx );
494 dummyRasterOffsets.push_back( 0.0 );
495 dummyRasterScales.push_back( 1.0 );
506 "Invalid boxes SRID" );
515 std::unique_ptr< te::rst::Raster > reprojectedInputRasterPtr;
517 if( outputRasterPtr->
getSRID() != inputRasterPtr->getSRID() )
519 std::map< std::string, std::string > rInfo;
520 rInfo[
"RTYPE" ] =
"EXPANSIBLE";
522 reprojectedInputRasterPtr.reset( inputRasterPtr->transform(
523 outputRasterPtr->
getSRID(), rInfo,
525 inputRasterPtr = reprojectedInputRasterPtr.get();
534 std::unique_ptr< te::mem::CachedRaster > cachedInputRasterPtr;
539 ( reprojectedInputRasterPtr.get() == nullptr )
544 *inputRasterPtr, 20, 0 ) );
545 inputRasterPtr = cachedInputRasterPtr.get();
553 std::unique_ptr< te::gm::GeometricTransformation > transPtr;
563 outputRasterPtr->
getGrid()->
gridToGeo( auxTP.first.x, auxTP.first.y, auxX, auxY );
564 inputRasterPtr->getGrid()->geoToGrid( auxX, auxY, auxTP.second.x, auxTP.second.y );
568 auxTP.first.y =
static_cast<double>( outputRasterPtr->
getNumberOfRows() - 1 );
569 outputRasterPtr->
getGrid()->
gridToGeo( auxTP.first.x, auxTP.first.y, auxX, auxY );
570 inputRasterPtr->getGrid()->geoToGrid( auxX, auxY, auxTP.second.x, auxTP.second.y );
575 outputRasterPtr->
getGrid()->
gridToGeo( auxTP.first.x, auxTP.first.y, auxX, auxY );
576 inputRasterPtr->getGrid()->geoToGrid( auxX, auxY, auxTP.second.x, auxTP.second.y );
580 auxTP.first.y =
static_cast<double>( outputRasterPtr->
getNumberOfRows() - 1 );
581 outputRasterPtr->
getGrid()->
gridToGeo( auxTP.first.x, auxTP.first.y, auxX, auxY );
582 inputRasterPtr->getGrid()->geoToGrid( auxX, auxY, auxTP.second.x, auxTP.second.y );
589 "Could not initialize a geometric transformation" );
594 std::vector< double > currentRasterBandsOffsets = dummyRasterOffsets;
595 std::vector< double > currentRasterBandsScales = dummyRasterScales;
599 double currentRasterVariance = 0;
600 double currentRasterMean = 0;
602 for(
unsigned int inputRastersBandsIdx = 0 ; inputRastersBandsIdx <
604 ++inputRastersBandsIdx )
607 inputRastersBandsIdx ];
610 ( mosaicTargetMeans[ inputRastersBandsIdx ] != 0.0 )
612 ( mosaicTargetVariances[ inputRastersBandsIdx ] != 0.0 )
619 currentRasterVariance );
621 currentRasterBandsScales[ inputRastersBandsIdx ] =
623 std::sqrt( mosaicTargetVariances[ inputRastersBandsIdx ] )
625 std::sqrt( currentRasterVariance )
627 currentRasterBandsOffsets[ inputRastersBandsIdx ] =
629 mosaicTargetMeans[ inputRastersBandsIdx ]
632 currentRasterBandsScales[ inputRastersBandsIdx ]
658 currentRasterBandsOffsets,
659 currentRasterBandsScales,
660 mosaicBBoxesUnionPtr.get(),
664 false ),
"Blender initiazing error" );
667 "Error blending images" );
675 std::unique_ptr< te::gm::Geometry > boxesUnionResultPtr;
677 "Invalid mosaic bounding boxes union geometry" );
679 "Invalid raster bounding boxes union geometry (raster index:" 680 + boost::lexical_cast< std::string >( inputRasterIdx ) +
")" );
684 boxesUnionResultPtr.reset( mosaicBBoxesUnionPtr->Union(
685 &( rastersBBoxes[ inputRasterIdx ] ) ) );
687 catch(
const std::exception&)
694 boxesUnionResultPtr->setSRID( outputRasterPtr->
getSRID() );
706 mosaicBBoxesUnionPtr.reset( static_cast<te::gm::MultiPolygon*>(boxesUnionResultPtr.release()) );
721 auxMultiPol->
add( boxesUnionResultPtr.release() );
723 mosaicBBoxesUnionPtr.reset( auxMultiPol );
732 cachedInputRasterPtr.reset();
740 progressPtr->pulse();
741 if( ! progressPtr->isActive() )
return false;
747 if( outputCachedRasterInstancePtr.get() ) outputCachedRasterInstancePtr.reset();
774 "Invalid m_feederRasterPtr" )
778 "Invalid number of rasters" )
787 for( std::vector< std::vector< unsigned int > >::size_type
788 inputRastersBandsIdx = 0 ; inputRastersBandsIdx <
792 inputRastersBandsIdx ].size() > 0,
"Invalid bands number" );
796 0 ].size(),
"Bands number mismatch" );
810 const bool& forceNoDataValue,
811 const double& noDataValue,
812 double& mean,
double& variance )
817 double internalNoDataValue = 0;
818 if( forceNoDataValue )
819 internalNoDataValue = noDataValue;
826 double pixelsNumber = 0;
828 unsigned int col = 0;
829 unsigned int line = 0;
832 for( col = 0 ; col <
nCols ; ++
col )
836 if( value != internalNoDataValue )
843 if( pixelsNumber != 0.0 )
845 mean /= pixelsNumber;
848 for( col = 0 ; col <
nCols ; ++
col )
852 if( value != internalNoDataValue )
854 variance += ( ( value - mean ) * ( value - mean ) ) / pixelsNumber;
MultiPolygon is a MultiSurface whose elements are Polygons.
Near neighborhood interpolation method.
std::vector< TiePoint > m_tiePoints
Tie points.
const OutputParameters & operator=(const OutputParameters ¶ms)
TERASTEREXPORT void GetDataTypeRanges(const int &dataType, double &min, double &max)
Return the values range of a given data type.
Index into a lookup table.
AbstractParameters * clone() const
Create a clone copy of this instance.
bool isInitialized() const
Returns true if the algorithm instance is initialized and 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.
It interpolates one pixel based on a selected algorithm. Methods currently available are Nearest Neig...
int m_nblocksy
The number of blocks in y.
#define TERP_LOGWARN(message)
Logs a warning message.
This class can be used to inform the progress of a task.
Raster Processing algorithm output parameters base interface.
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<double>::max().
virtual te::rst::Raster const * getCurrentObj() const =0
Return the current sequence object.
std::pair< Coord2D, Coord2D > TiePoint
Tie point type definition.
std::string m_rType
Output raster data source type (as described in te::raster::RasterFactory ).
void geoToGrid(const double &x, const double &y, double &col, double &row) const
Get the grid point associated to a spatial location.
bool execute(AlgorithmOutputParameters &outputParams) _NOEXCEPT_OP(false)
Executes the algorithm using the supplied parameters.
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.
te::common::AccessPolicy getAccessPolicy() const
Returns the raster access policy.
unsigned int unsigned int nCols
std::unique_ptr< te::rst::Raster > m_outputRasterPtr
The generated output mosaic raster.
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.
virtual std::size_t getNumberOfBands() const =0
Returns the number of bands (dimension of cells attribute values) in the raster.
BandProperty * getProperty()
Returns the band property.
int m_blkw
Block width (pixels).
Create a mosaic from a set of geo-referenced rasters.
double getResolutionX() const
Returns the grid horizontal (x-axis) resolution.
A RAM cache adaptor to an external existent raster that must always be avaliable. ...
A raster band description.
virtual const Band * getBand(std::size_t i) const =0
Returns the raster i-th band.
Grid * getGrid()
It returns the raster grid.
static GeometricTransformation * make(const std::string &factoryKey)
It creates an object with the appropriated factory.
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 forceRaster1NoDataValue, const bool forceRaster2NoDataValue, 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.
virtual void setValue(unsigned int c, unsigned int r, const double value)=0
Sets the cell attribute value.
Abstract parameters base interface.
bool m_isInitialized
Tells if this instance is initialized.
int getSRID() const
Returns the raster spatial reference system identifier.
bool initialize(const AlgorithmInputParameters &inputParams) _NOEXCEPT_OP(false)
Initialize the algorithm instance making it ready for execution.
static void calcBandStatistics(const te::rst::Band &band, const bool &forceNoDataValue, const double &noDataValue, double &mean, double &variance)
Raster band statistics calcule.
GeoMosaic output parameters.
void add(Geometry *g)
It adds the geometry into the collection.
GeoMosaic::InputParameters m_inputParameters
Input execution parameters.
static Raster * make()
It creates and returns an empty raster with default raster driver.
#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 gridToGeo(const double &col, const double &row, double &x, double &y) const
Get the spatial location of a grid point.
2D Geometric transformation parameters.
void reset() _NOEXCEPT_OP(false)
Clear all internal allocated objects and reset the algorithm to its initial state.
Raster Processing functions.
virtual bool moveNext()=0
Advances to the next sequence obeject.
int m_blkh
Block height (pixels).
#define TERP_DEBUG_TRUE_OR_THROW(value, message)
Checks if value is true and throws an exception if not.
virtual void reset() _NOEXCEPT_OP(false)
Clear all internal allocated objects and reset the algorithm to its initial state.
A rectified grid is the spatial support for raster data.
#define TERP_INSTANCE_TRUE_OR_RETURN_FALSE(value, message)
Checks if value is true. For false values a warning message will be logged, the current instance erro...
#define TERP_TRUE_OR_THROW(value, message)
Checks if value is true and throws an exception if not.
TEGEOMEXPORT Geometry * GetGeomFromEnvelope(const Envelope *const e, int srid)
It creates a Geometry (a polygon) from the given envelope.
void reset() _NOEXCEPT_OP(false)
Clear all internal allocated resources and reset the parameters instance to its initial state...
virtual void getValue(unsigned int c, unsigned int r, double &value) const =0
Returns the cell attribute value.
virtual unsigned int getCurrentOffset() const =0
Return the index of the current object.
virtual unsigned int getObjsCount() const =0
Return the total number of feeder objects.
std::map< std::string, std::string > m_rInfo
The necessary information to create the output rasters (as described in te::raster::RasterFactory).
#define TERP_INSTANCE_LOG_AND_RETURN_FALSE(message)
Logs a warning message, update the current instance error messsage and return false.