Skip to content

Commit 5d80e1b

Browse files
mhugentrldhont
authored andcommitted
WMS 1.3. compliance: distort image if width/height ratio of bbox is different to WIDTH/HEIGHT of requested image
1 parent 6d68e9e commit 5d80e1b

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

src/server/qgswmsserver.cpp

+45-2
Original file line numberDiff line numberDiff line change
@@ -1453,6 +1453,17 @@ QImage* QgsWMSServer::getMap( HitTest* hitTest )
14531453
// theImage->save( QDir::tempPath() + QDir::separator() + "lastrender.png" );
14541454
//#endif
14551455

1456+
thePainter.end();
1457+
1458+
//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)
1459+
int widthParam = mParameters.value( "WIDTH", "0" ).toInt();
1460+
int heightParam = mParameters.value( "HEIGHT", "0" ).toInt();
1461+
if ( widthParam != theImage->width() || heightParam != theImage->height() )
1462+
{
1463+
//scale image
1464+
*theImage = theImage->scaled( widthParam, heightParam, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
1465+
}
1466+
14561467
return theImage;
14571468
}
14581469

@@ -1574,7 +1585,6 @@ int QgsWMSServer::getFeatureInfo( QDomDocument& result, const QString& version )
15741585
QgsRectangle mapExtent = mMapRenderer->extent();
15751586
double scaleDenominator = scaleCalc.calculate( mapExtent, outputImage->width() );
15761587
mConfigParser->setScaleDenominator( scaleDenominator );
1577-
delete outputImage; //no longer needed for feature info
15781588

15791589
//read FEATURE_COUNT
15801590
int featureCount = 1;
@@ -1614,6 +1624,16 @@ int QgsWMSServer::getFeatureInfo( QDomDocument& result, const QString& version )
16141624
j = -1;
16151625
}
16161626

1627+
//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
1628+
int widthParam = mParameters.value( "WIDTH", "-1" ).toInt();
1629+
int heightParam = mParameters.value( "HEIGHT", "-1" ).toInt();
1630+
if (( i != -1 && j != -1 && widthParam != -1 && heightParam != -1 ) && ( widthParam != outputImage->width() || heightParam != outputImage->height() ) )
1631+
{
1632+
i *= ( outputImage->width() / ( double )widthParam );
1633+
j *= ( outputImage->height() / ( double )heightParam );
1634+
}
1635+
delete outputImage; //no longer needed for feature info
1636+
16171637
//Normally, I/J or X/Y are mandatory parameters.
16181638
//However, in order to make attribute only queries via the FILTER parameter, it is allowed to skip them if the FILTER parameter is there
16191639

@@ -1950,6 +1970,29 @@ QImage* QgsWMSServer::createImage( int width, int height ) const
19501970
}
19511971
}
19521972

1973+
//Adapt width / height if the aspect ratio does not correspond with the BBOX.
1974+
//Required by WMS spec. 1.3.
1975+
bool bboxOk;
1976+
QgsRectangle mapExtent = _parseBBOX( mParameters.value( "BBOX" ), bboxOk );
1977+
if ( bboxOk )
1978+
{
1979+
double mapWidthHeightRatio = mapExtent.width() / mapExtent.height();
1980+
double imageWidthHeightRatio = ( double )width / ( double )height;
1981+
if ( !qgsDoubleNear( mapWidthHeightRatio, imageWidthHeightRatio, 0.0001 ) )
1982+
{
1983+
if ( mapWidthHeightRatio >= imageWidthHeightRatio )
1984+
{
1985+
//decrease image height
1986+
height = width / mapWidthHeightRatio;
1987+
}
1988+
else
1989+
{
1990+
//decrease image width
1991+
width = height * mapWidthHeightRatio;
1992+
}
1993+
}
1994+
}
1995+
19531996
if ( width < 0 || height < 0 )
19541997
{
19551998
return nullptr;
@@ -2032,7 +2075,7 @@ int QgsWMSServer::configureMapRender( const QPaintDevice* paintDevice ) const
20322075
throw QgsMapServiceException( "InvalidParameterValue", "Invalid BBOX parameter" );
20332076
}
20342077

2035-
if ( mapExtent.isEmpty() )
2078+
if ( mParameters.contains( "BBOX" ) && mapExtent.isEmpty() )
20362079
{
20372080
throw QgsMapServiceException( "InvalidParameterValue", "BBOX is empty" );
20382081
}

0 commit comments

Comments
 (0)