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

« back to all changes in this revision

Viewing changes to src/terralib/functions/TeCellAlgorithms.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 "TeCellAlgorithms.h"
 
25
#include "TeGeometryAlgorithms.h"
 
26
#include "TeDatabase.h"
 
27
#include "TeProgress.h"
 
28
#include "TeRepresentation.h"
 
29
#include "TeGeneralizedProxMatrix.h"
 
30
 
 
31
TeLayer* 
 
32
TeCreateCells( const string& layerName, TeLayer* layerBase, 
 
33
                           double resX, double resY, TeBox& box, bool mask)
 
34
{
 
35
        if (!layerBase || layerName.empty())
 
36
                return 0;
 
37
 
 
38
        TeDatabase* db = layerBase->database();
 
39
        if (!db)
 
40
                return 0;
 
41
 
 
42
        TeDatabasePortal* portal = db->getPortal();
 
43
        if (!portal)
 
44
                return 0;
 
45
 
 
46
        TeProjection* proj = layerBase->projection();
 
47
 
 
48
        string newLayerName = layerName;
 
49
        TeLayerMap& layerMap = db->layerMap();
 
50
        TeLayerMap::iterator it;
 
51
        bool flag = true;
 
52
        int n = 1;
 
53
        while (flag)
 
54
        {
 
55
                for (it = layerMap.begin(); it != layerMap.end(); ++it)
 
56
                {
 
57
                        if (TeStringCompare(it->second->name(),newLayerName))
 
58
                                break;
 
59
                }
 
60
                if (it == layerMap.end())
 
61
                        flag = 0;
 
62
                else
 
63
                        newLayerName = layerName + "_" +Te2String(n);
 
64
                n++;    
 
65
        }
 
66
 
 
67
        string polTableName;
 
68
        if (mask)
 
69
        {
 
70
                TeRepresentation* repp = layerBase->getRepresentation(TePOLYGONS);
 
71
                if (repp)
 
72
                {
 
73
                        if (!box.isValid())
 
74
                                box = adjustToCut(repp->box_,resX,resY);
 
75
                        polTableName = repp->tableName_;
 
76
                }
 
77
                else
 
78
                        mask = false;
 
79
        }
 
80
        else 
 
81
        {
 
82
                if (!box.isValid())
 
83
                        box = adjustToCut(layerBase->box(),resX,resY);
 
84
        }
 
85
 
 
86
        TeBox newBox = adjustToCut(box,resX,resY);
 
87
        double x,y,x1,x2,y1,y2;
 
88
        x1 = newBox.x1_;
 
89
        y1 = newBox.y1_;
 
90
        x2 = newBox.x2_;
 
91
        y2 = newBox.y2_;
 
92
 
 
93
        int maxcols, maxlines;
 
94
        maxlines = TeRound((y2-y1)/resY);
 
95
        maxcols = TeRound((x2-x1)/resX);
 
96
 
 
97
        TeAttribute attribute;
 
98
        TeAttributeList attList;
 
99
        attribute.rep_.name_ = "object_id_";
 
100
        attribute.rep_.type_ = TeSTRING;
 
101
        attribute.rep_.numChar_ = 48;
 
102
        attribute.rep_.isPrimaryKey_ = true;
 
103
        attList.push_back ( attribute );
 
104
        attribute.rep_.name_ = "Col";
 
105
        attribute.rep_.type_ = TeINT;
 
106
        attList.push_back ( attribute );
 
107
        attribute.rep_.name_ = "Lin";
 
108
        attribute.rep_.type_ = TeINT;
 
109
        attribute.rep_.isPrimaryKey_ = false;
 
110
        attList.push_back ( attribute );        
 
111
 
 
112
        TeLayer* newLayer = new TeLayer(layerName,db,newBox,proj);
 
113
        if (!newLayer || newLayer->id() <= 0)
 
114
        {
 
115
                delete portal;
 
116
                return 0;
 
117
        }
 
118
        newLayer->addGeometry(TeCELLS);
 
119
        TeRepresentation* repp = newLayer->getRepresentation(TeCELLS);
 
120
        if (!repp)
 
121
        {
 
122
                db->deleteLayer(newLayer->id());
 
123
                delete portal;
 
124
                return 0;
 
125
        }
 
126
        repp->box_ = newBox;
 
127
        repp->resX_ = resX;
 
128
        repp->resY_ = resY;
 
129
        repp->nCols_ = maxcols;
 
130
        repp->nLins_ = maxlines;
 
131
        db->updateRepresentation(newLayer->id(),*repp);
 
132
        
 
133
        TeTable attTable (layerName,attList,"object_id_","object_id_",TeAttrStatic);
 
134
        newLayer->createAttributeTable(attTable);
 
135
 
 
136
        TeCellSet cells;
 
137
        cells.resX(resX);
 
138
        cells.resY(resY);
 
139
 
 
140
        int col,lin,ncels=0;
 
141
        bool status;
 
142
        if(TeProgress::instance())
 
143
                TeProgress::instance()->setTotalSteps(maxlines);
 
144
 
 
145
        TePolygon curPol;
 
146
        bool found = false;
 
147
 
 
148
        TePrecision::instance().setPrecision(TeGetPrecision(proj));
 
149
        
 
150
        y=y2;
 
151
        for (lin=0; lin<maxlines; ++lin) 
 
152
        {       
 
153
                double yu = y;
 
154
                y=y-resY;
 
155
                x=x1;
 
156
 
 
157
                for (col=0; col<maxcols; ++col)
 
158
                {
 
159
                        TeBox box(x,y,x+resX,yu);
 
160
                        found = false;
 
161
                        // check if there should be used the polygon representation as a mask
 
162
                        if (mask) 
 
163
                        {
 
164
                                TePolygon polBox = polygonFromBox(box);
 
165
                                // check if the cell intersects the current polygon
 
166
                                if (TeIntersects(polBox,curPol))
 
167
                                        found = true;
 
168
                                else
 
169
                                {
 
170
                                        if (db->spatialRelation(polTableName, TePOLYGONS, 
 
171
                                                (TePolygon*)&polBox, portal, TeINTERSECTS))
 
172
                                        {
 
173
                                                portal->fetchGeometry(curPol);
 
174
                                                found = true;
 
175
                                        }
 
176
                                        portal->freeResult();
 
177
                                }
 
178
                        }
 
179
 
 
180
                        if (!mask || found)
 
181
                        {
 
182
                                // build geometry
 
183
                                TeCell cell(box,col,lin);
 
184
                                char celId[32];
 
185
                                sprintf(celId,"C%02dL%02d",col,lin);
 
186
                                cell.objectId(string(celId));
 
187
                                cells.add(cell);
 
188
                                
 
189
                                // build default attributes
 
190
                                TeTableRow row;
 
191
                                row.push_back(cell.objectId()); 
 
192
                                row.push_back(Te2String(col)); 
 
193
                                row.push_back(Te2String(lin)); 
 
194
                                attTable.add(row);
 
195
                                ncels++;
 
196
                        }
 
197
                        x=x+resX;
 
198
                }
 
199
                if (attTable.size() > 0)        // if there is some attributes in this line
 
200
                {
 
201
                        status = newLayer->saveAttributeTable(attTable);
 
202
                        attTable.clear();
 
203
                        status = newLayer->addCells(cells);
 
204
                        cells.clear();
 
205
                        if(TeProgress::instance())
 
206
                        {
 
207
                                if (TeProgress::instance()->wasCancelled())
 
208
                                        break;
 
209
                                else
 
210
                                        TeProgress::instance()->setProgress(lin);
 
211
                        }                       
 
212
                }
 
213
        }
 
214
        delete portal;
 
215
        if (TeProgress::instance())
 
216
                TeProgress::instance()->reset();
 
217
 
 
218
        if (ncels > 0)
 
219
                return newLayer;
 
220
        else
 
221
        {
 
222
                db->deleteLayer(newLayer->id());
 
223
                return 0;
 
224
        }
 
225
}
 
