Skip to content

Commit

Permalink
WMS 1.3. compliance: distort image if width/height ratio of bbox is d…
Browse files Browse the repository at this point in the history
…ifferent to WIDTH/HEIGHT of requested image
  • Loading branch information
mhugent committed Nov 29, 2016
1 parent 1bc6ee3 commit 120c8d3
Showing 1 changed file with 45 additions and 2 deletions.
47 changes: 45 additions & 2 deletions src/server/qgswmsserver.cpp
Expand Up @@ -1464,6 +1464,17 @@ QImage* QgsWmsServer::getMap( HitTest* hitTest )
// theImage->save( QDir::tempPath() + QDir::separator() + "lastrender.png" );
//#endif

thePainter.end();

//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)
int widthParam = mParameters.value( "WIDTH", "0" ).toInt();
int heightParam = mParameters.value( "HEIGHT", "0" ).toInt();
if ( widthParam != theImage->width() || heightParam != theImage->height() )
{
//scale image
*theImage = theImage->scaled( widthParam, heightParam, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
}

return theImage;
}

Expand Down Expand Up @@ -1585,7 +1596,6 @@ int QgsWmsServer::getFeatureInfo( QDomDocument& result, const QString& version )
QgsRectangle mapExtent = mMapRenderer->extent();
double scaleDenominator = scaleCalc.calculate( mapExtent, outputImage->width() );
mConfigParser->setScaleDenominator( scaleDenominator );
delete outputImage; //no longer needed for feature info

//read FEATURE_COUNT
int featureCount = 1;
Expand Down Expand Up @@ -1625,6 +1635,16 @@ int QgsWmsServer::getFeatureInfo( QDomDocument& result, const QString& version )
j = -1;
}

//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
int widthParam = mParameters.value( "WIDTH", "-1" ).toInt();
int heightParam = mParameters.value( "HEIGHT", "-1" ).toInt();
if (( i != -1 && j != -1 && widthParam != -1 && heightParam != -1 ) && ( widthParam != outputImage->width() || heightParam != outputImage->height() ) )
{
i *= ( outputImage->width() / ( double )widthParam );
j *= ( outputImage->height() / ( double )heightParam );
}
delete outputImage; //no longer needed for feature info

//Normally, I/J or X/Y are mandatory parameters.
//However, in order to make attribute only queries via the FILTER parameter, it is allowed to skip them if the FILTER parameter is there

Expand Down Expand Up @@ -1966,6 +1986,29 @@ QImage* QgsWmsServer::createImage( int width, int height ) const
}
}

//Adapt width / height if the aspect ratio does not correspond with the BBOX.
//Required by WMS spec. 1.3.
bool bboxOk;
QgsRectangle mapExtent = _parseBBOX( mParameters.value( "BBOX" ), bboxOk );
if ( bboxOk )
{
double mapWidthHeightRatio = mapExtent.width() / mapExtent.height();
double imageWidthHeightRatio = ( double )width / ( double )height;
if ( !qgsDoubleNear( mapWidthHeightRatio, imageWidthHeightRatio, 0.0001 ) )
{
if ( mapWidthHeightRatio >= imageWidthHeightRatio )
{
//decrease image height
height = width / mapWidthHeightRatio;
}
else
{
//decrease image width
width = height * mapWidthHeightRatio;
}
}
}

if ( width < 0 || height < 0 )
{
return nullptr;
Expand Down Expand Up @@ -2048,7 +2091,7 @@ int QgsWmsServer::configureMapRender( const QPaintDevice* paintDevice ) const
throw QgsMapServiceException( QStringLiteral( "InvalidParameterValue" ), QStringLiteral( "Invalid BBOX parameter" ) );
}

if ( mapExtent.isEmpty() )
if ( mParameters.contains( "BBOX" ) && mapExtent.isEmpty() )
{
throw QgsMapServiceException( "InvalidParameterValue", "BBOX is empty" );
}
Expand Down

1 comment on commit 120c8d3

@rldhont
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We found an issue with this commit, the distortion is not made the right way. We have to invert it.

Please sign in to comment.