diff --git a/src/server/qgswmsprojectparser.cpp b/src/server/qgswmsprojectparser.cpp index 1000cef4737..72bab364e4f 100644 --- a/src/server/qgswmsprojectparser.cpp +++ b/src/server/qgswmsprojectparser.cpp @@ -24,7 +24,6 @@ #include "qgsmaplayerstylemanager.h" #include "qgsmapserviceexception.h" #include "qgspallabeling.h" -#include "qgsproject.h" #include "qgsrenderer.h" #include "qgsvectorlayer.h" diff --git a/src/server/qgswmsserver.cpp b/src/server/qgswmsserver.cpp index 1ee1c64817f..c15d99b14ee 100644 --- a/src/server/qgswmsserver.cpp +++ b/src/server/qgswmsserver.cpp @@ -847,8 +847,6 @@ QImage* QgsWmsServer::getLegendGraphics() } QgsLayerTreeModel legendModel( &rootGroup ); - QList rootChildren = rootGroup.children(); - if ( scaleDenominator > 0 ) legendModel.setLegendFilterByScale( scaleDenominator ); @@ -889,7 +887,7 @@ QImage* QgsWmsServer::getLegendGraphics() } // find out DPI - QImage* tmpImage = createImage( 1, 1 ); + QImage* tmpImage = createImage( 1, 1, false ); if ( !tmpImage ) return nullptr; qreal dpmm = tmpImage->dotsPerMeterX() / 1000.0; @@ -917,7 +915,7 @@ QImage* QgsWmsServer::getLegendGraphics() if ( !rule.isEmpty() ) { //create second image with the right dimensions - QImage* paintImage = createImage( ruleSymbolWidth, ruleSymbolHeight ); + QImage* paintImage = createImage( ruleSymbolWidth, ruleSymbolHeight, false ); //go through the items a second time for painting QPainter p( paintImage ); @@ -939,6 +937,7 @@ QImage* QgsWmsServer::getLegendGraphics() return paintImage; } + QList rootChildren = rootGroup.children(); Q_FOREACH ( QgsLayerTreeNode* node, rootChildren ) { if ( QgsLayerTree::isLayer( node ) ) @@ -978,7 +977,7 @@ QImage* QgsWmsServer::getLegendGraphics() QSizeF minSize = legendRenderer.minimumSize(); QSize s( minSize.width() * dpmm, minSize.height() * dpmm ); - QImage* paintImage = createImage( s.width(), s.height() ); + QImage* paintImage = createImage( s.width(), s.height(), false ); QPainter p( paintImage ); p.setRenderHint( QPainter::Antialiasing, true ); @@ -1422,7 +1421,7 @@ QImage* QgsWmsServer::getMap( QgsMapSettings& mapSettings, HitTest* hitTest ) QStringList highlightLayersId = QgsWmsConfigParser::addHighlightLayers( mParameters, layerSetIds ); QList layerSet; - Q_FOREACH( QString layerSetId, layerSetIds ) + Q_FOREACH ( QString layerSetId, layerSetIds ) { layerSet.append( QgsProject::instance()->mapLayer( layerSetId ) ); } @@ -1967,7 +1966,7 @@ QImage* QgsWmsServer::initializeRendering( QStringList& layersList, QStringList& #endif QList layers; - Q_FOREACH( QString layerId, layerIdList ) + Q_FOREACH ( QString layerId, layerIdList ) { layers.append( QgsProject::instance()->mapLayer( layerId ) ); } @@ -1979,7 +1978,7 @@ QImage* QgsWmsServer::initializeRendering( QStringList& layersList, QStringList& return theImage; } -QImage* QgsWmsServer::createImage( int width, int height ) const +QImage* QgsWmsServer::createImage( int width, int height, bool useBbox ) const { bool conversionSuccess; @@ -2001,23 +2000,26 @@ 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 ) + if ( useBbox ) { - double mapWidthHeightRatio = mapExtent.width() / mapExtent.height(); - double imageWidthHeightRatio = ( double )width / ( double )height; - if ( !qgsDoubleNear( mapWidthHeightRatio, imageWidthHeightRatio, 0.0001 ) ) + bool bboxOk; + QgsRectangle mapExtent = _parseBBOX( mParameters.value( "BBOX" ), bboxOk ); + if ( bboxOk ) { - if ( mapWidthHeightRatio >= imageWidthHeightRatio ) + double mapWidthHeightRatio = mapExtent.width() / mapExtent.height(); + double imageWidthHeightRatio = ( double )width / ( double )height; + if ( !qgsDoubleNear( mapWidthHeightRatio, imageWidthHeightRatio, 0.0001 ) ) { - //decrease image height - height = width / mapWidthHeightRatio; - } - else - { - //decrease image width - width = height * mapWidthHeightRatio; + if ( mapWidthHeightRatio >= imageWidthHeightRatio ) + { + //decrease image height + height = width / mapWidthHeightRatio; + } + else + { + //decrease image width + width = height * mapWidthHeightRatio; + } } } } diff --git a/src/server/qgswmsserver.h b/src/server/qgswmsserver.h index 7a1c0a09542..ec04e2b8ac9 100644 --- a/src/server/qgswmsserver.h +++ b/src/server/qgswmsserver.h @@ -136,8 +136,9 @@ class QgsWmsServer: public QgsOWSServer /** Creates a QImage from the HEIGHT and WIDTH parameters @param width image width (or -1 if width should be taken from WIDTH wms parameter) @param height image height (or -1 if height should be taken from HEIGHT wms parameter) + @param useBbox flag to indicate if the BBOX has to be used to adapt aspect ratio @return 0 in case of error*/ - QImage* createImage( int width = -1, int height = -1 ) const; + QImage* createImage( int width = -1, int height = -1, bool useBbox = true ) const; /** Configures mapSettings to the parameters HEIGHT, WIDTH, BBOX, CRS. diff --git a/tests/src/python/test_qgsserver.py b/tests/src/python/test_qgsserver.py index 1fda2d39697..60e8898a3b4 100644 --- a/tests/src/python/test_qgsserver.py +++ b/tests/src/python/test_qgsserver.py @@ -781,40 +781,41 @@ class TestQgsServer(unittest.TestCase): r, h = self._result(self.server.handleRequest(qs)) self._img_diff_error(r, h, "WMS_GetLegendGraphic_SymbolSize") - #def test_wms_GetLegendGraphic_BBox(self): - # qs = "&".join(["%s=%s" % i for i in list({ - # "MAP": urllib.parse.quote(self.projectPath), - # "SERVICE": "WMS", - # "VERSION": "1.1.1", - # "REQUEST": "GetLegendGraphic", - # "LAYER": "Country,Hello,db_point", - # "LAYERTITLE": "FALSE", - # "FORMAT": "image/png", - # "HEIGHT": "500", - # "WIDTH": "500", - # "BBOX": "-151.7,-38.9,51.0,78.0", - # "CRS": "EPSG:4326" - # }.items())]) - # r, h = self._result(self.server.handleRequest(qs)) - # self._img_diff_error(r, h, "WMS_GetLegendGraphic_BBox") + def test_wms_GetLegendGraphic_BBox(self): + qs = "&".join(["%s=%s" % i for i in list({ + "MAP": urllib.parse.quote(self.projectPath), + "SERVICE": "WMS", + "VERSION": "1.1.1", + "REQUEST": "GetLegendGraphic", + "LAYER": "Country,Hello,db_point", + "LAYERTITLE": "FALSE", + "FORMAT": "image/png", + "HEIGHT": "500", + "WIDTH": "500", + "BBOX": "-151.7,-38.9,51.0,78.0", + "CRS": "EPSG:4326" + }.items())]) - #def test_wms_GetLegendGraphic_BBox2(self): - # qs = "&".join(["%s=%s" % i for i in list({ - # "MAP": urllib.parse.quote(self.projectPath), - # "SERVICE": "WMS", - # "VERSION": "1.1.1", - # "REQUEST": "GetLegendGraphic", - # "LAYER": "Country,Hello,db_point", - # "LAYERTITLE": "FALSE", - # "FORMAT": "image/png", - # "HEIGHT": "500", - # "WIDTH": "500", - # #"BBOX": "-76.08,38.04,109.95,-6.4", - # "SRS": "EPSG:4326" - # }.items())]) + r, h = self._result(self.server.handleRequest(qs)) + self._img_diff_error(r, h, "WMS_GetLegendGraphic_BBox") - # r, h = self._result(self.server.handleRequest(qs)) - # self._img_diff_error(r, h, "WMS_GetLegendGraphic_BBox2") + def test_wms_GetLegendGraphic_BBox2(self): + qs = "&".join(["%s=%s" % i for i in list({ + "MAP": urllib.parse.quote(self.projectPath), + "SERVICE": "WMS", + "VERSION": "1.1.1", + "REQUEST": "GetLegendGraphic", + "LAYER": "Country,Hello,db_point", + "LAYERTITLE": "FALSE", + "FORMAT": "image/png", + "HEIGHT": "500", + "WIDTH": "500", + "BBOX": "-76.08,-6.4,-19.38,38.04", + "SRS": "EPSG:4326" + }.items())]) + + r, h = self._result(self.server.handleRequest(qs)) + self._img_diff_error(r, h, "WMS_GetLegendGraphic_BBox2") def _result(self, data): headers = {} diff --git a/tests/testdata/control_images/qgis_server/WMS_GetLegendGraphic_BBox/WMS_GetLegendGraphic_BBox.png b/tests/testdata/control_images/qgis_server/WMS_GetLegendGraphic_BBox/WMS_GetLegendGraphic_BBox.png index 22210c4ec57..607250a5864 100644 Binary files a/tests/testdata/control_images/qgis_server/WMS_GetLegendGraphic_BBox/WMS_GetLegendGraphic_BBox.png and b/tests/testdata/control_images/qgis_server/WMS_GetLegendGraphic_BBox/WMS_GetLegendGraphic_BBox.png differ diff --git a/tests/testdata/control_images/qgis_server/WMS_GetLegendGraphic_BBox2/WMS_GetLegendGraphic_BBox2.png b/tests/testdata/control_images/qgis_server/WMS_GetLegendGraphic_BBox2/WMS_GetLegendGraphic_BBox2.png index 607250a5864..22210c4ec57 100644 Binary files a/tests/testdata/control_images/qgis_server/WMS_GetLegendGraphic_BBox2/WMS_GetLegendGraphic_BBox2.png and b/tests/testdata/control_images/qgis_server/WMS_GetLegendGraphic_BBox2/WMS_GetLegendGraphic_BBox2.png differ