226
 
 
227
        
 
228
TeLayer*
 
229
TeCreateCells(const string& layerName,TeTheme* theme, double resX, double resY, TeBox& box)
 
230
{
 
231
        TeLayer* inputLayer = theme->layer();
 
232
        TeRepresentation* pp = inputLayer->getRepresentation(TePOLYGONS);
 
233
        if (!pp)
 
234
                return 0;
 
235
 
 
236
        TeDatabase* db = inputLayer->database();
 
237
        TeDatabasePortal* portal = db->getPortal();
 
238
        if (!portal)
 
239
                return 0;
 
240
 
 
241
        string newLayerName = layerName;
 
242
        TeLayerMap& layerMap = db->layerMap();
 
243
        TeLayerMap::iterator it;
 
244
        bool flag = true;
 
245
        int n = 1;
 
246
        while (flag)
 
247
        {
 
248
                for (it = layerMap.begin(); it != layerMap.end(); ++it)
 
249
                {
 
250
                        if (TeStringCompare(it->second->name(),newLayerName))
 
251
                                break;
 
252
                }
 
253
                if (it == layerMap.end())
 
254
                        flag = 0;
 
255
                else
 
256
                        newLayerName = layerName + "_" +Te2String(n);
 
257
                n++;    
 
258
        }
 
259
 
 
260
        if (!box.isValid())
 
261
                box = pp->box_;
 
262
 
 
263
        TeBox newBox = adjustToCut(box,resX,resY);
 
264
        double x,y,x1,x2,y1,y2;
 
265
        x1 = newBox.x1_;
 
266
        y1 = newBox.y1_;
 
267
        x2 = newBox.x2_;
 
268
        y2 = newBox.y2_;
 
269
 
 
270
        int maxcols, maxlines;
 
271
        maxlines = TeRound((y2-y1)/resY);
 
272
        maxcols = TeRound((x2-x1)/resX);
 
273
 
 
274
        TeAttribute attribute;
 
275
        TeAttributeList attList;
 
276
        attribute.rep_.name_ = "object_id_";
 
277
        attribute.rep_.type_ = TeSTRING;
 
278
        attribute.rep_.isPrimaryKey_ = true;
 
279
        attribute.rep_.numChar_ = 48;
 
280
        attList.push_back ( attribute );
 
281
 
 
282
        attribute.rep_.name_ = "Col";
 
283
        attribute.rep_.type_ = TeINT;
 
284
        attribute.rep_.isPrimaryKey_ = false;
 
285
        attList.push_back ( attribute );
 
286
 
 
287
        attribute.rep_.name_ = "Lin";
 
288
        attribute.rep_.type_ = TeINT;
 
289
        attribute.rep_.isPrimaryKey_ = false;
 
290
        attList.push_back ( attribute );        
 
291
 
 
292
        TeLayer* newLayer = new TeLayer(layerName,db,newBox,inputLayer->projection());
 
293
        if (!newLayer || newLayer->id() <= 0)
 
294
        {
 
295
                return 0;
 
296
                delete portal;
 
297
        }
 
298
        newLayer->addGeometry(TeCELLS);
 
299
        TeRepresentation* repp = newLayer->getRepresentation(TeCELLS);
 
300
        if (!repp)
 
301
        {
 
302
                db->deleteLayer(newLayer->id());
 
303
                delete portal;
 
304
                return 0;
 
305
        }
 
306
        repp->box_ = newBox;
 
307
        repp->resX_ = resX;
 
308
        repp->resY_ = resY;
 
309
        repp->nCols_ = maxcols;
 
310
        repp->nLins_ = maxlines;
 
311
        db->updateRepresentation(newLayer->id(),*repp);
 
312
 
 
313
        TeTable attTable (layerName,attList,"object_id_","object_id_",TeAttrStatic);
 
314
        newLayer->createAttributeTable(attTable);
 
315
 
 
316
        string polTableName = inputLayer->tableName(TePOLYGONS);
 
317
 
 
318
        TeCellSet cells;
 
319
        cells.resX(resX);
 
320
        cells.resY(resY);
 
321
 
 
322
        TePolygon curPol;
 
323
        int col,lin;
 
324
        bool status;
 
325
        int ncels = 0;
 
326
 
 
327
        TePrecision::instance().setPrecision(TeGetPrecision(newLayer->projection()));
 
328
 
 
329
        if(TeProgress::instance())
 
330
                TeProgress::instance()->setTotalSteps(maxlines);
 
331
 
 
332
        bool found;
 
333
        y=y2;
 
334
        for (lin=0; lin<maxlines; ++lin) 
 
335
        {       
 
336
                double yu = y;
 
337
                y=y-resY;
 
338
                x=x1;
 
339
                for (col=0; col<maxcols; ++col)
 
340
                {
 
341
                        found = false;
 
342
                        TeBox box(x,y,x+resX,yu);
 
343
 
 
344
                        TePolygon polBox = polygonFromBox(box);
 
345
                        // check if the cell intersects the current polygon
 
346
                        if (TeIntersects(polBox,curPol))
 
347
                                found = true;
 
348
                        else
 
349
                        {
 
350
                                if (db->spatialRelation(polTableName, TePOLYGONS, 
 
351
                                        (TePolygon*)&polBox, portal, TeINTERSECTS, theme->collectionTable()))
 
352
                                {
 
353
                                        portal->fetchGeometry(curPol);
 
354
                                        found = true;
 
355
                                }
 
356
                                portal->freeResult();
 
357
                        }
 
358
                        if (found)
 
359
                        {
 
360
                                TeCell cell(box,col,lin);
 
361
                                char celId[32];
 
362
                                sprintf(celId,"C%02dL%02d",col,lin);
 
363
                                cell.objectId(string(celId));
 
364
                                cells.add(cell);
 
365
 
 
366
                                TeTableRow row;
 
367
                                row.push_back(cell.objectId()); 
 
368
                                row.push_back(Te2String(col)); 
 
369
                                row.push_back(Te2String(lin)); 
 
370
                                attTable.add(row);
 
371
                                ++ncels;                
 
372
                        }
 
373
                        x=x+resX;
 
374
                }
 
375
                if (attTable.size() > 0)        // if there is some attributes in this line
 
376
                {
 
377
                        status = newLayer->saveAttributeTable(attTable);
 
378
                        attTable.clear();
 
379
                        status = newLayer->addCells(cells);
 
380
                        cells.clear();
 
381
                        if(TeProgress::instance())
 
382
                        {
 
383
                                if (TeProgress::instance()->wasCancelled())
 
384
                                        break;
 
385
                                else
 
386
                                        TeProgress::instance()->setProgress(lin);
 
387
                        }       
 
388
                }
 
389
        }
 
390
        delete portal;
 
391
        if (TeProgress::instance())
 
392
                TeProgress::instance()->reset();
 
393
 
 
394
        if (ncels > 0)
 
395
                return newLayer;
 
396
        else
 
397
        {
 
398
                db->deleteLayer(newLayer->id());
 
399
                return 0;
 
400
        }
 
401
}
 
