Repository navigation
Expand file tree
/
Copy pathHttpWMSService.cpp
More file actions
338 lines (273 loc) · 8.58 KB
/
Copy pathHttpWMSService.cpp
File metadata and controls
338 lines (273 loc) · 8.58 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include "StdAfx.h"
#include "HttpWMSService.h"
#include "ParamDef.h"
#include "WMTSRepository.h"
#include "Tile.h"
//#include "glog/logging.h"
//#include "glog/raw_logging.h"
#include "Bundle.h"
#include <CLogThreadMgr.h>
#include "WMTSConfig.h"
extern thp::WMTSRepository* g_pWMTSDataCache;
HttpWMSService::HttpWMSService(void)
{
_initLogWriter();
}
bool HttpWMSService::_initLogWriter()
{
// 获取写日志对象
m_pLogWriter = CLogThreadMgr::instance()->getLogWriter("HttpWMSService.log");
// 如果不存在,创建日志文件,加载日志配置
if (m_pLogWriter == NULL)
{
CLogAppender * pLogAppender = new CLogAppender("HttpWMSService", "HttpWMSService.log", "", "General");
// 获取写日志对象
m_pLogWriter = CLogThreadMgr::instance()->createLogWriter(pLogAppender);
}
return true;
}
HttpWMSService::~HttpWMSService(void)
{
}
int HttpWMSService::dwmGetMap(struct soap *soap, char* path)
{
if( 0 == WMTSConfig::Instance()->getFileSysType() )
return _dwmGetMapWindows(soap, path);
else
return _dwmGetMapUnix(soap, path);
}
int HttpWMSService::_dwmGetMapWindows(struct soap * soap, char * path)
{
// /WMTS?service=WMTS&request=GetTile&version=1.0.0&
// layer=img&style=default&format=tiles&TileMatrixSet=c&TileMatrix=3&TileRow=2&TileCol=2
#ifdef _DEBUG
clock_t tStart = clock();
#endif
try
{
std::string sUrlTemp(path);
QString qs( GB(path) );
QUrl urlSpe( qs.toUpper() );// = QUrl::fromEncoded(path);
QString sTemp = urlSpe.queryItemValue(WMTS_SERVICE);
if( 0 != sTemp.compare("wmts", Qt::CaseInsensitive) )
return _sendCapabilities(soap);
sTemp = urlSpe.queryItemValue( WMTS_REQUEST );
//处理请求capatibility
if( 0 == sTemp.compare(WMTS_REQUEST_VL_CAPABILITIES, Qt::CaseInsensitive) )
return _sendCapabilities(soap);
if( 0 != sTemp.compare(WMTS_REQUEST_VL_GETTILE), Qt::CaseInsensitive)
return _sendCapabilities(soap);
sTemp = urlSpe.queryItemValue( WMTS_VERSION );
if( sTemp.isEmpty() )
return _sendCapabilities(soap);
sTemp = urlSpe.queryItemValue(WMTS_LAYER);
if( sTemp.isEmpty() )
return _sendCapabilities(soap);
std::string stdstrLayr = sTemp.toLocal8Bit();
// 未使用
sTemp = urlSpe.queryItemValue(WMTS_LAYER_STYLE);
if( sTemp.isEmpty() )
return _sendCapabilities(soap);
// PNG
sTemp = urlSpe.queryItemValue(WMTS_TILE_FORMAT);
if( sTemp.isEmpty() )
return _sendCapabilities(soap);
// 未使用,内部算法应该是只有google的切片方案
sTemp = urlSpe.queryItemValue(WMTS_TILEMATRIXSET);
if( sTemp.isEmpty() )
return _sendCapabilities(soap);
// 等级
sTemp = urlSpe.queryItemValue(WMTS_TILEMATRIX);
if( sTemp.isEmpty() )
return _sendCapabilities(soap);
int nlv = sTemp.toInt();
// 行号
sTemp = urlSpe.queryItemValue(WMTS_TILEROW);
if( sTemp.isEmpty() )
return _sendCapabilities(soap);
int nRow = sTemp.toInt();
// 列号
sTemp = urlSpe.queryItemValue(WMTS_TILECOL);
if( sTemp.isEmpty() )
return _sendCapabilities(soap);
int nCol = sTemp.toInt();
// 从服务程序获取tile
int nDetail = 0;
QByteArray qAr;
int nSize = g_pWMTSDataCache->getTile( stdstrLayr, nlv, nRow, nCol, qAr, nDetail);
if( nSize > 0)
{
#ifdef _DEBUG
clock_t tEnd = clock();
int nSpan = 1000*(tEnd - tStart)/CLOCKS_PER_SEC;
std::cout << "Get Tile Spend Time:"<< nSpan <<" ms" << std::endl;
#endif
#ifdef _DEBUG
tStart = clock();
#endif
int nRes = _sendData(soap, qAr, "image/png");
#ifdef _DEBUG
tEnd = clock();
nSpan = 1000*(tEnd - tStart)/CLOCKS_PER_SEC;
std::cout << "Send Tile Spend Time:"<< nSpan <<" ms" << std::endl;;
#endif
return nRes;
}
QString strMessage = QString(GB("没有制定瓦片数据,layer:%0,lv:%1,row:%2,col:%3")).arg(stdstrLayr.c_str()).arg(nlv).arg(nRow).arg(nCol);
m_pLogWriter->errorLog(strMessage);
return _sendExceptionMessage(soap, strMessage, "");
}
catch(...)
{
m_pLogWriter->errorLog( GB("解析url出错") );
QString strMessage = GB("解析url出错");
return _sendExceptionMessage(soap, strMessage, "");
}
}
int HttpWMSService::_dwmGetMapUnix(struct soap * soap, char * path)
{
try
{
#ifdef _DEBUG
clock_t tStart = clock();
#endif
QUrl urlSpe = QUrl::fromEncoded(path);
QString sTemp = urlSpe.queryItemValue(WMTS_SERVICE);
if( 0 != sTemp.compare("wmts", Qt::CaseInsensitive) )
return _sendCapabilities(soap);
sTemp = urlSpe.queryItemValue( WMTS_REQUEST );
//处理请求capatibility
if( 0 == sTemp.compare(WMTS_REQUEST_VL_CAPABILITIES, Qt::CaseInsensitive) )
return _sendCapabilities(soap);
if( 0 != sTemp.compare(WMTS_REQUEST_VL_GETTILE), Qt::CaseInsensitive)
return _sendCapabilities(soap);
sTemp = urlSpe.queryItemValue( WMTS_VERSION );
if( sTemp.isEmpty() )
return _sendCapabilities(soap);
sTemp = urlSpe.queryItemValue(WMTS_LAYER);
if( sTemp.isEmpty() )
return _sendCapabilities(soap);
std::string stdstrLayr = sTemp.toLocal8Bit();
// 未使用
sTemp = urlSpe.queryItemValue(WMTS_LAYER_STYLE);
if( sTemp.isEmpty() )
return _sendCapabilities(soap);
// PNG
sTemp = urlSpe.queryItemValue(WMTS_TILE_FORMAT);
if( sTemp.isEmpty() )
return _sendCapabilities(soap);
// 未使用,内部算法应该是只有google的切片方案
sTemp = urlSpe.queryItemValue(WMTS_TILEMATRIXSET);
if( sTemp.isEmpty() )
return _sendCapabilities(soap);
// 等级
sTemp = urlSpe.queryItemValue(WMTS_TILEMATRIX);
if( sTemp.isEmpty() )
return _sendCapabilities(soap);
int nlv = sTemp.toInt();
// 行号
sTemp = urlSpe.queryItemValue(WMTS_TILEROW);
if( sTemp.isEmpty() )
return _sendCapabilities(soap);
int nRow = sTemp.toInt();
// 列号
sTemp = urlSpe.queryItemValue(WMTS_TILECOL);
if( sTemp.isEmpty() )
return _sendCapabilities(soap);
int nCol = sTemp.toInt();
// 从服务程序获取tile
int nDetail = 0;
QByteArray qAr;
int nSize = g_pWMTSDataCache->getTile( stdstrLayr, nlv, nRow, nCol, qAr, nDetail);
if( nSize > 0)
{
#ifdef _DEBUG
clock_t tEnd = clock();
int nSpan = 1000*(tEnd - tStart)/CLOCKS_PER_SEC;
//RAW_DLOG(INFO, "Get Tile Spend Time:%d ms", nSpan);
std::cout << "Get Tile Spend Time:"<< nSpan <<" ms" << std::endl;
#endif
#ifdef _DEBUG
tStart = clock();
#endif
int nRes = _sendData(soap, qAr, "image/png");
#ifdef _DEBUG
tEnd = clock();
nSpan = 1000*(tEnd - tStart)/CLOCKS_PER_SEC;
//DLOG(INFO) << "Send Tile Spend Time:" << nSpan << "ms";
std::cout << "Send Tile Spend Time:"<< nSpan <<" ms" << std::endl;
#endif
return nRes;
}
QString strMessage = QString(GB("没有制定瓦片数据,layer:%0,lv:%1,row:%2,col:%3")).arg(stdstrLayr.c_str()).arg(nlv).arg(nRow).arg(nCol);
m_pLogWriter->errorLog(strMessage);
return _sendExceptionMessage(soap, strMessage, "");
}
catch(...)
{
m_pLogWriter->errorLog( GB("解析url出错") );
QString strMessage = GB("解析url出错");
return _sendExceptionMessage(soap, strMessage, "");
}
}
int HttpWMSService::_sendCapabilities(struct soap *soap)
{
std::string sPath = g_pWMTSDataCache->getCapabilities();
QFile qfile( GB( sPath.c_str() ) );
qfile.open(QIODevice::ReadOnly | QIODevice::Text);
QByteArray qAr = qfile.readAll();
qfile.close();
return _sendData(soap, qAr, "text/xml");
}
int HttpWMSService::_sendData(struct soap *soap, QByteArray &baMapData, const char* szformat)
{
soap->bRequestSucess = true;
// 5.发送GisData
//soap->http_content = "image/png" ;
//std::string strFormat = (const char*)strformat.toLocal8Bit();
soap->http_content = szformat;
//soap->http_content = (const char*)strformat.toLocal8Bit();
if (soap_response(soap, SOAP_FILE)) /* OK HTTP response header */
{
soap_end_send(soap);
return soap->error;
}
QBuffer buffer(&baMapData);
buffer.open(QIODevice::ReadOnly);
QDataStream readMapData(&buffer);
for (;;)
{
size_t r = readMapData.readRawData (soap->tmpbuf, sizeof(soap->tmpbuf));
if (!r)
{
break;
}
if (soap_send_raw(soap, soap->tmpbuf, r))
{
soap_end_send(soap);
buffer.close();
return soap->error;
}
}
buffer.close();
return soap_end_send(soap);
}
int HttpWMSService::_sendExceptionMessage(struct soap *soap, const QString & strMessage, const QString &strCode, QString strVersion)
{
soap->bRequestSucess = false;
//创建标准的文件
QDomDocument doc("MyML");
//创建根
QDomElement root = doc.createElement("ServiceExceptionReport");
doc.appendChild(root);
root.setAttribute("version", strVersion);
QDomElement tagServiceException = doc.createElement("ServiceException");
tagServiceException.setAttribute("code", strCode);
QDomText t = doc.createTextNode(strMessage);
tagServiceException.appendChild(t);
root.appendChild(tagServiceException);
QString strxml = doc.toString();
strxml = strxml.remove("<!DOCTYPE MyML>\n");
QByteArray byaException = strxml.toUtf8 ();
return _sendData(soap, byaException , "text/xml");
}