Skip to content

Commit 547fd68

Browse files
committedMar 19, 2019
[Bugfix][Regression][Server] JPEG output for WMS GetPrint request has gone
In QGIS Server 2.* the WMS GetPrint request could genrate JPEG image. QGIS Server 3.4 has lost this capabilities. ``` <ServiceExceptionReport xmlns="http://www.opengis.net/ogc" version="1.3.0" capture-installed="true"> <ServiceException code="InvalidFormat"> Output format jpg is not supported by the GetPrint request </ServiceException> </ServiceExceptionReport> ``` To fix this regression, it is necessary to accept JPEG output format. And to avoid this regression to come back, the QGIS Server tests has been updated to accept jpg image test.
1 parent 1e8aec4 commit 547fd68

File tree

6 files changed

+71
-34
lines changed

6 files changed

+71
-34
lines changed
 

‎src/server/services/wms/qgswmsgetprint.cpp

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,24 @@ namespace QgsWms
3535
QString contentType;
3636

3737
// GetPrint supports svg/png/pdf
38-
if ( format == QgsWmsParameters::PNG )
38+
switch ( format )
3939
{
40-
contentType = "image/png";
41-
}
42-
else if ( format == QgsWmsParameters::SVG )
43-
{
44-
contentType = "image/svg+xml";
45-
}
46-
else if ( format == QgsWmsParameters::PDF )
47-
{
48-
contentType = "application/pdf";
49-
}
50-
else
51-
{
52-
throw QgsServiceException( QStringLiteral( "InvalidFormat" ),
53-
QString( "Output format %1 is not supported by the GetPrint request" ).arg( wmsParameters.formatAsString() ) );
40+
case QgsWmsParameters::PNG:
41+
contentType = QStringLiteral( "image/png" );
42+
break;
43+
case QgsWmsParameters::JPG:
44+
contentType = QStringLiteral( "image/jpeg" );
45+
break;
46+
case QgsWmsParameters::SVG:
47+
contentType = QStringLiteral( "image/svg+xml" );
48+
break;
49+
case QgsWmsParameters::PDF:
50+
contentType = QStringLiteral( "application/pdf" );
51+
break;
52+
default:
53+
throw QgsServiceException( QStringLiteral( "InvalidFormat" ),
54+
QString( "Output format %1 is not supported by the GetPrint request" ).arg( wmsParameters.formatAsString() ) );
55+
break;
5456
}
5557

5658
response.setHeader( QStringLiteral( "Content-Type" ), contentType );

‎src/server/services/wms/qgswmsrenderer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ namespace QgsWms
514514
exporter.exportToSvg( tempOutputFile.fileName(), exportSettings );
515515
}
516516
}
517-
else if ( format == QgsWmsParameters::PNG )
517+
else if ( format == QgsWmsParameters::PNG || format == QgsWmsParameters::JPG )
518518
{
519519
// Settings for the layout exporter
520520
QgsLayoutExporter::ImageExportSettings exportSettings;

‎tests/src/python/test_qgsserver.py

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,20 @@ def _result(self, data):
176176

177177
return data[1], headers
178178

179-
def _img_diff(self, image, control_image, max_diff, max_size_diff=QSize()):
180-
temp_image = os.path.join(tempfile.gettempdir(), "%s_result.png" % control_image)
179+
def _img_diff(self, image, control_image, max_diff, max_size_diff=QSize(), outputJpg=False):
180+
181+
extFile = 'png'
182+
if outputJpg:
183+
extFile = 'jpg'
184+
185+
temp_image = os.path.join(tempfile.gettempdir(), "%s_result.%s" % (control_image, extFile))
181186

182187
with open(temp_image, "wb") as f:
183188
f.write(image)
184189

190+
if outputJpg:
191+
return (True, "QgsRenderChecker can't be used for JPG images")
192+
185193
control = QgsRenderChecker()
186194
control.setControlPathPrefix("qgis_server")
187195
control.setControlName(control_image)
@@ -190,35 +198,41 @@ def _img_diff(self, image, control_image, max_diff, max_size_diff=QSize()):
190198
control.setSizeTolerance(max_size_diff.width(), max_size_diff.height())
191199
return control.compareImages(control_image, max_diff), control.report()
192200

193-
def _img_diff_error(self, response, headers, image, max_diff=100, max_size_diff=QSize(), unittest_data_path='control_images'):
201+
def _img_diff_error(self, response, headers, image, max_diff=100, max_size_diff=QSize(), unittest_data_path='control_images', outputJpg=False):
202+
203+
extFile = 'png'
204+
contentType = 'image/png'
205+
if outputJpg:
206+
extFile = 'jpg'
207+
contentType = 'image/jpeg'
194208

195-
reference_path = unitTestDataPath(unittest_data_path) + '/qgis_server/' + image + '/' + image + '.png'
209+
reference_path = unitTestDataPath(unittest_data_path) + '/qgis_server/' + image + '/' + image + '.' + extFile
196210
self.store_reference(reference_path, response)
197211

198212
self.assertEqual(
199-
headers.get("Content-Type"), "image/png",
200-
"Content type is wrong: %s\n%s" % (headers.get("Content-Type"), response))
213+
headers.get("Content-Type"), contentType,
214+
"Content type is wrong: %s instead of %s\n%s" % (headers.get("Content-Type"), contentType, response))
201215

