~ubuntu-branches/ubuntu/hardy/libterralib/hardy

« back to all changes in this revision

Viewing changes to src/terralib/kernel/TeTemporalSeries.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Daniel T Chen
  • Date: 2005-11-25 22:32:59 UTC
  • Revision ID: james.westby@ubuntu.com-20051125223259-3zubal8ux4ki4fjg
Tags: upstream-3.0.3b2
Import upstream version 3.0.3b2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/************************************************************************************
 
2
TerraLib - a library for developing GIS applications.
 
3
Copyright � 2001-2004 INPE and Tecgraf/PUC-Rio.
 
4
 
 
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.
 
10
 
 
11
You should have received a copy of the GNU Lesser General Public
 
12
License along with this library.
 
13
 
 
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
*************************************************************************************/
 
23
 
 
24
#include "TeTemporalSeries.h"
 
25
#include "TeDatabase.h"
 
26
 
 
27
 
 
28
TeTSEntry::TeTSEntry(const double& val, TeTimeInterval& time, int timeFrame, int nItens, TeTSDataType dType)
 
29
{
 
30
        timeFrame_= timeFrame;
 
31
        value_ = val;
 
32
        type_ = dType;
 
33
        nItens_ = nItens;
 
34
        time_ = time;
 
35
        timeInt_ = -1;
 
36
 
 
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();
 
51
}
 
52
 
 
53
 
 
54
 
 
55
TeTSEntry& 
 
56
TeTSEntry::operator=(const TeTSEntry& other)
 
57
{
 
58
        if ( this != &other )
 
59
        {
 
60
                timeFrame_ = other.timeFrame_;
 
61
                value_ = other.value_;
 
62
                time_ = other.time_;
 
63
                nItens_ = other.nItens_;
 
64
                type_ = other.type_;
 
65
                timeInt_ = other.timeInt_;
 
66
        }
 
67
        return (*this);
 
68
}
 
69
 
 
70
TeTSParams& 
 
71
TeTSParams::operator=(const TeTSParams& other)
 
72
{
 
73
        if ( this != &other )
 
74
        {
 
75
                type_ = other.type_;
 
76
                chronon_ = other.chronon_;
 
77
                time_ = other.time_; 
 
78
                timeOper_ = other.timeOper_;
 
79
                spatOper_ = other.spatOper_;
 
80
                orTheme_ = other.orTheme_;
 
81
                objectId_ = other.objectId_;
 
82
                attrTable_ = other.attrTable_;  
 
83
                attrColumn_ = other.attrColumn_;
 
84
        }
 
85
        return (*this);
 
86
}
 
87
 
 
88
 
 
89
TeTemporalSeries& 
 
90
TeTemporalSeries::operator=(const TeTemporalSeries& other)
 
91
{
 
92
        if ( this != &other )
 
93
        {
 
94
                TSparams_ = other.TSparams_;
 
95
                series_ = other.series_;
 
96
        }
 
97
        return (*this);
 
98
}
 
99
 
 
100
 
 
101
bool 
 
102
TeTemporalSeries::getTSEntry(TeTSEntry& ts, unsigned int frame)
 
103
{
 
104
        if(frame >= (series_.size()))
 
105
                return false;
 
106
 
 
107
        ts = series_[frame];
 
108
        return true;
 
109
}
 
110
 
 
111
bool 
 
112
TeTemporalSeries::buildFrameIntervals()
 
