-
Notifications
You must be signed in to change notification settings - Fork 1
/
qgsmaptoolsensorinfo.cpp
287 lines (248 loc) · 9.32 KB
/
qgsmaptoolsensorinfo.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
/***************************************************************************
qgsmaptoolsensorinfo.cpp - description
--------------------------------------
begin : June 2013
copyright : (C) 2013 by Marco Hugentobler
email : marco dot hugentobler at sourcepole dot ch
***************************************************************************/
/***************************************************************************
* *
* 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 "qgsmaptoolsensorinfo.h"
#include "qgsmapcanvas.h"
#include "qgsmapmouseevent.h"
#include "qgsmaptoolidentify.h"
#include "qgsnetworkaccessmanager.h"
#include "qgsrubberband.h"
#include "qgssensorinfodialog.h"
#include "qgsvectordataprovider.h"
#include "qgsvectorlayer.h"
#include <QDateTime>
#include <QMouseEvent>
#include <QNetworkReply>
#include <QNetworkRequest>
QgsMapToolSensorInfo::QgsMapToolSensorInfo( QgsMapCanvas* canvas ): QgsMapTool( canvas ),
mDataAvailabilityRequestFinished( true ), mSensorInfoDialog( 0 ), mRubberBand( 0 ), mDragging( false )
{
}
QgsMapToolSensorInfo::~QgsMapToolSensorInfo()
{
delete mSensorInfoDialog;
delete mRubberBand;
}
void QgsMapToolSensorInfo::canvasMoveEvent( QgsMapMouseEvent* e )
{
if ( e->buttons() != Qt::LeftButton )
return;
if ( !mDragging )
{
mDragging = true;
mSelectRect.setTopLeft( e->pos() );
}
mSelectRect.setBottomRight( e->pos() );
if( mRubberBand )
{
mRubberBand->setToCanvasRectangle( mSelectRect );
}
}
void QgsMapToolSensorInfo::canvasPressEvent( QgsMapMouseEvent* e )
{
mSelectRect.setRect( 0, 0, 0, 0 );
delete mRubberBand;
mRubberBand = new QgsRubberBand( mCanvas, QgsWkbTypes::PolygonGeometry );
mRubberBand->setFillColor( QColor( 255, 0, 0, 127 ) );
}
void QgsMapToolSensorInfo::canvasReleaseEvent( QgsMapMouseEvent* e )
{
delete mRubberBand;
mRubberBand = 0;
QList< QgsMapLayer* > sensorLayerList = sensorLayers();
if ( sensorLayerList.isEmpty() )
{
return;
}
showSensorInfoDialog();
mSensorInfoDialog->clearObservables();
QList< QgsMapLayer* >::const_iterator layerIt = sensorLayerList.begin();
for(; layerIt != sensorLayerList.end(); ++layerIt )
{
QgsVectorLayer* vl = dynamic_cast<QgsVectorLayer*>( *layerIt );
if( !vl )
{
return;
}
QgsDataProvider* dp = vl->dataProvider();
if( !dp )
{
return;
}
//rectangle in layer CRS
QgsPointXY llRect;
QgsPointXY urRect;
if( mDragging )
{
llRect = toMapCoordinates( mSelectRect.bottomLeft () );
urRect = toMapCoordinates( mSelectRect.topRight() );
}
else //click
{
QPoint clickPoint = e->originalPixelPoint();
//+- 5 pixel tolerance
llRect = toMapCoordinates( QPoint( clickPoint.x() - 5, clickPoint.y() + 5 ) );
urRect = toMapCoordinates( QPoint( clickPoint.x() + 5, clickPoint.y() - 5 ) );
}
QgsRectangle selectRect = toLayerCoordinates( *layerIt, QgsRectangle( llRect.x(), llRect.y(), urRect.x(), urRect.y() ) );
QgsFeatureRequest fReq( selectRect );
QgsFeatureIterator fIt = vl->getFeatures( fReq );
QgsFeature f;
while( fIt.nextFeature( f ) )
{
QString name = f.attribute( "name" ).toString();
QString id = f.attribute( "identifier" ).toString();
//data structure with id / name / list< observable, start time, end time>
QStringList observedProperties;
QList< QDateTime > beginList;
QList< QDateTime > endList;
QStringList filteredProperties;
QList< QDateTime > filteredBegin;
QList< QDateTime > filteredEnd;
getDataAvailability( dp->dataSourceUri(), id, observedProperties, beginList, endList, filteredProperties, filteredBegin, filteredEnd );
mSensorInfoDialog->addObservables( dp->dataSourceUri(), id, name, observedProperties, beginList, endList );
mSensorInfoDialog->addHiddenObservables( dp->dataSourceUri(), id, name, filteredProperties, filteredBegin, filteredEnd );
}
}
mDragging = false;
}
QList< QgsMapLayer* > QgsMapToolSensorInfo::sensorLayers()
{
QList< QgsMapLayer* > sensorLayerList;
if ( !mCanvas )
{
return sensorLayerList;
}
QList<QgsMapLayer*> layerList = mCanvas->layers();
QList<QgsMapLayer*>::iterator layerIt = layerList.begin();
for ( ; layerIt != layerList.end(); ++layerIt )
{
QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( *layerIt );
if ( vl && vl->dataProvider() )
{
if ( vl->dataProvider()->name() == "SOS" )
{
sensorLayerList.append( vl );
}
}
}
return sensorLayerList;
}
int QgsMapToolSensorInfo::getDataAvailability( const QString& serviceUrl, const QString& station_id,
QStringList& observedPropertyList, QList< QDateTime >& beginList, QList< QDateTime >& endList,
QStringList& filteredPropertyList, QList< QDateTime >& filteredBeginList, QList< QDateTime >& filteredEndList )
{
if ( !mDataAvailabilityRequestFinished ) //another request is still in progress
{
return 1;
}
QUrl url( serviceUrl );
QUrlQuery query(url);
//filter list to only get the observables selected for the sos layer
QSet<QString> selectedObservables = QUrl::fromPercentEncoding( query.queryItemValue( "observedProperty" )\
.toLocal8Bit() ).split( ",", QString::SkipEmptyParts ).toSet();
query.removeQueryItem( "observedProperty" );
query.removeQueryItem( "request" );
query.addQueryItem( "request", "GetDataAvailability" );
query.addQueryItem( "featureofinterest", station_id );
url.setQuery( query );
QString debug = url.toString();
mDataAvailabilityRequestFinished = false;
QUrl u = QUrl::fromEncoded( url.toString().toLocal8Bit() );
QNetworkRequest request( u );
QNetworkReply* reply = QgsNetworkAccessManager::instance()->get( request );
connect( reply, SIGNAL( finished() ), this, SLOT( dataAvailabilityRequestFinished() ) );
while ( !mDataAvailabilityRequestFinished )
{
QCoreApplication::processEvents( QEventLoop::ExcludeUserInputEvents, 200 );
}
//read results from xml
QByteArray response = reply->readAll();
reply->deleteLater();
QDomDocument dataAvailabilityDocument;
if ( !dataAvailabilityDocument.setContent( response, true ) )
{
return 2;
}
QSet<QString> observedPropertySet; //keep track of duplicates
QDomNodeList dataAvailabilityList = dataAvailabilityDocument.elementsByTagNameNS( "http://www.opengis.net/sosgda/1.0", "dataAvailabilityMember" );
for ( int i = 0; i < dataAvailabilityList.size(); ++i )
{
QDomElement observedPropertyElem = dataAvailabilityList.at( i ).firstChildElement( "observedProperty" );
if ( observedPropertyElem.isNull() )
{
continue;
}
//property was not requested
QString property = observedPropertyElem.attribute( "href" );
bool propertyRequested = true;
if( !selectedObservables.isEmpty() && !selectedObservables.contains( property ) )
{
propertyRequested = false;
}
QDateTime beginTime;
QDateTime endTime;
QDomElement phenomenonTimeElem = dataAvailabilityList.at( i ).firstChildElement( "phenomenonTime" );
if ( !phenomenonTimeElem.isNull() )
{
QDomElement timePeriodElem = phenomenonTimeElem.firstChildElement( "TimePeriod" );
if ( !timePeriodElem.isNull() )
{
QDomElement beginElem = timePeriodElem.firstChildElement( "beginPosition" );
if ( !beginElem.isNull() )
{
beginTime = QDateTime::fromString( beginElem.text(), "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.000+00:00'" );
}
QDomElement endElem = timePeriodElem.firstChildElement( "endPosition" );
if ( !endElem.isNull() )
{
endTime = QDateTime::fromString( endElem.text(), "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.000+00:00'" );
}
}
}
if ( observedPropertySet.contains( property ) )
{
continue;
}
observedPropertySet.insert( property );
if( propertyRequested )
{
observedPropertyList.push_back( property );
beginList.push_back( beginTime );
endList.push_back( endTime );
}
else
{
filteredPropertyList.push_back( property );
filteredBeginList.push_back( beginTime );
filteredEndList.push_back( endTime );
}
}
return 0;
}
void QgsMapToolSensorInfo::dataAvailabilityRequestFinished()
{
mDataAvailabilityRequestFinished = true;
}
void QgsMapToolSensorInfo::showSensorInfoDialog()
{
if ( !mSensorInfoDialog )
{
mSensorInfoDialog = new QgsSensorInfoDialog();
}
mSensorInfoDialog->openObservablesTab();
mSensorInfoDialog->show();
mSensorInfoDialog->raise();
}