-
Notifications
You must be signed in to change notification settings - Fork 1
/
qgswatermldata.cpp
161 lines (144 loc) · 4.44 KB
/
qgswatermldata.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
/***************************************************************************
qgswatermldata.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 "qgswatermldata.h"
#include "qgsmessagelog.h"
#include "qgsnetworkaccessmanager.h"
#include <QApplication>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QProgressDialog>
const char NS_SEPARATOR = '?';
const QString WML_NS = "http://www.opengis.net/waterml/2.0";
const QString timeString = WML_NS + NS_SEPARATOR + "time";
const QString valueString = WML_NS + NS_SEPARATOR + "value";
const QString measurementString = WML_NS + NS_SEPARATOR + "MeasurementTVP";
QgsWaterMLData::QgsWaterMLData( const QString& url ): QgsXMLData( url ), mParseMode( None )
{
}
QgsWaterMLData::~QgsWaterMLData()
{
}
int QgsWaterMLData::getData( QMap< QDateTime, double >* timeValueMap )
{
if ( !timeValueMap )
{
return 2;
}
mTimeValueMap = timeValueMap;
mTimeValueMap->clear();
QWidget* mainWindow = 0;
QWidgetList topLevelWidgets = qApp->topLevelWidgets();
for ( QWidgetList::iterator it = topLevelWidgets.begin(); it != topLevelWidgets.end(); ++it )
{
if (( *it )->objectName() == "QgisApp" )
{
mainWindow = *it;
break;
}
}
QProgressDialog* progressDialog = 0;
if ( mainWindow )
{
progressDialog = new QProgressDialog( tr( "Loading sensor data" ), tr( "Abort" ), 0, 0, mainWindow );
connect( this, SIGNAL( dataReadProgress( int ) ), progressDialog, SLOT( setValue( int ) ) );
connect( this, SIGNAL( totalStepsUpdate( int ) ) , progressDialog, SLOT( setMaximum( int ) ) );
connect( progressDialog, SIGNAL( canceled() ), this, SLOT( setFinished() ) );
}
return getXMLData( progressDialog );
delete progressDialog;
return 0;
}
void QgsWaterMLData::startElement( const XML_Char* el, const XML_Char** attr )
{
Q_UNUSED( attr );
if ( el == timeString )
{
mParseMode = Time;
mStringCache.clear();
}
else if ( el == valueString )
{
mParseMode = Value;
mStringCache.clear();
}
else
{
mParseMode = None;
}
}
void QgsWaterMLData::endElement( const XML_Char* el )
{
if ( el == timeString )
{
mCurrentDateTime = convertTimeString( mStringCache );
mParseMode = None;
}
else if ( el == valueString )
{
mCurrentValue = mStringCache.toDouble();
mParseMode = None;
}
else if ( el == measurementString )
{
mTimeValueMap->insert( mCurrentDateTime, mCurrentValue );
}
}
void QgsWaterMLData::characters( const XML_Char* chars, int len )
{
if ( mParseMode == Time || mParseMode == Value )
{
mStringCache.append( QString::fromUtf8( chars, len ) );
}
}
QDateTime QgsWaterMLData::convertTimeString( const QString& str )
{
QStringList timeSplit = str.split( "T" );
if ( timeSplit.size() < 2 )
{
//only date given
int day = 1;
int month = 1;
int year = 1;
QStringList yearMonthSplit = str.split( "-" );
if ( yearMonthSplit.size() > 0 )
{
year = yearMonthSplit.at( 0 ).toInt();
}
if ( yearMonthSplit.size() > 1 )
{
month = yearMonthSplit.at( 1 ).toInt();
}
if ( yearMonthSplit.size() > 2 )
{
day = yearMonthSplit.at( 2 ).toInt();
}
QDate date;
date.setDate( year, month, day );
QDateTime dateTime( date );
return dateTime;
}
else
{
QStringList plusSplit = str.split( "+" );
QDateTime dateTime = QDateTime::fromString( plusSplit.at( 0 ), "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'z" );
dateTime.setTimeSpec( Qt::OffsetFromUTC );
if ( plusSplit.size() > 1 )
{
//offset from UTC
}
return dateTime;
}
}