Skip to content

Commit

Permalink
fix getLegendGraphics when BBox parameter is used
Browse files Browse the repository at this point in the history
  • Loading branch information
pblottiere committed Dec 12, 2016
1 parent f3a5c98 commit 8cda1bb
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 58 deletions.
1 change: 0 additions & 1 deletion src/server/qgswmsprojectparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include "qgsmaplayerstylemanager.h"
#include "qgsmapserviceexception.h"
#include "qgspallabeling.h"
#include "qgsproject.h"
#include "qgsrenderer.h"
#include "qgsvectorlayer.h"

Expand Down
46 changes: 24 additions & 22 deletions src/server/qgswmsserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -847,8 +847,6 @@ QImage* QgsWmsServer::getLegendGraphics()
}
QgsLayerTreeModel legendModel( &rootGroup );

QList<QgsLayerTreeNode*> rootChildren = rootGroup.children();

if ( scaleDenominator > 0 )
legendModel.setLegendFilterByScale( scaleDenominator );

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 );
Expand All @@ -939,6 +937,7 @@ QImage* QgsWmsServer::getLegendGraphics()
return paintImage;
}

QList<QgsLayerTreeNode*> rootChildren = rootGroup.children();
Q_FOREACH ( QgsLayerTreeNode* node, rootChildren )
{
if ( QgsLayerTree::isLayer( node ) )
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -1422,7 +1421,7 @@ QImage* QgsWmsServer::getMap( QgsMapSettings& mapSettings, HitTest* hitTest )
QStringList highlightLayersId = QgsWmsConfigParser::addHighlightLayers( mParameters, layerSetIds );

QList<QgsMapLayer *> layerSet;
Q_FOREACH( QString layerSetId, layerSetIds )
Q_FOREACH ( QString layerSetId, layerSetIds )
{
layerSet.append( QgsProject::instance()->mapLayer( layerSetId ) );
}
Expand Down Expand Up @@ -1967,7 +1966,7 @@ QImage* QgsWmsServer::initializeRendering( QStringList& layersList, QStringList&
#endif

QList<QgsMapLayer *> layers;
Q_FOREACH( QString layerId, layerIdList )
Q_FOREACH ( QString layerId, layerIdList )
{
layers.append( QgsProject::instance()->mapLayer( layerId ) );
}
Expand All @@ -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;

Expand All @@ -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;
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/server/qgswmsserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
69 changes: 35 additions & 34 deletions tests/src/python/test_qgsserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -781,40 +781,41 @@ def test_wms_GetLegendGraphic_SymbolSize(self):
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_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_BBox2")
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_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 = {}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8cda1bb

Please sign in to comment.