-
Notifications
You must be signed in to change notification settings - Fork 4
/
qgswmsutils.cpp
192 lines (171 loc) · 6.59 KB
/
qgswmsutils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/***************************************************************************
qgswmsutils.cpp
-------------------------
begin : December 20 , 2016
copyright : (C) 2007 by Marco Hugentobler ( parts fron qgswmshandler)
(C) 2014 by Alessandro Pasotti ( parts from qgswmshandler)
(C) 2016 by David Marteau
email : marco dot hugentobler at karto dot baug dot ethz dot ch
a dot pasotti at itopen dot it
david dot marteau at 3liz dot com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsmodule.h"
#include "qgswmsutils.h"
#include "qgsmediancut.h"
#include "qgsconfigcache.h"
#include "qgsserverprojectutils.h"
namespace QgsWms
{
QString ImplementationVersion()
{
return QStringLiteral( "1.3.0" );
}
QUrl serviceUrl( const QgsServerRequest &request, const QgsProject *project )
{
QUrl href;
if ( project )
{
href.setUrl( QgsServerProjectUtils::wmsServiceUrl( *project ) );
}
// Build default url
if ( href.isEmpty() )
{
href = request.url();
QUrlQuery q( href );
q.removeAllQueryItems( QStringLiteral( "REQUEST" ) );
q.removeAllQueryItems( QStringLiteral( "VERSION" ) );
q.removeAllQueryItems( QStringLiteral( "SERVICE" ) );
q.removeAllQueryItems( QStringLiteral( "LAYERS" ) );
q.removeAllQueryItems( QStringLiteral( "SLD_VERSION" ) );
q.removeAllQueryItems( QStringLiteral( "_DC" ) );
href.setQuery( q );
}
return href;
}
ImageOutputFormat parseImageFormat( const QString &format )
{
if ( format.compare( QLatin1String( "png" ), Qt::CaseInsensitive ) == 0 ||
format.compare( QLatin1String( "image/png" ), Qt::CaseInsensitive ) == 0 )
{
return PNG;
}
else if ( format.compare( QLatin1String( "jpg " ), Qt::CaseInsensitive ) == 0 ||
format.compare( QLatin1String( "image/jpeg" ), Qt::CaseInsensitive ) == 0 )
{
return JPEG;
}
else
{
// lookup for png with mode
QRegularExpression modeExpr = QRegularExpression( QStringLiteral( "image/png\\s*;\\s*mode=([^;]+)" ),
QRegularExpression::CaseInsensitiveOption );
QRegularExpressionMatch match = modeExpr.match( format );
QString mode = match.captured( 1 );
if ( mode.compare( QLatin1String( "16bit" ), Qt::CaseInsensitive ) == 0 )
return PNG16;
if ( mode.compare( QLatin1String( "8bit" ), Qt::CaseInsensitive ) == 0 )
return PNG8;
if ( mode.compare( QLatin1String( "1bit" ), Qt::CaseInsensitive ) == 0 )
return PNG1;
}
return UNKN;
}
void readLayersAndStyles( const QgsServerRequest::Parameters ¶meters, QStringList &layersList, QStringList &stylesList )
{
//get layer and style lists from the parameters trying LAYERS and LAYER as well as STYLE and STYLES for GetLegendGraphic compatibility
layersList = parameters.value( QStringLiteral( "LAYER" ) ).split( ',', QString::SkipEmptyParts );
layersList = layersList + parameters.value( QStringLiteral( "LAYERS" ) ).split( ',', QString::SkipEmptyParts );
stylesList = parameters.value( QStringLiteral( "STYLE" ) ).split( ',', QString::SkipEmptyParts );
stylesList = stylesList + parameters.value( QStringLiteral( "STYLES" ) ).split( ',', QString::SkipEmptyParts );
}
// Write image response
void writeImage( QgsServerResponse &response, QImage &img, const QString &formatStr,
int imageQuality )
{
ImageOutputFormat outputFormat = parseImageFormat( formatStr );
QImage result;
QString saveFormat;
QString contentType;
switch ( outputFormat )
{
case PNG:
result = img;
contentType = "image/png";
saveFormat = "PNG";
break;
case PNG8:
{
QVector<QRgb> colorTable;
medianCut( colorTable, 256, img );
result = img.convertToFormat( QImage::Format_Indexed8, colorTable,
Qt::ColorOnly | Qt::ThresholdDither |
Qt::ThresholdAlphaDither | Qt::NoOpaqueDetection );
}
contentType = "image/png";
saveFormat = "PNG";
break;
case PNG16:
result = img.convertToFormat( QImage::Format_ARGB4444_Premultiplied );
contentType = "image/png";
saveFormat = "PNG";
break;
case PNG1:
result = img.convertToFormat( QImage::Format_Mono,
Qt::MonoOnly | Qt::ThresholdDither |
Qt::ThresholdAlphaDither | Qt::NoOpaqueDetection );
contentType = "image/png";
saveFormat = "PNG";
break;
case JPEG:
result = img;
contentType = "image/jpeg";
saveFormat = "JPEG";
break;
default:
QgsMessageLog::logMessage( QString( "Unsupported format string %1" ).arg( formatStr ) );
saveFormat = UNKN;
break;
}
if ( outputFormat != UNKN )
{
response.setHeader( "Content-Type", contentType );
if ( saveFormat == "JPEG" )
{
result.save( response.io(), qPrintable( saveFormat ), imageQuality );
}
else
{
result.save( response.io(), qPrintable( saveFormat ) );
}
}
else
{
throw QgsServiceException( "InvalidFormat",
QString( "Output format '%1' is not supported in the GetMap request" ).arg( formatStr ) );
}
}
QgsRectangle parseBbox( const QString &bboxStr )
{
QStringList lst = bboxStr.split( ',' );
if ( lst.count() != 4 )
return QgsRectangle();
double d[4];
bool ok;
for ( int i = 0; i < 4; i++ )
{
lst[i].replace( ' ', '+' );
d[i] = lst[i].toDouble( &ok );
if ( !ok )
return QgsRectangle();
}
return QgsRectangle( d[0], d[1], d[2], d[3] );
}
} // namespace QgsWms