Skip to content

Commit 2b35644

Browse files
authored
Merge pull request #9726 from signedav/backport34_SRCWIDTH
[Backport release-3_4] of SRCHEIGHT/SRCWIDTH for GetLegendGraphic request
2 parents e445a25 + 95cfcf9 commit 2b35644

File tree

7 files changed

+164
-31
lines changed

7 files changed

+164
-31
lines changed

python/core/auto_generated/qgslegendsettings.sip.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ Returns the factor of map units per pixel for symbols with size given in map uni
200200

201201
.. seealso:: :py:func:`setMapUnitsPerPixel`
202202

203-
.. versionadded:: 3.8
203+
.. versionadded:: 3.4
204204
%End
205205

206206
void setMapUnitsPerPixel( double mapUnitsPerPixel );
@@ -209,7 +209,7 @@ Sets the mMmPerMapUnit calculated by ``mapUnitsPerPixel`` mostly taken from the
209209

210210
.. seealso:: :py:func:`mapUnitsPerPixel`
211211

212-
.. versionadded:: 3.8
212+
.. versionadded:: 3.4
213213
%End
214214

215215
int dpi() const;

src/core/qgslegendsettings.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,14 @@ class CORE_EXPORT QgsLegendSettings
180180
/**
181181
* Returns the factor of map units per pixel for symbols with size given in map units calculated by mDpi and mMmPerMapUnit
182182
* \see setMapUnitsPerPixel()
183-
* \since QGIS 3.8
183+
* \since QGIS 3.4
184184
*/
185185
double mapUnitsPerPixel() const;
186186

187187
/**
188188
* Sets the mMmPerMapUnit calculated by \a mapUnitsPerPixel mostly taken from the map settings.
189189
* \see mapUnitsPerPixel()
190-
* \since QGIS 3.8
190+
* \since QGIS 3.4
191191
*/
192192
void setMapUnitsPerPixel( double mapUnitsPerPixel );
193193

src/server/services/wms/qgswmsparameters.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,16 @@ namespace QgsWms
366366
QVariant( 0 ) );
367367
save( pWidth );
368368

369+
const QgsWmsParameter pSrcHeight( QgsWmsParameter::SRCHEIGHT,
370+
QVariant::Int,
371+
QVariant( 0 ) );
372+
save( pSrcHeight );
373+
374+
const QgsWmsParameter pSrcWidth( QgsWmsParameter::SRCWIDTH,
375+
QVariant::Int,
376+
QVariant( 0 ) );
377+
save( pSrcWidth );
378+
369379
const QgsWmsParameter pBbox( QgsWmsParameter::BBOX );
370380
save( pBbox );
371381

@@ -681,6 +691,26 @@ namespace QgsWms
681691
return mWmsParameters[ QgsWmsParameter::WIDTH ].toInt();
682692
}
683693

694+
QString QgsWmsParameters::srcHeight() const
695+
{
696+
return mWmsParameters[ QgsWmsParameter::SRCHEIGHT ].toString();
697+
}
698+
699+
QString QgsWmsParameters::srcWidth() const
700+
{
701+
return mWmsParameters[ QgsWmsParameter::SRCWIDTH ].toString();
702+
}
703+
704+
int QgsWmsParameters::srcHeightAsInt() const
705+
{
706+
return mWmsParameters[ QgsWmsParameter::SRCHEIGHT ].toInt();
707+
}
708+
709+
int QgsWmsParameters::srcWidthAsInt() const
710+
{
711+
return mWmsParameters[ QgsWmsParameter::SRCWIDTH ].toInt();
712+
}
713+
684714
QString QgsWmsParameters::dpi() const
685715
{
686716
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
@@ -168,7 +168,9 @@ namespace QgsWms
168168
GRID_INTERVAL_Y,
169169
WITH_GEOMETRY,
170170
WITH_MAPTIP,
171-
WMTVER
171+
WMTVER,
172+
SRCWIDTH,
173+
SRCHEIGHT
172174
};
173175
Q_ENUM( Name )
174176

@@ -376,6 +378,40 @@ namespace QgsWms
376378
*/
377379
int heightAsInt() const;
378380

