Skip to content

Commit

Permalink
backport of SRCWIDTH/SRCHEIGHT
Browse files Browse the repository at this point in the history
it takes these values as map size in case of GetLegendGraphics Request and still HEIGHT and WIDTH if not a GetLegendGraphics Request

cherry-picked commits 4c667ad e184772 6722ad5 61a89af$
  • Loading branch information
signedav committed Apr 5, 2019
1 parent 76efbc6 commit 2366db8
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 28 deletions.
30 changes: 30 additions & 0 deletions src/server/services/wms/qgswmsparameters.cpp
Expand Up @@ -359,6 +359,16 @@ namespace QgsWms
QVariant( 0 ) );
save( pWidth );

const QgsWmsParameter pSrcHeight( QgsWmsParameter::SRCHEIGHT,
QVariant::Int,
QVariant( 0 ) );
save( pSrcHeight );

const QgsWmsParameter pSrcWidth( QgsWmsParameter::SRCWIDTH,
QVariant::Int,
QVariant( 0 ) );
save( pSrcWidth );

const QgsWmsParameter pBbox( QgsWmsParameter::BBOX );
save( pBbox );

Expand Down Expand Up @@ -678,6 +688,26 @@ namespace QgsWms
return mWmsParameters[ QgsWmsParameter::WIDTH ].toInt();
}

QString QgsWmsParameters::srcHeight() const
{
return mWmsParameters[ QgsWmsParameter::SRCHEIGHT ].toString();
}

QString QgsWmsParameters::srcWidth() const
{
return mWmsParameters[ QgsWmsParameter::SRCWIDTH ].toString();
}

int QgsWmsParameters::srcHeightAsInt() const
{
return mWmsParameters[ QgsWmsParameter::SRCHEIGHT ].toInt();
}

int QgsWmsParameters::srcWidthAsInt() const
{
return mWmsParameters[ QgsWmsParameter::SRCWIDTH ].toInt();
}

QString QgsWmsParameters::dpi() const
{
return mWmsParameters[ QgsWmsParameter::DPI ].toString();
Expand Down
38 changes: 37 additions & 1 deletion src/server/services/wms/qgswmsparameters.h
Expand Up @@ -167,7 +167,9 @@ namespace QgsWms
WITH_GEOMETRY,
WITH_MAPTIP,
WMTVER,
ATLAS_PK
ATLAS_PK,
SRCWIDTH,
SRCHEIGHT
};
Q_ENUM( Name )

Expand Down Expand Up @@ -367,6 +369,40 @@ namespace QgsWms
*/
int heightAsInt() const;

/**
* Returns SRCWIDTH parameter or an empty string if not defined.
* \returns srcWidth parameter
* \since QGIS 3.8
*/
QString srcWidth() const;

/**
* Returns SRCWIDTH parameter as an int or its default value if not
* defined. An exception is raised if SRCWIDTH is defined and cannot be
* converted.
* \returns srcWidth parameter
* \throws QgsBadRequestException
* \since QGIS 3.8
*/
int srcWidthAsInt() const;

/**
* Returns SRCHEIGHT parameter or an empty string if not defined.
* \returns srcHeight parameter
* \since QGIS 3.8
*/
QString srcHeight() const;

/**
* Returns SRCHEIGHT parameter as an int or its default value if not
* defined. An exception is raised if SRCHEIGHT is defined and cannot be
* converted.
* \returns srcHeight parameter
* \throws QgsBadRequestException
* \since QGIS 3.8
*/
int srcHeightAsInt() const;

