/
qgswcsdataitems.cpp
317 lines (265 loc) · 9.25 KB
/
qgswcsdataitems.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
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
/***************************************************************************
qgswcsdataitems.cpp
---------------------
begin : 2 July, 2012
copyright : (C) 2012 by Radim Blazek
email : radim dot blazek at gmail.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 "qgsapplication.h"
#include "qgswcsdataitems.h"
#include "qgswcsprovider.h"
#include "qgslogger.h"
#include "qgsdatasourceuri.h"
#include "qgswcssourceselect.h"
#include "qgsowsconnection.h"
#include "qgsnewhttpconnection.h"
#include <QFileInfo>
#include <QSettings>
QgsWCSConnectionItem::QgsWCSConnectionItem( QgsDataItem* parent, QString name, QString path , QString uri )
: QgsDataCollectionItem( parent, name, path )
, mUri( uri )
{
mIcon = QgsApplication::getThemeIcon( "mIconWcs.svg" );
}
QgsWCSConnectionItem::~QgsWCSConnectionItem()
{
QgsDebugMsg( "Entered" );
}
QVector<QgsDataItem*> QgsWCSConnectionItem::createChildren()
{
QgsDebugMsg( "Entered" );
QVector<QgsDataItem*> children;
QgsDataSourceURI uri;
uri.setEncodedUri( mUri );
QgsDebugMsg( "mUri = " + mUri );
mCapabilities.setUri( uri );
// Attention: supportedLayers() gives tree leafes, not top level
if ( !mCapabilities.lastError().isEmpty() )
{
//children.append( new QgsErrorItem( this, tr( "Failed to retrieve layers" ), mPath + "/error" ) );
// TODO: show the error without adding child
return children;
}
foreach ( QgsWcsCoverageSummary coverageSummary, mCapabilities.capabilities().contents.coverageSummary )
{
// Attention, the name may be empty
QgsDebugMsg( QString::number( coverageSummary.orderId ) + " " + coverageSummary.identifier + " " + coverageSummary.title );
QString pathName = coverageSummary.identifier.isEmpty() ? QString::number( coverageSummary.orderId ) : coverageSummary.identifier;
QgsWCSLayerItem * layer = new QgsWCSLayerItem( this, coverageSummary.title, mPath + "/" + pathName, mCapabilities.capabilities(), uri, coverageSummary );
children.append( layer );
}
return children;
}
bool QgsWCSConnectionItem::equal( const QgsDataItem *other )
{
if ( type() != other->type() )
{
return false;
}
const QgsWCSConnectionItem *o = dynamic_cast<const QgsWCSConnectionItem *>( other );
if ( !o )
{
return false;
}
return ( mPath == o->mPath && mName == o->mName );
}
QList<QAction*> QgsWCSConnectionItem::actions()
{
QList<QAction*> lst;
QAction* actionEdit = new QAction( tr( "Edit..." ), this );
connect( actionEdit, SIGNAL( triggered() ), this, SLOT( editConnection() ) );
lst.append( actionEdit );
QAction* actionDelete = new QAction( tr( "Delete" ), this );
connect( actionDelete, SIGNAL( triggered() ), this, SLOT( deleteConnection() ) );
lst.append( actionDelete );
return lst;
}
void QgsWCSConnectionItem::editConnection()
{
QgsNewHttpConnection nc( 0, "/Qgis/connections-wcs/", mName );
if ( nc.exec() )
{
// the parent should be updated
mParent->refresh();
}
}
void QgsWCSConnectionItem::deleteConnection()
{
QgsOWSConnection::deleteConnection( "WCS", mName );
// the parent should be updated
mParent->refresh();
}
// ---------------------------------------------------------------------------
QgsWCSLayerItem::QgsWCSLayerItem( QgsDataItem* parent, QString name, QString path, QgsWcsCapabilitiesProperty capabilitiesProperty, QgsDataSourceURI dataSourceUri, QgsWcsCoverageSummary coverageSummary )
: QgsLayerItem( parent, name, path, QString(), QgsLayerItem::Raster, "wcs" ),
mCapabilities( capabilitiesProperty ),
mDataSourceUri( dataSourceUri ),
mCoverageSummary( coverageSummary )
{
QgsDebugMsg( "uri = " + mDataSourceUri.encodedUri() );
mUri = createUri();
// Populate everything, it costs nothing, all info about layers is collected
foreach ( QgsWcsCoverageSummary coverageSummary, mCoverageSummary.coverageSummary )
{
// Attention, the name may be empty
QgsDebugMsg( QString::number( coverageSummary.orderId ) + " " + coverageSummary.identifier + " " + coverageSummary.title );
QString pathName = coverageSummary.identifier.isEmpty() ? QString::number( coverageSummary.orderId ) : coverageSummary.identifier;
QgsWCSLayerItem * layer = new QgsWCSLayerItem( this, coverageSummary.title, mPath + "/" + pathName, mCapabilities, mDataSourceUri, coverageSummary );
mChildren.append( layer );
}
if ( mChildren.size() == 0 )
{
//mIcon = iconRaster();
mIcon = QgsApplication::getThemeIcon( "mIconWcs.svg" );
}
mPopulated = true;
}
QgsWCSLayerItem::~QgsWCSLayerItem()
{
}
QString QgsWCSLayerItem::createUri()
{
if ( mCoverageSummary.identifier.isEmpty() )
return ""; // layer collection
// Number of styles must match number of layers
mDataSourceUri.setParam( "identifier", mCoverageSummary.identifier );
// TODO(?): with WCS 1.0 GetCapabilities does not contain CRS and formats,
// to get them we would need to call QgsWcsCapabilities::describeCoverage
// but it is problematic to get QgsWcsCapabilities here (copy not allowed
// by QObject, pointer is dangerous (OWS provider is changing parent))
// We leave CRS and format default for now.
QString format;
// get first supported by GDAL and server
// TODO
//QStringList mimes = QgsGdalProvider::supportedMimes().keys();
QStringList mimes;
// prefer tiff
if ( mimes.contains( "image/tiff" ) && mCoverageSummary.supportedFormat.contains( "image/tiff" ) )
{
format = "image/tiff";
}
else
{
foreach ( QString f, mimes )
{
if ( mCoverageSummary.supportedFormat.indexOf( f ) >= 0 )
{
format = f;
break;
}
}
}
if ( !format.isEmpty() )
{
mDataSourceUri.setParam( "format", format );
}
QString crs;
// TODO: prefer project CRS
// get first known if possible
QgsCoordinateReferenceSystem testCrs;
foreach ( QString c, mCoverageSummary.supportedCrs )
{
testCrs.createFromOgcWmsCrs( c );
if ( testCrs.isValid() )
{
crs = c;
break;
}
}
if ( crs.isEmpty() && mCoverageSummary.supportedCrs.size() > 0 )
{
crs = mCoverageSummary.supportedCrs.value( 0 );
}
if ( !crs.isEmpty() )
{
mDataSourceUri.setParam( "crs", crs );
}
return mDataSourceUri.encodedUri();
}
// ---------------------------------------------------------------------------
QgsWCSRootItem::QgsWCSRootItem( QgsDataItem* parent, QString name, QString path )
: QgsDataCollectionItem( parent, name, path )
{
mIcon = QgsApplication::getThemeIcon( "mIconWcs.svg" );
populate();
}
QgsWCSRootItem::~QgsWCSRootItem()
{
}
QVector<QgsDataItem*>QgsWCSRootItem::createChildren()
{
QVector<QgsDataItem*> connections;
foreach ( QString connName, QgsOWSConnection::connectionList( "WCS" ) )
{
QgsOWSConnection connection( "WCS", connName );
QgsDataItem * conn = new QgsWCSConnectionItem( this, connName, mPath + "/" + connName, connection.uri().encodedUri() );
conn->setIcon( QgsApplication::getThemeIcon( "mIconConnect.png" ) );
connections.append( conn );
}
return connections;
}
QList<QAction*> QgsWCSRootItem::actions()
{
QList<QAction*> lst;
QAction* actionNew = new QAction( tr( "New Connection..." ), this );
connect( actionNew, SIGNAL( triggered() ), this, SLOT( newConnection() ) );
lst.append( actionNew );
return lst;
}
QWidget * QgsWCSRootItem::paramWidget()
{
QgsWCSSourceSelect *select = new QgsWCSSourceSelect( 0, 0, true, true );
connect( select, SIGNAL( connectionsChanged() ), this, SLOT( connectionsChanged() ) );
return select;
}
void QgsWCSRootItem::connectionsChanged()
{
refresh();
}
void QgsWCSRootItem::newConnection()
{
QgsNewHttpConnection nc( 0, "/Qgis/connections-wcs/" );
if ( nc.exec() )
{
refresh();
}
}
// ---------------------------------------------------------------------------
static QString filterString;
static QStringList extensions = QStringList();
static QStringList wildcards = QStringList();
QGISEXTERN int dataCapabilities()
{
return QgsDataProvider::Net;
}
QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
{
QgsDebugMsg( "thePath = " + thePath );
if ( thePath.isEmpty() )
{
// Top level WCS
return new QgsWCSRootItem( parentItem, "WCS", "wcs:" );
}
// path schema: wcs:/connection name (used by OWS)
if ( thePath.startsWith( "wcs:/" ) )
{
QString connectionName = thePath.split( '/' ).last();
if ( QgsOWSConnection::connectionList( "WCS" ).contains( connectionName ) )
{
QgsOWSConnection connection( "WCS", connectionName );
return new QgsWCSConnectionItem( parentItem, "WCS", thePath, connection.uri().encodedUri() );
}
}
return 0;
}
QGISEXTERN QgsWCSSourceSelect * selectWidget( QWidget * parent, Qt::WindowFlags fl )
{
return new QgsWCSSourceSelect( parent, fl );
}