Skip to content

Commit 51ab846

Browse files
authoredMar 27, 2019
Merge pull request #9545 from signedav/getlegendgraphics_HEIGHTWIDTH
SRCHEIGHT/SRCWIDTH for GetLegendGraphic request
2 parents 8e70c08 + cf595f9 commit 51ab846

File tree

5 files changed

+161
-27
lines changed

5 files changed

+161
-27
lines changed
 

‎src/server/services/wms/qgswmsparameters.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,16 @@ namespace QgsWms
363363
QVariant( 0 ) );
364364
save( pWidth );
365365

366+
const QgsWmsParameter pSrcHeight( QgsWmsParameter::SRCHEIGHT,
367+
QVariant::Int,
368+
QVariant( 0 ) );
369+
save( pSrcHeight );
370+
371+
const QgsWmsParameter pSrcWidth( QgsWmsParameter::SRCWIDTH,
372+
QVariant::Int,
373+
QVariant( 0 ) );
374+
save( pSrcWidth );
375+
366376
const QgsWmsParameter pBbox( QgsWmsParameter::BBOX );
367377
save( pBbox );
368378

@@ -686,6 +696,26 @@ namespace QgsWms
686696
return mWmsParameters[ QgsWmsParameter::WIDTH ].toInt();
687697
}
688698

699+
QString QgsWmsParameters::srcHeight() const
700+
{
701+
return mWmsParameters[ QgsWmsParameter::SRCHEIGHT ].toString();
702+
}
703+
704+
QString QgsWmsParameters::srcWidth() const
705+
{
706+
return mWmsParameters[ QgsWmsParameter::SRCWIDTH ].toString();
707+
}
708+
709+
int QgsWmsParameters::srcHeightAsInt() const
710+
{
711+
return mWmsParameters[ QgsWmsParameter::SRCHEIGHT ].toInt();
712+
}
713+
714+
int QgsWmsParameters::srcWidthAsInt() const
715+
{
716+
return mWmsParameters[ QgsWmsParameter::SRCWIDTH ].toInt();
717+
}
718+
689719
QString QgsWmsParameters::dpi() const
690720
{
691721
return mWmsParameters[ QgsWmsParameter::DPI ].toString();

‎src/server/services/wms/qgswmsparameters.h

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,9 @@ namespace QgsWms
176176
WITH_MAPTIP,
177177
WMTVER,
178178
ATLAS_PK,
179-
FORMAT_OPTIONS
179+
FORMAT_OPTIONS,
180+
SRCWIDTH,
181+
SRCHEIGHT
180182
};
181183
Q_ENUM( Name )
182184

@@ -390,6 +392,40 @@ namespace QgsWms
390392
*/
391393
int heightAsInt() const;
392394