/**
* Returns VERSION parameter if defined or its default value.
* \returns version
Expand Down
31 changes: 24 additions & 7 deletions src/server/services/wms/qgswmsrenderer.cpp
Expand Up @@ -187,7 +187,7 @@ namespace QgsWms
if ( !mWmsParameters.bbox().isEmpty() )
{
QgsMapSettings mapSettings;
image.reset( createImage( mWmsParameters.widthAsInt(), mWmsParameters.heightAsInt(), false ) );
image.reset( createImage( width(), height(), false ) );
configureMapSettings( image.get(), mapSettings );
legendSettings.setMapScale( mapSettings.scale() );
legendSettings.setMapUnitsPerPixel( mapSettings.mapUnitsPerPixel() );
Expand Down Expand Up @@ -1151,10 +1151,10 @@ namespace QgsWms
QImage *QgsRenderer::createImage( int width, int height, bool useBbox ) const
{
if ( width < 0 )
width = mWmsParameters.widthAsInt();
width = this->width();

if ( height < 0 )
height = mWmsParameters.heightAsInt();
height = this->height();

//Adapt width / height if the aspect ratio does not correspond with the BBOX.
//Required by WMS spec. 1.3.
Expand Down Expand Up @@ -2055,14 +2055,14 @@ namespace QgsWms
{
//test if maxWidth / maxHeight set and WIDTH / HEIGHT parameter is in the range
int wmsMaxWidth = QgsServerProjectUtils::wmsMaxWidth( *mProject );
int width = mWmsParameters.widthAsInt();
int width = this->width();
if ( wmsMaxWidth != -1 && width > wmsMaxWidth )
{
return false;
}

int wmsMaxHeight = QgsServerProjectUtils::wmsMaxHeight( *mProject );
int height = mWmsParameters.heightAsInt();
int height = this->height();
if ( wmsMaxHeight != -1 && height > wmsMaxHeight )
{
return false;
Expand Down Expand Up @@ -3277,8 +3277,8 @@ namespace QgsWms
// WIDTH / HEIGHT parameters. If not, the image has to be scaled (required
// by WMS spec)
QImage *scaledImage = nullptr;
int width = mWmsParameters.widthAsInt();
int height = mWmsParameters.heightAsInt();
int width = this->width();
int height = this->height();
if ( width != image->width() || height != image->height() )
{
scaledImage = new QImage( image->scaled( width, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation ) );
Expand Down Expand Up @@ -3515,5 +3515,22 @@ namespace QgsWms
return result;
}

int QgsRenderer::height() const
{
if ( ( mWmsParameters.request().compare( QStringLiteral( "GetLegendGraphic" ), Qt::CaseInsensitive ) == 0 ||
mWmsParameters.request().compare( QStringLiteral( "GetLegendGraphics" ), Qt::CaseInsensitive ) == 0 ) &&
mWmsParameters.srcHeightAsInt() > 0 )
return mWmsParameters.srcHeightAsInt();
return mWmsParameters.heightAsInt();
}

int QgsRenderer::width() const
{
if ( ( mWmsParameters.request().compare( QStringLiteral( "GetLegendGraphic" ), Qt::CaseInsensitive ) == 0 ||
mWmsParameters.request().compare( QStringLiteral( "GetLegendGraphics" ), Qt::CaseInsensitive ) == 0 ) &&
mWmsParameters.srcWidthAsInt() > 0 )
return mWmsParameters.srcWidthAsInt();
return mWmsParameters.widthAsInt();
}

} // namespace QgsWms
14 changes: 13 additions & 1 deletion src/server/services/wms/qgswmsrenderer.h
Expand Up @@ -291,7 +291,19 @@ namespace QgsWms

void handlePrintErrors( const QgsLayout *layout ) const;

private:
/**
* Returns QgsWmsParameter SRCWIDTH if it's a GetLegendGraphics request and otherwise HEIGHT parameter
* \returns height parameter
* \since QGIS 3.8
*/
int height() const;

/**
* Returns QgsWmsParameter SRCWIDTH parameter if it's a GetLegendGraphics request and otherwise WIDTH parameter
* \returns width parameter
* \since QGIS 3.8
*/
int width() const;

const QgsWmsParameters &mWmsParameters;

Expand Down
74 changes: 55 additions & 19 deletions tests/src/python/test_qgsserver_wms_getlegendgraphic.py
Expand Up @@ -460,6 +460,42 @@ def test_wms_GetLegendGraphic_ItemFont(self):
self._img_diff_error(r, h, "WMS_GetLegendGraphic_ItemFont", max_size_diff=QSize(1, 1))

def test_wms_GetLegendGraphic_BBox(self):
qs = "?" + "&".join(["%s=%s" % i for i in list({
"MAP": urllib.parse.quote(self.projectPath),
"SERVICE": "WMS",
"VERSION": "1.1.1",
"REQUEST": "GetLegendGraphic",
"LAYER": "Country,Hello,db_point",
"LAYERTITLE": "FALSE",
"FORMAT": "image/png",
"SRCHEIGHT": "500",
"SRCWIDTH": "500",
"BBOX": "-151.7,-38.9,51.0,78.0",
"CRS": "EPSG:4326"
}.items())])

r, h = self._result(self._execute_request(qs))
self._img_diff_error(r, h, "WMS_GetLegendGraphic_BBox")

def test_wms_GetLegendGraphic_BBox2(self):
qs = "?" + "&".join(["%s=%s" % i for i in list({
"MAP": urllib.parse.quote(self.projectPath),
"SERVICE": "WMS",
"VERSION": "1.1.1",
"REQUEST": "GetLegendGraphic",
"LAYER": "Country,Hello,db_point",
"LAYERTITLE": "FALSE",
"FORMAT": "image/png",
"SRCHEIGHT": "500",
"SRCWIDTH": "500",
"BBOX": "-76.08,-6.4,-19.38,38.04",
"SRS": "EPSG:4326"
}.items())])

r, h = self._result(self._execute_request(qs))
self._img_diff_error(r, h, "WMS_GetLegendGraphic_BBox2")

