querierGroupChronon.cpp

Shows how to querier from theme, grouping spatiotemporal instances by a chronon

00001 /************************************************************************************
00002 TerraLib - a library for developing GIS applications.
00003 Copyright © 2001-2004 INPE and Tecgraf/PUC-Rio.
00004 
00005 This code is part of the TerraLib library.
00006 This library is free software; you can redistribute it and/or
00007 modify it under the terms of the GNU Lesser General Public
00008 License as published by the Free Software Foundation; either
00009 version 2.1 of the License, or (at your option) any later version.
00010 
00011 You should have received a copy of the GNU Lesser General Public
00012 License along with this library.
00013 
00014 The authors reassure the license terms regarding the warranties.
00015 They specifically disclaim any warranties, including, but not limited to,
00016 the implied warranties of merchantability and fitness for a particular purpose.
00017 The library provided hereunder is on an "as is" basis, and the authors have no
00018 obligation to provide maintenance, support, updates, enhancements, or modifications.
00019 In no event shall INPE and Tecgraf / PUC-Rio be held liable to any party for direct,
00020 indirect, special, incidental, or consequential damages arising out of the use
00021 of this library and its documentation.
00022 *************************************************************************************/
00023 /* 
00024    This example shows how to use a querier from theme, grouping  
00025    spatiotemporal instances by a chronon. 
00026           
00027    Author: Karine Reis 
00028 */
00029 
00030 #include "../../src/terralib/drivers/MySQL/TeMySQL.h"
00031 #include "../../src/terralib/kernel/TeQuerier.h"
00032 #include "../../src/terralib/kernel/TeQuerierParams.h"
00033 
00034 #include <time.h>
00035 #include <iostream>
00036 
00037 using std::cout;
00038 
00039 int 
00040 main()
00041 {       
00042         
00043         // Datatabase server parameters
00044      string host = "localhost";
00045      string dbname = "saudavel";
00046      string user = "root";
00047      string pass = "karine";
00048 
00049          // Connects to database
00050      TeDatabase* db_ = new TeMySQL();
00051      if (!db_->connect(host,user,pass,dbname))
00052      {
00053          cout << "Error: " << db_->errorMessage() << endl;
00054          return 1;
00055      }
00056 
00057         // Load the layer 
00058         TeTheme* coletas = new TeTheme("Coletas");
00059         if (!db_->loadTheme(coletas))
00060     {
00061         cout << "Error: " << db_->errorMessage() << endl; 
00062                 db_->close(); 
00063         return 1;
00064     }
00065                 
00066         //Attributes to group 
00067         bool loadGeometries = false;
00068         TeGroupingAttr attributes;
00069         
00070         pair<TeAttributeRep, TeStatisticType> attr1(TeAttributeRep("Coletas.NRO_OVOS_PAL1"), TeSUM);
00071         attributes.push_back(attr1);
00072 
00073         pair<TeAttributeRep, TeStatisticType> attr2(TeAttributeRep("Coletas.NRO_OVOS_PAL2"), TeSUM);
00074         attributes.push_back(attr2);
00075 
00076         pair<TeAttributeRep, TeStatisticType> attr3(TeAttributeRep("Coletas.NRO_OVOS_PAL3"), TeSUM);
00077         attributes.push_back(attr3);
00078 
00079         pair<TeAttributeRep, TeStatisticType> attr4(TeAttributeRep("Coletas.NRO_OVOS"), TeSUM);
00080         attributes.push_back(attr4);
00081 
00082         // Set querier parameters - load only the grouped attributes 
00083         TeQuerierParams querierParams(loadGeometries, attributes);
00084         querierParams.setParams(coletas, TeMONTH);
00085 
00086         TeQuerier  querier(querierParams);
00087 
00088         // number of time frames generated by chronon TeMONTH
00089         int numTimeFrames = querier.getNumTimeFrames();
00090         
00091         // Load instances from theme for each time frame or month 
00092         for(int frame=0; frame < numTimeFrames; ++frame)
00093         {
00094                 TeTSEntry ts;
00095                 querier.getTSEntry(ts, frame);
00096                 
00097                 cout << " Time frame: " << Te2String(frame) << " --------------------------- " << endl << endl;
00098 
00099                 string initialDate = ts.time_.getInitialDateTime("DDsMMsYYYY"); 
00100                 string finalDate = ts.time_.getFinalDateTime("DDsMMsYYYY");
00101                 
00102                 cout << " Time Interval: " << initialDate << " to " << finalDate << endl << endl;
00103 
00104                 
00105                 if(!querier.loadInstances(frame))
00106                         continue;
00107 
00108                 // Traverse all the elements 
00109                 TeSTInstance sti;
00110                 while(querier.fetchInstance(sti))
00111                 {
00112                         cout << " Object: " << sti.objectId() << endl;
00113                         
00114                         // Plot each attribute, its name and value
00115                         TePropertyVector vec = sti.getPropertyVector();
00116                         for(unsigned int i=0; i<vec.size(); ++i)
00117                         {
00118                                 string  attrName = vec[i].attr_.rep_.name_;
00119                                 string  attrValue = vec[i].value_;
00120 
00121                                 cout << attrName  << " : " << attrValue << endl;
00122 
00123                         }
00124                 }
00125 
00126                 cout << endl << endl;
00127         }
00128 
00129         cout << " End " << endl;
00130         getchar(); 
00131         db_->close ();
00132         return 0;
00133 }
00134 
00135 
00136 
00137 

Generated on Sun Jul 29 04:01:03 2012 for TerraLib - Development Source by  doxygen 1.5.3