Skip to content

Commit 7e40fb4

Browse files
authoredDec 12, 2016
Merge pull request #3858 from rldhont/fix-getLegendGraphics-bbox
fix getLegendGraphics when BBox parameter is used
2 parents e5127c9 + 3e4a19e commit 7e40fb4

File tree

10 files changed

+156
-22
lines changed

10 files changed

+156
-22
lines changed
 

‎src/server/qgswmsserver.cpp

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -849,8 +849,6 @@ QImage* QgsWMSServer::getLegendGraphics()
849849
}
850850
QgsLayerTreeModel legendModel( &rootGroup );
851851

852-
QList<QgsLayerTreeNode*> rootChildren = rootGroup.children();
853-
854852
if ( scaleDenominator > 0 )
855853
legendModel.setLegendFilterByScale( scaleDenominator );
856854

@@ -890,7 +888,7 @@ QImage* QgsWMSServer::getLegendGraphics()
890888
}
891889

892890
// find out DPI
893-
QImage* tmpImage = createImage( 1, 1 );
891+
QImage* tmpImage = createImage( 1, 1, false );
894892
if ( !tmpImage )
895893
return nullptr;
896894
qreal dpmm = tmpImage->dotsPerMeterX() / 1000.0;
@@ -920,7 +918,7 @@ QImage* QgsWMSServer::getLegendGraphics()
920918
if ( !rule.isEmpty() )
921919
{
922920
//create second image with the right dimensions
923-
QImage* paintImage = createImage( ruleSymbolWidth, ruleSymbolHeight );
921+
QImage* paintImage = createImage( ruleSymbolWidth, ruleSymbolHeight, false );
924922

925923
//go through the items a second time for painting
926924
QPainter p( paintImage );
@@ -942,6 +940,7 @@ QImage* QgsWMSServer::getLegendGraphics()
942940
return paintImage;
943941
}
944942

