Skip to content

Commit abaa11d

Browse files
committed
[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 49b66a5 commit abaa11d

File tree

5 files changed

+67
-24
lines changed

5 files changed

+67
-24
lines changed

src/server/services/wms/qgswmsgetprint.cpp

+14-6
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,29 @@ namespace QgsWms
4242
if ( format.compare( QLatin1String( "image/png" ), Qt::CaseInsensitive ) == 0 ||
4343
format.compare( QLatin1String( "png" ), Qt::CaseInsensitive ) == 0 )
4444
{
45-
format = "png";
46-
contentType = "image/png";
45+
format = QStringLiteral( "png" );
46+
contentType = QStringLiteral( "image/png" );
47+
}
48+
else if ( format.compare( QLatin1String( "image/jpg" ), Qt::CaseInsensitive ) == 0 ||
49+
format.compare( QLatin1String( "image/jpeg" ), Qt::CaseInsensitive ) == 0 ||
50+
format.compare( QLatin1String( "jpg" ), Qt::CaseInsensitive ) == 0 ||
51+
format.compare( QLatin1String( "jpeg" ), Qt::CaseInsensitive ) == 0 )
52+
{
53+
format = QStringLiteral( "jpg" );
54+
contentType = QStringLiteral( "image/jpeg" );
4755
}
4856
else if ( format.compare( QLatin1String( "image/svg" ), Qt::CaseInsensitive ) == 0 ||
4957
format.compare( QLatin1String( "image/svg+xml" ), Qt::CaseInsensitive ) == 0 ||
5058
format.compare( QLatin1String( "svg" ), Qt::CaseInsensitive ) == 0 )
5159
{
52-
format = "svg";
53-
contentType = "image/svg+xml";
60+
format = QStringLiteral( "svg" );
61+
contentType = QStringLiteral( "image/svg+xml" );
5462
}
5563
else if ( format.compare( QLatin1String( "application/pdf" ), Qt::CaseInsensitive ) == 0 ||
5664
format.compare( QLatin1String( "pdf" ), Qt::CaseInsensitive ) == 0 )
5765
{
58-
format = "pdf";
59-
contentType = "application/pdf";
66+
format = QStringLiteral( "pdf" );
67+
contentType = QStringLiteral( "application/pdf" );
6068
}
6169
else
6270
{

tests/src/python/test_qgsserver.py

+30-16
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

+7-2
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

+16
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,22 @@ def test_wms_getprint_basic(self):
9191
r, h = self._result(self._execute_request(qs))
9292
self._img_diff_error(r, h, "WMS_GetPrint_Basic")
9393

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

0 commit comments

Comments
 (0)