402
 
 
403
 
 
404
bool 
 
405
TeCellStatistics(TeTheme* themeOut, TeTable& newAttrTable, TeGroupingAttr& stat, 
 
406
                                 TeTheme* themeIn, TeDatabase* db)
 
407
{
 
408
 
 
409
        TeGroupingAttr tempStat = stat;
 
410
        string sqlResut = db->getSQLStatistics (tempStat);
 
411
 
 
412
        bool dbStat = true;
 
413
        TeGroupingAttr::iterator it = tempStat.begin();
 
414
        while(it!= tempStat.end())
 
415
        {
 
416
                if(it->second != TeNOSTATISTIC)
 
417
                {
 
418
                        dbStat = false;
 
419
                        break;
 
420
                }
 
421
                ++it;
 
422
        }
 
423
                
 
424
        if((!dbStat) || (sqlResut.empty()))
 
425
                return false; 
 
426
 
 
427
        TeDatabasePortal* portal = db->getPortal();
 
428
 
 
429
        // mount sql 
 
430
        //geom and collection tables from cell layer (cell)
 
431
        string tableGeomOut = themeOut->layer()->tableName (TeCELLS);
 
432
        string tableCollOut; 
 
433
        if(themeOut->hasRestriction())
 
434
                tableCollOut = themeOut->collectionTable();
 
435
 
 
436
        //geom and collection tables from in layer (point or cell)
 
437
        string tableGeomIn = themeIn->layer()->tableName (TePOINTS);
 
438
        TeGeomRep geomRepIn = TePOINTS;
 
439
        if(tableGeomIn.empty())
 
440
        {
 
441
                geomRepIn = TeCELLS;
 
442
                tableGeomIn = themeIn->layer()->tableName (TeCELLS);
 
443
        }
 
444
 
 
445
        string tableCollIn; 
 
446
        if(themeIn->hasRestriction())
 
447
                tableCollIn = themeIn->collectionTable();
 
448
 
 
449
        //sql from database
 
450
        string sql = " SELECT " + tableGeomOut + ".object_id,  ";
 
451
        sql +=  sqlResut;
 
452
        sql += " FROM " + tableGeomOut +","+ tableGeomIn;
 
453
 
 
454
        //for each point attr table 
 
455
        string sqlWhere ="";
 
456
        TeAttrTableVector vec = themeIn->attrTables();
 
457
        for(unsigned int index=0; index<vec.size(); ++index)
 
458
        {
 
459
                if( (vec[index].tableType() != TeAttrStatic) && 
 
460
                        (vec[index].tableType() != TeAttrEvent)  &&
 
461
                        (vec[index].tableType() != TeFixedGeomDynAttr)) //only to static attribute table
 
462
                        continue;
 
463
 
 
464
                //from clause
 
465
                sql += ","+ vec[index].name();
 
466
 
 
467
                //where clause
 
468
                if(!sqlWhere.empty())
 
469
                        sqlWhere += " AND ";
 
470
                sqlWhere += vec[index].name() +"."+ vec[index].linkName() +" = "+ tableGeomIn +".object_id ";
 
471
        }
 
472
 
 
473
        if(!tableCollIn.empty())
 
474
        {
 
475
                sql += ", "+ tableCollIn;
 
476
                if(!sqlWhere.empty())
 
477
                        sqlWhere += " AND ";
 
478
        
 
479
                sqlWhere = tableGeomIn +".object_id = "+ tableCollIn +".c_object_id ";
 
480
        }
 
481
        if(!tableCollOut.empty())
 
482
        {
 
483
                sql += ", "+ tableCollOut;
 
484
                if(!sqlWhere.empty())
 
485
                        sqlWhere += " AND ";
 
486
        
 
487
                sqlWhere += tableGeomOut +".object_id = "+ tableCollOut +".c_object_id ";
 
488
        }
 
489
 
 
490
        //box where
 
491
        if(!sqlWhere.empty())
 
492
                sqlWhere += " AND ";
 
493
        
 
494
        sqlWhere += db->getSQLBoxWhere (tableGeomOut, tableGeomIn, geomRepIn);
 
495
 
 
496
        sql += " WHERE "+ sqlWhere; 
 
497
        sql += " GROUP BY "+ tableGeomOut +".object_id";
 
498
 
 
499
        if(!portal->query(sql))
 
500
        {
 
501
                delete portal;
 
502
                return false;
 
503
        }
 
504
        
 
505
        //Keep statistics in the tableAttrCell table   
 
506
        if(!TeKeepStatistics(newAttrTable, portal))
 
507
        {
 
508
                delete portal;
 
509
                return false;
 
510
        }
 
511
        
 
512
        delete portal;
 
513
        return true;
 
514
}
 