395+
/**
396+
* Returns SRCWIDTH parameter or an empty string if not defined.
397+
* \returns srcWidth parameter
398+
* \since QGIS 3.8
399+
*/
400+
QString srcWidth() const;
401+
402+
/**
403+
* Returns SRCWIDTH parameter as an int or its default value if not
404+
* defined. An exception is raised if SRCWIDTH is defined and cannot be
405+
* converted.
406+
* \returns srcWidth parameter
407+
* \throws QgsBadRequestException
408+
* \since QGIS 3.8
409+
*/
410+
int srcWidthAsInt() const;
411+
412+
/**
413+
* Returns SRCHEIGHT parameter or an empty string if not defined.
414+
* \returns srcHeight parameter
415+
* \since QGIS 3.8
416+
*/
417+
QString srcHeight() const;
418+
419+
/**
420+
* Returns SRCHEIGHT parameter as an int or its default value if not
421+
* defined. An exception is raised if SRCHEIGHT is defined and cannot be
422+
* converted.
423+
* \returns srcHeight parameter
424+
* \throws QgsBadRequestException
425+
* \since QGIS 3.8
426+
*/
427+
int srcHeightAsInt() const;
428+
393429
/**
394430
* Returns VERSION parameter if defined or its default value.
395431
* \returns version

‎src/server/services/wms/qgswmsrenderer.cpp

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ namespace QgsWms
187187
if ( !mWmsParameters.bbox().isEmpty() )
188188
{
189189
QgsMapSettings mapSettings;
190-
image.reset( createImage( mWmsParameters.widthAsInt(), mWmsParameters.heightAsInt(), false ) );
190+
image.reset( createImage( width(), height(), false ) );
191191
configureMapSettings( image.get(), mapSettings );
192192
legendSettings.setMapScale( mapSettings.scale() );
193193
legendSettings.setMapUnitsPerPixel( mapSettings.mapUnitsPerPixel() );
@@ -1123,10 +1123,10 @@ namespace QgsWms
11231123
QImage *QgsRenderer::createImage( int width, int height, bool useBbox ) const
11241124
{
11251125
if ( width < 0 )
1126-
width = mWmsParameters.widthAsInt();
1126+
width = this->width();
11271127

11281128
if ( height < 0 )
1129-
height = mWmsParameters.heightAsInt();
1129+
height = this->height();
11301130

11311131
//Adapt width / height if the aspect ratio does not correspond with the BBOX.
11321132
//Required by WMS spec. 1.3.
@@ -1994,14 +1994,14 @@ namespace QgsWms
19941994
{
19951995
//test if maxWidth / maxHeight set and WIDTH / HEIGHT parameter is in the range
19961996
int wmsMaxWidth = QgsServerProjectUtils::wmsMaxWidth( *mProject );
1997-
int width = mWmsParameters.widthAsInt();
1997+
int width = this->width();
19981998
if ( wmsMaxWidth != -1 && width > wmsMaxWidth )
19991999
{
20002000
return false;
20012001
}
20022002

20032003
int wmsMaxHeight = QgsServerProjectUtils::wmsMaxHeight( *mProject );
2004-
int height = mWmsParameters.heightAsInt();
2004+
int height = this->height();
20052005
if ( wmsMaxHeight != -1 && height > wmsMaxHeight )
20062006
{
20072007
return false;
@@ -3206,8 +3206,8 @@ namespace QgsWms
32063206
// WIDTH / HEIGHT parameters. If not, the image has to be scaled (required
32073207
// by WMS spec)
32083208
QImage *scaledImage = nullptr;
3209-
int width = mWmsParameters.widthAsInt();
3210-
int height = mWmsParameters.heightAsInt();
3209+
int width = this->width();
3210+
int height = this->height();
32113211
if ( width != image->width() || height != image->height() )
32123212
{
32133213
scaledImage = new QImage( image->scaled( width, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation ) );
@@ -3418,4 +3418,22 @@ namespace QgsWms
34183418
}
34193419
}
34203420

3421+
int QgsRenderer::height() const
3422+
{
3423+
if ( ( mWmsParameters.request().compare( QStringLiteral( "GetLegendGraphic" ), Qt::CaseInsensitive ) == 0 ||
3424+
mWmsParameters.request().compare( QStringLiteral( "GetLegendGraphics" ), Qt::CaseInsensitive ) == 0 ) &&
3425+
mWmsParameters.srcHeightAsInt() > 0 )
3426+
return mWmsParameters.srcHeightAsInt();
3427+
return mWmsParameters.heightAsInt();
3428+
}
3429+
3430+
int QgsRenderer::width() const
3431+
{
3432+
if ( ( mWmsParameters.request().compare( QStringLiteral( "GetLegendGraphic" ), Qt::CaseInsensitive ) == 0 ||
3433+
mWmsParameters.request().compare( QStringLiteral( "GetLegendGraphics" ), Qt::CaseInsensitive ) == 0 ) &&
3434+
mWmsParameters.srcWidthAsInt() > 0 )
3435+
return mWmsParameters.srcWidthAsInt();
3436+
return mWmsParameters.widthAsInt();
3437+
}
3438+
34213439
} // namespace QgsWms

‎src/server/services/wms/qgswmsrenderer.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,20 @@ namespace QgsWms
297297

298298
void handlePrintErrors( const QgsLayout *layout ) const;
299299

300+
/**
301+
* Returns QgsWmsParameter SRCWIDTH if it's a GetLegendGraphics request and otherwise HEIGHT parameter
302+
* \returns height parameter
303+
* \since QGIS 3.8
304+
*/
305+
int height() const;
306+
307+
/**
308+
* Returns QgsWmsParameter SRCWIDTH parameter if it's a GetLegendGraphics request and otherwise WIDTH parameter
309+
* \returns width parameter
310+
* \since QGIS 3.8
311+
*/
312+
int width() const;
313+
300314
const QgsWmsParameters &mWmsParameters;
301315

