Skip to content

Commit

Permalink
Merge pull request #37068 from m-kuhn/qgis_server_webp
Browse files Browse the repository at this point in the history
WEBP for QGIS Server
  • Loading branch information
m-kuhn committed Jun 15, 2020
2 parents 0a0fc29 + 78b02d5 commit 816d4b0
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 16 deletions.
11 changes: 10 additions & 1 deletion src/server/services/wms/qgswmsutils.cpp
Expand Up @@ -79,6 +79,11 @@ namespace QgsWms
{
return JPEG;
}
else if ( format.compare( QLatin1String( "webp" ), Qt::CaseInsensitive ) == 0 ||
format.compare( QLatin1String( "image/webp" ), Qt::CaseInsensitive ) == 0 )
{
return WEBP;
}
else
{
// lookup for png with mode
Expand Down Expand Up @@ -146,6 +151,10 @@ namespace QgsWms
contentType = "image/jpeg";
saveFormat = "JPEG";
break;
case WEBP:
contentType = QStringLiteral( "image/webp" );
saveFormat = QStringLiteral( "WEBP" );
break;
default:
QgsMessageLog::logMessage( QString( "Unsupported format string %1" ).arg( formatStr ) );
saveFormat = UNKN;
Expand All @@ -159,7 +168,7 @@ namespace QgsWms
if ( outputFormat != UNKN )
{
response.setHeader( "Content-Type", contentType );
if ( saveFormat == "JPEG" )
if ( saveFormat == QLatin1String( "JPEG" ) || saveFormat == QLatin1String( "WEBP" ) )
{
result.save( response.io(), qPrintable( saveFormat ), imageQuality );
}
Expand Down
3 changes: 2 additions & 1 deletion src/server/services/wms/qgswmsutils.h
Expand Up @@ -43,7 +43,8 @@ namespace QgsWms
PNG8,
PNG16,
PNG1,
JPEG
JPEG,
WEBP
};

/**
Expand Down
34 changes: 24 additions & 10 deletions tests/src/python/test_qgsserver.py
Expand Up @@ -174,19 +174,24 @@ def _result(self, data):

return data[1], headers

def _img_diff(self, image, control_image, max_diff, max_size_diff=QSize(), outputJpg=False):
def _img_diff(self, image, control_image, max_diff, max_size_diff=QSize(), outputFormat='PNG'):

extFile = 'png'
if outputJpg:
if outputFormat == 'PNG':
extFile = 'png'
elif outputFormat == 'JPG':
extFile = 'jpg'
elif outputFormat == 'WEBP':
extFile = 'webp'
else:
raise RuntimeError('Yeah, new format implemented')

temp_image = os.path.join(tempfile.gettempdir(), "%s_result.%s" % (control_image, extFile))

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

if outputJpg:
return (True, "QgsRenderChecker can't be used for JPG images")
if outputFormat != 'PNG':
return (True, "QgsRenderChecker can only be used for PNG")

control = QgsMultiRenderChecker()
control.setControlPathPrefix("qgis_server")
Expand All @@ -196,13 +201,22 @@ def _img_diff(self, image, control_image, max_diff, max_size_diff=QSize(), outpu
control.setSizeTolerance(max_size_diff.width(), max_size_diff.height())
return control.runTest(control_image, max_diff), control.report()

def _img_diff_error(self, response, headers, image, max_diff=100, max_size_diff=QSize(), unittest_data_path='control_images', outputJpg=False):
def _img_diff_error(self, response, headers, image, max_diff=100, max_size_diff=QSize(), unittest_data_path='control_images', outputFormat='PNG'):
"""
:param outputFormat: PNG, JPG or WEBP
"""

extFile = 'png'
contentType = 'image/png'
if outputJpg:
if outputFormat == 'PNG':
extFile = 'png'
contentType = 'image/png'
elif outputFormat == 'JPG':
extFile = 'jpg'
contentType = 'image/jpeg'
elif outputFormat == 'WEBP':
extFile = 'webp'
contentType = 'image/webp'
else:
raise RuntimeError('Yeah, new format implemented')

reference_path = unitTestDataPath(unittest_data_path) + '/qgis_server/' + image + '/' + image + '.' + extFile
self.store_reference(reference_path, response)
Expand All @@ -211,7 +225,7 @@ def _img_diff_error(self, response, headers, image, max_diff=100, max_size_diff=
headers.get("Content-Type"), contentType,
"Content type is wrong: %s instead of %s\n%s" % (headers.get("Content-Type"), contentType, response))

test, report = self._img_diff(response, image, max_diff, max_size_diff, outputJpg)
test, report = self._img_diff(response, image, max_diff, max_size_diff, outputFormat)

with open(os.path.join(tempfile.gettempdir(), image + "_result." + extFile), "rb") as rendered_file:
encoded_rendered_file = base64.b64encode(rendered_file.read())
Expand Down
11 changes: 8 additions & 3 deletions tests/src/python/test_qgsserver_accesscontrol.py
Expand Up @@ -200,11 +200,16 @@ def _post_restricted(self, data, query_string=None):
self._server.putenv("QGIS_PROJECT_FILE", '')
return result

def _img_diff(self, image, control_image, max_diff, max_size_diff=QSize(), outputJpg=False):
def _img_diff(self, image, control_image, max_diff, max_size_diff=QSize(), outputFormat='PNG'):

extFile = 'png'
if outputJpg:
if outputFormat == 'PNG':
extFile = 'png'
elif outputFormat == 'JPG':
extFile = 'jpg'
elif outputFormat == 'WEBP':
extFile = 'webp'
else:
raise RuntimeError('Yeah, new format implemented')

temp_image = os.path.join(tempfile.gettempdir(), "%s_result.%s" % (control_image, extFile))

Expand Down
18 changes: 18 additions & 0 deletions tests/src/python/test_qgsserver_wms_getmap.py
Expand Up @@ -99,6 +99,24 @@ def test_wms_getmap_basic_mode(self):
r, h = self._result(self._execute_request(qs))
self._img_diff_error(r, h, "WMS_GetMap_Mode_16bit", 20000)

# webp
qs = "?" + "&".join(["%s=%s" % i for i in list({
"MAP": urllib.parse.quote(self.projectPath),
"SERVICE": "WMS",
"VERSION": "1.1.1",
"REQUEST": "GetMap",
"LAYERS": "Country",
"STYLES": "",
"FORMAT": "image/webp",
"BBOX": "-16817707,-4710778,5696513,14587125",
"HEIGHT": "500",
"WIDTH": "500",
"CRS": "EPSG:3857"
}.items())])

r, h = self._result(self._execute_request(qs))
self._img_diff_error(r, h, "WMS_GetMap_Mode_16bit", 20000, outputFormat='WEBP')

def test_wms_getmap_basic(self):
qs = "?" + "&".join(["%s=%s" % i for i in list({
"MAP": urllib.parse.quote(self.projectPath),
Expand Down
2 changes: 1 addition & 1 deletion tests/src/python/test_qgsserver_wms_getprint.py
Expand Up @@ -235,7 +235,7 @@ def test_wms_getprint_basic(self):
}.items())])

r, h = self._result(self._execute_request(qs))
self._img_diff_error(r, h, "WMS_GetPrint_Basic", outputJpg=True)
self._img_diff_error(r, h, "WMS_GetPrint_Basic", outputFormat='JPG')

# Output PDF
qs = "?" + "&".join(["%s=%s" % i for i in list({
Expand Down

0 comments on commit 816d4b0

Please sign in to comment.