Skip to content

Commit 8cda1bb

Browse files
committed
fix getLegendGraphics when BBox parameter is used
1 parent f3a5c98 commit 8cda1bb

File tree

6 files changed

+61
-58
lines changed

6 files changed

+61
-58
lines changed

src/server/qgswmsprojectparser.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include "qgsmaplayerstylemanager.h"
2525
#include "qgsmapserviceexception.h"
2626
#include "qgspallabeling.h"
27-
#include "qgsproject.h"
2827
#include "qgsrenderer.h"
2928
#include "qgsvectorlayer.h"
3029

src/server/qgswmsserver.cpp

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -847,8 +847,6 @@ QImage* QgsWmsServer::getLegendGraphics()
847847
}
848848
QgsLayerTreeModel legendModel( &rootGroup );
849849

850-
QList<QgsLayerTreeNode*> rootChildren = rootGroup.children();
851-
852850
if ( scaleDenominator > 0 )
853851
legendModel.setLegendFilterByScale( scaleDenominator );
854852

@@ -889,7 +887,7 @@ QImage* QgsWmsServer::getLegendGraphics()
889887
}
890888

891889
// find out DPI
892-
QImage* tmpImage = createImage( 1, 1 );
890+
QImage* tmpImage = createImage( 1, 1, false );
893891
if ( !tmpImage )
894892
return nullptr;
895893
qreal dpmm = tmpImage->dotsPerMeterX() / 1000.0;
@@ -917,7 +915,7 @@ QImage* QgsWmsServer::getLegendGraphics()
917915
if ( !rule.isEmpty() )
918916
{
919917
//create second image with the right dimensions
920-
QImage* paintImage = createImage( ruleSymbolWidth, ruleSymbolHeight );
918+
QImage* paintImage = createImage( ruleSymbolWidth, ruleSymbolHeight, false );
921919

922920
//go through the items a second time for painting
923921
QPainter p( paintImage );
@@ -939,6 +937,7 @@ QImage* QgsWmsServer::getLegendGraphics()
939937
return paintImage;
940938
}
941939

