QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgswmsutils.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgswmsutils.cpp
3 -------------------------
4 begin : December 20 , 2016
5 copyright : (C) 2007 by Marco Hugentobler ( parts from qgswmshandler)
6 (C) 2014 by Alessandro Pasotti ( parts from qgswmshandler)
7 (C) 2016 by David Marteau
8 email : marco dot hugentobler at karto dot baug dot ethz dot ch
9 a dot pasotti at itopen dot it
10 david dot marteau at 3liz dot com
11 ***************************************************************************/
12
13/***************************************************************************
14 * *
15 * This program is free software; you can redistribute it and/or modify *
16 * it under the terms of the GNU General Public License as published by *
17 * the Free Software Foundation; either version 2 of the License, or *
18 * (at your option) any later version. *
19 * *
20 ***************************************************************************/
21
22#include <QRegularExpression>
23
24#include "qgsmodule.h"
25#include "qgswmsutils.h"
26#include "qgsmediancut.h"
29#include "qgsproject.h"
30
31namespace QgsWms
32{
34 {
35 return QStringLiteral( "1.3.0" );
36 }
37
38 QUrl serviceUrl( const QgsServerRequest &request, const QgsProject *project, const QgsServerSettings &settings )
39 {
40 QUrl href;
41 href.setUrl( QgsServerProjectUtils::wmsServiceUrl( project ? *project : *QgsProject::instance(), request, settings ) );
42
43 // Build default url
44 if ( href.isEmpty() )
45 {
46 static const QSet<QString> sFilter {
47 QStringLiteral( "REQUEST" ),
48 QStringLiteral( "VERSION" ),
49 QStringLiteral( "SERVICE" ),
50 QStringLiteral( "LAYERS" ),
51 QStringLiteral( "STYLES" ),
52 QStringLiteral( "SLD_VERSION" ),
53 QStringLiteral( "_DC" )
54 };
55
56 href = request.originalUrl();
57 QUrlQuery q( href );
58
59 const QList<QPair<QString, QString>> queryItems = q.queryItems();
60 for ( const QPair<QString, QString> &param : queryItems )
61 {
62 if ( sFilter.contains( param.first.toUpper() ) )
63 q.removeAllQueryItems( param.first );
64 }
65
66 href.setQuery( q );
67 }
68
69 return href;
70 }
71
72
73 ImageOutputFormat parseImageFormat( const QString &format )
74 {
75 if ( format.compare( QLatin1String( "png" ), Qt::CaseInsensitive ) == 0 || format.compare( QLatin1String( "image/png" ), Qt::CaseInsensitive ) == 0 )
76 {
78 }
79 else if ( format.compare( QLatin1String( "jpg " ), Qt::CaseInsensitive ) == 0 || format.compare( QLatin1String( "image/jpeg" ), Qt::CaseInsensitive ) == 0 )
80 {
82 }
83 else if ( format.compare( QLatin1String( "webp" ), Qt::CaseInsensitive ) == 0 || format.compare( QLatin1String( "image/webp" ), Qt::CaseInsensitive ) == 0 )
84 {
86 }
87 else
88 {
89 // lookup for png with mode
90 const thread_local QRegularExpression modeExpr = QRegularExpression( QStringLiteral( "image/png\\s*;\\s*mode=([^;]+)" ), QRegularExpression::CaseInsensitiveOption );
91
92 const QRegularExpressionMatch match = modeExpr.match( format );
93 const QString mode = match.captured( 1 );
94 if ( mode.compare( QLatin1String( "16bit" ), Qt::CaseInsensitive ) == 0 )
96 if ( mode.compare( QLatin1String( "8bit" ), Qt::CaseInsensitive ) == 0 )
98 if ( mode.compare( QLatin1String( "1bit" ), Qt::CaseInsensitive ) == 0 )
100 }
101
103 }
104
105 // Write image response
106 void writeImage( QgsServerResponse &response, QImage &img, const QString &formatStr, int imageQuality )
107 {
108 const ImageOutputFormat outputFormat = parseImageFormat( formatStr );
109 QImage result;
110 QString saveFormat;
111 QString contentType;
112 switch ( outputFormat )
113 {
115 result = img;
116 contentType = QStringLiteral( "image/png" );
117 saveFormat = QStringLiteral( "PNG" );
118 break;
120 {
121 QVector<QRgb> colorTable;
122
123 // Rendering is made with the format QImage::Format_ARGB32_Premultiplied
124 // So we need to convert it in QImage::Format_ARGB32 in order to properly build
125 // the color table.
126 const QImage img256 = img.convertToFormat( QImage::Format_ARGB32 );
127 medianCut( colorTable, 256, img256 );
128 result = img256.convertToFormat( QImage::Format_Indexed8, colorTable, Qt::ColorOnly | Qt::ThresholdDither | Qt::ThresholdAlphaDither | Qt::NoOpaqueDetection );
129 }
130 contentType = QStringLiteral( "image/png" );
131 saveFormat = QStringLiteral( "PNG" );
132 break;
134 result = img.convertToFormat( QImage::Format_ARGB4444_Premultiplied );
135 contentType = QStringLiteral( "image/png" );
136 saveFormat = QStringLiteral( "PNG" );
137 break;
139 result = img.convertToFormat( QImage::Format_Mono, Qt::MonoOnly | Qt::ThresholdDither | Qt::ThresholdAlphaDither | Qt::NoOpaqueDetection );
140 contentType = QStringLiteral( "image/png" );
141 saveFormat = QStringLiteral( "PNG" );
142 break;
144 result = img;
145 contentType = QStringLiteral( "image/jpeg" );
146 saveFormat = QStringLiteral( "JPEG" );
147 break;
149 result = img;
150 contentType = QStringLiteral( "image/webp" );
151 saveFormat = QStringLiteral( "WEBP" );
152 break;
154 QgsMessageLog::logMessage( QStringLiteral( "Unsupported format string %1" ).arg( formatStr ) );
155 saveFormat = QStringLiteral( "Unknown" );
156 break;
157 }
158
159 // Preserve DPI, some conversions, in particular the one for 8bit will drop this information
160 result.setDotsPerMeterX( img.dotsPerMeterX() );
161 result.setDotsPerMeterY( img.dotsPerMeterY() );
162
163 if ( outputFormat != ImageOutputFormat::Unknown )
164 {
165 response.setHeader( "Content-Type", contentType );
166 if ( saveFormat == QLatin1String( "JPEG" ) || saveFormat == QLatin1String( "WEBP" ) )
167 {
168 result.save( response.io(), qPrintable( saveFormat ), imageQuality );
169 }
170 else
171 {
172 result.save( response.io(), qPrintable( saveFormat ) );
173 }
174 }
175 else
176 {
178 parameter.mValue = formatStr;
180 }
181 }
182} // namespace QgsWms
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:107
static QgsProject * instance()
Returns the QgsProject singleton instance.
QgsServerRequest Class defining request interface passed to services QgsService::executeRequest() met...
QUrl originalUrl() const
Returns the request url as seen by the web server, by default this is equal to the url seen by QGIS s...
QgsServerResponse Class defining response interface passed to services QgsService::executeRequest() m...
virtual void setHeader(const QString &key, const QString &value)=0
Set Header entry Add Header entry to the response Note that it is usually an error to set Header afte...
virtual QIODevice * io()=0
Returns the underlying QIODevice.
Provides a way to retrieve settings by prioritizing according to environment variables,...
Exception thrown in case of malformed request.
WMS parameter received from the client.
SERVER_EXPORT QString wmsServiceUrl(const QgsProject &project, const QgsServerRequest &request=QgsServerRequest(), const QgsServerSettings &settings=QgsServerSettings())
Returns the WMS service url.
Median cut implementation.
void writeImage(QgsServerResponse &response, QImage &img, const QString &formatStr, int imageQuality)
Write image response.
void medianCut(QVector< QRgb > &colorTable, int nColors, const QImage &inputImage)
Median cut implementation used when reducing RGB colors to palletized colors.
QString implementationVersion()
Returns the highest version supported by this implementation.
ImageOutputFormat
Supported image output format.
Definition qgswmsutils.h:41
@ Unknown
Unknown/invalid format.
ImageOutputFormat parseImageFormat(const QString &format)
Parse image format parameter.
QUrl serviceUrl(const QgsServerRequest &request, const QgsProject *project, const QgsServerSettings &settings)
Returns WMS service URL.