def test_wms_GetLegendGraphic_BBox_Fallback(self):
qs = "?" + "&".join(["%s=%s" % i for i in list({
"MAP": urllib.parse.quote(self.projectPath),
"SERVICE": "WMS",
Expand All @@ -477,7 +513,7 @@ def test_wms_GetLegendGraphic_BBox(self):
r, h = self._result(self._execute_request(qs))
self._img_diff_error(r, h, "WMS_GetLegendGraphic_BBox")

def test_wms_GetLegendGraphic_BBox2(self):
def test_wms_GetLegendGraphic_BBox2_Fallback(self):
qs = "?" + "&".join(["%s=%s" % i for i in list({
"MAP": urllib.parse.quote(self.projectPath),
"SERVICE": "WMS",
Expand All @@ -503,8 +539,8 @@ def test_wms_GetLegendGraphic_EmptyLegend(self):
"REQUEST": "GetLegendGraphic",
"LAYER": "QGIS%20Server%20Hello%20World",
"FORMAT": "image/png",
"HEIGHT": "840",
"WIDTH": "1226",
"SRCHEIGHT": "840",
"SRCWIDTH": "1226",
"BBOX": "10.38450,-49.6370,73.8183,42.9461",
"SRS": "EPSG:4326",
"SCALE": "15466642"
Expand All @@ -525,8 +561,8 @@ def test_wms_GetLegendGraphic_wmsRootName(self):
"REQUEST": "GetLegendGraphic",
"LAYER": "QGIS%20Server%20-%20Grouped%20Layer",
"FORMAT": "image/png",
"HEIGHT": "840",
"WIDTH": "1226",
"SRCHEIGHT": "840",
"SRCWIDTH": "1226",
"BBOX": "609152,5808188,625492,5814318",
"SRS": "EPSG:25832",
"SCALE": "38976"
Expand All @@ -544,8 +580,8 @@ def test_wms_GetLegendGraphic_wmsRootName(self):
"REQUEST": "GetLegendGraphic",
"LAYER": "All_grouped_layers",
"FORMAT": "image/png",
"HEIGHT": "840",
"WIDTH": "1226",
"SRCHEIGHT": "840",
"SRCWIDTH": "1226",
"BBOX": "609152,5808188,625492,5814318",
"SRS": "EPSG:25832",
"SCALE": "38976"
Expand All @@ -563,8 +599,8 @@ def test_wms_GetLegendGraphic_ScaleSymbol_Min(self):
"REQUEST": "GetLegendGraphic",
"LAYER": "testlayer",
"FORMAT": "image/png",
"HEIGHT": "550",
"WIDTH": "850",
"SRCHEIGHT": "550",
"SRCWIDTH": "850",
"BBOX": "-608.4,-1002.6,698.2,1019.0",
"CRS": "EPSG:4326"
}.items())])
Expand All @@ -579,8 +615,8 @@ def test_wms_GetLegendGraphic_ScaleSymbol_Min(self):
"REQUEST": "GetLegendGraphic",
"LAYER": "testlayer",
"FORMAT": "image/png",
"HEIGHT": "550",
"WIDTH": "850",
"SRCHEIGHT": "550",
"SRCWIDTH": "850",
"BBOX": "-1261.7,-2013.5,1351.5,2029.9",
"CRS": "EPSG:4326"
}.items())])
Expand All @@ -596,8 +632,8 @@ def test_wms_GetLegendGraphic_ScaleSymbol_Scaled_01(self):
"REQUEST": "GetLegendGraphic",
"LAYER": "testlayer",
"FORMAT": "image/png",
"HEIGHT": "550",
"WIDTH": "850",
"SRCHEIGHT": "550",
"SRCWIDTH": "850",
"BBOX": "31.8,-12.0,58.0,28.4",
"CRS": "EPSG:4326"
}.items())])
Expand All @@ -613,8 +649,8 @@ def test_wms_GetLegendGraphic_ScaleSymbol_Scaled_02(self):
"REQUEST": "GetLegendGraphic",
"LAYER": "testlayer",
"FORMAT": "image/png",
"HEIGHT": "550",
"WIDTH": "850",
"SRCHEIGHT": "550",
"SRCWIDTH": "850",
"BBOX": "25.3,-22.1,64.5,38.5",
"CRS": "EPSG:4326"
}.items())])
Expand All @@ -630,8 +666,8 @@ def test_wms_GetLegendGraphic_ScaleSymbol_Max(self):
"REQUEST": "GetLegendGraphic",
"LAYER": "testlayer",
"FORMAT": "image/png",
"HEIGHT": "550",
"WIDTH": "850",
"SRCHEIGHT": "550",
"SRCWIDTH": "850",
"BBOX": "44.8,8.0,45.0,8.4",
"CRS": "EPSG:4326"
}.items())])
Expand All @@ -646,8 +682,8 @@ def test_wms_GetLegendGraphic_ScaleSymbol_Max(self):
"REQUEST": "GetLegendGraphic",
"LAYER": "testlayer",
"FORMAT": "image/png",
"HEIGHT": "550",
"WIDTH": "850",
"SRCHEIGHT": "550",
"SRCWIDTH": "850",
"BBOX": "43.6,6.2,46.2,10.2",
"CRS": "EPSG:4326"
}.items())])
Expand Down

0 comments on commit 2366db8

Please sign in to comment.