113
{
 
114
        // ----- information about temporal table 
 
115
        TeTable tempTable;
 
116
        TeDatabase* db = TSparams_.orTheme_->layer()->database();
 
117
 
 
118
        if(!TSparams_.orTheme_->getTemporalTable(tempTable))
 
119
                return false;
 
120
 
 
121
        TSparams_.attrTable_ = tempTable.name();
 
122
        string initialtime = tempTable.name() +"."+ tempTable.attInitialTime(); 
 
123
        string finaltime = tempTable.name() +"."+ tempTable.attFinalTime(); 
 
124
 
 
125
        TeTime iTime, fTime;
 
126
        
 
127
        // ----- fill time interval
 
128
        // ---------------------------- you don�t know how many time frames will be generated
 
129
 
 
130
        if(     (TSparams_.chronon_==TeYEAR)   || (TSparams_.chronon_==TeMONTH) ||
 
131
                (TSparams_.chronon_==TeDAY)    || (TSparams_.chronon_==TeHOUR)  ||
 
132
                (TSparams_.chronon_==TeMINUTE) || (TSparams_.chronon_==TeSECOND) )
 
133
        {
 
134
                
 
135
                string fromClause, whereClause;
 
136
                if(!db->tableExist(TSparams_.orTheme_->collectionAuxTable()))
 
137
                {
 
138
                        TeAttrTableVector atts; 
 
139
                        TSparams_.orTheme_->getAttTables(atts); 
 
140
                        if(atts.empty())
 
141
                                return false;
 
142
 
 
143
                        fromClause = " FROM " + tableJoin(atts);
 
144
                        whereClause = TSparams_.orTheme_->sqlWhereRestrictions();
 
145
                }
 
146
                else
 
147
                {
 
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 +" = ";;
 
153
                        
 
154
                        if(tempTable.tableType()==TeFixedGeomDynAttr)
 
155
                                fromClause += collAuxTable +".aux0";
 
156
                        else
 
157
                                fromClause += collAuxTable +".unique_id";
 
158
                }
 
159
 
 
160
                string sql = " SELECT min("+ initialtime +"), max("+ finaltime +") ";
 
161
                sql += fromClause;      
 
162
                
 
163
                if(!whereClause.empty())
 
164
                        sql += " WHERE "+ whereClause;
 
165
 
 
166
                TeDatabasePortal* portal = db->getPortal();
 
167
                if(!portal)
 
168
                        return false;
 
169
 
 
170
                if((!portal->query(sql)) || (!portal->fetchRow()))
 
171
                {
 
172
                        delete portal;
 
173
                        return false;
 
174
                }
 
175
 
 
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
 
181
 
 
182
                iTime = interval.getT1();
 
183
                fTime = interval.getT2();
 
184
 
 
185
                delete (portal);
 
186
        }
 
187
 
 
188
        // ---------------------------- you know how many time frames will be generated
 
189
 
 
190
        else if (TSparams_.chronon_==TeMONTHOFYEAR) 
 
191
        {
 
192
                TeTime t1("01/01/2001", TeMONTHOFYEAR, "DDsMMsYYYY");
 
193
                TeTime t2("01/12/2001", TeMONTHOFYEAR, "DDsMMsYYYY");
 
194
                iTime = t1;
 
195
                fTime = t2;
 
196
        }
 
197
        else if(TSparams_.chronon_==TeDAYOFMONTH)
 
198
        {
 
199
                TeTime t1("01/01/2001", TeDAYOFMONTH, "DDsMMsYYYY");
 
200
                TeTime t2("31/01/2001", TeDAYOFMONTH, "DDsMMsYYYY");
 
201
                iTime = t1;
 
202
                fTime = t2;
 
203
        }
 
204
        else if(TSparams_.chronon_==TeDAYOFWEEK)
 
205
        {
 
206
                TeTime t1("07/01/2001", TeDAYOFWEEK, "DDsMMsYYYY");
 
207
                TeTime t2("13/01/2001", TeDAYOFWEEK, "DDsMMsYYYY");
 
208
                iTime = t1;
 
209
                fTime = t2;
 
210
        }
 
211
        else if(TSparams_.chronon_==TeDAYOFYEAR)
 
212
        {
 
213
                TeTime t1("01/01/2001", TeDAYOFYEAR, "DDsMMsYYYY");
 
214
                TeTime t2("31/12/2001", TeDAYOFYEAR, "DDsMMsYYYY");
 
215
                iTime = t1;
 
216
                fTime = t2;
 
217
        }
 
218
        else if(TSparams_.chronon_==TeHOUROFDAY)
 
219
        {
 
220
                TeTime t1("01/01/2001 00", TeHOUROFDAY, "DDsMMsYYYYsHH");
 
221
                TeTime t2("01/01/2001 23", TeHOUROFDAY, "DDsMMsYYYYsHH");
 
222
                iTime = t1;
 
223
                fTime = t2;
 
224
        }
 
225
        else if(TSparams_.chronon_==TeMINUTEOFHOUR)
 
226
        {
 
227
                TeTime t1("01/01/2001 00:00", TeMINUTEOFHOUR, "DDsMMsYYYYsHHsmm");
 
228
                TeTime t2("01/01/2001 00:59", TeMINUTEOFHOUR, "DDsMMsYYYYsHHsmm");
 
229
                iTime = t1;
 
230
                fTime = t2;
 
231
        }
 
232
        else if(TSparams_.chronon_==TeSECONDOFMINUTE)
 
233
        {
 
234
                TeTime t1("01/01/2001 00:00:00", TeSECONDOFMINUTE, "DDsMMsYYYYsHHsmmsSS");
 
235
                TeTime t2("01/01/2001 00:00:59", TeSECONDOFMINUTE, "DDsMMsYYYYsHHsmmsSS");
 
236
                iTime = t1;
 
237
                fTime = t2;
 
238
        }
 
239
 
 
240
        int timeFrame = 0;
 
241
        while (iTime <= fTime)
 
242
        {
 
243
                TeTimeInterval inter(iTime,iTime);
 
244
                inter.intervalChronon(TSparams_.chronon_); //min/max
 
245
                TeTSEntry tsEntry(0., inter, timeFrame);
 
246
                this->series_.push_back(tsEntry);
 
247
                ++iTime;
 
248
                ++timeFrame;
 
249
        }
 
250
 
 
251
        return true;
 
252
}
 
253
 
 
254
bool 
 
255
TeTemporalSeries::setTSEntryValue(unsigned int frame, double val, TeTSDataType type)
 
256
{
 
257
 
 
258
        TeTSEntry entry;
 
259
        if(!getTSEntry(entry, frame))
 
260
                return false;
 
261
 
 
262
        double val1 = entry.value_;
 
263
        double result = val1;
 
264
 
 
265
        if(TSparams_.timeOper_==TeCOUNT)
 
266
                result += 1;
 
267
        else if (TSparams_.timeOper_==TeSUM)
 
268
                result += val;
 
269
        else if ((TSparams_.timeOper_==TeMAXVALUE) && (val>val1))
 
270
                result = val;
 
271
        else if((TSparams_.timeOper_==TeMINVALUE) && (val<val1))
 
272
                result = val;
 
273
        else if(TSparams_.timeOper_==TeMEAN)
 
274
                result = (val1+val)/2;
 
275
 
 
276
        series_[frame].value_ = result;
 
277
        series_[frame].type_ = type;
 
278
        return true;
 
279
}
 
280
 
 
281
 
 
282
 
 
283