515
 
 
516
 
 
517
bool    
 
518
TeKeepStatistics(TeTable& tableAttrCell, TeDatabasePortal* portal)
 
519
{
 
520
        
 
521
        TeAttributeList attr = portal->AttributeList();
 
522
 
 
523
        //Mount the update sql from portal
 
524
        string insert = "INSERT INTO "+ tableAttrCell.name() +" VALUES ( ";
 
525
        while(portal->fetchRow())
 
526
        {
 
527
                string insert2 = "";
 
528
                for(unsigned int i=0; i<attr.size(); ++i)
 
529
                {
 
530
                        if(i>0)
 
531
                                insert2 += ",";
 
532
                        
 
533
                        string val = portal->getData(i);
 
534
                        if (attr[i].rep_.type_ == TeSTRING) 
 
535
                                insert2 += "'"+ val +"'";
 
536
                        else
 
537
                                insert2 += val;
 
538
                }
 
539
 
 
540
                string result = insert + insert2 +" )";
 
541
                
 
542
                if(!portal->getDatabase()->execute(result))
 
543
                        return false;
 
544
        }
 
545
                
 
546
        return true;
 
547
}       
 
548
 
 
549
 
 
550
 
 
551
 
 
552
 
 
553
//////////////////////////////////////////////////////////////////////
 
554
//
 
555
//                              Fill Cell Auxiliary Funcions
 
556
//
 
557
/////////////////////////////////////////////////////////////////////
 
558
 
 
559
 
 
560
bool TeFillCellInitLoad (TeLayer* cell_layer, const string& cell_tablename, TeCellSet& cells)
 
561
{
 
562
        if (!cell_layer) return false;
 
563
 
 
564
        // get cells
 
565
        if (!cell_layer->getCells (cells)) return false;
 
566
 
 
567
        // if dynamic table was not created, create it
 
568
        TeTable table;
 
569
        if (!cell_layer->getAttrTablesByName(cell_tablename, table, TeFixedGeomDynAttr))
 
570
                if (!TeCreateBasicDymanicCellTable (cell_layer, cell_tablename))
 
571
                        return false;
 
572
 
 
573
        return true;
 
574
}
 
