Skip to content

Commit

Permalink
Keep the destination image of the same size
Browse files Browse the repository at this point in the history
Use a bigger extent for getFeatures but keep the destination image of the
same size.
  • Loading branch information
Éric Lemoine committed Jun 18, 2019
1 parent 9b799eb commit 7724759
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 28 deletions.
1 change: 1 addition & 0 deletions src/core/qgsmaprendererjob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ LayerRenderJobs QgsMapRendererJob::prepareJobs( QPainter *painter, QgsLabelingEn
}

QgsRectangle r1 = mSettings.visibleExtent(), r2;
r1.grow( mSettings.extentBuffer() );
QgsCoordinateTransform ct;

ct = mSettings.layerTransform( ml );
Expand Down
10 changes: 10 additions & 0 deletions src/core/qgsmapsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ void QgsMapSettings::setExtent( const QgsRectangle &extent, bool magnified )
updateDerived();
}

double QgsMapSettings::extentBuffer() const
{
return mExtentBuffer;
}

void QgsMapSettings::setExtentBuffer( double buffer )
{
mExtentBuffer = buffer;
}

double QgsMapSettings::rotation() const
{
return mRotation;
Expand Down
11 changes: 11 additions & 0 deletions src/core/qgsmapsettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ class CORE_EXPORT QgsMapSettings
*/
void setExtent( const QgsRectangle &rect, bool magnified = true );

/**
* \since QGIS 3.10
*/
double extentBuffer() const;

/**
* \since QGIS 3.10
*/
void setExtentBuffer( double buffer );

//! Returns the size of the resulting map image
QSize outputSize() const;
//! Sets the size of the resulting map image
Expand Down Expand Up @@ -539,6 +549,7 @@ class CORE_EXPORT QgsMapSettings
float mDevicePixelRatio = 1.0;

QgsRectangle mExtent;
double mExtentBuffer = 0.0;

double mRotation = 0.0;
double mMagnificationFactor = 1.0;
Expand Down
4 changes: 3 additions & 1 deletion src/core/qgsrendercontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,10 @@ bool QgsRenderContext::testFlag( QgsRenderContext::Flag flag ) const
QgsRenderContext QgsRenderContext::fromMapSettings( const QgsMapSettings &mapSettings )
{
QgsRenderContext ctx;
QgsRectangle extent = mapSettings.visibleExtent();
extent.grow( mapSettings.extentBuffer() );
ctx.setMapToPixel( mapSettings.mapToPixel() );
ctx.setExtent( mapSettings.visibleExtent() );
ctx.setExtent( extent );
ctx.setMapExtent( mapSettings.visibleExtent() );
ctx.setFlag( DrawEditingInfo, mapSettings.testFlag( QgsMapSettings::DrawEditingInfo ) );
ctx.setFlag( ForceVectorOutput, mapSettings.testFlag( QgsMapSettings::ForceVectorOutput ) );
Expand Down
41 changes: 24 additions & 17 deletions src/server/services/wms/qgswmsrendercontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ int QgsWmsRenderContext::tileBuffer() const
{
tileBuffer = QgsServerProjectUtils::wmsTileBuffer( *mProject );
}

return tileBuffer;
}

Expand Down Expand Up @@ -648,36 +649,42 @@ bool QgsWmsRenderContext::isValidWidthHeight() const
return true;
}

QgsRectangle QgsWmsRenderContext::mapExtent() const
double QgsWmsRenderContext::mapTileBuffer(int mapWidth) const
{
QgsRectangle extent = mParameters.bboxAsRectangle();
if ( !mParameters.bbox().isEmpty() && extent.isEmpty() )
double buffer;
if ( mFlags & UseTileBuffer )
{
throw QgsBadRequestException( QgsServiceException::QGIS_InvalidParameterValue,
mParameters[QgsWmsParameter::BBOX] );
QgsRectangle extent = mParameters.bboxAsRectangle();
if ( !mParameters.bbox().isEmpty() && extent.isEmpty() )
{
throw QgsBadRequestException( QgsServiceException::QGIS_InvalidParameterValue,
mParameters[QgsWmsParameter::BBOX] );
}
buffer = tileBuffer() * ( extent.width() / mapWidth );
}
const double resolutionX = extent.width() / mapWidth();
const double resolutionY = extent.height() / mapHeight();
const int tBuffer = mFlags & UseTileBuffer ? tileBuffer() : 0;
const double xMin = extent.xMinimum() - tBuffer * resolutionX;
const double yMin = extent.yMinimum() - tBuffer * resolutionY;
const double xMax = extent.xMaximum() + tBuffer * resolutionX;
const double yMax = extent.yMaximum() + tBuffer * resolutionY;
return QgsRectangle( xMin, yMin, xMax, yMax );
else
{
buffer = 0;
}
return buffer;
}

