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" 46 #include <boost/shared_ptr.hpp> 47 #include <boost/scoped_array.hpp> 137 m_outputRasterPtr.reset();
164 throw( te::rp::Exception )
174 std::unique_ptr< te::common::TaskProgress > progressPtr;
181 progressPtr->setMessage(
"Mosaic" );
186 double mosaicXResolution = 0.0;
187 double mosaicYResolution = 0.0;
188 double mosaicLLX = DBL_MAX;
189 double mosaicLLY = DBL_MAX;
190 double mosaicURX = -1.0 * DBL_MAX;
191 double mosaicURY = -1.0 * DBL_MAX;
193 std::vector< double > mosaicBandsRangeMin;
194 std::vector< double > mosaicBandsRangeMax;
196 std::vector< te::gm::Polygon > rastersBBoxes;
199 std::vector< boost::shared_ptr< te::gm::GeometricTransformation > >
200 eachRasterPixelToFirstRasterPixelGeomTransfms;
205 std::vector< te::rst::Grid > rastersGrids;
208 unsigned int inputRasterIdx = 0;
219 rastersGrids.push_back( (*inputRasterPtr->
getGrid()) );
223 if( inputRasterIdx == 0 )
240 auxLinearRingPtr->
setPoint( 0, mosaicLLX, mosaicURY );
241 auxLinearRingPtr->
setPoint( 1, mosaicURX, mosaicURY );
242 auxLinearRingPtr->
setPoint( 2, mosaicURX, mosaicLLY );
243 auxLinearRingPtr->
setPoint( 3, mosaicLLX, mosaicLLY );
244 auxLinearRingPtr->
setPoint( 4, mosaicLLX, mosaicURY );
245 auxLinearRingPtr->
setSRID( mosaicSRID );
248 auxPolygon.
push_back( auxLinearRingPtr );
249 auxPolygon.
setSRID( mosaicSRID );
250 rastersBBoxes.push_back( auxPolygon );
256 if( ( inputRasterIdx == 1 ) ||
267 const std::vector< te::gm::GTParameters::TiePoint >& inputTPs =
269 const unsigned int inputTPsSize =
static_cast<unsigned int>(inputTPs.size());
271 (*eachRasterPixelToFirstRasterPixelGeomTransfms[ inputRasterIdx - 2 ].get());
273 for(
unsigned int inputTPsIdx = 0 ; inputTPsIdx < inputTPsSize ;
276 auxTP.second = inputTPs[ inputTPsIdx ].second;
277 lastTransf.
inverseMap( inputTPs[ inputTPsIdx ].first, auxTP.first );
285 boost::shared_ptr< te::gm::GeometricTransformation > auxTransPtr(
288 "Geometric transformation instatiation error" );
290 "Geometric transformation parameters calcule error" );
291 eachRasterPixelToFirstRasterPixelGeomTransfms.push_back( auxTransPtr );
297 *inputRasterPtr->
getGrid(), inRasterIndexedDetailedExtent ),
298 "Error creating the raster detailed extent" );
312 for(
unsigned int inRasterDetExtentIdx = 0 ; inRasterDetExtentIdx <
313 inRasterIndexedDetailedExtent.
size() ; ++inRasterDetExtentIdx )
315 auxTransPtr->inverseMap(
316 inRasterIndexedDetailedExtent.
getX( inRasterDetExtentIdx ),
317 inRasterIndexedDetailedExtent.
getY( inRasterDetExtentIdx ),
321 rastersGrids[ 0 ].gridToGeo( mappedX, mappedY, geoX,
324 inRasterDetailedExtent.
setPoint( inRasterDetExtentIdx, geoX, geoY );
340 auxPolygon.setSRID( mosaicSRID );
341 rastersBBoxes.push_back( auxPolygon );
346 for( std::vector< unsigned int >::size_type inputRastersBandsIdx = 0 ;
347 inputRastersBandsIdx <
349 ++inputRastersBandsIdx )
351 const unsigned int& currBand =
367 progressPtr->pulse();
368 if( ! progressPtr->isActive() )
return false;
376 mosaicBandsRangeMin.resize(
378 mosaicBandsRangeMax.resize(
381 std::vector< te::rst::BandProperty* > bandsProperties;
382 for( std::vector< unsigned int >::size_type bandIdx = 0 ; bandIdx <
390 mosaicBandsRangeMin[ bandIdx ],
391 mosaicBandsRangeMax[ bandIdx ]);
396 mosaicURY ), mosaicSRID );
407 "Output raster creation error" );
412 std::unique_ptr< te::mem::CachedRaster > cachedOutputRasterInstancePtr;
419 outputRasterPtr = cachedOutputRasterInstancePtr.get();
424 progressPtr->pulse();
425 if( ! progressPtr->isActive() )
return false;
432 std::vector< boost::shared_ptr< te::gm::GeometricTransformation > >
433 eachRasterPixelToMosaicRasterPixelGeomTransfms;
436 const double firstRasterColOffset = std::abs( rastersBBoxes[ 0 ].getMBR()->m_llx -
439 const double firstRasterLinOffset = std::abs( rastersBBoxes[ 0 ].getMBR()->m_ury -
449 const double prevRasterColOffset = std::abs( rastersBBoxes[ tiePointsIdx ].getMBR()->m_llx -
452 const double prevRasterLinOffset = std::abs( rastersBBoxes[ tiePointsIdx ].getMBR()->m_ury -
456 for(
unsigned int tpIdx = 0 ; tpIdx < transfParams.
m_tiePoints.size() ;
461 transfParams.
m_tiePoints[ tpIdx ].first.x += firstRasterColOffset;
462 transfParams.
m_tiePoints[ tpIdx ].first.y += firstRasterLinOffset;
466 transfParams.
m_tiePoints[ tpIdx ].first.x += prevRasterColOffset;
467 transfParams.
m_tiePoints[ tpIdx ].first.y += prevRasterLinOffset;
471 boost::shared_ptr< te::gm::GeometricTransformation > auxTransPtr(
474 "Geometric transformation instatiation error" );
476 "Geometric transformation parameters calcule error" );
477 eachRasterPixelToMosaicRasterPixelGeomTransfms.push_back( auxTransPtr );
483 progressPtr->pulse();
484 if( ! progressPtr->isActive() )
return false;
490 const unsigned int nBands =
static_cast<unsigned int>(outputRasterPtr->
getNumberOfBands());
493 unsigned int col = 0;
494 unsigned int row = 0;
495 unsigned int bandIdx = 0;
497 for( bandIdx = 0 ; bandIdx < nBands ; ++bandIdx )
501 for( row = 0 ; row < nRows ; ++row )
503 for( col = 0 ; col <
nCols ; ++
col )
513 progressPtr->pulse();
514 if( ! progressPtr->isActive() )
return false;
520 std::vector< double > mosaicTargetMeans( outputRasterPtr->
getNumberOfBands(), 0 );
521 std::vector< double > mosaicTargetVariances( outputRasterPtr->
getNumberOfBands(), 0 );
530 double inXStartGeo = 0;
531 double inYStartGeo = 0;
534 double outFirstRowDouble = 0;
535 double outFirstColDouble = 0;
537 outFirstColDouble, outFirstRowDouble );
539 const double outRowsBoundDouble = outFirstRowDouble +
541 const double outColsBoundDouble = outFirstColDouble +
544 const unsigned int outFirstRow = (
unsigned int)std::max( 0u,
545 te::common::Round< double, unsigned int >( outFirstRowDouble ) );
546 const unsigned int outFirstCol = (
unsigned int)std::max( 0u,
547 te::common::Round< double, unsigned int >( outFirstColDouble ) );
549 const unsigned int outRowsBound = (
unsigned int)std::min(
550 te::common::Round< double, unsigned int >( outRowsBoundDouble ),
552 const unsigned int outColsBound = (
unsigned int)std::min(
553 te::common::Round< double, unsigned int >( outColsBoundDouble ),
556 const unsigned int nBands = (
unsigned int)
558 unsigned int outCol = 0;
559 unsigned int outRow = 0;
562 double bandNoDataValue = -1.0 * DBL_MAX;
563 std::complex< double > pixelCValue = 0;
566 unsigned int inputBandIdx = 0;
568 for(
unsigned int inputRastersBandsIdx = 0 ; inputRastersBandsIdx <
569 nBands ; ++inputRastersBandsIdx )
572 inputRastersBandsIdx ] ;
577 (*outputRasterPtr->
getBand( inputRastersBandsIdx ));
578 unsigned long int validPixelsNumber = 0;
582 for( outRow = outFirstRow ; outRow < outRowsBound ; ++outRow )
584 inRow = ((double)outRow) - outFirstRowDouble;
586 for( outCol = outFirstCol ; outCol < outColsBound ; ++outCol )
588 inCol = ((double)outCol) - outFirstColDouble;
590 interpInstance.
getValue( inCol, inRow, pixelCValue, inputBandIdx );
592 if( pixelCValue.real() != bandNoDataValue )
594 outBand.
setValue( outCol, outRow, pixelCValue );
595 mean += pixelCValue.real();
605 mean /= ( (double)validPixelsNumber );
606 mosaicTargetMeans[ inputRastersBandsIdx ] = mean;
608 double& variance = mosaicTargetVariances[ inputRastersBandsIdx ];
613 for( outRow = outFirstRow ; outRow < outRowsBound ; ++outRow )
615 for( outCol = outFirstCol ; outCol < outColsBound ; ++outCol )
617 outBand.
getValue( outCol, outRow, pixelValue );
621 variance += ( ( pixelValue - mean ) * ( pixelValue -
622 mean ) ) / ( (
double)validPixelsNumber );
632 progressPtr->pulse();
633 if( ! progressPtr->isActive() )
return false;
638 std::unique_ptr< te::gm::MultiPolygon > mosaicBBoxesUnionPtr(
640 rastersBBoxes[ 0 ].getSRID(),
nullptr ) );
646 std::vector< unsigned int > outputRasterBands;
647 std::vector< double > dummyRasterOffsets;
648 std::vector< double > dummyRasterScales;
649 for(
unsigned int outputRasterBandsIdx = 0 ; outputRasterBandsIdx <
652 outputRasterBands.push_back( outputRasterBandsIdx );
653 dummyRasterOffsets.push_back( 0.0 );
654 dummyRasterScales.push_back( 1.0 );
670 std::unique_ptr< te::mem::CachedRaster > cachedInputRasterPtr;
674 *nonCachedInputRasterPtr, 25, 0 ) );
675 inputRasterPtr = cachedInputRasterPtr.get();
680 std::vector< double > currentRasterBandsOffsets = dummyRasterOffsets;
681 std::vector< double > currentRasterBandsScales = dummyRasterScales;
685 double currentRasterVariance = 0;
686 double currentRasterMean = 0;
688 for(
unsigned int inputRastersBandsIdx = 0 ; inputRastersBandsIdx <
690 ++inputRastersBandsIdx )
693 inputRastersBandsIdx ];
696 ( mosaicTargetMeans[ inputRastersBandsIdx ] != 0.0 )
698 ( mosaicTargetVariances[ inputRastersBandsIdx ] != 0.0 )
705 currentRasterVariance );
707 currentRasterBandsScales[ inputRastersBandsIdx ] =
709 std::sqrt( mosaicTargetVariances[ inputRastersBandsIdx ] )
711 std::sqrt( currentRasterVariance )
713 currentRasterBandsOffsets[ inputRastersBandsIdx ] =
715 mosaicTargetMeans[ inputRastersBandsIdx ]
718 currentRasterBandsScales[ inputRastersBandsIdx ]
744 currentRasterBandsOffsets,
745 currentRasterBandsScales,
746 mosaicBBoxesUnionPtr.get(),
748 *( eachRasterPixelToMosaicRasterPixelGeomTransfms[ inputRasterIdx - 1 ] ),
751 "Blender initiazing error" );
754 "Error blending images" );
762 std::unique_ptr< te::gm::Geometry > unionMultiPolPtr;
763 unionMultiPolPtr.reset( mosaicBBoxesUnionPtr->Union(
764 &( rastersBBoxes[ inputRasterIdx ] ) ) );
766 unionMultiPolPtr->setSRID( mosaicBBoxesUnionPtr->getSRID() );
791 unionMultiPolPtr->getSRID(), nullptr );
793 mosaicBBoxesUnionPtr.reset( mPolPtr );
803 cachedInputRasterPtr.reset();
809 progressPtr->pulse();
810 if( ! progressPtr->isActive() )
return false;
826 throw( te::rp::Exception )
839 "Invalid m_feederRasterPtr" )
843 "Invalid number of rasters" )
852 for( std::vector< std::vector< unsigned int > >::size_type
853 inputRastersBandsIdx = 0 ; inputRastersBandsIdx <
857 inputRastersBandsIdx ].size() > 0,
"Invalid bands number" );
861 0 ].size(),
"Bands number mismatch" );
882 const bool& forceNoDataValue,
883 const double& noDataValue,
884 double& mean,
double& variance )
889 double internalNoDataValue = 0;
890 if( forceNoDataValue )
891 internalNoDataValue = noDataValue;
900 double pixelsNumber = 0;
902 unsigned int col = 0;
903 unsigned int line = 0;
906 for( col = 0 ; col <
nCols ; ++
col )
910 if( value != internalNoDataValue )
917 if( pixelsNumber != 0.0 )
919 mean /= pixelsNumber;
922 for( col = 0 ; col <
nCols ; ++
col )
926 if( value != internalNoDataValue )
928 variance += ( ( value - mean ) * ( value - mean ) ) / pixelsNumber;
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).
Near neighborhood interpolation method.
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.
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...
std::unique_ptr< te::rst::Raster > m_outputRasterPtr
The generated output mosaic raster.
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.
Base exception class for plugin module.
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.
const double & getUpperRightX() const
It returns a constant refernce to the x coordinate of the upper right corner.
#define TERP_LOGWARN(message)
Logs a warning message.
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.
const double & getLowerLeftY() const
It returns a constant refernce to the y coordinate of the lower left corner.
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().
const double & getUpperRightY() const
It returns a constant refernce to the x coordinate of the upper right corner.
virtual te::rst::Raster const * getCurrentObj() const =0
Return the current sequence object.
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.
const double & getY(std::size_t i) const
It returns the n-th y coordinate value.
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
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.
const Envelope * getMBR() const _NOEXCEPT_OP(true)
It returns the minimum bounding rectangle for the geometry in an internal representation.
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.
virtual std::size_t getNumberOfBands() const =0
Returns the number of bands (dimension of cells attribute values) in the raster.
const double & getX(std::size_t i) const
It returns the n-th x coordinate value.
BandProperty * getProperty()
Returns the band property.
int m_blkw
Block width (pixels).
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.
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.
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.
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.
const double & getLowerLeftX() const
It returns a constant reference to the x coordinate of the lower left corner.
Mosaic output parameters.
bool GetIndexedDetailedExtent(const te::rst::Grid &grid, te::gm::LinearRing &indexedDetailedExtent)
Create a indexed (lines,columns) datailed extent from the given grid.
#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.
void setSRID(int srid)
It sets the Spatial Reference System ID of the linestring.
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.
Raster Processing functions.
virtual bool moveNext()=0
Advances to the next sequence obeject.
int m_blkh
Block height (pixels).
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.
std::size_t size() const
It returns the number of points (vertexes) in the geometry.
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.