381+
/**
382+
* Returns SRCWIDTH parameter or an empty string if not defined.
383+
* \returns srcWidth parameter
384+
* \since QGIS 3.4
385+
*/
386+
QString srcWidth() const;
387+
388+
/**
389+
* Returns SRCWIDTH parameter as an int or its default value if not
390+
* defined. An exception is raised if SRCWIDTH is defined and cannot be
391+
* converted.
392+
* \returns srcWidth parameter
393+
* \throws QgsBadRequestException
394+
* \since QGIS 3.4
395+
*/
396+
int srcWidthAsInt() const;
397+
398+
/**
399+
* Returns SRCHEIGHT parameter or an empty string if not defined.
400+
* \returns srcHeight parameter
401+
* \since QGIS 3.4
402+
*/
403+
QString srcHeight() const;
404+
405+
/**
406+
* Returns SRCHEIGHT parameter as an int or its default value if not
407+
* defined. An exception is raised if SRCHEIGHT is defined and cannot be
408+
* converted.
409+
* \returns srcHeight parameter
410+
* \throws QgsBadRequestException
411+
* \since QGIS 3.4
412+
*/
413+
int srcHeightAsInt() const;
414+
379415
/**
380416
* Returns VERSION parameter if defined or its default value.
381417
* \returns version

src/server/services/wms/qgswmsrenderer.cpp

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ namespace QgsWms
197197
if ( !mWmsParameters.bbox().isEmpty() )
198198
{
199199
QgsMapSettings mapSettings;
200-
image.reset( createImage( mWmsParameters.widthAsInt(), mWmsParameters.heightAsInt(), false ) );
200+
image.reset( createImage( width(), height(), false ) );
201201
configureMapSettings( image.get(), mapSettings );
202202
legendSettings.setMapScale( mapSettings.scale() );
203203
legendSettings.setMapUnitsPerPixel( mapSettings.mapUnitsPerPixel() );
@@ -1015,10 +1015,10 @@ namespace QgsWms
10151015
QImage *QgsRenderer::createImage( int width, int height, bool useBbox ) const
10161016
{
10171017
if ( width < 0 )
1018-
width = mWmsParameters.widthAsInt();
1018+
width = this->width();
10191019

10201020
if ( height < 0 )
1021-
height = mWmsParameters.heightAsInt();
1021+
height = this->height();
10221022

10231023
//Adapt width / height if the aspect ratio does not correspond with the BBOX.
10241024
//Required by WMS spec. 1.3.
@@ -1919,14 +1919,14 @@ namespace QgsWms
19191919
{
19201920
//test if maxWidth / maxHeight set and WIDTH / HEIGHT parameter is in the range
19211921
int wmsMaxWidth = QgsServerProjectUtils::wmsMaxWidth( *mProject );
1922-
int width = mWmsParameters.widthAsInt();
1922+
int width = this->width();
19231923
if ( wmsMaxWidth != -1 && width > wmsMaxWidth )
19241924
{
19251925
return false;
19261926
}
19271927

19281928
int wmsMaxHeight = QgsServerProjectUtils::wmsMaxHeight( *mProject );
1929-
int height = mWmsParameters.heightAsInt();
1929+
int height = this->height();
19301930
if ( wmsMaxHeight != -1 && height > wmsMaxHeight )
19311931
{
19321932
return false;
@@ -2986,8 +2986,8 @@ namespace QgsWms
29862986
// WIDTH / HEIGHT parameters. If not, the image has to be scaled (required
29872987
// by WMS spec)
29882988
QImage *scaledImage = nullptr;
2989-
int width = mWmsParameters.widthAsInt();
2990-
int height = mWmsParameters.heightAsInt();
2989+
int width = this->width();
2990+
int height = this->height();
29912991
if ( width != image->width() || height != image->height() )
29922992
{
29932993
scaledImage = new QImage( image->scaled( width, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation ) );
@@ -3210,4 +3210,21 @@ namespace QgsWms
32103210
return result;
32113211
}
32123212

3213+
int QgsRenderer::height() const
3214+
{
3215+
if ( ( mWmsParameters.request().compare( QStringLiteral( "GetLegendGraphic" ), Qt::CaseInsensitive ) == 0 ||
3216+
mWmsParameters.request().compare( QStringLiteral( "GetLegendGraphics" ), Qt::CaseInsensitive ) == 0 ) &&
3217+
mWmsParameters.srcHeightAsInt() > 0 )
3218+
return mWmsParameters.srcHeightAsInt();
3219+
return mWmsParameters.heightAsInt();
3220+
}
3221+
3222+
int QgsRenderer::width() const
3223+
{
3224+
if ( ( mWmsParameters.request().compare( QStringLiteral( "GetLegendGraphic" ), Qt::CaseInsensitive ) == 0 ||
3225+
mWmsParameters.request().compare( QStringLiteral( "GetLegendGraphics" ), Qt::CaseInsensitive ) == 0 ) &&
3226+
mWmsParameters.srcWidthAsInt() > 0 )
3227+
return mWmsParameters.srcWidthAsInt();
3228+
return mWmsParameters.widthAsInt();
3229+
}
32133230
} // namespace QgsWms

src/server/services/wms/qgswmsrenderer.h

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

294294
private:
295295

296+
/**
297+
* Returns QgsWmsParameter SRCWIDTH if it's a GetLegendGraphics request and otherwise HEIGHT parameter
298+
* \returns height parameter
299+
* \since QGIS 3.4
300+
*/
301+
int height() const;
302+
303+
/**
304+
* Returns QgsWmsParameter SRCWIDTH parameter if it's a GetLegendGraphics request and otherwise WIDTH parameter
305+
* \returns width parameter
306+
* \since QGIS 3.4
307+
*/
308+
int width() const;
309+
296310
const QgsWmsParameters &mWmsParameters;
297311

