Skip to content

Commit 8ba90fc

Browse files
authored
Merge pull request #9287 from signedav/scale_getlegendgraphic_backport-3_4
Backport 3.4 Scaled symbols on GetLegendGraphics
2 parents fb81ce6 + 15585ce commit 8ba90fc

File tree

10 files changed

+591
-0
lines changed

10 files changed

+591
-0
lines changed

python/core/auto_generated/qgslegendsettings.sip.in

Lines changed: 18 additions & 0 deletions
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 mDpi and mMmPerMapUnit
200+
201+
.. seealso:: :py:func:`setMapUnitsPerPixel`
202+
203+
.. versionadded:: 3.8
204+
%End
205+
206+
void setMapUnitsPerPixel( double mapUnitsPerPixel );
207+
%Docstring
208+
Sets the mMmPerMapUnit 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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ QgsLegendSettings::QgsLegendSettings()
3535
rstyle( QgsLegendStyle::SymbolLabel ).rfont().setPointSizeF( 12.0 );
3636
}
3737

38+
double QgsLegendSettings::mapUnitsPerPixel() const
39+
{
40+
return 1 / ( mMmPerMapUnit * ( mDpi / 25.4 ) );
41+
}
42+
43+
void QgsLegendSettings::setMapUnitsPerPixel( double mapUnitsPerPixel )
44+
{
45+
mMmPerMapUnit = 1 / mapUnitsPerPixel / ( mDpi / 25.4 );
46+
}
47+
3848
QStringList QgsLegendSettings::splitStringForWrapping( const QString &stringToSplt ) const
3949
{
4050
QStringList list;

src/core/qgslegendsettings.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,20 @@ class CORE_EXPORT QgsLegendSettings
177177
*/
178178
void setMapScale( double scale ) { mMapScale = scale; }
179179

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

src/server/services/wms/qgswmsrenderer.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,16 @@ namespace QgsWms
193193
std::unique_ptr<QImage> image;
194194
std::unique_ptr<QPainter> painter;
195195

196+
// getting scale from bbox
197+
if ( !mWmsParameters.bbox().isEmpty() )
198+
{
199+
QgsMapSettings mapSettings;
200+
image.reset( createImage( mWmsParameters.widthAsInt(), mWmsParameters.heightAsInt(), false ) );
201+
configureMapSettings( image.get(), mapSettings );
202+
legendSettings.setMapScale( mapSettings.scale() );
203+
legendSettings.setMapUnitsPerPixel( mapSettings.mapUnitsPerPixel() );
204+
}
205+
196206
if ( !mWmsParameters.rule().isEmpty() )
197207
{
198208
QString rule = mWmsParameters.rule();

tests/src/python/test_qgsserver_wms_getlegendgraphic.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,106 @@ def test_wms_GetLegendGraphic_wmsRootName(self):
529529
self.assertEqual(-1, h.find(b'Content-Type: text/xml; charset=utf-8'), "Header: %s\nResponse:\n%s" % (h, r))
530530
self.assertNotEqual(-1, h.find(b'Content-Type: image/png'), "Header: %s\nResponse:\n%s" % (h, r))
531531

532+
def test_wms_GetLegendGraphic_ScaleSymbol_Min(self):
533+
# 1:500000000 min
534+
qs = "?" + "&".join(["%s=%s" % i for i in list({
535+
"MAP": self.testdata_path + 'test_project_scaledsymbols.qgs',
536+
"SERVICE": "WMS",
537+
"REQUEST": "GetLegendGraphic",
538+
"LAYER": "testlayer",
539+
"FORMAT": "image/png",
540+
"HEIGHT": "550",
541+
"WIDTH": "850",
542+
"BBOX": "-608.4,-1002.6,698.2,1019.0",
543+
"CRS": "EPSG:4326"
544+
}.items())])
545+
546+
r, h = self._result(self._execute_request(qs))
547+
self._img_diff_error(r, h, "WMS_GetLegendGraphic_ScaleSymbol_Min", max_size_diff=QSize(1, 1))
548+
549+
# 1:1000000000 min
550+
qs = "?" + "&".join(["%s=%s" % i for i in list({
551+
"MAP": self.testdata_path + 'test_project_scaledsymbols.qgs',
552+
"SERVICE": "WMS",
553+
"REQUEST": "GetLegendGraphic",
554+
"LAYER": "testlayer",
555+
"FORMAT": "image/png",
556+
"HEIGHT": "550",
557+
"WIDTH": "850",
558+
"BBOX": "-1261.7,-2013.5,1351.5,2029.9",
559+
"CRS": "EPSG:4326"
560+
}.items())])
561+
562+
r, h = self._result(self._execute_request(qs))
563+
self._img_diff_error(r, h, "WMS_GetLegendGraphic_ScaleSymbol_Min", max_size_diff=QSize(15, 15))
564+
565+
def test_wms_GetLegendGraphic_ScaleSymbol_Scaled_01(self):
566+
# 1:10000000 scaled
567+
qs = "?" + "&".join(["%s=%s" % i for i in list({
568+
"MAP": self.testdata_path + 'test_project_scaledsymbols.qgs',
569+
"SERVICE": "WMS",
570+
"REQUEST": "GetLegendGraphic",
571+
"LAYER": "testlayer",
572+
"FORMAT": "image/png",
573+
"HEIGHT": "550",
574+
"WIDTH": "850",
575+
"BBOX": "31.8,-12.0,58.0,28.4",
576+
"CRS": "EPSG:4326"
577+
}.items())])
578+
579+
r, h = self._result(self._execute_request(qs))
580+
self._img_diff_error(r, h, "WMS_GetLegendGraphic_ScaleSymbol_Scaled_01", max_size_diff=QSize(15, 15))
581+
582+
def test_wms_GetLegendGraphic_ScaleSymbol_Scaled_02(self):
583+
# 1:15000000 scaled
584+
qs = "?" + "&".join(["%s=%s" % i for i in list({
585+
"MAP": self.testdata_path + 'test_project_scaledsymbols.qgs',
586+
"SERVICE": "WMS",
587+
"REQUEST": "GetLegendGraphic",
588+
"LAYER": "testlayer",
589+
"FORMAT": "image/png",
590+
"HEIGHT": "550",
591+
"WIDTH": "850",
592+
"BBOX": "25.3,-22.1,64.5,38.5",
593+
"CRS": "EPSG:4326"
594+
}.items())])
595+
596+
r, h = self._result(self._execute_request(qs))
597+
self._img_diff_error(r, h, "WMS_GetLegendGraphic_ScaleSymbol_Scaled_02", max_size_diff=QSize(15, 15))
598+
599+
def test_wms_GetLegendGraphic_ScaleSymbol_Max(self):
600+
# 1:100000 max
601+
qs = "?" + "&".join(["%s=%s" % i for i in list({
602+
"MAP": self.testdata_path + 'test_project_scaledsymbols.qgs',
603+
"SERVICE": "WMS",
604+
"REQUEST": "GetLegendGraphic",
605+
"LAYER": "testlayer",
606+
"FORMAT": "image/png",
607+
"HEIGHT": "550",
608+
"WIDTH": "850",
609+
"BBOX": "44.8,8.0,45.0,8.4",
610+
"CRS": "EPSG:4326"
611+
}.items())])
612+
613+
r, h = self._result(self._execute_request(qs))
614+
self._img_diff_error(r, h, "WMS_GetLegendGraphic_ScaleSymbol_Max", max_size_diff=QSize(15, 15))
615+
616+
# 1:1000000 max
617+
qs = "?" + "&".join(["%s=%s" % i for i in list({
618+
"MAP": self.testdata_path + 'test_project_scaledsymbols.qgs',
619+
"SERVICE": "WMS",
620+
"REQUEST": "GetLegendGraphic",
621+
"LAYER": "testlayer",
622+
"FORMAT": "image/png",
623+
"HEIGHT": "550",
624+
"WIDTH": "850",
625+
"BBOX": "43.6,6.2,46.2,10.2",
626+
"CRS": "EPSG:4326"
627+
}.items())])
628+
629+
r, h = self._result(self._execute_request(qs))
630+
self._img_diff_error(r, h, "WMS_GetLegendGraphic_ScaleSymbol_Max", max_size_diff=QSize(15, 15))
631+
532632

533633
if __name__ == '__main__':
534634
unittest.main()

0 commit comments

Comments
 (0)