575
 
 
576
 
 
577
 
 
578
void TeFillCellInitSTO (const TeCell& cell, TeTimeInterval& t, TePropertyVector& result, TeSTElementSet& cellObjSet)
 
579
{
 
580
 
 
581
                TeSTInstance cellObj;
 
582
                cellObj.objectId (cell.objectId());
 
583
                string uniqueId = cell.objectId();
 
584
                cellObj.addUniqueId (uniqueId); // ANAP
 
585
                uniqueId += t.getInitialDate() + t.getInitialTime();
 
586
                uniqueId += t.getFinalDate() + t.getFinalTime() ;
 
587
                cellObj.addUniqueId (uniqueId);
 
588
                cellObj.timeInterval (t);
 
589
                TePropertyVector::iterator itProp = result.begin();
 
590
                while (itProp != result.end())
 
591
                {
 
592
                        cellObj.addProperty (*itProp);
 
593
                        itProp++;
 
594
                }
 
595
                cellObjSet.insertSTInstance (cellObj);
 
596
                return;
 
597
}
 
598
 
 
599
 
 
600
 
 
601
bool
 
602
TeCreateBasicDymanicCellTable (TeLayer* cell_layer, const string cell_tablename)
 
603
{
 
604
        if (!cell_layer) return false;
 
605
 
 
606
                TeAttribute             attribute;
 
607
                TeAttributeList attList;
 
608
                TeAttributeList keyList;
 
609
 
 
610
                // attr_id
 
611
                attribute.rep_.name_ = "attr_id";
 
612
                attribute.rep_.type_ = TeSTRING;
 
613
                attribute.rep_.numChar_ = 48;
 
614
                attribute.rep_.isPrimaryKey_ = true;
 
615
                attList.push_back (attribute);
 
616
                keyList.push_back(attribute);
 
617
 
 
618
                // object_id
 
619
                attribute.rep_.name_ = "object_id";
 
620
                attribute.rep_.type_ = TeSTRING;
 
621
                attribute.rep_.numChar_ = 48;
 
622
                attList.push_back ( attribute );
 
623
        
 
624
 
 
625
                //  initial_time
 
626
                attribute.rep_.name_ = "initial_time";
 
627
                attribute.rep_.type_ = TeDATETIME;   
 
628
                attribute.dateTimeFormat_ = "YYYYsMMsDDsHHsMMsSS";
 
629
                attribute.dateChronon_ = TeSECOND;
 
630
                attribute.rep_.numChar_ = 48;
 
631
                attList.push_back ( attribute );
 
632
 
 
633
 
 
634
                //  final_time
 
635
                attribute.rep_.name_ = "final_time";
 
636
                attribute.rep_.type_ = TeDATETIME;  
 
637
                attribute.dateTimeFormat_ = "YYYYsMMsDDsHHsMMsSS";
 
638
                attribute.dateChronon_ = TeSECOND;
 
639
                attribute.rep_.numChar_ = 48;
 
640
                attList.push_back ( attribute );
 
641
                
 
642
        // Create table and initialize attributes
 
643
 
 
644
                TeTable cells_attTable (cell_tablename);       
 
645
                cells_attTable.setAttributeList(attList);
 
646
                cells_attTable.setTableType(TeFixedGeomDynAttr);
 
647
                cells_attTable.setLinkName("object_id");
 
648
                cells_attTable.setUniqueName("attr_id");
 
649
                cells_attTable.attInitialTime ("initial_time"); 
 
650
                cells_attTable.attFinalTime ("final_time"); 
 
651
                cells_attTable.attTimeUnit (TeSECOND); 
 
652
                        
 
653
                if (!cell_layer->createAttributeTable(cells_attTable))
 
654
                        return false;
 
655
 
 
656
                TeAttributeList attr; 
 
657
                cell_layer->database ()->getAttributeList (cell_tablename, attr);
 
658
 
 
659
                return true;
 
660
}
 
661
 
 
662
 
 
663
 
 
664
//////////////////////////////////////////////////////////////////////
 
665
//
 
666
//                                      Fill Cell Operations
 
667
//
 
668
/////////////////////////////////////////////////////////////////////
 
669
 
 
670
bool TeFillCellSpatialOperation (TeDatabase* db,
 
671
                                                                                const string& input_layername, 
 
672
                                                                                TeGeomRep rep,
 
673
                                                                                const string& input_tablename, 
 
674
                                                                                const string& input_attrname,
 
675
                                                                                TeTimeInterval t,
 
676
                                                                                const string& cell_layername, 
 
677
                                                                                const string& cell_tablename, 
 
678
                                                                                const string& output_columnName,
 
679
                                                                                TeComputeAttrSpatialStrategy* operation)
 