302316
#ifdef HAVE_SERVER_PYTHON_PLUGINS

‎tests/src/python/test_qgsserver_wms_getlegendgraphic.py

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,42 @@ def test_wms_GetLegendGraphic_ItemFont(self):
460460
self._img_diff_error(r, h, "WMS_GetLegendGraphic_ItemFont", max_size_diff=QSize(1, 1))
461461

462462
def test_wms_GetLegendGraphic_BBox(self):
463+
qs = "?" + "&".join(["%s=%s" % i for i in list({
464+
"MAP": urllib.parse.quote(self.projectPath),
465+
"SERVICE": "WMS",
466+
"VERSION": "1.1.1",
467+
"REQUEST": "GetLegendGraphic",
468+
"LAYER": "Country,Hello,db_point",
469+
"LAYERTITLE": "FALSE",
470+
"FORMAT": "image/png",
471+
"SRCHEIGHT": "500",
472+
"SRCWIDTH": "500",
473+
"BBOX": "-151.7,-38.9,51.0,78.0",
474+
"CRS": "EPSG:4326"
475+
}.items())])
476+
477+
r, h = self._result(self._execute_request(qs))
478+
self._img_diff_error(r, h, "WMS_GetLegendGraphic_BBox")
479+
480+
def test_wms_GetLegendGraphic_BBox2(self):
481+
qs = "?" + "&".join(["%s=%s" % i for i in list({
482+
"MAP": urllib.parse.quote(self.projectPath),
483+
"SERVICE": "WMS",
484+
"VERSION": "1.1.1",
485+
"REQUEST": "GetLegendGraphic",
486+
"LAYER": "Country,Hello,db_point",
487+
"LAYERTITLE": "FALSE",
488+
"FORMAT": "image/png",
489+
"SRCHEIGHT": "500",
490+
"SRCWIDTH": "500",
491+
"BBOX": "-76.08,-6.4,-19.38,38.04",
492+
"SRS": "EPSG:4326"
493+
}.items())])
494+
495+
r, h = self._result(self._execute_request(qs))
496+
self._img_diff_error(r, h, "WMS_GetLegendGraphic_BBox2")
497+
498+
def test_wms_GetLegendGraphic_BBox_Fallback(self):
463499
qs = "?" + "&".join(["%s=%s" % i for i in list({
464500
"MAP": urllib.parse.quote(self.projectPath),
465501
"SERVICE": "WMS",
@@ -477,7 +513,7 @@ def test_wms_GetLegendGraphic_BBox(self):
477513
r, h = self._result(self._execute_request(qs))
478514
self._img_diff_error(r, h, "WMS_GetLegendGraphic_BBox")
479515

480-
def test_wms_GetLegendGraphic_BBox2(self):
516+
def test_wms_GetLegendGraphic_BBox2_Fallback(self):
481517
qs = "?" + "&".join(["%s=%s" % i for i in list({
482518
"MAP": urllib.parse.quote(self.projectPath),
483519
"SERVICE": "WMS",
@@ -503,8 +539,8 @@ def test_wms_GetLegendGraphic_EmptyLegend(self):
503539
"REQUEST": "GetLegendGraphic",
504540
"LAYER": "QGIS%20Server%20Hello%20World",
505541
"FORMAT": "image/png",
506-
"HEIGHT": "840",
507-
"WIDTH": "1226",
542+
"SRCHEIGHT": "840",
543+
"SRCWIDTH": "1226",
508544
"BBOX": "10.38450,-49.6370,73.8183,42.9461",
509545
"SRS": "EPSG:4326",
510546
"SCALE": "15466642"
@@ -525,8 +561,8 @@ def test_wms_GetLegendGraphic_wmsRootName(self):
525561
"REQUEST": "GetLegendGraphic",
526562
"LAYER": "QGIS%20Server%20-%20Grouped%20Layer",
527563
"FORMAT": "image/png",
528-
"HEIGHT": "840",
529-
"WIDTH": "1226",
564+
"SRCHEIGHT": "840",
565+
"SRCWIDTH": "1226",
530566
"BBOX": "609152,5808188,625492,5814318",
531567
"SRS": "EPSG:25832",
532568
"SCALE": "38976"
@@ -544,8 +580,8 @@ def test_wms_GetLegendGraphic_wmsRootName(self):
544580
"REQUEST": "GetLegendGraphic",
545581
"LAYER": "All_grouped_layers",
546582
"FORMAT": "image/png",
547-
"HEIGHT": "840",
548-
"WIDTH": "1226",
583+
"SRCHEIGHT": "840",
584+
"SRCWIDTH": "1226",
549585
"BBOX": "609152,5808188,625492,5814318",
550586
"SRS": "EPSG:25832",
551587
"SCALE": "38976"
@@ -563,8 +599,8 @@ def test_wms_GetLegendGraphic_ScaleSymbol_Min(self):
563599
"REQUEST": "GetLegendGraphic",
564600
"LAYER": "testlayer",
565601
"FORMAT": "image/png",
566-
"HEIGHT": "550",
567-
"WIDTH": "850",
602+
"SRCHEIGHT": "550",
603+
"SRCWIDTH": "850",
568604
"BBOX": "-608.4,-1002.6,698.2,1019.0",
569605
"CRS": "EPSG:4326"
570606
}.items())])
@@ -579,8 +615,8 @@ def test_wms_GetLegendGraphic_ScaleSymbol_Min(self):
579615
"REQUEST": "GetLegendGraphic",
580616
"LAYER": "testlayer",
581617
"FORMAT": "image/png",
582-
"HEIGHT": "550",
583-
"WIDTH": "850",
618+
"SRCHEIGHT": "550",
619+
"SRCWIDTH": "850",
584620
"BBOX": "-1261.7,-2013.5,1351.5,2029.9",
585621
"CRS": "EPSG:4326"
586622
}.items())])
@@ -596,8 +632,8 @@ def test_wms_GetLegendGraphic_ScaleSymbol_Scaled_01(self):
596632
"REQUEST": "GetLegendGraphic",
597633
"LAYER": "testlayer",
598634
"FORMAT": "image/png",
599-
"HEIGHT": "550",
600-
"WIDTH": "850",
635+
"SRCHEIGHT": "550",
636+
"SRCWIDTH": "850",
601637
"BBOX": "31.8,-12.0,58.0,28.4",
602638
"CRS": "EPSG:4326"
603639
}.items())])
@@ -613,8 +649,8 @@ def test_wms_GetLegendGraphic_ScaleSymbol_Scaled_02(self):
613649
"REQUEST": "GetLegendGraphic",
614650
"LAYER": "testlayer",
615651
"FORMAT": "image/png",
616-
"HEIGHT": "550",
617-
"WIDTH": "850",
652+
"SRCHEIGHT": "550",
653+
"SRCWIDTH": "850",
618654
"BBOX": "25.3,-22.1,64.5,38.5",
619655
"CRS": "EPSG:4326"
620656
}.items())])
@@ -630,8 +666,8 @@ def test_wms_GetLegendGraphic_ScaleSymbol_Max(self):
630666
"REQUEST": "GetLegendGraphic",
631667
"LAYER": "testlayer",
632668
"FORMAT": "image/png",
633-
"HEIGHT": "550",
634-
"WIDTH": "850",
669+
"SRCHEIGHT": "550",
670+
"SRCWIDTH": "850",
635671
"BBOX": "44.8,8.0,45.0,8.4",
636672
"CRS": "EPSG:4326"
637673
}.items())])
@@ -646,8 +682,8 @@ def test_wms_GetLegendGraphic_ScaleSymbol_Max(self):
646682
"REQUEST": "GetLegendGraphic",
647683
"LAYER": "testlayer",
648684
"FORMAT": "image/png",
649-
"HEIGHT": "550",
650-
"WIDTH": "850",
685+
"SRCHEIGHT": "550",
686+
"SRCWIDTH": "850",
651687
"BBOX": "43.6,6.2,46.2,10.2",
652688
"CRS": "EPSG:4326"
653689
}.items())])

0 commit comments

Comments
 (0)
Please sign in to comment.