Skip to content

Commit

Permalink
Fix bug causing raster blocks to be cleared when reprojecting
Browse files Browse the repository at this point in the history
If a QgsRasterBlock had no nodata value set, then reprojecting
the raster using QgsRasterProjector would result in the entire
block being set to nodata.

Cherry-picked from 920f2eb
  • Loading branch information
nyalldawson committed Jun 24, 2015
1 parent 4fbda18 commit 8702138
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
17 changes: 17 additions & 0 deletions python/core/raster/qgsrasterblock.sip
Expand Up @@ -180,6 +180,23 @@ class QgsRasterBlock
* @return true on success */
bool setIsNoDataExcept( const QRect & theExceptRect );

/** \brief Remove no data flag on pixel. If the raster block does not have an explicit
* no data value set then an internal map of no data pixels is maintained for the block.
* In this case it is possible to reset a pixel to flag it as having valid data using this
* method. This method has no effect for raster blocks with an explicit no data value set.
* @param row row index
* @param column column index
* @note added in QGIS 2.10 */
void setIsData( int row, int column );

/** \brief Remove no data flag on pixel. If the raster block does not have an explicit
* no data value set then an internal map of no data pixels is maintained for the block.
* In this case it is possible to reset a pixel to flag it as having valid data using this
* method. This method has no effect for raster blocks with an explicit no data value set.
* @param index data matrix index (long type in Python)
* @note added in QGIS 2.10 */
void setIsData( qgssize index );

/** \brief Get pointer to data
* @param row row index
* @param column column index
Expand Down
27 changes: 27 additions & 0 deletions src/core/raster/qgsrasterblock.cpp
Expand Up @@ -633,6 +633,33 @@ bool QgsRasterBlock::setIsNoDataExcept( const QRect & theExceptRect )
}
}

void QgsRasterBlock::setIsData( int row, int column )
{
setIsData(( qgssize )row*mWidth + column );
}

void QgsRasterBlock::setIsData( qgssize index )
{
if ( mHasNoDataValue )
{
//no data value set, so mNoDataBitmap is not being used
return;
}

if ( mNoDataBitmap == 0 )
{
return;
}

// TODO: optimize
int row = ( int ) index / mWidth;
int column = index % mWidth;
qgssize byte = ( qgssize )row * mNoDataBitmapWidth + column / 8;
int bit = column % 8;
int nodata = 0x80 >> bit;
mNoDataBitmap[byte] = mNoDataBitmap[byte] & ~nodata;
}

char * QgsRasterBlock::bits( qgssize index )
{
// Not testing type to avoid too much overhead because this method is called per pixel
Expand Down
17 changes: 17 additions & 0 deletions src/core/raster/qgsrasterblock.h
Expand Up @@ -242,6 +242,23 @@ class CORE_EXPORT QgsRasterBlock
* @return true on success */
bool setIsNoDataExcept( const QRect & theExceptRect );

/** \brief Remove no data flag on pixel. If the raster block does not have an explicit
* no data value set then an internal map of no data pixels is maintained for the block.
* In this case it is possible to reset a pixel to flag it as having valid data using this
* method. This method has no effect for raster blocks with an explicit no data value set.
* @param row row index
* @param column column index
* @note added in QGIS 2.10 */
void setIsData( int row, int column );

/** \brief Remove no data flag on pixel. If the raster block does not have an explicit
* no data value set then an internal map of no data pixels is maintained for the block.
* In this case it is possible to reset a pixel to flag it as having valid data using this
* method. This method has no effect for raster blocks with an explicit no data value set.
* @param index data matrix index (long type in Python)
* @note added in QGIS 2.10 */
void setIsData( qgssize index );

/** \brief Get pointer to data
* @param row row index
* @param column column index
Expand Down
1 change: 1 addition & 0 deletions src/core/raster/qgsrasterprojector.cpp
Expand Up @@ -934,6 +934,7 @@ QgsRasterBlock * QgsRasterProjector::block( int bandNo, QgsRectangle const & ex
continue;
}
memcpy( destBits, srcBits, pixelSize );
outputBlock->setIsData( i, j );
}
}

Expand Down

0 comments on commit 8702138

Please sign in to comment.