Skip to content

Commit ec30c48

Browse files
authored
Merge pull request #9286 from signedav/scale_getlegendgraphic_backport-3_6
Backport 3.6 Scaled symbols on GetLegendGraphics
2 parents a7dfa9e + f29207a commit ec30c48

File tree

11 files changed

+591
-0
lines changed

11 files changed

+591
-0
lines changed

python/core/auto_generated/qgslegendsettings.sip.in

+18
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,24 @@ Sets the legend map ``scale``.
192192
The ``scale`` value indicates the scale denominator, e.g. 1000.0 for a 1:1000 map.
193193

194194
.. seealso:: :py:func:`mapScale`
195+
%End
196+
197+
double mapUnitsPerPixel() const;
198+
%Docstring
199+
Returns the factor of map units per pixel for symbols with size given in map units calculated by dpi and mmPerMapUnit
200+
201+
.. seealso:: :py:func:`setMapUnitsPerPixel`
202+
203+
.. versionadded:: 3.8
204+
%End
205+
206+
void setMapUnitsPerPixel( double mapUnitsPerPixel );
207+
%Docstring
208+
Sets the mmPerMapUnit calculated by ``mapUnitsPerPixel`` mostly taken from the map settings.
209+
210+
.. seealso:: :py:func:`mapUnitsPerPixel`
211+
212+
.. versionadded:: 3.8
195213
%End
196214

197215
int dpi() const;

src/core/qgslegendsettings.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ QgsLegendSettings::QgsLegendSettings()
3737
rstyle( QgsLegendStyle::SymbolLabel ).rfont().setPointSizeF( 12.0 );
3838
}
3939

