Skip to content
Permalink
Browse files

Allow specifying width and height for GetPrint images

... to override layout provided width and height
  • Loading branch information
m-kuhn committed Mar 22, 2021
1 parent 74733e0 commit 75c1f780c3a761b5b831f72b8a407ee9298945f7
@@ -495,9 +495,35 @@ namespace QgsWms
exportSettings.flags |= QgsLayoutRenderContext::FlagDrawSelection;
// Destination image size in px
QgsLayoutSize layoutSize( layout->pageCollection()->page( 0 )->sizeWithUnits() );

QgsLayoutMeasurement width( layout->convertFromLayoutUnits( layoutSize.width(), QgsUnitTypes::LayoutUnit::LayoutMillimeters ) );
QgsLayoutMeasurement height( layout->convertFromLayoutUnits( layoutSize.height(), QgsUnitTypes::LayoutUnit::LayoutMillimeters ) );
exportSettings.imageSize = QSize( static_cast<int>( width.length() * dpi / 25.4 ), static_cast<int>( height.length() * dpi / 25.4 ) );

const QSize imageSize = QSize( static_cast<int>( width.length() * dpi / 25.4 ), static_cast<int>( height.length() * dpi / 25.4 ) );

const QString paramWidth = mWmsParameters.width();
const QString paramHeight = mWmsParameters.height();

// Prefer width and height from the http request
// Fallback to predefined values from layout
// Preserve aspect ratio if only one value is specified
if ( !paramWidth.isEmpty() && !paramHeight.isEmpty() )
{
exportSettings.imageSize = QSize( paramWidth.toInt(), paramHeight.toInt() );
}
else if ( !paramWidth.isEmpty() && paramHeight.isEmpty() )
{
exportSettings.imageSize = QSize( paramWidth.toInt(), static_cast<double>( paramWidth.toInt() ) / imageSize.width() * imageSize.height() );
}
else if ( paramWidth.isEmpty() && !paramHeight.isEmpty() )
{
exportSettings.imageSize = QSize( static_cast<double>( paramHeight.toInt() ) / imageSize.height() * imageSize.width(), paramHeight.toInt() );
}
else
{
exportSettings.imageSize = imageSize;
}

// Export first page only (unless it's a pdf, see below)
exportSettings.pages.append( 0 );
if ( atlas )
@@ -25,6 +25,8 @@

from test_qgsserver import QgsServerTestBase

from qgis.PyQt.QtCore import QSize


class TestQgsServerWMSGetPrint(QgsServerTestBase):
"""QGIS Server WMS Tests for GetPrint request"""
@@ -274,6 +276,24 @@ def test_wms_getprint_scale(self):
r, h = self._result(self._execute_request(qs))
self._img_diff_error(r, h, "WMS_GetPrint_Scale")

def test_wms_getprint_size(self):
qs = "?" + "&".join(["%s=%s" % i for i in list({
"MAP": urllib.parse.quote(self.projectPath),
"SERVICE": "WMS",
"VERSION": "1.1.1",
"REQUEST": "GetPrint",
"TEMPLATE": "layoutA4",
"FORMAT": "png",
"map0:EXTENT": "-33626185.498,-13032965.185,33978427.737,16020257.031",
"map0:LAYERS": "Country,Hello",
"map0:SCALE": "36293562",
"CRS": "EPSG:3857",
"HEIGHT": "100"
}.items())])

r, h = self._result(self._execute_request(qs))
self._img_diff_error(r, h, "WMS_GetPrint_Size", max_size_diff=QSize(1, 1))

def test_wms_getprint_grid(self):
qs = "?" + "&".join(["%s=%s" % i for i in list({
"MAP": urllib.parse.quote(self.projectPath),
Binary file not shown.

0 comments on commit 75c1f78

Please sign in to comment.