createSTElementSetFromLayer.cpp

Shows how to create a new Spatial Temporal Element Set (STElementSet) from a layer

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 /* 
00025    This file illustrates an example of how to create a new Spatial Temporal Element 
00026    Set (STElementSet) from a layer. A Spatial Temporal Element Set can be created 
00027    from a layer or from a theme.
00028    In this example the STElementSet is created from a layer and is filled only
00029    with some attributes.
00030 
00031    Author: Karine Reis   
00032 */
00033 
00034 #include "../../src/terralib/kernel/TeSTElementSet.h"
00035 #include "../../src/terralib/kernel/TeSTEFunctionsDB.h"
00036 #include "../../src/terralib/drivers/MySQL/TeMySQL.h"
00037 #include "../../src/terralib/drivers/shapelib/TeDriverSHPDBF.h"
00038 
00039 
00040 int main()
00041 {       
00042         // Datatabase server parameters
00043         string host = "localhost";
00044         string dbname = "DB320RC1";
00045         string user = "root";
00046         string password = "vinhas";
00047 
00048         // Open a connection to the DB320RC1 MySQL database 
00049         TeDatabase* db = new TeMySQL();
00050         if (!db->connect(host, user, password, dbname))
00051         {
00052                 cout << "Error: " << db->errorMessage() << endl;
00053                 cout << endl << "Press Enter\n";
00054                 getchar();
00055                 return 1;
00056         }
00057         cout << "Connection successful to the database \"" << dbname << "\" on MySQL server \"" << host << "\" !\n";;
00058         
00059         //Verify if there is the layer "EstadosBrasil"
00060         string layerName = "EstadosBrasil";
00061         if (!db->layerExist(layerName))
00062         {
00063                 // Create a new layer in the database
00064                 TeDatum sad69 = TeDatumFactory::make("SAD69");
00065                 TePolyconic* proj = new TePolyconic(sad69, -54);
00066 
00067                 TeLayer* layer = new TeLayer(layerName, db, proj);
00068                 string filename = "../data/EstadosBrasil.shp";  // Shapefile path
00069                 string tablename = "EstadosBrasil";     // Name of the attribute table
00070 
00071                 if (TeImportShape(layer, filename, tablename))
00072                         cout << "The shapefile \"EstadosBrasil.shp\" was imported successfully into the TerraLib database!\n" << endl;
00073                 else
00074                         cout << "Error: Fail to import the shapefile \"EstadosBrasil.shp\"!\n" << endl;
00075 
00076         }
00077         
00078         // Loads the layer
00079         TeLayer* estados = new TeLayer("EstadosBrasil");
00080         if (!db->loadLayer(estados))
00081         {
00082             cout << "Fail to load the layer \"EstadosBrasil\": " << db->errorMessage() << endl;
00083                 db->close();
00084                 cout << endl << "Press Enter\n";
00085                 getchar();
00086         return 1;
00087         }
00088 
00089         // Creates a elementSet from the layer "estados"
00090         TeSTElementSet steSet (estados);
00091 
00092         // Defines what attributes will be kept in the elementSet. 
00093         // The attribute name must be in the format "table_name.attribute_name"
00094         vector<string> attrs;
00095         attrs.push_back("EstadosBrasil.NOME_UF");
00096         attrs.push_back("EstadosBrasil.CAPITAL");
00097 
00098         // Fills the elementSet without geometries and with the 
00099         // attributes NOME_UF and CAPITAL 
00100         bool loadGeometries = false;
00101         bool loadAllAttributes = false;
00102         if(!TeSTOSetBuildDB(&steSet, loadGeometries, loadAllAttributes, attrs))
00103         {
00104                 cout << "Error! " << endl;
00105                 cout << endl << "Press Enter\n";
00106                 getchar();
00107                 return 1;
00108         }
00109 
00110         // Prints the number of elements of the elementSet
00111         cout << "Number of elements: " << steSet.numElements() << endl;
00112 
00113         //Traverse all instances of the set using iterator
00114         TeSTElementSet::iterator it = steSet.begin();
00115         while ( it != steSet.end())
00116         {
00117                 TeSTInstance st = (*it);
00118 
00119                 string desc;
00120                 // returns the attributes
00121                 TePropertyVector vectp = st.getPropertyVector();
00122 
00123                 cout << "Id: " << st.objectId() << " ---------------- " << endl;
00124                 for (unsigned int i=0; i<vectp.size(); i++)
00125                 {
00126                         cout << vectp[i].attr_.rep_.name_ << "  =  ";
00127                         cout << vectp[i].value_ << endl;
00128                 }
00129                 cout << endl;
00130                 ++it;
00131         }
00132 
00133         db->close();
00134         cout << endl << "Press Enter\n";
00135         getchar();
00136         return 0;
00137 }

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