Skip to content

Commit

Permalink
Fast track for first tiles
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Jan 13, 2020
1 parent 0c2c133 commit 4d5b3f0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
10 changes: 5 additions & 5 deletions src/providers/postgres/raster/qgspostgresrasterprovider.cpp
Expand Up @@ -375,8 +375,8 @@ bool QgsPostgresRasterProvider::readBlock( int bandNo, const QgsRectangle &viewE
}
}

qDebug() << "Overview desired: " << desiredOverviewFactor << "found:" << overviewFactor << tableToQuery;
qDebug() << "View extent" << viewExtent.toString( 1 ) << width << height << minPixelSize;
//qDebug() << "Overview desired: " << desiredOverviewFactor << "found:" << overviewFactor << tableToQuery;
//qDebug() << "View extent" << viewExtent.toString( 1 ) << width << height << minPixelSize;

// Get the the tiles we need to build the block
const QString dataSql { QStringLiteral( "SELECT %3, ENCODE( ST_AsBinary( %1, TRUE ), 'hex') "
Expand Down Expand Up @@ -435,7 +435,7 @@ bool QgsPostgresRasterProvider::readBlock( int bandNo, const QgsRectangle &viewE

GDALDataType gdalDataType { static_cast<GDALDataType>( sourceDataType( bandNo ) ) };

qDebug() << "Creating output raster: " << tilesExtent.toString() << tmpWidth << tmpHeight;
//qDebug() << "Creating output raster: " << tilesExtent.toString() << tmpWidth << tmpHeight;

gdal::dataset_unique_ptr tmpDS { QgsGdalUtils::createSingleBandMemoryDataset(
gdalDataType, tilesExtent, tmpWidth, tmpHeight, mCrs ) };
Expand All @@ -456,7 +456,7 @@ bool QgsPostgresRasterProvider::readBlock( int bandNo, const QgsRectangle &viewE
const int xOff { static_cast<int>( std::round( ( tile.upperLeftX - tilesExtent.xMinimum() ) / tile.scaleX ) ) };
const int yOff { static_cast<int>( std::round( ( tilesExtent.yMaximum() - tile.extent.yMaximum() ) / std::fabs( tile.scaleY ) ) )};

qDebug() << "Merging tile output raster: " << tile.tileId << xOff << yOff << tile.width << tile.height ;
//qDebug() << "Merging tile output raster: " << tile.tileId << xOff << yOff << tile.width << tile.height ;

CPLErr err = GDALRasterIO( GDALGetRasterBand( tmpDS.get(), 1 ),
GF_Write,
Expand Down Expand Up @@ -799,10 +799,10 @@ bool QgsPostgresRasterProvider::init()
QStringLiteral( "PostGIS" ), Qgis::Info );
mSrcHasNoDataValue.append( false );
mUseSrcNoDataValue.append( false );
nodataValue = std::numeric_limits<double>::min();
}
else
{
mSrcNoDataValue.append( nodataValue );
mSrcHasNoDataValue.append( true );
mUseSrcNoDataValue.append( true );
}
Expand Down
19 changes: 16 additions & 3 deletions src/providers/postgres/raster/qgspostgresrastershareddata.cpp
Expand Up @@ -37,18 +37,30 @@ QgsPostgresRasterSharedData::TilesResponse QgsPostgresRasterSharedData::tiles( c

QgsPostgresRasterSharedData::TilesResponse result;

bool hasIndex { true };

// First check for index existence
if ( mSpatialIndexes.find( request.overviewFactor ) == mSpatialIndexes.end() )
{
// Create the index
mSpatialIndexes.emplace( request.overviewFactor, new QgsGenericSpatialIndex<Tile>() );
mTiles.emplace( request.overviewFactor, std::map<TileIdType, std::unique_ptr<Tile>>() );
mLoadedIndexBounds[ request.overviewFactor] = QgsGeometry::fromRect( QgsRectangle() );
hasIndex = false;
}

// Now check if the requested extent was completely downloaded
const QgsGeometry requestedRect { QgsGeometry::fromRect( request.extent ) };
if ( ! mLoadedIndexBounds[ request.overviewFactor].contains( requestedRect ) )

// Fast track for first tile (where index is empty)
if ( ! hasIndex )
{
if ( ! fetchTilesIndex( requestedRect, request ) )
{
return result;
}
}
else if ( ! mLoadedIndexBounds[ request.overviewFactor].contains( requestedRect ) )
{
// Fetch index
const QgsGeometry geomDiff { requestedRect.difference( mLoadedIndexBounds[ request.overviewFactor] ) };
Expand Down Expand Up @@ -223,11 +235,12 @@ bool QgsPostgresRasterSharedData::fetchTilesIndex( const QgsGeometry &requestPol
{
mSpatialIndexes[ request.overviewFactor ]->insert( tile.get(), tile->extent );
mTiles[ request.overviewFactor ][ tileId ] = std::move( tile );
qDebug() << "Tile added:" << request.overviewFactor << " ID: " << tileId << "extent " << extent.toString( 4 ) << upperleftx << upperlefty << tileWidth << tileHeight << extent.width() << extent.height();
//qDebug() << "Tile added:" << request.overviewFactor << " ID: " << tileId << "extent " << extent.toString( 4 ) << upperleftx << upperlefty << tileWidth << tileHeight << extent.width() << extent.height();
}
else
{
qDebug() << "Tile already indexed:" << request.overviewFactor << " ID: " << tileId << "extent " << extent.toString( 4 ) << upperleftx << upperlefty << tileWidth << tileHeight << extent.width() << extent.height();
// This should never happen!
//qDebug() << "Tile already indexed:" << request.overviewFactor << " ID: " << tileId << "extent " << extent.toString( 4 ) << upperleftx << upperlefty << tileWidth << tileHeight << extent.width() << extent.height();
}
}
return true;
Expand Down

0 comments on commit 4d5b3f0

Please sign in to comment.