202-
test, report = self._img_diff(response, image, max_diff, max_size_diff)
216+
test, report = self._img_diff(response, image, max_diff, max_size_diff, outputJpg)
203217

204-
with open(os.path.join(tempfile.gettempdir(), image + "_result.png"), "rb") as rendered_file:
218+
with open(os.path.join(tempfile.gettempdir(), image + "_result." + extFile), "rb") as rendered_file:
205219
encoded_rendered_file = base64.b64encode(rendered_file.read())
206220
if not os.environ.get('ENCODED_OUTPUT'):
207-
message = "Image is wrong\: rendered file %s/%s_result.png" % (tempfile.gettempdir(), image)
221+
message = "Image is wrong\: rendered file %s/%s_result.%s" % (tempfile.gettempdir(), image, extFile)
208222
else:
209-
message = "Image is wrong\n%s\nImage:\necho '%s' | base64 -d >%s/%s_result.png" % (
210-
report, encoded_rendered_file.strip().decode('utf8'), tempfile.gettempdir(), image
223+
message = "Image is wrong\n%s\nImage:\necho '%s' | base64 -d >%s/%s_result.%s" % (
224+
report, encoded_rendered_file.strip().decode('utf8'), tempfile.gettempdir(), image, extFile
211225
)
212226

213227
# If the failure is in image sizes the diff file will not exists.
214-
if os.path.exists(os.path.join(tempfile.gettempdir(), image + "_result_diff.png")):
215-
with open(os.path.join(tempfile.gettempdir(), image + "_result_diff.png"), "rb") as diff_file:
228+
if os.path.exists(os.path.join(tempfile.gettempdir(), image + "_result_diff." + extFile)):
229+
with open(os.path.join(tempfile.gettempdir(), image + "_result_diff." + extFile), "rb") as diff_file:
216230
if not os.environ.get('ENCODED_OUTPUT'):
217-
message = "Image is wrong\: diff file %s/%s_result_diff.png" % (tempfile.gettempdir(), image)
231+
message = "Image is wrong\: diff file %s/%s_result_diff.%s" % (tempfile.gettempdir(), image, extFile)
218232
else:
219233
encoded_diff_file = base64.b64encode(diff_file.read())
220-
message += "\nDiff:\necho '%s' | base64 -d > %s/%s_result_diff.png" % (
221-
encoded_diff_file.strip().decode('utf8'), tempfile.gettempdir(), image
234+
message += "\nDiff:\necho '%s' | base64 -d > %s/%s_result_diff.%s" % (
235+
encoded_diff_file.strip().decode('utf8'), tempfile.gettempdir(), image, extFile
222236
)
223237

224238
self.assertTrue(test, message)

‎tests/src/python/test_qgsserver_accesscontrol.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,13 @@ def _post_restricted(self, data, query_string=None):
202202
self._server.putenv("QGIS_PROJECT_FILE", '')
203203
return result
204204

205-
def _img_diff(self, image, control_image, max_diff, max_size_diff=QSize()):
206-
temp_image = os.path.join(tempfile.gettempdir(), "%s_result.png" % control_image)
205+
def _img_diff(self, image, control_image, max_diff, max_size_diff=QSize(), outputJpg=False):
206+
207+
extFile = 'png'
208+
if outputJpg:
209+
extFile = 'jpg'
210+
211+
temp_image = os.path.join(tempfile.gettempdir(), "%s_result.%s" % (control_image, extFile))
207212

208213
with open(temp_image, "wb") as f:
209214
f.write(image)

‎tests/src/python/test_qgsserver_wms_getprint.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,22 @@ def test_wms_getprint_basic(self):
9090
r, h = self._result(self._execute_request(qs))
9191
self._img_diff_error(r, h, "WMS_GetPrint_Basic")
9292

93+
# Output JPEG
94+
qs = "?" + "&".join(["%s=%s" % i for i in list({
95+
"MAP": urllib.parse.quote(self.projectPath),
96+
"SERVICE": "WMS",
97+
"VERSION": "1.1.1",
98+
"REQUEST": "GetPrint",
99+
"TEMPLATE": "layoutA4",
100+
"FORMAT": "jpeg",
101+
"map0:EXTENT": "-33626185.498,-13032965.185,33978427.737,16020257.031",
102+
"LAYERS": "Country,Hello",
103+
"CRS": "EPSG:3857"
104+
}.items())])
105+
106+
r, h = self._result(self._execute_request(qs))
107+
self._img_diff_error(r, h, "WMS_GetPrint_Basic", outputJpg=True)
108+
93109
def test_wms_getprint_style(self):
94110
# default style
95111
qs = "?" + "&".join(["%s=%s" % i for i in list({
302 KB
Loading

0 commit comments

Comments
 (0)
Please sign in to comment.