27 #include "../common/Translator.h"
28 #include "../common/PlatformUtils.h"
29 #include "../geometry/Envelope.h"
30 #include "../raster/Utils.h"
31 #include "../raster/BlockUtils.h"
36 #ifndef TLINTERNAL_EXPANSIBLERASTER_MAXDISKFILESSIZE
37 #define TLINTERNAL_EXPANSIBLERASTER_MAXDISKFILESSIZE 2147483648ul
50 : te::rst::
Raster( grid, p )
56 const std::vector<te::rst::BandProperty*> bandsProperties )
61 unsigned int maxBlockSizeBytes = 0;
62 std::vector< unsigned int> numbersOfBlocksX;
63 std::vector< unsigned int> numbersOfBlocksY;
64 std::vector< unsigned int> blocksSizesBytes;
67 for(
unsigned int bandIdx = 0 ; bandIdx < bandsProperties.size() ; ++bandIdx )
70 bandsProperties[ bandIdx ]->m_nblocksx = bandsProperties[ 0 ]->m_nblocksx;
71 bandsProperties[ bandIdx ]->m_nblocksy = bandsProperties[ 0 ]->m_nblocksy;
72 bandsProperties[ bandIdx ]->m_blkw = bandsProperties[ 0 ]->m_blkw;
73 bandsProperties[ bandIdx ]->m_blkh = bandsProperties[ 0 ]->m_blkh;
74 bandsProperties[ bandIdx ]->m_nblocksx = (int)std::ceil(
76 ((
double)bandsProperties[ bandIdx ]->m_blkw) );
77 bandsProperties[ bandIdx ]->m_nblocksy = (int)std::ceil(
79 ((
double)bandsProperties[ bandIdx ]->m_blkh) );
81 const unsigned int blockSizeBytes = (
unsigned int)(
82 bandsProperties[ bandIdx ]->m_blkw *
83 bandsProperties[ bandIdx ]->m_blkh *
86 if( maxBlockSizeBytes < blockSizeBytes )
87 maxBlockSizeBytes = blockSizeBytes;
89 numbersOfBlocksX.push_back( (
unsigned int)bandsProperties[ bandIdx ]->m_nblocksx );
90 numbersOfBlocksY.push_back( (
unsigned int)bandsProperties[ bandIdx ]->m_nblocksy );
91 blocksSizesBytes.push_back( blockSizeBytes );
98 const double freeVMem = ( ((double)maxMemPercentUsed) / 100.0 ) *
99 std::min( totalPhysMem, ( totalVMem - usedVMem ) );
100 const unsigned int maxNumberOfBlocks = (
unsigned int)
101 std::max( 1.0, std::ceil( freeVMem / ((
double)maxBlockSizeBytes) ) );
104 numbersOfBlocksY, blocksSizesBytes,
106 throw Exception(
TE_TR(
"Cannot initialize the blocks menager") );
108 for(
unsigned int bandsIdx = 0 ; bandsIdx < bandsProperties.size() ;
112 *(bandsProperties[ bandsIdx ]) , bandsIdx ) );
113 delete ( bandsProperties[ bandsIdx ] );
120 const std::vector<te::rst::BandProperty*> bandsProperties,
121 const unsigned int maxNumberOfRAMBlocks )
126 std::vector< unsigned int> numbersOfBlocksX;
127 std::vector< unsigned int> numbersOfBlocksY;
128 std::vector< unsigned int> blocksSizesBytes;
131 for(
unsigned int bandIdx = 0 ; bandIdx < bandsProperties.size() ; ++bandIdx )
134 bandsProperties[ bandIdx ]->m_nblocksx = bandsProperties[ 0 ]->m_nblocksx;
135 bandsProperties[ bandIdx ]->m_nblocksy = bandsProperties[ 0 ]->m_nblocksy;
136 bandsProperties[ bandIdx ]->m_blkw = bandsProperties[ 0 ]->m_blkw;
137 bandsProperties[ bandIdx ]->m_blkh = bandsProperties[ 0 ]->m_blkh;
138 bandsProperties[ bandIdx ]->m_nblocksx = (int)std::ceil(
140 ((
double)bandsProperties[ bandIdx ]->m_blkw) );
141 bandsProperties[ bandIdx ]->m_nblocksy = (int)std::ceil(
143 ((
double)bandsProperties[ bandIdx ]->m_blkh) );
145 const unsigned int blockSizeBytes = (
unsigned int)(
146 bandsProperties[ bandIdx ]->m_blkw *
147 bandsProperties[ bandIdx ]->m_blkh *
150 numbersOfBlocksX.push_back( (
unsigned int)bandsProperties[ bandIdx ]->m_nblocksx );
151 numbersOfBlocksY.push_back( (
unsigned int)bandsProperties[ bandIdx ]->m_nblocksy );
152 blocksSizesBytes.push_back( blockSizeBytes );
157 numbersOfBlocksY, blocksSizesBytes,
159 throw Exception(
TE_TR(
"Cannot initialize the blocks menager") );
161 for(
unsigned int bandsIdx = 0 ; bandsIdx < bandsProperties.size() ;
165 *(bandsProperties[ bandsIdx ]) , bandsIdx ) );
166 delete ( bandsProperties[ bandsIdx ] );
184 return std::map<std::string, std::string>();
189 std::vector<te::rst::BandProperty*> bandsProperties;
190 for(
unsigned int bandsIdx = 0 ; bandsIdx < m_bands.size() ; ++bandsIdx )
193 *(m_bands[ bandsIdx ]->getProperty() ) ) );
197 m_blocksManager.getMaxNumberOfRAMBlocks() );
202 if( m_bands.empty() )
return false;
206 const unsigned int blockExpansionSize = (
unsigned int)std::ceil(
207 ((
double)number) / ((
double)m_bands[ 0 ]->getProperty()->m_blkh) );
209 std::vector< ExpansibleBandBlocksManager::BlockIndex3D > addedBlocksCoords;
211 for(
unsigned int bandsIdx = 0 ; bandsIdx < m_bands.size() ; ++bandsIdx )
213 std::vector< ExpansibleBandBlocksManager::BlockIndex3D > bandAddedBlocksCoords;
214 if( ! m_blocksManager.addTopBlocks( blockExpansionSize, bandsIdx,
215 bandAddedBlocksCoords ) )
218 addedBlocksCoords.insert( addedBlocksCoords.end(), bandAddedBlocksCoords.begin(),
219 bandAddedBlocksCoords.end() );
221 m_bands[ bandsIdx ]->getProperty()->m_nblocksy += blockExpansionSize;
224 dummyFillBlocks( addedBlocksCoords );
226 const unsigned int newLinesNumber =
227 ((
unsigned int)m_bands[ 0 ]->getProperty()->m_blkh) *
228 ((
unsigned int)m_bands[ 0 ]->getProperty()->m_nblocksy );
229 const unsigned int newColsNumber =
230 ((
unsigned int)m_bands[ 0 ]->getProperty()->m_blkw) *
231 ((
unsigned int)m_bands[ 0 ]->getProperty()->m_nblocksx );
233 const double addedLinesNumber = (double)( blockExpansionSize *
234 ((
unsigned int)m_bands[ 0 ]->getProperty()->m_blkh) );
237 m_grid->getExtent()->getLowerLeftX(),
238 m_grid->getExtent()->getUpperRightY() + ( addedLinesNumber *
239 m_grid->getResolutionY() ) );
242 newLinesNumber, m_grid->getResolutionX(), m_grid->getResolutionY(),
243 &newULC, m_grid->getSRID() ) );
253 if( m_bands.empty() )
return false;
257 const unsigned int currExtraLines =
258 ( (
unsigned int)( m_bands[ 0 ]->getProperty()->m_blkh *
259 m_bands[ 0 ]->getProperty()->m_nblocksy ) ) -
260 m_grid->getNumberOfRows();
263 m_grid->getExtent()->getLowerLeftX(),
264 m_grid->getExtent()->getUpperRightY() );
266 if( currExtraLines < number )
268 const unsigned int blockExpansionSize = (
unsigned int)std::ceil(
269 ((
double)number) / ((
double)m_bands[ 0 ]->getProperty()->m_blkh) );
271 std::vector< ExpansibleBandBlocksManager::BlockIndex3D > addedBlocksCoords;
273 for(
unsigned int bandsIdx = 0 ; bandsIdx < m_bands.size() ; ++bandsIdx )
275 std::vector< ExpansibleBandBlocksManager::BlockIndex3D > bandAddedBlocksCoords;
276 if( ! m_blocksManager.addBottomBlocks( blockExpansionSize, bandsIdx,
277 bandAddedBlocksCoords ) )
280 addedBlocksCoords.insert( addedBlocksCoords.end(), bandAddedBlocksCoords.begin(),
281 bandAddedBlocksCoords.end() );
283 m_bands[ bandsIdx ]->getProperty()->m_nblocksy += blockExpansionSize;
286 dummyFillBlocks( addedBlocksCoords );
288 const unsigned int newLinesNumber =
289 ((
unsigned int)m_bands[ 0 ]->getProperty()->m_blkh) *
290 ((
unsigned int)m_bands[ 0 ]->getProperty()->m_nblocksy );
291 const unsigned int newColsNumber =
292 ((
unsigned int)m_bands[ 0 ]->getProperty()->m_blkw) *
293 ((
unsigned int)m_bands[ 0 ]->getProperty()->m_nblocksx );
296 newLinesNumber, m_grid->getResolutionX(), m_grid->getResolutionY(),
297 &uLC, m_grid->getSRID() ) );
304 m_grid->getNumberOfColumns(),
305 m_grid->getNumberOfRows() + number,
306 m_grid->getResolutionX(), m_grid->getResolutionY(),
307 &uLC, m_grid->getSRID() ) );
318 if( m_bands.empty() )
return false;
322 const unsigned int oldColsNumber =
323 ((
unsigned int)m_bands[ 0 ]->getProperty()->m_blkw) *
324 ((
unsigned int)m_bands[ 0 ]->getProperty()->m_nblocksx );
326 const unsigned int blockExpansionSize = (
unsigned int)std::ceil(
327 ((
double)number) / ((
double)m_bands[ 0 ]->getProperty()->m_blkw) );
329 std::vector< ExpansibleBandBlocksManager::BlockIndex3D > addedBlocksCoords;
331 for(
unsigned int bandsIdx = 0 ; bandsIdx < m_bands.size() ; ++bandsIdx )
333 std::vector< ExpansibleBandBlocksManager::BlockIndex3D > bandAddedBlocksCoords;
334 if( ! m_blocksManager.addLeftBlocks( blockExpansionSize, bandsIdx,
335 bandAddedBlocksCoords ) )
338 addedBlocksCoords.insert( addedBlocksCoords.end(), bandAddedBlocksCoords.begin(),
339 bandAddedBlocksCoords.end() );
341 m_bands[ bandsIdx ]->getProperty()->m_nblocksx += blockExpansionSize;
344 dummyFillBlocks( addedBlocksCoords );
346 const unsigned int newLinesNumber =
347 ((
unsigned int)m_bands[ 0 ]->getProperty()->m_blkh) *
348 ((
unsigned int)m_bands[ 0 ]->getProperty()->m_nblocksy );
349 const unsigned int newColsNumber =
350 ((
unsigned int)m_bands[ 0 ]->getProperty()->m_blkw) *
351 ((
unsigned int)m_bands[ 0 ]->getProperty()->m_nblocksx );
353 const double addedColsNumber = (double)( newColsNumber - oldColsNumber );
356 m_grid->getExtent()->getLowerLeftX() - ( addedColsNumber *
357 m_grid->getResolutionX() ),
358 m_grid->getExtent()->getUpperRightY() );
361 newLinesNumber, m_grid->getResolutionX(), m_grid->getResolutionY(),
362 &newULC, m_grid->getSRID() ) );
372 if( m_bands.empty() )
return false;
376 const unsigned int currExtraCols =
377 ( (
unsigned int)( m_bands[ 0 ]->getProperty()->m_blkw *
378 m_bands[ 0 ]->getProperty()->m_nblocksx ) ) -
379 m_grid->getNumberOfColumns();
382 m_grid->getExtent()->getLowerLeftX(),
383 m_grid->getExtent()->getUpperRightY() );
385 if( currExtraCols < number )
387 const unsigned int blockExpansionSize = (
unsigned int)std::ceil(
388 ((
double)number) / ((
double)m_bands[ 0 ]->getProperty()->m_blkw) );
390 std::vector< ExpansibleBandBlocksManager::BlockIndex3D > addedBlocksCoords;
392 for(
unsigned int bandsIdx = 0 ; bandsIdx < m_bands.size() ; ++bandsIdx )
394 std::vector< ExpansibleBandBlocksManager::BlockIndex3D > bandAddedBlocksCoords;
395 if( ! m_blocksManager.addRightBlocks( blockExpansionSize, bandsIdx,
396 bandAddedBlocksCoords ) )
399 addedBlocksCoords.insert( addedBlocksCoords.end(), bandAddedBlocksCoords.begin(),
400 bandAddedBlocksCoords.end() );
402 m_bands[ bandsIdx ]->getProperty()->m_nblocksx += blockExpansionSize;
405 dummyFillBlocks( addedBlocksCoords );
407 const unsigned int newLinesNumber =
408 ((
unsigned int)m_bands[ 0 ]->getProperty()->m_blkh) *
409 ((
unsigned int)m_bands[ 0 ]->getProperty()->m_nblocksy );
410 const unsigned int newColsNumber =
411 ((
unsigned int)m_bands[ 0 ]->getProperty()->m_blkw) *
412 ((
unsigned int)m_bands[ 0 ]->getProperty()->m_nblocksx );
415 newLinesNumber, m_grid->getResolutionX(), m_grid->getResolutionY(),
416 &uLC, m_grid->getSRID() ) );
423 m_grid->getNumberOfColumns() + number,
424 m_grid->getNumberOfRows(),
425 m_grid->getResolutionX(), m_grid->getResolutionY(),
426 &uLC, m_grid->getSRID() ) );
437 if( m_bands.empty() )
return false;
441 std::vector< ExpansibleBandBlocksManager::BlockIndex3D > addedBlocksCoords;
443 if( ! m_blocksManager.addTopBands( number, addedBlocksCoords ) )
446 m_bands.insert( m_bands.begin(), number, 0 );
447 for(
unsigned int bIdx = 0 ; bIdx < m_bands.size() ; ++bIdx )
449 if( m_bands[ bIdx ] )
454 delete( oldBandPtr );
459 *( m_bands[ number ]->getProperty() ), bIdx );
463 dummyFillBlocks( addedBlocksCoords );
471 if( m_bands.empty() )
return false;
475 std::vector< ExpansibleBandBlocksManager::BlockIndex3D > addedBlocksCoords;
477 if( ! m_blocksManager.addBottomBands( number, addedBlocksCoords ) )
480 unsigned int lastBandIdx = (
unsigned int)m_bands.size() - 1;
481 m_bands.insert( m_bands.end(), number, 0 );
482 for(
unsigned int bIdx = lastBandIdx + 1 ; bIdx < m_bands.size() ; ++bIdx )
485 *( m_bands[ lastBandIdx ]->getProperty() ), bIdx );
488 dummyFillBlocks( addedBlocksCoords );
496 if( m_bands.size() > 0 )
498 for(
unsigned int bandsIdx = 0 ; bandsIdx < m_bands.size() ; ++bandsIdx )
499 delete (m_bands[ bandsIdx ]);
503 m_blocksManager.free();
508 for(
unsigned int bandsIdx = 0 ; bandsIdx < m_bands.size() ;
524 unsigned int blockXIdx = 0;
525 unsigned int blockYIdx = 0;
529 for( blockXIdx = 0 ; blockXIdx < nBlocksX ; ++blockXIdx )
531 for( blockYIdx = 0 ; blockYIdx < nBlocksY ; ++blockYIdx )
533 blockPtr = m_blocksManager.getBlockPointer( bandsIdx, blockXIdx,
537 for( eIdx = 0 ; eIdx < elementsNumber ; ++eIdx )
538 sb( eIdx, blockPtr, noDataValuePtr );
545 const std::vector< ExpansibleBandBlocksManager::BlockIndex3D >& blocksCoords )
547 for(
unsigned int coodsIdx = 0 ; coodsIdx < blocksCoords.size() ;
562 void* blockPtr = m_blocksManager.getBlockPointer( block3DIdx.
m_dim0Index,
565 const int elementsNumber = band.getProperty()->m_blkh *
566 band.getProperty()->m_blkw;
568 double* noDataValuePtr = &( band.getProperty()->m_noDataValue );
570 for(
int eIdx = 0 ; eIdx < elementsNumber ; ++eIdx )
571 sb( eIdx, blockPtr, noDataValuePtr );
A raster (stored in memory and eventually swapped to disk) where it is possible to dynamically add li...
unsigned int getNumberOfRows() const
Returns the grid number of rows.
bool addTopLines(const unsigned int number)
New lines will be added at the top of the raster (before the first line).
std::vector< ExpansibleBand * > m_bands
Internal raster bands.
A raster class for memory.
A raster band description.
int m_nblocksx
The number of blocks in x.
void dummyFillBlocks(const std::vector< ExpansibleBandBlocksManager::BlockIndex3D > &blocksCoords)
Fill the required blocks with dummy values.
int m_nblocksy
The number of blocks in y.
void dummyFillAllBlocks()
Fill all blocks with dummy values.
bool initialize(const unsigned int maxNumberRAMBlocks, const std::vector< unsigned int > &numbersOfBlocksX, const std::vector< unsigned int > &numbersOfBlocksY, const std::vector< unsigned int > &blocksSizesBytes, const unsigned long int maxDiskFilesSize)
Initialize this instance to an initial state.
An utility struct for representing 2D coordinates.
int m_type
The data type of the elements in the band.
double m_noDataValue
Value to indicate elements where there is no data, default is std::numeric_limits::max().
#define TE_TR(message)
It marks a string in order to get translated.
An exception class for the TerraLib In-Memory Data Access driver.
ExpansibleBandBlocksManager m_blocksManager
Internal blocks manager.
void open(const std::map< std::string, std::string > &rinfo, te::common::AccessPolicy p=te::common::RAccess)
Opens a raster.
AccessPolicy
Supported data access policies (can be used as bitfield).
void free()
Free all allocated internal resources and go back to the initial state.
TECOMMONEXPORT unsigned long int GetUsedVirtualMemory()
Returns the amount of used virtual memory (bytes) for the current process (physical + swapped)...
bool addRightColumns(const unsigned int number)
New columns will be added at the right of the raster (after the last column).
std::map< std::string, std::string > getInfo() const
It returns additional information about the raster.
BandProperty * getProperty()
Returns the band property.
CoordDataType m_dim0Index
Block Z (band) index.
int m_blkw
Block width (pixels).
void(* SetBufferValueFPtr)(int index, void *buffer, const double *value)
The type of function used to extract data from a buffer.
bool addBottomBands(const unsigned int number)
New bands will be added at the bottom of the raster (after de the last band).
unsigned int getNumberOfColumns() const
Returns the grid number of columns.
CoordDataType m_dim2Index
Block X index.
bool addBottomLines(const unsigned int number)
New lines will be added at the bottom of the raster (after de the last line).
A base class for values that can be retrieved from the data access module.
TERASTEREXPORT int GetPixelSize(int datatype)
Returns the byte size of a given datatype.
A raster (stored in memory and eventually swapped to disk) where it is possible to dynamically add li...
CoordDataType m_dim1Index
Block Y index.
te::dt::AbstractData * clone() const
It returns a clone of this object.
TECOMMONEXPORT unsigned long int GetTotalPhysicalMemory()
Returns the amount of total physical memory (bytes).
int m_blkh
Block height (pixels).
void(* GetBufferValueFPtr)(int index, void *buffer, double *value)
The type of function used to extract data from a buffer.
A rectified grid is the spatial support for raster data.
bool addLeftColumns(const unsigned int number)
New columns will be added at the left of the raster (before the first column).
bool addTopBands(const unsigned int number)
New bands will be added at the top of the raster (before the first band).
#define TLINTERNAL_EXPANSIBLERASTER_MAXDISKFILESSIZE
TERASTEREXPORT void SetBlockFunctions(GetBufferValueFPtr *gb, GetBufferValueFPtr *gbi, SetBufferValueFPtr *sb, SetBufferValueFPtr *sbi, int type)
Sets the pointers to functions that helps to extract a double or complex value from a specific buffer...
TECOMMONEXPORT unsigned long int GetTotalVirtualMemory()
Returns the amount of total virtual memory (bytes) that can be claimed by the current process (physic...