1
/************************************************************************************
2
TerraLib - a library for developing GIS applications.
3
Copyright � 2001-2004 INPE and Tecgraf/PUC-Rio.
5
This code is part of the TerraLib library.
6
This library is free software; you can redistribute it and/or
7
modify it under the terms of the GNU Lesser General Public
8
License as published by the Free Software Foundation; either
9
version 2.1 of the License, or (at your option) any later version.
11
You should have received a copy of the GNU Lesser General Public
12
License along with this library.
14
The authors reassure the license terms regarding the warranties.
15
They specifically disclaim any warranties, including, but not limited to,
16
the implied warranties of merchantability and fitness for a particular purpose.
17
The library provided hereunder is on an "as is" basis, and the authors have no
18
obligation to provide maintenance, support, updates, enhancements, or modifications.
19
In no event shall INPE and Tecgraf / PUC-Rio be held liable to any party for direct,
20
indirect, special, incidental, or consequential damages arising out of the use
21
of this library and its documentation.
22
*************************************************************************************/
24
#include "TeTemporalSeries.h"
25
#include "TeDatabase.h"
28
TeTSEntry::TeTSEntry(const double& val, TeTimeInterval& time, int timeFrame, int nItens, TeTSDataType dType)
30
timeFrame_= timeFrame;
37
if (time.intervalChronon()==TeMONTHOFYEAR)
38
timeInt_ = time.getT1().month();
39
else if(time.intervalChronon()==TeDAYOFMONTH)
40
timeInt_ = time.getT1().day();
41
else if(time.intervalChronon()==TeDAYOFWEEK)
42
timeInt_ = time.getT1().weekDay();
43
else if(time.intervalChronon()==TeDAYOFYEAR)
44
timeInt_ = time.getT1().yearDay();
45
else if(time.intervalChronon()==TeHOUROFDAY)
46
timeInt_ = time.getT1().hour();
47
else if(time.intervalChronon()==TeMINUTEOFHOUR)
48
timeInt_ = time.getT1().minute();
49
else if(time.intervalChronon()==TeSECONDOFMINUTE)
50
timeInt_ = time.getT1().hour();
56
TeTSEntry::operator=(const TeTSEntry& other)
60
timeFrame_ = other.timeFrame_;
61
value_ = other.value_;
63
nItens_ = other.nItens_;
65
timeInt_ = other.timeInt_;
71
TeTSParams::operator=(const TeTSParams& other)
76
chronon_ = other.chronon_;
78
timeOper_ = other.timeOper_;
79
spatOper_ = other.spatOper_;
80
orTheme_ = other.orTheme_;
81
objectId_ = other.objectId_;
82
attrTable_ = other.attrTable_;
83
attrColumn_ = other.attrColumn_;
90
TeTemporalSeries::operator=(const TeTemporalSeries& other)
94
TSparams_ = other.TSparams_;
95
series_ = other.series_;
102
TeTemporalSeries::getTSEntry(TeTSEntry& ts, unsigned int frame)
104
if(frame >= (series_.size()))
112
TeTemporalSeries::buildFrameIntervals()
114
// ----- information about temporal table
116
TeDatabase* db = TSparams_.orTheme_->layer()->database();
118
if(!TSparams_.orTheme_->getTemporalTable(tempTable))
121
TSparams_.attrTable_ = tempTable.name();
122
string initialtime = tempTable.name() +"."+ tempTable.attInitialTime();
123
string finaltime = tempTable.name() +"."+ tempTable.attFinalTime();
127
// ----- fill time interval
128
// ---------------------------- you don�t know how many time frames will be generated
130
if( (TSparams_.chronon_==TeYEAR) || (TSparams_.chronon_==TeMONTH) ||
131
(TSparams_.chronon_==TeDAY) || (TSparams_.chronon_==TeHOUR) ||
132
(TSparams_.chronon_==TeMINUTE) || (TSparams_.chronon_==TeSECOND) )
135
string fromClause, whereClause;
136
if(!db->tableExist(TSparams_.orTheme_->collectionAuxTable()))
138
TeAttrTableVector atts;
139
TSparams_.orTheme_->getAttTables(atts);
143
fromClause = " FROM " + tableJoin(atts);
144
whereClause = TSparams_.orTheme_->sqlWhereRestrictions();
148
//fromClause = TSparams_.orTheme_->sqlGridFrom();
149
string collAuxTable = TSparams_.orTheme_->collectionAuxTable();
150
string uniqueIdName = tempTable.name() +"."+ tempTable.uniqueName();
151
fromClause = " FROM "+ tempTable.name()+" RIGHT JOIN "+ collAuxTable;
152
fromClause += " ON "+ uniqueIdName +" = ";;
154
if(tempTable.tableType()==TeFixedGeomDynAttr)
155
fromClause += collAuxTable +".aux0";
157
fromClause += collAuxTable +".unique_id";
160
string sql = " SELECT min("+ initialtime +"), max("+ finaltime +") ";
163
if(!whereClause.empty())
164
sql += " WHERE "+ whereClause;
166
TeDatabasePortal* portal = db->getPortal();
170
if((!portal->query(sql)) || (!portal->fetchRow()))
176
//fill the temporal series
177
TeTime date1 = portal->getDate(0);
178
TeTime date2 = portal->getDate(1);
179
TeTimeInterval interval(date1, date2);
180
interval.intervalChronon(TSparams_.chronon_); //min/max
182
iTime = interval.getT1();
183
fTime = interval.getT2();
188
// ---------------------------- you know how many time frames will be generated
190
else if (TSparams_.chronon_==TeMONTHOFYEAR)
192
TeTime t1("01/01/2001", TeMONTHOFYEAR, "DDsMMsYYYY");
193
TeTime t2("01/12/2001", TeMONTHOFYEAR, "DDsMMsYYYY");
197
else if(TSparams_.chronon_==TeDAYOFMONTH)
199
TeTime t1("01/01/2001", TeDAYOFMONTH, "DDsMMsYYYY");
200
TeTime t2("31/01/2001", TeDAYOFMONTH, "DDsMMsYYYY");
204
else if(TSparams_.chronon_==TeDAYOFWEEK)
206
TeTime t1("07/01/2001", TeDAYOFWEEK, "DDsMMsYYYY");
207
TeTime t2("13/01/2001", TeDAYOFWEEK, "DDsMMsYYYY");
211
else if(TSparams_.chronon_==TeDAYOFYEAR)
213
TeTime t1("01/01/2001", TeDAYOFYEAR, "DDsMMsYYYY");
214
TeTime t2("31/12/2001", TeDAYOFYEAR, "DDsMMsYYYY");
218
else if(TSparams_.chronon_==TeHOUROFDAY)
220
TeTime t1("01/01/2001 00", TeHOUROFDAY, "DDsMMsYYYYsHH");
221
TeTime t2("01/01/2001 23", TeHOUROFDAY, "DDsMMsYYYYsHH");
225
else if(TSparams_.chronon_==TeMINUTEOFHOUR)
227
TeTime t1("01/01/2001 00:00", TeMINUTEOFHOUR, "DDsMMsYYYYsHHsmm");
228
TeTime t2("01/01/2001 00:59", TeMINUTEOFHOUR, "DDsMMsYYYYsHHsmm");
232
else if(TSparams_.chronon_==TeSECONDOFMINUTE)
234
TeTime t1("01/01/2001 00:00:00", TeSECONDOFMINUTE, "DDsMMsYYYYsHHsmmsSS");
235
TeTime t2("01/01/2001 00:00:59", TeSECONDOFMINUTE, "DDsMMsYYYYsHHsmmsSS");
241
while (iTime <= fTime)
243
TeTimeInterval inter(iTime,iTime);
244
inter.intervalChronon(TSparams_.chronon_); //min/max
245
TeTSEntry tsEntry(0., inter, timeFrame);
246
this->series_.push_back(tsEntry);
255
TeTemporalSeries::setTSEntryValue(unsigned int frame, double val, TeTSDataType type)
259
if(!getTSEntry(entry, frame))
262
double val1 = entry.value_;
263
double result = val1;
265
if(TSparams_.timeOper_==TeCOUNT)
267
else if (TSparams_.timeOper_==TeSUM)
269
else if ((TSparams_.timeOper_==TeMAXVALUE) && (val>val1))
271
else if((TSparams_.timeOper_==TeMINVALUE) && (val<val1))
273
else if(TSparams_.timeOper_==TeMEAN)
274
result = (val1+val)/2;
276
series_[frame].value_ = result;
277
series_[frame].type_ = type;