QSize QgsWmsRenderContext::mapSize( const bool aspectRatio ) const
{
int tBuffer = mFlags & UseTileBuffer ? tileBuffer() : 0;
int width = mapWidth() + tBuffer * 2;
int height = mapHeight() + tBuffer * 2;
int width = mapWidth();
int height = mapHeight();

// Adapt width / height if the aspect ratio does not correspond with the BBOX.
// Required by WMS spec. 1.3.
if ( aspectRatio
&& mParameters.versionAsNumber() >= QgsProjectVersion( 1, 3, 0 ) )
{
QgsRectangle extent = mapExtent();
QgsRectangle extent = mParameters.bboxAsRectangle();
if ( !mParameters.bbox().isEmpty() && extent.isEmpty() )
{
throw QgsBadRequestException( QgsServiceException::QGIS_InvalidParameterValue,
mParameters[QgsWmsParameter::BBOX] );
}

QString crs = mParameters.crs();
if ( crs.compare( "CRS:84", Qt::CaseInsensitive ) == 0 )
Expand Down
4 changes: 2 additions & 2 deletions src/server/services/wms/qgswmsrendercontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,10 @@ namespace QgsWms
QMap<QString, QList<QgsMapLayer *> > layerGroups() const;

/**
* Returns the extent of the map to render.
* Returns the tile buffer in geographical units for the given map width in pixels.
* \since QGIS 3.10
*/
QgsRectangle mapExtent() const;
double mapTileBuffer(int mapWidth) const;

/**
* Returns the size (in pixels) of the map to render, according to width
Expand Down
20 changes: 12 additions & 8 deletions src/server/services/wms/qgswmsrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -751,13 +751,18 @@ namespace QgsWms
QgsMapSettings mapSettings;
configureLayers( layers, &mapSettings );

const QSize mapSize = mContext.mapSize();

// create the output image and the painter
std::unique_ptr<QPainter> painter;
std::unique_ptr<QImage> image( createImage( mContext.mapSize() ) );
std::unique_ptr<QImage> image( createImage( mapSize ) );

// configure map settings (background, DPI, ...)
configureMapSettings( image.get(), mapSettings );

// set the extent buffer in the map settings
mapSettings.setExtentBuffer( mContext.mapTileBuffer( mapSize.width() ) );

// add layers to map settings
mapSettings.setLayers( layers );

Expand All @@ -770,12 +775,6 @@ namespace QgsWms
// painting is terminated
painter->end();

if ( mContext.testFlag( QgsWmsRenderContext::UseTileBuffer ) )
{
QRect rect( mContext.tileBuffer(), mContext.tileBuffer(), mContext.mapWidth(), mContext.mapHeight() );
image.reset( new QImage( image.get()->copy( rect ) ) );
}

// scale output image if necessary (required by WMS spec)
QImage *scaledImage = scaleImage( image.get() );
if ( scaledImage )
Expand Down Expand Up @@ -982,7 +981,12 @@ namespace QgsWms
mapSettings.setOutputDpi( paintDevice->logicalDpiX() );

//map extent
QgsRectangle mapExtent = mContext.mapExtent();
QgsRectangle mapExtent = mWmsParameters.bboxAsRectangle();
if ( !mWmsParameters.bbox().isEmpty() && mapExtent.isEmpty() )
{
throw QgsBadRequestException( QgsServiceException::QGIS_InvalidParameterValue,
mWmsParameters[QgsWmsParameter::BBOX] );
}

QString crs = mWmsParameters.crs();
if ( crs.compare( "CRS:84", Qt::CaseInsensitive ) == 0 )
Expand Down

0 comments on commit 7724759

Please sign in to comment.