40+
double QgsLegendSettings::mapUnitsPerPixel() const
41+
{
42+
return 1 / ( mMmPerMapUnit * ( mDpi / 25.4 ) );
43+
}
44+
45+
void QgsLegendSettings::setMapUnitsPerPixel( double mapUnitsPerPixel )
46+
{
47+
mMmPerMapUnit = 1 / mapUnitsPerPixel / ( mDpi / 25.4 );
48+
}
49+
4050
QStringList QgsLegendSettings::evaluateItemText( const QString &text, const QgsExpressionContext &context ) const
4151
{
4252
const QString textToRender = QgsExpression::replaceExpressionText( text, &context );

src/core/qgslegendsettings.h

+14
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,20 @@ class CORE_EXPORT QgsLegendSettings
178178
*/
179179
void setMapScale( double scale ) { mMapScale = scale; }
180180

181+
/**
182+
* Returns the factor of map units per pixel for symbols with size given in map units calculated by dpi and mmPerMapUnit
183+
* \see setMapUnitsPerPixel()
184+
* \since QGIS 3.8
185+
*/
186+
double mapUnitsPerPixel() const;
187+
188+
/**
189+
* Sets the mmPerMapUnit calculated by \a mapUnitsPerPixel mostly taken from the map settings.
190+
* \see mapUnitsPerPixel()
191+
* \since QGIS 3.8
192+
*/
193+
void setMapUnitsPerPixel( double mapUnitsPerPixel );
194+
181195
int dpi() const { return mDpi; }
182196
void setDpi( int dpi ) { mDpi = dpi; }
183197

src/server/services/wms/qgswmsrenderer.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,16 @@ namespace QgsWms
183183
std::unique_ptr<QImage> image;
184184
std::unique_ptr<QPainter> painter;
185185

186+
// getting scale from bbox
187+
if ( !mWmsParameters.bbox().isEmpty() )
188+
{
189+
QgsMapSettings mapSettings;
190+
image.reset( createImage( mWmsParameters.widthAsInt(), mWmsParameters.heightAsInt(), false ) );
191+
configureMapSettings( image.get(), mapSettings );
192+
legendSettings.setMapScale( mapSettings.scale() );
193+
legendSettings.setMapUnitsPerPixel( mapSettings.mapUnitsPerPixel() );
194+
}
195+
186196
if ( !mWmsParameters.rule().isEmpty() )
187197
{
188198
QString rule = mWmsParameters.rule();

tests/src/python/test_qgsserver_wms_getlegendgraphic.py

+100
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,106 @@ def test_wms_GetLegendGraphic_wmsRootName(self):
555555
self.assertEqual(-1, h.find(b'Content-Type: text/xml; charset=utf-8'), "Header: %s\nResponse:\n%s" % (h, r))
556556
self.assertNotEqual(-1, h.find(b'Content-Type: image/png'), "Header: %s\nResponse:\n%s" % (h, r))
557557

558+
def test_wms_GetLegendGraphic_ScaleSymbol_Min(self):
559+
# 1:500000000 min
560+
qs = "?" + "&".join(["%s=%s" % i for i in list({
561+
"MAP": self.testdata_path + 'test_project_scaledsymbols.qgs',
562+
"SERVICE": "WMS",
563+
"REQUEST": "GetLegendGraphic",
564+
"LAYER": "testlayer",
565+
"FORMAT": "image/png",
566+
"HEIGHT": "550",
567+
"WIDTH": "850",
568+
"BBOX": "-608.4,-1002.6,698.2,1019.0",
569+
"CRS": "EPSG:4326"
570+
}.items())])
571+
572+
r, h = self._result(self._execute_request(qs))
573+
self._img_diff_error(r, h, "WMS_GetLegendGraphic_ScaleSymbol_Min", max_size_diff=QSize(1, 1))
574+
575+
# 1:1000000000 min
576+
qs = "?" + "&".join(["%s=%s" % i for i in list({
577+
"MAP": self.testdata_path + 'test_project_scaledsymbols.qgs',
578+
"SERVICE": "WMS",
579+
"REQUEST": "GetLegendGraphic",
580+
"LAYER": "testlayer",
581+
"FORMAT": "image/png",
582+
"HEIGHT": "550",
583+
"WIDTH": "850",
584+
"BBOX": "-1261.7,-2013.5,1351.5,2029.9",
585+
"CRS": "EPSG:4326"
586+
}.items())])
587+
588+
r, h = self._result(self._execute_request(qs))
589+
self._img_diff_error(r, h, "WMS_GetLegendGraphic_ScaleSymbol_Min", max_size_diff=QSize(15, 15))
590+
591+
def test_wms_GetLegendGraphic_ScaleSymbol_Scaled_01(self):
592+
# 1:10000000 scaled
593+
qs = "?" + "&".join(["%s=%s" % i for i in list({
594+
"MAP": self.testdata_path + 'test_project_scaledsymbols.qgs',
595+
"SERVICE": "WMS",
596+
"REQUEST": "GetLegendGraphic",
597+
"LAYER": "testlayer",
598+
"FORMAT": "image/png",
599+
"HEIGHT": "550",
600+
"WIDTH": "850",
601+
"BBOX": "31.8,-12.0,58.0,28.4",
602+
"CRS": "EPSG:4326"
603+
}.items())])
604+
605+
r, h = self._result(self._execute_request(qs))
606+
self._img_diff_error(r, h, "WMS_GetLegendGraphic_ScaleSymbol_Scaled_01", max_size_diff=QSize(15, 15))
607+
608+
def test_wms_GetLegendGraphic_ScaleSymbol_Scaled_02(self):
609+
# 1:15000000 scaled
610+
qs = "?" + "&".join(["%s=%s" % i for i in list({
611+
"MAP": self.testdata_path + 'test_project_scaledsymbols.qgs',
612+
"SERVICE": "WMS",
613+
"REQUEST": "GetLegendGraphic",
614+
"LAYER": "testlayer",
615+
"FORMAT": "image/png",
616+
"HEIGHT": "550",
617+
"WIDTH": "850",
618+
"BBOX": "25.3,-22.1,64.5,38.5",
619+
"CRS": "EPSG:4326"
620+
}.items())])
621+
622+
r, h = self._result(self._execute_request(qs))
623+
self._img_diff_error(r, h, "WMS_GetLegendGraphic_ScaleSymbol_Scaled_02", max_size_diff=QSize(15, 15))
624+
625+
def test_wms_GetLegendGraphic_ScaleSymbol_Max(self):
626+
# 1:100000 max
627+
qs = "?" + "&".join(["%s=%s" % i for i in list({
628+
"MAP": self.testdata_path + 'test_project_scaledsymbols.qgs',
629+
"SERVICE": "WMS",
630+
"REQUEST": "GetLegendGraphic",
631+
"LAYER": "testlayer",
632+
"FORMAT": "image/png",
633+
"HEIGHT": "550",
634+
"WIDTH": "850",
635+
"BBOX": "44.8,8.0,45.0,8.4",
636+
"CRS": "EPSG:4326"
637+
}.items())])
638+
639+
r, h = self._result(self._execute_request(qs))
640+
self._img_diff_error(r, h, "WMS_GetLegendGraphic_ScaleSymbol_Max", max_size_diff=QSize(15, 15))
641+
642+
# 1:1000000 max
643+
qs = "?" + "&".join(["%s=%s" % i for i in list({
644+
"MAP": self.testdata_path + 'test_project_scaledsymbols.qgs',
645+
"SERVICE": "WMS",
646+
"REQUEST": "GetLegendGraphic",
647+
"LAYER": "testlayer",
648+
"FORMAT": "image/png",
649+
"HEIGHT": "550",
650+
"WIDTH": "850",
651+
"BBOX": "43.6,6.2,46.2,10.2",
652+
"CRS": "EPSG:4326"
653+
}.items())])
654+
655+
r, h = self._result(self._execute_request(qs))
656+
self._img_diff_error(r, h, "WMS_GetLegendGraphic_ScaleSymbol_Max", max_size_diff=QSize(15, 15))
657+
558658

559659
if __name__ == '__main__':
560660
unittest.main()

0 commit comments

Comments
 (0)