680
 
 
681
{
 
682
        if (!operation) return false;
 
683
        if (!db) return false;
 
684
 
 
685
// Load input layers
 
686
        TeLayer* input_layer = new TeLayer (input_layername);
 
687
        if (!db->loadLayer (input_layer))
 
688
        {
 
689
                 cout << "\tLayer de entrada inexistente: " << input_layername << endl; 
 
690
                 db->close();
 
691
                 return false;
 
692
        }
 
693
 
 
694
// Load output cells layer with geometry previously created
 
695
        TeLayer* cell_layer = new TeLayer (cell_layername);
 
696
        if (!db->loadLayer (cell_layer))
 
697
        {
 
698
                 cout << "\tLayer de entrada inexistente: " << cell_layername << endl; 
 
699
                 db->close();
 
700
                 return false;
 
701
        }
 
702
 
 
703
// Initialize cell set
 
704
        TeCellSet cells;
 
705
        if (!TeFillCellInitLoad (cell_layer, cell_tablename, cells)) return false;
 
706
 
 
707
 
 
708
// Initialize object set to store cell properties
 
709
        TeSTElementSet cellObjSet (cell_layer);
 
710
 
 
711
// Initialize theme
 
712
 
 
713
        TeTheme* theme = new TeTheme ("", input_layer);
 
714
        vector<string> attrTableNames;
 
715
        attrTableNames.push_back (input_tablename);
 
716
        TeAttrTableVector atts;
 
717
        if (!input_layer->getAttrTablesByName(attrTableNames, atts)) return false;
 
718
        theme->setAttTables (atts);
 
719
 
 
720
// Process
 
721
        TePropertyVector result;
 
722
        TeCellSet::iterator cell_it = cells.begin();
 
723
        while (cell_it != cells.end())
 
724
        {
 
725
                // Set restrictions on a theme and create stoset
 
726
                theme->setSpatialRest((*cell_it).box(), rep);   
 
727
                result = operation->compute (theme, input_attrname, (*cell_it).box(), output_columnName);
 
728
                TeFillCellInitSTO ((*cell_it), t, result, cellObjSet);
 
729
                cell_it++;
 
730
 
 
731
        } 
 
732
 
 
733
// Update DB
 
734
        if (!TeUpdateDBFromSet(&cellObjSet, cell_tablename))
 
735
                        return  false;
 
736
        return true;
 
737
}
 
738
 
 
739
 
 
740
 
 
741
bool TeFillCellNonSpatialOperation (TeDatabase* db,
 
742
                                                                                const string& input_layername, 
 
743
                                                                                TeGeomRep rep,
 
744
                                                                                const string& input_tablename, 
 
745
                                                                                const string& input_attrname,
 
746
                                                                                TeTimeInterval t,
 
747
                                                                                const string& cell_layername, 
 
748
                                                                                const string& cell_tablename, 
 
749
                                                                                const string& output_columnName,
 
750
                                                                                TeComputeAttrStrategy<TeSTElementSet::propertyIterator>* operation)
 
751
{
 
752
        if (!db) return false;
 
753
 
 
754
// Load input layers
 
755
        TeLayer* input_layer = new TeLayer (input_layername);
 
756
        if (!db->loadLayer (input_layer))
 
757
        {
 
758
                 cout << "\tLayer de entrada inexistente: " << input_layername << endl; 
 
759
                 db->close();
 
760
                 return false;
 
761
        }
 
762
 
 
763
// Load output cells layer with geometry previously created
 
764
        TeLayer* cell_layer = new TeLayer (cell_layername);
 
765
        if (!db->loadLayer (cell_layer))
 
766
        {
 
767
                 cout << "\tLayer de entrada inexistente: " << cell_layername << endl; 
 
768
                 db->close();
 
769
                 return false;
 
770
        }
 
771
 
 
772
// Initialize cell set
 
773
        TeCellSet cells;
 
774
        if (!TeFillCellInitLoad (cell_layer, cell_tablename, cells)) return false;
 
775
 
 
776
// Initialize object set to store cell properties
 
777
        TeSTElementSet cellObjSet (cell_layer);
 
778
 
 
779
// Initialize theme
 
780
        TeTheme* theme = new TeTheme ("", input_layer);
 
781
        vector<string> attrTableNames;
 
782
        attrTableNames.push_back (input_tablename);
 
783
        TeAttrTableVector atts;
 
784
        if (!input_layer->getAttrTablesByName(attrTableNames, atts)) return false;
 
785
        theme->setAttTables (atts);
 
786
 
 
787
// Process
 
788
        TePropertyVector result;
 
789
        TeCellSet::iterator cell_it = cells.begin();
 
790
        while (cell_it != cells.end())
 
791
        {
 
792
                // Set restrictions on a theme and create stoset
 
793
                theme->setSpatialRest((*cell_it).box(), rep);   
 
794
                TeSTElementSet stos (theme);
 
795
                vector<string> attrNames;
 
796
                attrNames.push_back (input_attrname);   
 
797
                if (TeSTOSetBuildDB (&stos, false, false, attrNames))
 
798
                                result = operation->compute (stos.begin(input_attrname), stos.end(input_attrname), output_columnName);  // if property not found, stos iterator will return zero to operation. To check this, have to be less general and use getProperty inside the compute method.            
 
799
 
 
800
                TeFillCellInitSTO ((*cell_it), t, result, cellObjSet);  
 
801
                cell_it++;
 
802
 
 
803
        } 
 
804
 
 
805
// Update DB
 
806
        if (!TeUpdateDBFromSet (&cellObjSet, cell_tablename))
 
807
                        return  false;
 
808
        return  true;
 
809
}
 
810
 
 
811
 
 
812
 
 
813
 
 
814
 
 
815
bool TeFillCellNonSpatialRasterOperation (TeDatabase* db,
 
816
                                                                                const string& input_raster_layername, 
 
817
                                                                                TeTimeInterval t,
 
818
                                                                                const string& cell_layername, 
 
819
                                                                                const string& cell_tablename, 
 
820
                                                                                const string& output_columnName,
 
821
                                                                                TeComputeAttrStrategy<TeRaster::iteratorPoly>* operation)
 