943+
QList<QgsLayerTreeNode*> rootChildren = rootGroup.children();
945944
Q_FOREACH ( QgsLayerTreeNode* node, rootChildren )
946945
{
947946
if ( QgsLayerTree::isLayer( node ) )
@@ -981,7 +980,7 @@ QImage* QgsWMSServer::getLegendGraphics()
981980
QSizeF minSize = legendRenderer.minimumSize();
982981
QSize s( minSize.width() * dpmm, minSize.height() * dpmm );
983982

984-
QImage* paintImage = createImage( s.width(), s.height() );
983+
QImage* paintImage = createImage( s.width(), s.height(), false );
985984

986985
QPainter p( paintImage );
987986
p.setRenderHint( QPainter::Antialiasing, true );
@@ -1980,7 +1979,7 @@ QImage* QgsWMSServer::initializeRendering( QStringList& layersList, QStringList&
19801979
return theImage;
19811980
}
19821981

1983-
QImage* QgsWMSServer::createImage( int width, int height ) const
1982+
QImage* QgsWMSServer::createImage( int width, int height, bool useBbox ) const
19841983
{
19851984
bool conversionSuccess;
19861985

@@ -2002,23 +2001,26 @@ QImage* QgsWMSServer::createImage( int width, int height ) const
20022001

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

‎src/server/qgswmsserver.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,11 @@ class QgsWMSServer: public QgsOWSServer
133133
/** Creates a QImage from the HEIGHT and WIDTH parameters
134134
@param width image width (or -1 if width should be taken from WIDTH wms parameter)
135135
@param height image height (or -1 if height should be taken from HEIGHT wms parameter)
136+
@param useBbox flag to indicate if the BBOX has to be used to adapt aspect ratio
136137
@return 0 in case of error*/
137-
QImage* createImage( int width = -1, int height = -1 ) const;
138-
/** Configures mMapRenderer to the parameters
138+
QImage* createImage( int width = -1, int height = -1, bool useBbox = true ) const;
139+
140+
/** Configures mapSettings to the parameters
139141
HEIGHT, WIDTH, BBOX, CRS.
140142
@param paintDevice the device that is used for painting (for dpi)
141143
@return 0 in case of success*/

‎tests/src/python/test_qgsserver.py

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ class TestQgsServer(unittest.TestCase):
3636
def setUp(self):
3737
"""Create the server instance"""
3838
self.testdata_path = unitTestDataPath('qgis_server') + '/'
39+
40+
d = unitTestDataPath('qgis_server_accesscontrol') + '/'
41+
self.projectPath = os.path.join(d, "project.qgs")
42+
3943
# Clean env just to be sure
4044
env_vars = ['QUERY_STRING', 'QGIS_PROJECT_FILE']
4145
for ev in env_vars:
@@ -423,6 +427,132 @@ def test_getLegendGraphics_layertitle(self):
423427
r, h = self._result(self.server.handleRequest(qs))
424428
self._img_diff_error(r, h, "WMS_GetLegendGraphic_test_layertitle_false", 250, QSize(10, 10))
425429

430+
def test_wms_GetLegendGraphic_Basic(self):
431+
qs = "&".join(["%s=%s" % i for i in list({
432+
"MAP": urllib.quote(self.projectPath),
433+
"SERVICE": "WMS",
434+
"VERSION": "1.1.1",
435+
"REQUEST": "GetLegendGraphic",
436+
"LAYER": "Country,Hello",
437+
"LAYERTITLE": "FALSE",
438+
"FORMAT": "image/png",
439+
"HEIGHT": "500",
440+
"WIDTH": "500",
441+
"CRS": "EPSG:3857"
442+
}.items())])
443+
444+
r, h = self._result(self.server.handleRequest(qs))
445+
self._img_diff_error(r, h, "WMS_GetLegendGraphic_Basic")
446+
447+
def test_wms_GetLegendGraphic_BoxSpace(self):
448+
qs = "&".join(["%s=%s" % i for i in list({
449+
"MAP": urllib.quote(self.projectPath),
450+
"SERVICE": "WMS",
451+
"VERSION": "1.1.1",
452+
"REQUEST": "GetLegendGraphic",
453+
"LAYER": "Country,Hello",
454+
"LAYERTITLE": "FALSE",
455+
"BOXSPACE": "100",
456+
"FORMAT": "image/png",
457+
"HEIGHT": "500",
458+
"WIDTH": "500",
459+
"CRS": "EPSG:3857"
460+
}.items())])
461+
462+
r, h = self._result(self.server.handleRequest(qs))
463+
self._img_diff_error(r, h, "WMS_GetLegendGraphic_BoxSpace")
464+
465+
def test_wms_GetLegendGraphic_SymbolSpace(self):
466+
qs = "&".join(["%s=%s" % i for i in list({
467+
"MAP": urllib.quote(self.projectPath),
468+
"SERVICE": "WMS",
469+
"VERSION": "1.1.1",
470+
"REQUEST": "GetLegendGraphic",
471+
"LAYER": "Country,Hello",
472+
"LAYERTITLE": "FALSE",
473+
"SYMBOLSPACE": "100",
474+
"FORMAT": "image/png",
475+
"HEIGHT": "500",
476+
"WIDTH": "500",
477+
"CRS": "EPSG:3857"
478+
}.items())])
479+
480+
r, h = self._result(self.server.handleRequest(qs))
481+
self._img_diff_error(r, h, "WMS_GetLegendGraphic_SymbolSpace")
482+
483+
def test_wms_GetLegendGraphic_IconLabelSpace(self):
484+
qs = "&".join(["%s=%s" % i for i in list({
485+
"MAP": urllib.quote(self.projectPath),
486+
"SERVICE": "WMS",
487+
"VERSION": "1.1.1",
488+
"REQUEST": "GetLegendGraphic",
489+
"LAYER": "Country,Hello",
490+
"LAYERTITLE": "FALSE",
491+
"ICONLABELSPACE": "100",
492+
"FORMAT": "image/png",
493+
"HEIGHT": "500",
494+
"WIDTH": "500",
495+
"CRS": "EPSG:3857"
496+
}.items())])
497+
498+
r, h = self._result(self.server.handleRequest(qs))
499+
self._img_diff_error(r, h, "WMS_GetLegendGraphic_IconLabelSpace")
500+
501+
def test_wms_GetLegendGraphic_SymbolSize(self):
502+
qs = "&".join(["%s=%s" % i for i in list({
503+
"MAP": urllib.quote(self.projectPath),
504+
"SERVICE": "WMS",
505+
"VERSION": "1.1.1",
506+
"REQUEST": "GetLegendGraphic",
507+
"LAYER": "Country,Hello",
508+
"LAYERTITLE": "FALSE",
509+
"SYMBOLWIDTH": "50",
510+
"SYMBOLHEIGHT": "30",
511+
"FORMAT": "image/png",
512+
"HEIGHT": "500",
513+
"WIDTH": "500",
514+
"CRS": "EPSG:3857"
515+
}.items())])
516+
517+
r, h = self._result(self.server.handleRequest(qs))
518+
self._img_diff_error(r, h, "WMS_GetLegendGraphic_SymbolSize")
519+
520+
def test_wms_GetLegendGraphic_BBox(self):
521+
qs = "&".join(["%s=%s" % i for i in list({
522+
"MAP": urllib.quote(self.projectPath),
523+
"SERVICE": "WMS",
524+
"VERSION": "1.1.1",
525+
"REQUEST": "GetLegendGraphic",
526+
"LAYER": "Country,Hello,db_point",
527+
"LAYERTITLE": "FALSE",
528+
"FORMAT": "image/png",
529+
"HEIGHT": "500",
530+
"WIDTH": "500",
531+
"BBOX": "-151.7,-38.9,51.0,78.0",
532+
"CRS": "EPSG:4326"
533+
}.items())])
534+
535+
r, h = self._result(self.server.handleRequest(qs))
536+
self._img_diff_error(r, h, "WMS_GetLegendGraphic_BBox")
537+
538+
def test_wms_GetLegendGraphic_BBox2(self):
539+
qs = "&".join(["%s=%s" % i for i in list({
540+
"MAP": urllib.quote(self.projectPath),
541+
"SERVICE": "WMS",
542+
"VERSION": "1.1.1",
543+
"REQUEST": "GetLegendGraphic",
544+
"LAYER": "Country,Hello,db_point",
545+
"LAYERTITLE": "FALSE",
546+
"FORMAT": "image/png",
547+
"HEIGHT": "500",
548+
"WIDTH": "500",
549+
"BBOX": "-76.08,-6.4,-19.38,38.04",
550+
"SRS": "EPSG:4326"
551+
}.items())])
552+
553+
r, h = self._result(self.server.handleRequest(qs))
554+
self._img_diff_error(r, h, "WMS_GetLegendGraphic_BBox2")
555+
426556
def _result(self, data):
427557
headers = {}
428558
for line in data[0].decode('UTF-8').split("\n"):

0 commit comments

Comments
 (0)
Please sign in to comment.