298312
#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
@@ -434,6 +434,42 @@ def test_wms_GetLegendGraphic_ItemFont(self):
434434
self._img_diff_error(r, h, "WMS_GetLegendGraphic_ItemFont", max_size_diff=QSize(1, 1))
435435

436436
def test_wms_GetLegendGraphic_BBox(self):
437+
qs = "?" + "&".join(["%s=%s" % i for i in list({
438+
"MAP": urllib.parse.quote(self.projectPath),
439+
"SERVICE": "WMS",
440+
"VERSION": "1.1.1",
441+
"REQUEST": "GetLegendGraphic",
442+
"LAYER": "Country,Hello,db_point",
443+
"LAYERTITLE": "FALSE",
444+
"FORMAT": "image/png",
445+
"SRCHEIGHT": "500",
446+
"SRCWIDTH": "500",
447+
"BBOX": "-151.7,-38.9,51.0,78.0",
448+
"CRS": "EPSG:4326"
449+
}.items())])
450+
451+
r, h = self._result(self._execute_request(qs))
452+
self._img_diff_error(r, h, "WMS_GetLegendGraphic_BBox")
453+
454+
def test_wms_GetLegendGraphic_BBox2(self):
455+
qs = "?" + "&".join(["%s=%s" % i for i in list({
456+
"MAP": urllib.parse.quote(self.projectPath),
457+
"SERVICE": "WMS",
458+
"VERSION": "1.1.1",
459+
"REQUEST": "GetLegendGraphic",
460+
"LAYER": "Country,Hello,db_point",
461+
"LAYERTITLE": "FALSE",
462+
"FORMAT": "image/png",
463+
"SRCHEIGHT": "500",
464+
"SRCWIDTH": "500",
465+
"BBOX": "-76.08,-6.4,-19.38,38.04",
466+
"SRS": "EPSG:4326"
467+
}.items())])
468+
469+
r, h = self._result(self._execute_request(qs))
470+
self._img_diff_error(r, h, "WMS_GetLegendGraphic_BBox2")
471+
472+
def test_wms_GetLegendGraphic_BBox_Fallback(self):
437473
qs = "?" + "&".join(["%s=%s" % i for i in list({
438474
"MAP": urllib.parse.quote(self.projectPath),
439475
"SERVICE": "WMS",
@@ -451,7 +487,7 @@ def test_wms_GetLegendGraphic_BBox(self):
451487
r, h = self._result(self._execute_request(qs))
452488
self._img_diff_error(r, h, "WMS_GetLegendGraphic_BBox")
453489

454-
def test_wms_GetLegendGraphic_BBox2(self):
490+
def test_wms_GetLegendGraphic_BBox2_Fallback(self):
455491
qs = "?" + "&".join(["%s=%s" % i for i in list({
456492
"MAP": urllib.parse.quote(self.projectPath),
457493
"SERVICE": "WMS",
@@ -477,8 +513,8 @@ def test_wms_GetLegendGraphic_EmptyLegend(self):
477513
"REQUEST": "GetLegendGraphic",
478514
"LAYER": "QGIS%20Server%20Hello%20World",
479515
"FORMAT": "image/png",
480-
"HEIGHT": "840",
481-
"WIDTH": "1226",
516+
"SRCHEIGHT": "840",
517+
"SRCWIDTH": "1226",
482518
"BBOX": "10.38450,-49.6370,73.8183,42.9461",
483519
"SRS": "EPSG:4326",
484520
"SCALE": "15466642"
@@ -499,8 +535,8 @@ def test_wms_GetLegendGraphic_wmsRootName(self):
499535
"REQUEST": "GetLegendGraphic",
500536
"LAYER": "QGIS%20Server%20-%20Grouped%20Layer",
501537
"FORMAT": "image/png",
502-
"HEIGHT": "840",
503-
"WIDTH": "1226",
538+
"SRCHEIGHT": "840",
539+
"SRCWIDTH": "1226",
504540
"BBOX": "609152,5808188,625492,5814318",
505541
"SRS": "EPSG:25832",
506542
"SCALE": "38976"
@@ -518,8 +554,8 @@ def test_wms_GetLegendGraphic_wmsRootName(self):
518554
"REQUEST": "GetLegendGraphic",
519555
"LAYER": "All_grouped_layers",
520556
"FORMAT": "image/png",
521-
"HEIGHT": "840",
522-
"WIDTH": "1226",
557+
"SRCHEIGHT": "840",
558+
"SRCWIDTH": "1226",
523559
"BBOX": "609152,5808188,625492,5814318",
524560
"SRS": "EPSG:25832",
525561
"SCALE": "38976"
@@ -537,8 +573,8 @@ def test_wms_GetLegendGraphic_ScaleSymbol_Min(self):
537573
"REQUEST": "GetLegendGraphic",
538574
"LAYER": "testlayer",
539575
"FORMAT": "image/png",
540-
"HEIGHT": "550",
541-
"WIDTH": "850",
576+
"SRCHEIGHT": "550",
577+
"SRCWIDTH": "850",
542578
"BBOX": "-608.4,-1002.6,698.2,1019.0",
543579
"CRS": "EPSG:4326"
544580
}.items())])
@@ -553,8 +589,8 @@ def test_wms_GetLegendGraphic_ScaleSymbol_Min(self):
553589
"REQUEST": "GetLegendGraphic",
554590
"LAYER": "testlayer",
555591
"FORMAT": "image/png",
556-
"HEIGHT": "550",
557-
"WIDTH": "850",
592+
"SRCHEIGHT": "550",
593+
"SRCWIDTH": "850",
558594
"BBOX": "-1261.7,-2013.5,1351.5,2029.9",
559595
"CRS": "EPSG:4326"
560596
}.items())])
@@ -570,8 +606,8 @@ def test_wms_GetLegendGraphic_ScaleSymbol_Scaled_01(self):
570606
"REQUEST": "GetLegendGraphic",
571607
"LAYER": "testlayer",
572608
"FORMAT": "image/png",
573-
"HEIGHT": "550",
574-
"WIDTH": "850",
609+
"SRCHEIGHT": "550",
610+
"SRCWIDTH": "850",
575611
"BBOX": "31.8,-12.0,58.0,28.4",
576612
"CRS": "EPSG:4326"
577613
}.items())])
@@ -587,8 +623,8 @@ def test_wms_GetLegendGraphic_ScaleSymbol_Scaled_02(self):
587623
"REQUEST": "GetLegendGraphic",
588624
"LAYER": "testlayer",
589625
"FORMAT": "image/png",
590-
"HEIGHT": "550",
591-
"WIDTH": "850",
626+
"SRCHEIGHT": "550",
627+
"SRCWIDTH": "850",
592628
"BBOX": "25.3,-22.1,64.5,38.5",
593629
"CRS": "EPSG:4326"
594630
}.items())])
@@ -604,8 +640,8 @@ def test_wms_GetLegendGraphic_ScaleSymbol_Max(self):
604640
"REQUEST": "GetLegendGraphic",
605641
"LAYER": "testlayer",
606642
"FORMAT": "image/png",
607-
"HEIGHT": "550",
608-
"WIDTH": "850",
643+
"SRCHEIGHT": "550",
644+
"SRCWIDTH": "850",
609645
"BBOX": "44.8,8.0,45.0,8.4",
610646
"CRS": "EPSG:4326"
611647
}.items())])
@@ -620,8 +656,8 @@ def test_wms_GetLegendGraphic_ScaleSymbol_Max(self):
620656
"REQUEST": "GetLegendGraphic",
621657
"LAYER": "testlayer",
622658
"FORMAT": "image/png",
623-
"HEIGHT": "550",
624-
"WIDTH": "850",
659+
"SRCHEIGHT": "550",
660+
"SRCWIDTH": "850",
625661
"BBOX": "43.6,6.2,46.2,10.2",
626662
"CRS": "EPSG:4326"
627663
}.items())])

0 commit comments

Comments
 (0)