Skip to content
Permalink
Browse files

Merge pull request #37068 from m-kuhn/qgis_server_webp

WEBP for QGIS Server
  • Loading branch information
m-kuhn committed Jun 15, 2020
2 parents 0a0fc29 + 78b02d5 commit 816d4b04ffba436c54ead7ad200775ce2d0d2d8e
@@ -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
@@ -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;
@@ -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 );
}
@@ -43,7 +43,8 @@ namespace QgsWms
PNG8,
PNG16,
PNG1,
JPEG
JPEG,
WEBP
};

/**
@@ -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")
@@ -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)
@@ -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())
@@ -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))

@@ -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),
@@ -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({

0 comments on commit 816d4b0

Please sign in to comment.
You can’t perform that action at this time.