822
{
 
823
        if (!db) return false;
 
824
 
 
825
// Load input layers
 
826
        TeLayer* input_layer = new TeLayer (input_raster_layername);
 
827
        if (!db->loadLayer (input_layer))
 
828
        {
 
829
                 cout << "\tLayer de entrada inexistente: " << input_raster_layername << endl; 
 
830
                 db->close();
 
831
                 return false;
 
832
        }
 
833
 
 
834
        TeRaster* raster = input_layer->raster();
 
835
        if (!raster) return false;
 
836
 
 
837
 
 
838
// Load output cells layer with geometry previously created
 
839
        TeLayer* cell_layer = new TeLayer (cell_layername);
 
840
        if (!db->loadLayer (cell_layer))
 
841
        {
 
842
                 cout << "\tLayer de entrada inexistente: " << cell_layername << endl; 
 
843
                 db->close();
 
844
                 return false;
 
845
        }
 
846
 
 
847
 
 
848
// Initialize cell set
 
849
        TeCellSet cells;
 
850
        if (!TeFillCellInitLoad (cell_layer, cell_tablename, cells)) return false;
 
851
 
 
852
 
 
853
// Initialize object set to store cell properties
 
854
        TeSTElementSet cellObjSet (cell_layer);
 
855
 
 
856
 
 
857
// Process
 
858
        TePropertyVector result;
 
859
        TeCellSet::iterator cell_it = cells.begin();
 
860
        while (cell_it != cells.end())
 
861
        {
 
862
                TePolygon p = TeBox2Polygon((*cell_it).box());
 
863
                TeRaster::iteratorPoly rasterItBegin(raster);
 
864
                rasterItBegin = raster->begin(p, TeBBoxPixelInters, 0);
 
865
                TeRaster::iteratorPoly rasterItEnd(raster);
 
866
                rasterItEnd = raster->end(p, TeBBoxPixelInters, 0);
 
867
 
 
868
                result = operation->compute (rasterItBegin, rasterItEnd, output_columnName);                                                                            
 
869
                TeFillCellInitSTO ((*cell_it), t, result, cellObjSet);  
 
870
                cell_it++;
 
871
        } 
 
872
 
 
873
// Update DB
 
874
        if (!TeUpdateDBFromSet (&cellObjSet, cell_tablename))
 
875
                        return  false;
 
876
        return  true;
 
877
}
 
878
 
 
879
 
 
880
 
 
881
bool TeFillCellAggregateOperation (  TeDatabase* db,
 
882
                                                                                const string& input_layername, 
 
883
                                                                                const string& input_tablename, 
 
884
                                                                                TeTimeInterval t,
 
885
                                                                                const string& cell_layername, 
 
886
                                                                                const string& cell_tablename, 
 
887
                                                                                vector<string>& attrNames,
 
888
                                                                                TeComputeAttrStrategy<TeSTElementSet::propertyIterator>* operation)
 
889
{
 
890
        if (!db) return false;
 
891
 
 
892
 
 
893
// Load input layers
 
894
        TeLayer* input_layer = new TeLayer (input_layername);
 
895
        if (!db->loadLayer (input_layer))
 
896
        {
 
897
                 cout << "\tLayer de entrada inexistente: " << input_layername << endl; 
 
898
                 db->close();
 
899
                 return false;
 
900
        }
 
901
 
 
902
 
 
903
// Load output cells layer with geometry previously created
 
904
        TeLayer* cell_layer = new TeLayer (cell_layername);
 
905
 
 
906
        if (!db->loadLayer (cell_layer))
 
907
        {
 
908
                 cout << "\tLayer de entrada inexistente: " << cell_layername << endl; 
 
909
                 db->close();
 
910
                 return false;
 
911
        }
 
912
 
 
913
// Initialize cell set
 
914
        TeCellSet cells;
 
915
        if (!TeFillCellInitLoad (cell_layer, cell_tablename, cells)) return false;
 
916
 
 
917
 
 
918
// Initialize object set to store cell properties
 
919
        TeSTElementSet cellObjSet (cell_layer);
 
920
 
 
921
// Initialize theme
 
922
 
 
923
        TeTheme* theme = new TeTheme ("", input_layer);
 
924
        vector<string> attrTableNames;
 
925
        attrTableNames.push_back (input_tablename);
 
926
        TeAttrTableVector atts;
 
927
        if (!input_layer->getAttrTablesByName(attrTableNames, atts)) return false;
 
928
        theme->setAttTables (atts);
 
929
 
 
930
 
 
931
// Process
 
932
        TeCellSet::iterator cell_it = cells.begin();
 
933
        while (cell_it != cells.end())
 
934
        {
 
935
                TeSTInstance cellObj;
 
936
                cellObj.objectId ((*cell_it).objectId());
 
937
                string uniqueId = (*cell_it).objectId();
 
938
                cellObj.addUniqueId (uniqueId); 
 
939
                uniqueId += t.getInitialDate() + t.getInitialTime();
 
940
                uniqueId += t.getFinalDate() + t.getFinalTime() ;
 
941
                cellObj.addUniqueId (uniqueId);
 
942
                cellObj.timeInterval (t);
 
943
                
 
944
                // Set restrictions on a theme and create stoset
 
945
                theme->setSpatialRest((*cell_it).box(), TeCELLS); //????? TeWITHIN ok? ANAP     
 
946
                TeSTElementSet stos (theme);
 
947
                TeSTOSetBuildDB (&stos, true, false, attrNames);
 
948
                        
 
949
                // Create stoset based on input layer
 
950
                TePropertyVector result;
 
951
                vector<string>::iterator attIt = attrNames.begin();
 
952
                while (attIt != attrNames.end())
 
953
                {
 
954
                        result = operation->compute (stos.begin(*attIt), stos.end(*attIt), (*attIt));   // if property not found, stos iterator will return zero to operation. To check this, have to be less general and use getProperty inside the compute method.            
 
955
                        TePropertyVector::iterator itProp = result.begin();
 
956
                        while (itProp != result.end())
 
957
                        {
 
958
                                cellObj.addProperty (*itProp);
 
959
                                itProp++;
 
960
                        }
 
961
                        attIt++;
 
962
                }
 
963
                cellObjSet.insertSTInstance (cellObj);
 
964
 
 
965
                cell_it++;
 
966
        }
 
967
 
 
968
// Update DB
 
969
        if (!TeUpdateDBFromSet (&cellObjSet, cell_tablename))
 
970
                        return  false;
 
971
 
 
972
        return  true;
 
973
}
 