940+
QList<QgsLayerTreeNode*> rootChildren = rootGroup.children();
942941
Q_FOREACH ( QgsLayerTreeNode* node, rootChildren )
943942
{
944943
if ( QgsLayerTree::isLayer( node ) )
@@ -978,7 +977,7 @@ QImage* QgsWmsServer::getLegendGraphics()
978977
QSizeF minSize = legendRenderer.minimumSize();
979978
QSize s( minSize.width() * dpmm, minSize.height() * dpmm );
980979

981-
QImage* paintImage = createImage( s.width(), s.height() );
980+
QImage* paintImage = createImage( s.width(), s.height(), false );
982981

983982
QPainter p( paintImage );
984983
p.setRenderHint( QPainter::Antialiasing, true );
@@ -1422,7 +1421,7 @@ QImage* QgsWmsServer::getMap( QgsMapSettings& mapSettings, HitTest* hitTest )
14221421
QStringList highlightLayersId = QgsWmsConfigParser::addHighlightLayers( mParameters, layerSetIds );
14231422

14241423
QList<QgsMapLayer *> layerSet;
1425-
Q_FOREACH( QString layerSetId, layerSetIds )
1424+
Q_FOREACH ( QString layerSetId, layerSetIds )
14261425
{
14271426
layerSet.append( QgsProject::instance()->mapLayer( layerSetId ) );
14281427
}
@@ -1967,7 +1966,7 @@ QImage* QgsWmsServer::initializeRendering( QStringList& layersList, QStringList&
19671966
#endif
19681967

19691968
QList<QgsMapLayer *> layers;
1970-
Q_FOREACH( QString layerId, layerIdList )
1969+
Q_FOREACH ( QString layerId, layerIdList )
19711970
{
19721971
layers.append( QgsProject::instance()->mapLayer( layerId ) );
19731972
}
@@ -1979,7 +1978,7 @@ QImage* QgsWmsServer::initializeRendering( QStringList& layersList, QStringList&
19791978
return theImage;
19801979
}
19811980

1982-
QImage* QgsWmsServer::createImage( int width, int height ) const
1981+
QImage* QgsWmsServer::createImage( int width, int height, bool useBbox ) const
19831982
{
19841983
bool conversionSuccess;
19851984

@@ -2001,23 +2000,26 @@ QImage* QgsWmsServer::createImage( int width, int height ) const
20012000

20022001
//Adapt width / height if the aspect ratio does not correspond with the BBOX.
20032002
//Required by WMS spec. 1.3.
2004-
bool bboxOk;
2005-
QgsRectangle mapExtent = _parseBBOX( mParameters.value( "BBOX" ), bboxOk );
2006-
if ( bboxOk )
2003+
if ( useBbox )
20072004
{
2008-
double mapWidthHeightRatio = mapExtent.width() / mapExtent.height();
2009-
double imageWidthHeightRatio = ( double )width / ( double )height;
2010-
if ( !qgsDoubleNear( mapWidthHeightRatio, imageWidthHeightRatio, 0.0001 ) )
2005+
bool bboxOk;
2006+
QgsRectangle mapExtent = _parseBBOX( mParameters.value( "BBOX" ), bboxOk );
2007+
if ( bboxOk )
20112008
{
2012-
if ( mapWidthHeightRatio >= imageWidthHeightRatio )
2009+
double mapWidthHeightRatio = mapExtent.width() / mapExtent.height();
2010+
double imageWidthHeightRatio = ( double )width / ( double )height;
2011+
if ( !qgsDoubleNear( mapWidthHeightRatio, imageWidthHeightRatio, 0.0001 ) )
20132012
{
2014-
//decrease image height
2015-
height = width / mapWidthHeightRatio;
2016-
}
2017-
else
2018-
{
2019-
//decrease image width
2020-
width = height * mapWidthHeightRatio;
2013+
if ( mapWidthHeightRatio >= imageWidthHeightRatio )
2014+
{
2015+
//decrease image height
2016+
height = width / mapWidthHeightRatio;
2017+
}
2018+
else
2019+
{
2020+
//decrease image width
2021+
width = height * mapWidthHeightRatio;
2022+
}
20212023
}
20222024
}
20232025
}

src/server/qgswmsserver.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,9 @@ class QgsWmsServer: public QgsOWSServer
136136
/** Creates a QImage from the HEIGHT and WIDTH parameters
137137
@param width image width (or -1 if width should be taken from WIDTH wms parameter)
138138
@param height image height (or -1 if height should be taken from HEIGHT wms parameter)
139+
@param useBbox flag to indicate if the BBOX has to be used to adapt aspect ratio
139140
@return 0 in case of error*/
140-
QImage* createImage( int width = -1, int height = -1 ) const;
141+
QImage* createImage( int width = -1, int height = -1, bool useBbox = true ) const;
141142

142143
/** Configures mapSettings to the parameters
143144
HEIGHT, WIDTH, BBOX, CRS.

tests/src/python/test_qgsserver.py

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -781,40 +781,41 @@ def test_wms_GetLegendGraphic_SymbolSize(self):
781781
r, h = self._result(self.server.handleRequest(qs))
782782
self._img_diff_error(r, h, "WMS_GetLegendGraphic_SymbolSize")
783783

784-
#def test_wms_GetLegendGraphic_BBox(self):
785-
# qs = "&".join(["%s=%s" % i for i in list({
786-
# "MAP": urllib.parse.quote(self.projectPath),
787-
# "SERVICE": "WMS",
788-
# "VERSION": "1.1.1",
789-
# "REQUEST": "GetLegendGraphic",
790-
# "LAYER": "Country,Hello,db_point",
791-
# "LAYERTITLE": "FALSE",
792-
# "FORMAT": "image/png",
793-
# "HEIGHT": "500",
794-
# "WIDTH": "500",
795-
# "BBOX": "-151.7,-38.9,51.0,78.0",
796-
# "CRS": "EPSG:4326"
797-
# }.items())])
798-
# r, h = self._result(self.server.handleRequest(qs))
799-
# self._img_diff_error(r, h, "WMS_GetLegendGraphic_BBox")
800-
801-
#def test_wms_GetLegendGraphic_BBox2(self):
802-
# qs = "&".join(["%s=%s" % i for i in list({
803-
# "MAP": urllib.parse.quote(self.projectPath),
804-
# "SERVICE": "WMS",
805-
# "VERSION": "1.1.1",
806-
# "REQUEST": "GetLegendGraphic",
807-
# "LAYER": "Country,Hello,db_point",
808-
# "LAYERTITLE": "FALSE",
809-
# "FORMAT": "image/png",
810-
# "HEIGHT": "500",
811-
# "WIDTH": "500",
812-
# #"BBOX": "-76.08,38.04,109.95,-6.4",
813-
# "SRS": "EPSG:4326"
814-
# }.items())])
815-
816-
# r, h = self._result(self.server.handleRequest(qs))
817-
# self._img_diff_error(r, h, "WMS_GetLegendGraphic_BBox2")
784+
def test_wms_GetLegendGraphic_BBox(self):
785+
qs = "&".join(["%s=%s" % i for i in list({
786+
"MAP": urllib.parse.quote(self.projectPath),
787+
"SERVICE": "WMS",
788+
"VERSION": "1.1.1",
789+
"REQUEST": "GetLegendGraphic",
790+
"LAYER": "Country,Hello,db_point",
791+
"LAYERTITLE": "FALSE",
792+
"FORMAT": "image/png",
793+
"HEIGHT": "500",
794+
"WIDTH": "500",
795+
"BBOX": "-151.7,-38.9,51.0,78.0",
796+
"CRS": "EPSG:4326"
797+
}.items())])
798+
799+
r, h = self._result(self.server.handleRequest(qs))
800+
self._img_diff_error(r, h, "WMS_GetLegendGraphic_BBox")
801+
802+
def test_wms_GetLegendGraphic_BBox2(self):
803+
qs = "&".join(["%s=%s" % i for i in list({
804+
"MAP": urllib.parse.quote(self.projectPath),
805+
"SERVICE": "WMS",
806+
"VERSION": "1.1.1",
807+
"REQUEST": "GetLegendGraphic",
808+
"LAYER": "Country,Hello,db_point",
809+
"LAYERTITLE": "FALSE",
810+
"FORMAT": "image/png",
811+
"HEIGHT": "500",
812+
"WIDTH": "500",
813+
"BBOX": "-76.08,-6.4,-19.38,38.04",
814+
"SRS": "EPSG:4326"
815+
}.items())])
816+
817+
r, h = self._result(self.server.handleRequest(qs))
818+
self._img_diff_error(r, h, "WMS_GetLegendGraphic_BBox2")
818819

819820
def _result(self, data):
820821
headers = {}

0 commit comments

Comments
 (0)