spatialQuery.cpp

Shows how to use the database interface to do some spatial queries involving objects with points, lines and polygon geometries.

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 shows an example of how to use the database interface to do some
00026         spatial queries involving objects with points, lines and polygon geometries.
00027 
00028         Authors: 
00029                 Karine Reis Ferreira  
00030                 Lubia Vinhas
00031 */
00032 
00033 #include "../../src/terralib/drivers/MySQL/TeMySQL.h"
00034 #include "../../src/terralib/drivers/shapelib/TeDriverSHPDBF.h"
00035 
00036 int main()
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 }
00186 

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