974
 
 
975
 
 
976
 
 
977
 
 
978
 
 
979
 
 
980
bool TeFillCellConnectionOperation (    TeDatabase* db,
 
981
                                                                                TeSTElementSet objects,
 
982
                                                                                TeGeomRep rep,
 
983
                                                                                TeGraphNetwork* net,
 
984
                                                                                const string& input_attrName,
 
985
 
 
986
                                                                                TeTimeInterval t,
 
987
                                                                                const string& cell_layername, 
 
988
                                                                                const string& cell_tablename, 
 
989
                                                                                const string& output_columnName,
 
990
                                                                                
 
991
                                                                
 
992
                                                                                TeComputeAttrStrategy<TeSTElementSet::propertyIterator>* operation,
 
993
                                                                                double local_distance_factor,
 
994
                                                                                double net_distance_factor,
 
995
                                                                                double net_conn_factor,
 
996
                                                                                double mult_factor)
 
997
{
 
998
 
 
999
        if (!operation) return false;
 
1000
        if (!net) return false;
 
1001
        if (!db) return false;
 
1002
 
 
1003
        // Load output cells layer with geometry previously created
 
1004
 
 
1005
        TeLayer* cell_layer = new TeLayer (cell_layername);
 
1006
 
 
1007
        if (!db->loadLayer (cell_layer))
 
1008
        {
 
1009
                 cout << "\tLayer de entrada inexistente: " << cell_layername << endl; 
 
1010
                 db->close();
 
1011
                 return false;
 
1012
        }
 
1013
 
 
1014
// Initialize cell set
 
1015
        TeCellSet cells;
 
1016
        if (!TeFillCellInitLoad (cell_layer, cell_tablename, cells)) return false;
 
1017
 
 
1018
 
 
1019
// Initialize object set to store cell properties
 
1020
        TeSTElementSet cellObjSet (cell_layer);
 
1021
 
 
1022
 
 
1023
// Process
 
1024
        TePropertyVector result;
 
1025
        TeCellSet::iterator cell_it = cells.begin();
 
1026
        while (cell_it != cells.end())
 
1027
        {
 
1028
                TeSTInstance cellObj;
 
1029
                cellObj.objectId ((*cell_it).objectId());
 
1030
                string uniqueId = (*cell_it).objectId();
 
1031
                cellObj.addUniqueId (uniqueId);
 
1032
                uniqueId += t.getInitialDate() + t.getInitialTime();
 
1033
                uniqueId += t.getFinalDate() + t.getFinalTime() ;
 
1034
                cellObj.addUniqueId (uniqueId);
 
1035
                cellObj.timeInterval (t);       
 
1036
                cellObjSet.insertSTInstance (cellObj);
 
1037
                cell_it++;
 
1038
        } 
 
1039
 
 
1040
        TeProxMatrixOpenNetworkStrategy2    sc_net (&cellObjSet, TeCELLS, &objects, rep, 0, TeMAXFLOAT, TeMAXFLOAT, net);
 
1041
 //     TeProxMatrixConnectionStrenghtStrategy sw(4, 0.0, false);
 
1042
        TeProxMatrixInverseDistanceStrategy sw (local_distance_factor, net_distance_factor, net_conn_factor, mult_factor, false);
 
1043
        TeProxMatrixNoSlicingStrategy ss_no;
 
1044
        TeGeneralizedProxMatrix mat  (&sc_net, &sw, &ss_no);
 
1045
 
 
1046
        // initialize object set to store cell properties
 
1047
        TeSTElementSet cellObjSet2 (cell_layer);
 
1048
        TeCellSet::iterator cell_it2 = cells.begin();
 
1049
        while (cell_it2 != cells.end())
 
1050
        {
 
1051
                TeSTElementSet neigh = mat.getSTENeighbours((*cell_it2).objectId());    
 
1052
 
 
1053
                result = operation->compute (neigh.begin(input_attrName), neigh.end(input_attrName), output_columnName);        // if property not found, stos iterator will return zero to operation. To check this, have to be less general and use getProperty inside the compute method.            
 
1054
                TeFillCellInitSTO ((*cell_it2), t, result, cellObjSet2);        
 
1055
                cell_it2++;
 
1056
        } 
 
1057
 
 
1058
 
 
1059
// Update DB
 
1060
        if (!TeUpdateDBFromSet (&cellObjSet2, cell_tablename))
 
1061
                        return  false;
 
1062
 
 
1063
        return  true;
 
1064
}
 
1065
 
 
1066