00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include "../../src/terralib/drivers/MySQL/TeMySQL.h"
00034 #include "../../src/terralib/drivers/shapelib/TeDriverSHPDBF.h"
00035
00036 int main()
00037 {
00038
00039 string host = "localhost";
00040 string dbname = "DB320RC1";
00041 string user = "root";
00042 string password = "vinhas";
00043
00044
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
00056
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
00065 TeLayer* layer = new TeLayer(layerName, db, proj);
00066 string filename = "../data/Mapa_Distritos_SP.shp";
00067 string tablename = "Mapa_Distritos_SP";
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
00076
00077 layerName = "Mapa_Drenagem_SP";
00078
00079 if (db->layerExist(layerName) == false)
00080 {
00081
00082 TeLayer* layer = new TeLayer(layerName, db, proj);
00083 string filename = "../data/Mapa_Drenagem_SP.shp";
00084 string tablename = "Mapa_Drenagem_SP";
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
00093
00094 layerName = "Mapa_Industrias_SP";
00095
00096 if (db->layerExist(layerName) == false)
00097 {
00098
00099 TeLayer* layer = new TeLayer(layerName, db, proj);
00100 string filename = "../data/Mapa_Industrias_SP.shp";
00101 string tablename = "Mapa_Industrias_SP";
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
00110 TeLayer* regions = new TeLayer("Mapa_Distritos_SP");
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");
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");
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
00138 TePrecision::instance().setPrecision(TeGetPrecision(proj));
00139
00140 vector<string> objsOut;
00141
00142 vector<string> objsIn;
00143 objsIn.push_back("48");
00144
00145
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
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
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