spatialQuery.cpp File Reference

#include "../../src/terralib/drivers/MySQL/TeMySQL.h"
#include "../../src/terralib/drivers/shapelib/TeDriverSHPDBF.h"

Go to the source code of this file.

Functions

int main ()


Function Documentation

int main (  ) 

Definition at line 36 of file spatialQuery.cpp.

References TeDatabase::close(), TeDatabase::connect(), db, TeDatabase::errorMessage(), TeSingleton< TePrecision >::instance(), TeDatabase::layerExist(), TeDatabase::loadLayer(), TeDatumFactory::make(), TePrecision::setPrecision(), TeDatabase::spatialRelation(), TeLayer::tableName(), TeCDR, TeCROSSES, TeGetPrecision(), TeImportShape(), TeLINES, TePOINTS, TePOLYGONS, TeTOUCHES, and TeWITHIN.

00037 {
00038         // Datatabase server parameters
00039         string host = "localhost";
00040         string dbname = "DB320RC1";
00041         string user = "root";
00042         string password = "vinhas";
00043 
00044         // Open a connection to the DB320RC1 MySQL database 
00045         TeDatabase* db = new TeMySQL();
00046         if (!db->connect(host, user, password, dbname))
00047         {
00048                 cout << "Error: " << db->errorMessage() << endl;
00049                 cout << endl << "Press Enter\n";
00050                 getchar();
00051                 return 1;
00052         }
00053         cout << "Connection successful to the database \"" << dbname << "\" on MySQL server \"" << host << "\" !\n";;
00054 
00055         // Check whether there is a layer of polygons called "Mapa_Distritos_SP".
00056         // In negative case, import it from the Mapa_Distritos_SP.shp file
00057         string layerName = "Mapa_Distritos_SP";
00058 
00059         TeDatum sad69 = TeDatumFactory::make("SAD69");
00060         TePolyconic* proj = new TePolyconic(sad69, -54*TeCDR);
00061 
00062         if (db->layerExist(layerName) == false)
00063         {
00064                 // Create a new layer in the database
00065                 TeLayer* layer = new TeLayer(layerName, db, proj);
00066                 string filename = "../data/Mapa_Distritos_SP.shp";      // Shapefile path
00067                 string tablename = "Mapa_Distritos_SP"; // Name of the attribute table
00068 
00069                 if (TeImportShape(layer, filename, tablename))
00070                         cout << "The shapefile \"Mapa_Distritos_SP.shp\" was imported successfully into the TerraLib database!\n" << endl;
00071                 else
00072                         cout << "Error: Fail to import the shapefile \"Mapa_Distritos_SP.shp\"!\n" << endl;
00073         }       
00074 
00075         // Check whether there is a layer of lines called "Mapa_Drenagem_SP".
00076         // In negative case, import it from the Mapa_Drenagem_SP.shp file
00077         layerName = "Mapa_Drenagem_SP";
00078 
00079         if (db->layerExist(layerName) == false)
00080         {
00081                 // Create a new layer in the database
00082                 TeLayer* layer = new TeLayer(layerName, db, proj);
00083                 string filename = "../data/Mapa_Drenagem_SP.shp";       // Shapefile path
00084                 string tablename = "Mapa_Drenagem_SP";  // Name of the attribute table
00085 
00086                 if (TeImportShape(layer, filename, tablename))
00087                         cout << "The shapefile \"Mapa_Drenagem_SP.shp\" was imported successfully into the TerraLib database!\n" << endl;
00088                 else
00089                         cout << "Error: Fail to import the shapefile \"Mapa_Drenagem_SP.shp\"!\n" << endl;
00090         }
00091 
00092         // Check whether there is a layer of points called "Mapa_Bairros_SP".
00093         // In negative case, import it from the Mapa_Bairros_SP.shp file
00094         layerName = "Mapa_Industrias_SP";
00095 
00096         if (db->layerExist(layerName) == false)
00097         {
00098                 // Create a new layer in the database
00099                 TeLayer* layer = new TeLayer(layerName, db, proj);
00100                 string filename = "../data/Mapa_Industrias_SP.shp";     // Shapefile path
00101                 string tablename = "Mapa_Industrias_SP";        // Name of the attribute table
00102 
00103                 if (TeImportShape(layer, filename, tablename))
00104                         cout << "The shapefile \"Mapa_Industrias_SP.shp\" was imported successfully into the TerraLib database!\n" << endl;
00105                 else
00106                         cout << "Error: Fail to import the shapefile \"Mapa_Industrias_SP.shp\"!\n" << endl;
00107         }
00108 
00109         // Retrieve from the database a layer of polygons, a layer of lines and a layer of points
00110     TeLayer* regions = new TeLayer("Mapa_Distritos_SP");                // regions have polygons
00111     if (!db->loadLayer(regions))                
00112     {                                                                                               
00113         cout << "Fail to load layer \"Mapa_Distritos_SP\": " << db->errorMessage() << endl;
00114                 cout << endl << "Press Enter\n";
00115                 getchar();
00116                 return 1;
00117     }
00118 
00119     TeLayer* rivers = new TeLayer("Mapa_Drenagem_SP");  // rivers have lines
00120     if (!db->loadLayer(rivers))                
00121     {                                                                                               
00122         cout << "Fail to load layer \"Mapa_Drenagem_SP\": " << db->errorMessage() << endl;
00123                 cout << endl << "Press Enter\n";
00124                 getchar();
00125                 return 1;
00126     }
00127 
00128         TeLayer* industries = new TeLayer("Mapa_Industrias_SP");        // districts have points
00129     if (!db->loadLayer(industries))                
00130     {                                                                                               
00131         cout << "Fail to load layer \"Mapa_Industrias_SP\": " << db->errorMessage() << endl;
00132                 cout << endl << "Press Enter\n";
00133                 getchar();
00134                 return 1;
00135     }
00136 
00137         //get precision from projection
00138         TePrecision::instance().setPrecision(TeGetPrecision(proj));     
00139         
00140         vector<string> objsOut; // holds the identification of the resulting objects
00141         
00142         vector<string> objsIn;  // objects to be queried                
00143         objsIn.push_back("48");
00144 
00145         // Retrieve the regions that are adjacent or touch the district 48
00146         bool res = db->spatialRelation(regions->tableName(TePOLYGONS), TePOLYGONS, objsIn,
00147                                       objsOut, TeTOUCHES);
00148         if (res)
00149         {
00150                 cout << "Regions that touch the region \"48\": \n";
00151                 unsigned int i;
00152                 for (i=0; i<objsOut.size(); i++)
00153                         cout << "Regions: " << objsOut[i] << endl;
00154 
00155         }
00156 
00157         // Retrieve the rivers that cross the district 48
00158         res = db->spatialRelation(regions->tableName(TePOLYGONS), TePOLYGONS, objsIn,
00159                                             rivers->tableName(TeLINES), TeLINES, objsOut, TeCROSSES);
00160         if (res)
00161         {
00162                 cout << "\nRivers that cross the region \"48\": \n";
00163                 unsigned int i;
00164                 for (i=0; i<objsOut.size(); i++)
00165                         cout << "River: " << objsOut[i] << endl;
00166         }
00167 
00168         // Retrieve the industries that are within the district 48
00169         res = db->spatialRelation(regions->tableName(TePOLYGONS), TePOLYGONS, objsIn,
00170                                       industries->tableName(TePOINTS), TePOINTS, objsOut, TeWITHIN);
00171         if (res)
00172         {
00173                 cout << "\nIndustries that are within the region \"48\": \n";
00174                 unsigned int i;
00175                 for (i=0; i<objsOut.size(); i++)
00176                         cout << "Industry: " << objsOut[i] << endl;
00177 
00178         }
00179 
00180         db->close();
00181         cout << endl << "Press Enter\n";
00182         cout.flush();
00183         getchar();
00184         return 0;
00185 }


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