Skip to content

Commit 97eb48e

Browse files
mhugentrldhont
authored andcommittedDec 12, 2016
WMS 1.3. compliance: distort image if width/height ratio of bbox is different to WIDTH/HEIGHT of requested image
1 parent 9561f71 commit 97eb48e

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed
 

‎src/server/qgswmsserver.cpp

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,6 +1460,17 @@ QImage* QgsWMSServer::getMap( HitTest* hitTest )
14601460
// theImage->save( QDir::tempPath() + QDir::separator() + "lastrender.png" );
14611461
//#endif
14621462

1463+
thePainter.end();
1464+
1465+
//test if width / height ratio of image is the same as the ratio of WIDTH / HEIGHT parameters. If not, the image has to be scaled (required by WMS spec)
1466+
int widthParam = mParameters.value( "WIDTH", "0" ).toInt();
1467+
int heightParam = mParameters.value( "HEIGHT", "0" ).toInt();
1468+
if ( widthParam != theImage->width() || heightParam != theImage->height() )
1469+
{
1470+
//scale image
1471+
*theImage = theImage->scaled( widthParam, heightParam, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
1472+
}
1473+
14631474
return theImage;
14641475
}
14651476

@@ -1581,7 +1592,6 @@ int QgsWMSServer::getFeatureInfo( QDomDocument& result, const QString& version )
15811592
QgsRectangle mapExtent = mMapRenderer->extent();
15821593
double scaleDenominator = scaleCalc.calculate( mapExtent, outputImage->width() );
15831594
mConfigParser->setScaleDenominator( scaleDenominator );
1584-
delete outputImage; //no longer needed for feature info
15851595

15861596
//read FEATURE_COUNT
15871597
int featureCount = 1;
@@ -1621,6 +1631,16 @@ int QgsWMSServer::getFeatureInfo( QDomDocument& result, const QString& version )
16211631
j = -1;
16221632
}
16231633

1634+
//In case the output image is distorted (WIDTH/HEIGHT ratio not equal to BBOX width/height), I and J need to be adapted as well
1635+
int widthParam = mParameters.value( "WIDTH", "-1" ).toInt();
1636+
int heightParam = mParameters.value( "HEIGHT", "-1" ).toInt();
1637+
if (( i != -1 && j != -1 && widthParam != -1 && heightParam != -1 ) && ( widthParam != outputImage->width() || heightParam != outputImage->height() ) )
1638+
{
1639+
i *= ( outputImage->width() / ( double )widthParam );
1640+
j *= ( outputImage->height() / ( double )heightParam );
1641+
}
1642+
delete outputImage; //no longer needed for feature info
1643+
16241644
//Normally, I/J or X/Y are mandatory parameters.
16251645
//However, in order to make attribute only queries via the FILTER parameter, it is allowed to skip them if the FILTER parameter is there
16261646

@@ -1957,6 +1977,29 @@ QImage* QgsWMSServer::createImage( int width, int height ) const
19571977
}
19581978
}
19591979

1980+
//Adapt width / height if the aspect ratio does not correspond with the BBOX.
1981+
//Required by WMS spec. 1.3.
1982+
bool bboxOk;
1983+
QgsRectangle mapExtent = _parseBBOX( mParameters.value( "BBOX" ), bboxOk );
1984+
if ( bboxOk )
1985+
{
1986+
double mapWidthHeightRatio = mapExtent.width() / mapExtent.height();
1987+
double imageWidthHeightRatio = ( double )width / ( double )height;
1988+
if ( !qgsDoubleNear( mapWidthHeightRatio, imageWidthHeightRatio, 0.0001 ) )
1989+
{
1990+
if ( mapWidthHeightRatio >= imageWidthHeightRatio )
1991+
{
1992+
//decrease image height
1993+
height = width / mapWidthHeightRatio;
1994+
}
1995+
else
1996+
{
1997+
//decrease image width
1998+
width = height * mapWidthHeightRatio;
1999+
}
2000+
}
2001+
}
2002+
19602003
if ( width < 0 || height < 0 )
19612004
{
19622005
return nullptr;
@@ -2039,7 +2082,7 @@ int QgsWMSServer::configureMapRender( const QPaintDevice* paintDevice ) const
20392082
throw QgsMapServiceException( "InvalidParameterValue", "Invalid BBOX parameter" );
20402083
}
20412084

2042-
if ( mapExtent.isEmpty() )
2085+
if ( mParameters.contains( "BBOX" ) && mapExtent.isEmpty() )
20432086
{
20442087
throw QgsMapServiceException( "InvalidParameterValue", "BBOX is empty" );
20452088
}

0 commit comments

Comments
 (0)
Please sign in to comment.