TeOracleSpatial.cpp

Go to the documentation of this file.
00001 /************************************************************************************
00002 TerraLib - a library for developing GIS applications.
00003 Copyright © 2001-2007 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 #include "TeOracleSpatial.h"
00025 
00026 #include <sys/stat.h>
00027 #include "TeSpatialOperations.h"
00028 #include "TeGeometryAlgorithms.h"
00029 
00030 #include "TeOCICursor.h"
00031 #include "TeOCIConnect.h"
00032 
00033 string 
00034 getOracleSpatialRelation(int relation)
00035 {
00036         string spatialRel="";
00037         switch (relation)
00038         {
00039                 case TeDISJOINT:
00040                 case TeINTERSECTS:
00041                         spatialRel = "ANYINTERACT";     
00042                 break;
00043 
00044                 case TeTOUCHES:
00045                         spatialRel = "TOUCH";
00046                 break;
00047 
00048                 case TeOVERLAPS:
00049                         spatialRel = "OVERLAPBDYINTERSECT";
00050                 break;
00051 
00052                 case TeCOVERS:
00053                         spatialRel = "COVERS"; 
00054                 break;
00055 
00056                 case TeCOVEREDBY:
00057                         spatialRel = "COVEREDBY";
00058                 break;
00059                 
00060                 case TeCONTAINS:
00061                         spatialRel = "CONTAINS";
00062                 break;
00063                 
00064                 case TeWITHIN:
00065                         spatialRel = "INSIDE";
00066                 break;
00067                 
00068                 case TeEQUALS:
00069                         spatialRel = "EQUAL";
00070                 break;
00071                 
00072                 case TeCROSSES:
00073                         spatialRel = "OVERLAPBDYDISJOINT";
00074                 break;
00075                 default:
00076                         spatialRel = "";
00077                 break;
00078         }
00079         
00080         return spatialRel;
00081 }
00082 
00083 
00084 TeOracleSpatial::TeOracleSpatial() : TeOCIOracle()  
00085 {
00086         dbmsName_ = "OracleSpatial";
00087 }
00088 
00089 
00090 bool
00091 TeOracleSpatial::createSpatialIndex(const string &table, const string &column,TeSpatialIndexType type, short level, short tile)
00092 {
00093         TeOracleSpatialPortal  *ocip = (TeOracleSpatialPortal*)getPortal();
00094         if(!ocip)
00095                 return false;
00096 
00097         string name;
00098         if(table.size()>21)
00099                 name = TeConvertToUpperCase(table.substr(0,20)) + "_IDX";
00100         else
00101                 name = TeConvertToUpperCase(table) + "_IDX";
00102         
00103         string index = "SELECT * FROM USER_SDO_INDEX_INFO";
00104         index += " WHERE table_name = '" + TeConvertToUpperCase(table) + "'";
00105         if(!ocip->query(index))
00106         {
00107                 delete ocip;
00108                 return false;
00109         }
00110         
00111         if(ocip->fetchRow())
00112         {
00113                 errorMessage_ = "Spatial Index table already exist!";
00114                 delete ocip;
00115                 return false;
00116         }
00117                 
00118         delete ocip;
00119 
00120         index = " CREATE INDEX " + name;
00121         index += " ON " + table + "(" + column + ")";
00122         index += " INDEXTYPE IS MDSYS.SPATIAL_INDEX ";
00123         
00124         if(type == TeQUADTREE)
00125         {
00126                 if(level==0)
00127                         return false;
00128 
00129                 index += " PARAMETERS ('";
00130                 index += "SDO_LEVEL = " + Te2String(level);
00131                 index += " SDO_NUMTILES = " + Te2String(tile) + "'";
00132         }
00133 
00134         if(!execute(index))
00135                 return false;
00136         
00137         return true;
00138 }
00139 
00140 bool
00141 TeOracleSpatial::insertMetadata(const string &table, const string &column, double tolx,double toly,TeBox &box,short /* srid */)
00142 {       
00143         TeOracleSpatialPortal   *ocip = (TeOracleSpatialPortal*)getPortal();
00144         if(!ocip)
00145                 return false;
00146 
00147         string check = "SELECT * FROM USER_SDO_GEOM_METADATA ";
00148         check += " WHERE TABLE_NAME = '" + TeConvertToUpperCase(table);
00149         check += "' AND COLUMN_NAME = '" + TeConvertToUpperCase(column) + "'";
00150         if(!ocip->query(check))
00151         {
00152                 delete ocip;
00153                 return false;
00154         }
00155 
00156         if(ocip->fetchRow())
00157         {
00158                 delete ocip;
00159                 return false;
00160         }
00161                         
00162         delete ocip;
00163         
00164         double xmin = box.x1();
00165         double ymin = box.y1();
00166         double xmax = box.x2();
00167         double ymax = box.y2();
00168 
00169         string inser = "INSERT INTO USER_SDO_GEOM_METADATA VALUES ( ";
00170         inser += "'" + TeConvertToUpperCase(table) + "' ," ;
00171         inser += "'" + TeConvertToUpperCase(column) + "' ," ; 
00172         inser += " MDSYS.SDO_DIM_ARRAY(";
00173         inser += " MDSYS.SDO_DIM_ELEMENT('X',";
00174         inser += Te2String(xmin,15) + "," + Te2String(xmax,15) + "," + Te2String(tolx,15) + "), ";
00175         inser += " MDSYS.SDO_DIM_ELEMENT('Y',";
00176         inser += Te2String(ymin,15) + "," + Te2String(ymax,15) + "," + Te2String(toly,15) + ")), ";
00177         inser += " NULL ) ";
00178 
00179         if(!(execute(inser.c_str()))) 
00180         {
00181                 if(errorMessage_.empty())
00182                         errorMessage_ = "Error inserting in the table USER_SDO_GEOM_METADATA! "; 
00183                 return false;
00184         }
00185 
00186         return true;
00187 }
00188 
00189 string 
00190 TeOracleSpatial::getSpatialIdxColumn(TeGeomRep rep)
00191 {
00192         if (rep == TeRASTER)
00193                 return "block_box";
00194 
00195         return "spatial_data";
00196 }
00197 
00198 
00199 bool
00200 TeOracleSpatial::connect(const string& host, const string& user, const string& password, const string& database, int port)
00201 {
00202         if(connection_ == NULL)
00203         {
00204                 connection_ = new TeOCIConnection();
00205         }
00206         else if (connection_->isConnected())
00207         { 
00208                 delete (connection_);
00209                 connection_ = new TeOCIConnection();
00210         }
00211 
00212         isConnected_ = false;
00213         if (connection_->connect(host.c_str(),user.c_str(),password.c_str(), true))
00214         {
00215                 isConnected_ = true;
00216                 host_ = host;
00217                 user_ = user;
00218                 password_ = password;
00219                 database_ = database;
00220                 portNumber_ = port;
00221                 return true;
00222         }
00223         else
00224         {
00225                 isConnected_ = false;
00226                 errorMessage_ = "Error connecting to database server: " + connection_->getErrorMessage();
00227                 delete (connection_); //disconect
00228                 connection_=NULL;
00229                 return false;
00230         }
00231 }
00232 
00233 bool
00234 TeOracleSpatial::createTable(const string& table, TeAttributeList &attr)
00235 {
00236         short   cont=0;
00237         string pkeys ="";
00238         bool    hasAutoNumber=false;
00239         string  fieldName="";
00240 
00241         if(tableExist(table))
00242         {
00243                 errorMessage_= "Table already exist!";
00244                 return false;
00245         }
00246 
00247         TeAttributeList::iterator it = attr.begin();
00248         string tablec;
00249         tablec = "CREATE TABLE " + table +" (";
00250         
00251         while ( it != attr.end())
00252         {
00253                 if (cont)
00254                         tablec += ", ";
00255                         
00256                 switch ((*it).rep_.type_)
00257                 {
00258                         case TeSTRING:
00259                                 if((*it).rep_.numChar_ > 0)
00260                                 {
00261                                         tablec += (*it).rep_.name_ + " VARCHAR2(" + Te2String((*it).rep_.numChar_) + ")";
00262                                 }
00263                                 else
00264                                 {
00265                                         tablec += (*it).rep_.name_ + " VARCHAR2(4000)";
00266                                 }
00267                                 if(!((*it).rep_.defaultValue_.empty()))
00268                                         tablec += " DEFAULT '" + (*it).rep_.defaultValue_ + "' ";
00269 
00270                                 if(!((*it).rep_.null_))
00271                                         tablec += " NOT NULL ";
00272                         break;
00273                         
00274                         case TeREAL:
00275                                 if((*it).rep_.decimals_>0)
00276                                         tablec += (*it).rep_.name_ +" NUMBER(*,"+ Te2String((*it).rep_.decimals_) +") ";
00277                                 else
00278                                         tablec += (*it).rep_.name_ +" NUMBER(*,38) ";
00279 
00280                                 if(!((*it).rep_.defaultValue_.empty()))
00281                                         tablec += " DEFAULT " + (*it).rep_.defaultValue_ + " ";
00282 
00283                                 if(!((*it).rep_.null_))
00284                                         tablec += " NOT NULL ";
00285                         break;
00286                         
00287                         case TeINT:
00288                         case TeUNSIGNEDINT:
00289                                 tablec += (*it).rep_.name_ + " NUMBER(32) ";
00290                                 if((*it).rep_.isAutoNumber_)
00291                                 {
00292                                         hasAutoNumber=true;
00293                                         fieldName=(*it).rep_.name_;
00294                                 }
00295 
00296                                 if(!((*it).rep_.defaultValue_.empty()))
00297                                         tablec += " DEFAULT " + (*it).rep_.defaultValue_ + " ";
00298 
00299                                 if(!((*it).rep_.null_))
00300                                         tablec += " NOT NULL ";
00301 
00302                         break;
00303 
00304                         case TeDATETIME:
00305                                 tablec += (*it).rep_.name_ + " DATE ";
00306                                 if(!((*it).rep_.defaultValue_.empty()))
00307                                         tablec += " DEFAULT " + (*it).rep_.defaultValue_ + " ";
00308 
00309                                 if(!((*it).rep_.null_))
00310                                         tablec += " NOT NULL ";
00311                         break;
00312 
00313                         case TeCHARACTER:
00314                                 tablec += (*it).rep_.name_ + " CHAR ";
00315                                 if(!((*it).rep_.defaultValue_.empty()))
00316                                         tablec += " DEFAULT " + (*it).rep_.defaultValue_ + " ";
00317 
00318                                 if(!((*it).rep_.null_))
00319                                         tablec += " NOT NULL ";
00320                         break;
00321 
00322                         case TeBOOLEAN:
00323                                 tablec += (*it).rep_.name_ + " NUMBER(1) ";
00324                                 if(!((*it).rep_.defaultValue_.empty()))
00325                                         tablec += " DEFAULT " + (*it).rep_.defaultValue_ + " ";
00326 
00327                                 if(!((*it).rep_.null_))
00328                                         tablec += " NOT NULL ";
00329                         break;
00330 
00331                         case TeBLOB:
00332                                 tablec += (*it).rep_.name_ + " BLOB ";
00333                         break;
00334 
00335                         case TePOINTTYPE:
00336                         case TePOINTSETTYPE:
00337                                                         tablec += "spatial_data MDSYS.SDO_GEOMETRY ";
00338                                                         ++it;
00339                                                         cont++;
00340                                                         continue;
00341 
00342                         case TeLINE2DTYPE:
00343                         case TeLINESETTYPE:
00344                                                         tablec += "spatial_data MDSYS.SDO_GEOMETRY ";
00345                                                         ++it;
00346                                                         cont++;
00347                                                         continue;
00348 
00349                         case TePOLYGONTYPE:
00350                         case TePOLYGONSETTYPE:
00351                                                         tablec += "spatial_data MDSYS.SDO_GEOMETRY ";
00352                                                         ++it;
00353                                                         cont++;
00354                                                         continue;
00355 
00356                         case TeCELLTYPE:
00357                         case TeCELLSETTYPE:
00358                                                         tablec += "col_number   NUMBER(32) NOT NULL,";
00359                                                         tablec += "row_number   NUMBER(32) NOT NULL,";
00360                                                         tablec += "spatial_data MDSYS.SDO_GEOMETRY ";
00361 
00362                                                         ++it;
00363                                                         cont++;
00364                                                         continue;                                       
00365 
00366                         case TeRASTERTYPE:
00367                                                         tablec += "band_id NUMBER(32) NOT NULL, ";
00368                                                         tablec += "resolution_factor NUMBER(32), ";
00369                                                         tablec += "subband NUMBER(32),";
00370                                                         tablec += "block_box  MDSYS.SDO_GEOMETRY, ";
00371                                                         tablec += "block_size NUMBER(32), ";
00372                                                         tablec += "spatial_data BLOB ";
00373                                                         
00374                                                         ++it;
00375                                                         cont++;
00376                                                         continue;
00377 
00378                         case TeNODETYPE:
00379                         case TeNODESETTYPE:
00380                                                         tablec += "spatial_data MDSYS.SDO_GEOMETRY ";
00381 
00382                                                         ++it;
00383                                                         cont++;
00384                                                         continue;
00385 
00386                         case TeTEXTTYPE:
00387                         case TeTEXTSETTYPE:
00388 
00389                         default:
00390                                 tablec += (*it).rep_.name_ + " VARCHAR2(255) ";
00391                                 if(!((*it).rep_.defaultValue_.empty()))
00392                                         tablec += " DEFAULT " + (*it).rep_.defaultValue_ + " ";
00393 
00394                                 if(!((*it).rep_.null_))
00395                                         tablec += " NOT NULL ";
00396                         break;
00397                 }
00398 
00399                 // check if column is part of primary key
00400                 if ((*it).rep_.isPrimaryKey_ && (*it).rep_.type_ != TeBLOB )
00401                 {
00402                         if (!pkeys.empty())
00403                                 pkeys += ", ";
00404                         pkeys += (*it).rep_.name_;
00405                 }
00406 
00407                 ++it;
00408                 cont++;
00409         }
00410 
00411         if(!pkeys.empty())
00412                 tablec += ", PRIMARY KEY (" + pkeys + ") ";
00413 
00414         tablec += ")";
00415 
00416         if(!execute(tablec))
00417         {
00418                 if(errorMessage_.empty())
00419                         errorMessage_ = "Error creating table " + table;
00420 
00421                 return false;
00422         }
00423 
00424         if(hasAutoNumber)
00425         {
00426                 string dropSql = " DROP TRIGGER "+ getNameTrigger(table); 
00427                 execute(dropSql);
00428                 dropSql = " DROP SEQUENCE "+ getNameSequence(table); 
00429                 execute(dropSql);
00430 
00431                 if(!createSequence(table))
00432                 {
00433                         deleteTable(table);
00434                         return false;
00435                 }
00436                 
00437                 if(!createAutoIncrementTrigger(table,fieldName))
00438                 {
00439                         deleteTable(table);
00440                         string sql= "DROP SEQUENCE "+ getNameSequence(table);
00441                         execute(sql); 
00442                         return false;
00443                 }
00444         }
00445         return true;
00446 }
00447 
00448 TeDatabasePortal*  
00449 TeOracleSpatial::getPortal ()
00450 {
00451         TeOracleSpatialPortal* ocip = new TeOracleSpatialPortal (this);
00452         return ocip;
00453 }
00454 
00455 std::string TeOracleSpatial::getSQLOrderBy(const TeGeomRep& rep) const
00456 {
00457         std::string orderBy = "object_id ASC";
00458         return orderBy;
00459 }
00460 
00461 string 
00462 TeOracleSpatial::getSQLBoxWhere (const TeBox& box, const TeGeomRep rep, const std::string& tableName)
00463 {
00464         std::string columnName = "spatial_data";
00465 
00466         if(rep == TeRASTER)
00467                 columnName = "block_box";
00468 
00469         return getSQLBoxWhere(box, rep, tableName, columnName);
00470 }
00471 
00472 string TeOracleSpatial::getSQLBoxWhere(const TeBox& box, const TeGeomRep rep, const std::string& tableName, const std::string& columnName)
00473 {
00474         std::string plainTableName = tableName;
00475         std::string owner = this->user();
00476         
00477         std::vector<std::string> vecTableName;
00478         TeSplitString(tableName, ".", vecTableName);
00479                 
00480         if(vecTableName.size() == 2)
00481         {
00482                 owner = vecTableName[0];
00483                 plainTableName = vecTableName[1];
00484         }
00485         
00486         string wherebox;
00487         
00488 
00489         if(rep == TeTEXT)
00490         {
00491                 wherebox = TeDatabase::getSQLBoxWhere (box, rep, tableName);
00492                 return wherebox;
00493         }
00494 
00495         std::string strProj = "null";
00496         if(!tableName.empty())
00497         {
00498                 strProj = "(select sdo_SRID from mdsys.sdo_geom_metadata_table where " + toUpper("sdo_table_name") + " = " + toUpper("'" + plainTableName + "'") + " AND upper(sdo_owner) = upper('" + owner + "'))";
00499         }
00500 
00501         wherebox = "mdsys.sdo_filter (" + columnName +",";
00502         wherebox += "mdsys.sdo_geometry(2003," + strProj + ",null,";
00503         wherebox += "mdsys.sdo_elem_info_array(1,1003,3),";
00504         wherebox += "mdsys.sdo_ordinate_array(";
00505         wherebox += Te2String(box.x1(),15) + ", " + Te2String(box.y1(),15);
00506         wherebox += ", " + Te2String(box.x2(),15) + ", " + Te2String(box.y2(),15) + ")),";
00507         wherebox += "'mask=anyinteract querytype=window') = 'TRUE'";
00508 
00509         return wherebox;
00510 }
00511 
00512 string 
00513 TeOracleSpatial::getSQLBoxWhere (const string& table1, const string& table2, TeGeomRep rep2, TeGeomRep rep1)
00514 {
00515         string wherebox;
00516         string colname1, colname2; 
00517         colname1 = colname2 = "spatial_data";
00518         
00519         if(rep1 == TeRASTER) 
00520                 colname1 = "block_box";
00521 
00522         if(rep2 == TeRASTER)
00523                 colname2 = "block_box";
00524                 
00525         wherebox =  "MDSYS.SDO_FILTER ("+ table1 +"."+ colname1 +",";
00526         wherebox += table2 +"."+ colname2 +", 'querytype = window') = 'TRUE'";  
00527 
00528         return wherebox;
00529 }
00530 
00531 
00532 string 
00533 TeOracleSpatial::getSQLBoxSelect (const string& tableName, TeGeomRep rep)
00534 {
00535         
00536         string select;
00537         string colname = "spatial_data";
00538 
00539         if(rep == TeRASTER)
00540                 colname = "block_box";
00541 
00542         select =  tableName +".* , ";
00543         select += " SDO_GEOM.SDO_MIN_MBR_ORDINATE("+ tableName +"."+ colname +", 1) as lower_x,";
00544         select += " SDO_GEOM.SDO_MIN_MBR_ORDINATE("+ tableName +"."+ colname +", 2) as lower_y,";
00545         select += " SDO_GEOM.SDO_MAX_MBR_ORDINATE("+ tableName +"."+ colname +", 1) as upper_x,"; 
00546         select += " SDO_GEOM.SDO_MAX_MBR_ORDINATE("+ tableName +"."+ colname +", 2) as upper_y ";
00547         return select;
00548 }
00549 
00550 bool 
00551 TeOracleSpatial::getMBRSelectedObjects(string /* geomTable */,string colGeom, string fromClause, string whereClause, string /* afterWhereClause */, TeGeomRep /* repType */,TeBox &bout, const double& tol)
00552 {
00553         TeOracleSpatialPortal* portal = (TeOracleSpatialPortal*)getPortal();
00554         if (!portal)
00555                 return false;
00556 
00557         string sql = "SELECT SDO_AGGR_MBR(" + colGeom + ") ";
00558         sql += " FROM " + fromClause;
00559         
00560         if(!whereClause.empty())
00561                 sql += " WHERE " + whereClause;  
00562         
00563         if(!portal->query(sql)) 
00564         {
00565                 delete portal;
00566                 return false;
00567         }
00568 
00569         if(!portal->fetchRow())
00570         {
00571                 delete portal;
00572                 return false;
00573         }
00574 
00575         try
00576         {
00577                 TeCoord2D coord1,coord2;
00578                 portal->getCoordinates (1, coord1);
00579                 portal->getCoordinates (2, coord2);
00580                 TeBox b(coord1.x()-tol, coord1.y()-tol, coord2.x()+tol, coord2.y()+tol);
00581                 bout = b;
00582         }
00583         
00584         catch(...)
00585         {
00586                 delete portal;
00587                 return false;
00588         }
00589         
00590         delete portal;
00591         return true;
00592 }
00593 
00594 
00595 bool 
00596 TeOracleSpatial::getMBRGeom(string tableGeom, string object_id, TeBox& box, string colGeom)
00597 {
00598         TeOracleSpatialPortal* portal = (TeOracleSpatialPortal*)getPortal();
00599         if (!portal)
00600                 return false;
00601 
00602         string sql = "SELECT SDO_GEOM.SDO_MBR(" + tableGeom + "." + colGeom + ") ";
00603         sql += " FROM " + tableGeom;
00604         sql += " WHERE object_id = '" + object_id + "'";
00605 
00606         if((!portal->query(sql)) || (!portal->fetchRow()))
00607         {
00608                 delete portal;
00609                 return false;
00610         }
00611         
00612         TeCoord2D coord1,coord2;
00613         portal->getCoordinates (1, coord1);
00614         portal->getCoordinates (2, coord2);
00615         TeBox b(coord1.x(), coord1.y(), coord2.x(), coord2.y());
00616         box = b;
00617         
00618         delete portal;
00619         return true;
00620 }
00621 
00622 
00623 bool 
00624 TeOracleSpatial::insertRasterBlock(const string& table, const string& blockId, const TeCoord2D& ll, const TeCoord2D& ur, 
00625                                                                    unsigned char *buf,unsigned long size, int band, unsigned int res, unsigned int subband)
00626 {
00627         if (blockId.empty()) // no block identifier provided
00628         {
00629                 errorMessage_ = "bloco sem identificador";
00630                 return false;
00631         }
00632 
00633         TeOracleSpatialPortal* portal = (TeOracleSpatialPortal*) this->getPortal();
00634         if (!portal)
00635                 return false;
00636 
00637         bool update = false;
00638         string q =" SELECT * FROM " + table; 
00639         q += " WHERE block_id='" + blockId + "'";
00640 
00641         if (!portal->query(q))
00642         {
00643                 delete portal;
00644                 return false;
00645         }
00646         // check if this block is alread in the database
00647         if (portal->fetchRow())
00648                 update = true;
00649         delete portal;
00650 
00651         string sdo_geom = " MDSYS.SDO_GEOMETRY(2003, NULL, NULL";
00652         sdo_geom += ", MDSYS.SDO_ELEM_INFO_ARRAY( 1, 1003, 3 )";
00653         sdo_geom += ", MDSYS.SDO_ORDINATE_ARRAY( " ;
00654         sdo_geom += Te2String(ll.x(),15);
00655         sdo_geom += ", " + Te2String(ll.y(),15);
00656         sdo_geom += ", " + Te2String(ur.x(),15);
00657         sdo_geom += ", " + Te2String(ur.y(),15);
00658         sdo_geom += ")) ";
00659 
00660         try
00661         {
00662                 if (!update)
00663                 {
00664                         q = "INSERT INTO "+ table +" (block_id, band_id, subband, ";
00665                         q += " resolution_factor, block_box, block_size, spatial_data) VALUES ( ";
00666                         q += "'" + blockId + "'";
00667                         q += ", " + Te2String(band);
00668                         q += ", " + Te2String(subband);
00669                         q += ", " + Te2String(res);
00670                         q += ", " + sdo_geom;
00671                         q += ", " + Te2String(size);
00672                         q += ", :blobValue";
00673                         q += ")";
00674                         
00675                         if (!connection_->executeBLOBSTM(q, buf, size, ":blobValue"))
00676                                 return false;
00677                 }
00678                 else
00679                 {
00680                         q = " UPDATE "+ table +" SET spatial_data=:blobValue ";
00681                         q += " WHERE block_id='" + blockId + "'";
00682                         if (!connection_->executeBLOBSTM(q, buf, size, ":blobValue"))
00683                                 return false;
00684                 }
00685         }
00686         catch(...)
00687         {
00688                 errorMessage_ = "Error inserting raster block!";
00689                 return false;
00690         }
00691         return true;
00692 }
00693 
00694 bool 
00695 TeOracleSpatial::allocateOrdinatesObject(TePolygon &poly, string& elInfo, TeOCICursor* cursor_)
00696 {
00697         int             totalsize, ni, size;
00698         double  xult, yult;
00699         short   orient;
00700 
00701         ni = poly.size () - 1;
00702 
00703         xult = -9999.99;
00704         yult = -9999.99;
00705 
00706         totalsize = 0;
00707 
00708         try
00709         {
00710                 //OCI: create the ordinates array
00711                 if(!cursor_)
00712                         connection_->allocateObjectOrdinates();
00713                 
00714                 for (int k = 0; k <= ni; k++ )
00715                 {
00716                         TeLinearRing ring ( poly[k] );
00717                         totalsize += ring.size();
00718                         size = ring.size();
00719                         orient = TeOrientation(ring);
00720                         
00721                         if (k==0)  //external polygon: UNCLOCKWISE
00722                         {
00723                                 elInfo = "1, 1003, 1";
00724                                 if(orient == TeCOUNTERCLOCKWISE)   
00725                                 {
00726                                         for (int i=0;i<size;i++)
00727                                         {
00728                                                 if(xult != ring[i].x() || yult != ring[i].y())
00729                                                 {
00730                                                         if(cursor_)
00731                                                         {
00732                                                                 cursor_->appendOrdinates(ring[i].x());
00733                                                                 cursor_->appendOrdinates (ring[i].y()); 
00734                                                         }
00735                                                         else
00736                                                         {
00737                                                                 connection_->appendOrdinates(ring[i].x());
00738                                                                 connection_->appendOrdinates (ring[i].y());     
00739                                                         }
00740                                                 
00741                                                         xult = ring[i].x();
00742                                                         yult = ring[i].y();
00743                                                 }
00744                                         }
00745                                 }
00746                                 //keep UNCLOCKWISE ring 
00747                                 else
00748                                 {
00749                                         for (int i=0;i<size;i++)
00750                                         {
00751                                                 if(xult != ring[size-1-i].x() || yult != ring[size-1-i].y())
00752                                                 {
00753                                                         if(cursor_)
00754                                                         {
00755                                                                 cursor_->appendOrdinates(ring[size-1-i].x());
00756                                                                 cursor_->appendOrdinates (ring[size-1-i].y());
00757                                                         }
00758                                                         else
00759                                                         {
00760                                                                 connection_->appendOrdinates(ring[size-1-i].x());
00761                                                                 connection_->appendOrdinates (ring[size-1-i].y());
00762                                                         }
00763                                                         
00764                                                         xult = ring[size-1-i].x();
00765                                                         yult = ring[size-1-i].y();
00766                                                 }
00767                                         }
00768                                 }
00769                         }
00770 
00771                         else  //internal polygon: CLOCKWISE
00772                         {
00773                                 int pos = ((totalsize - size) * 2) + 1; 
00774                                 elInfo += ", " + Te2String(pos) + ", 2003, 1";
00775                                                 
00776                                 if(orient == TeCLOCKWISE)   
00777                                 {
00778                                         for (int i=0;i<size;i++)
00779                                         {
00780                                                 if(xult != ring[i].x() || yult != ring[i].y())
00781                                                 {
00782                                                         if(cursor_)
00783                                                         {
00784                                                                 cursor_->appendOrdinates(ring[i].x());
00785                                                                 cursor_->appendOrdinates (ring[i].y());
00786                                                         }
00787                                                         else
00788                                                         {
00789                                                                 connection_->appendOrdinates(ring[i].x());
00790                                                                 connection_->appendOrdinates (ring[i].y());
00791                                                         }
00792 
00793                                                         xult = ring[i].x();
00794                                                         yult = ring[i].y();
00795                                                 }
00796                                         }
00797                                 }
00798                                 //keep CLOCKWISE ring
00799                                 else
00800                                 {
00801                                         for (int i=0;i<size;i++)
00802                                         {
00803                                                 if(xult != ring[size-1-i].x() || yult != ring[size-1-i].y())
00804                                                 {
00805                                                         connection_->appendOrdinates(ring[size-1-i].x());
00806                                                         connection_->appendOrdinates (ring[size-1-i].y());
00807                                                         
00808                                                         xult = ring[size-1-i].x();
00809                                                         yult = ring[size-1-i].y();
00810                                                 }
00811                                         }
00812                                 }
00813                         }
00814                 }//for all rings
00815         }
00816         catch(...)
00817         {
00818                 return false;
00819         }
00820         
00821         return true;    
00822 }
00823 
00824 bool 
00825 TeOracleSpatial::allocateOrdinatesObject(TeLine2D &line, TeOCICursor* cursor_)
00826 {
00827         int size = line.size();
00828         double  xult, yult;
00829         xult = -9999.99;
00830         yult = -9999.99;
00831 
00832         try
00833         {
00834                 //OCI: create the ordinates array
00835                 if(!cursor_)
00836                         connection_->allocateObjectOrdinates ();
00837 
00838                 for (int i=0;i<size;i++)
00839                 {               
00840                         if(xult != line[i].x() || yult != line[i].y())
00841                         {
00842                                 if(cursor_)
00843                                 {
00844                                         cursor_->appendOrdinates(line[i].x());
00845                                         cursor_->appendOrdinates(line[i].y());
00846                                 }
00847                                 else
00848                                 {
00849                                         connection_->appendOrdinates(line[i].x());
00850                                         connection_->appendOrdinates(line[i].y());
00851                                 }
00852                                 xult = line[i].x();
00853                                 yult = line[i].y();
00854                         }
00855                 }
00856         }
00857         catch(...)
00858         {
00859                 return false;
00860         }
00861 
00862         return true;
00863 }
00864 
00865 
00866 bool 
00867 TeOracleSpatial::insertPolygon (const string& table, TePolygon &poly)
00868 {
00869         string  elinfo;
00870         
00871         if(!allocateOrdinatesObject(poly, elinfo))
00872                 return false;
00873         
00874         string ins = "INSERT INTO " + table + " ( ";
00875         ins += " geom_id, object_id, spatial_data) VALUES ( ";
00876         ins += getNameSequence(table) +".NEXTVAL";
00877         ins += ", '" + escapeSequence(poly.objectId()) + "'";
00878         ins += ", MDSYS.SDO_GEOMETRY(2003, NULL, NULL";
00879         ins += ", MDSYS.SDO_ELEM_INFO_ARRAY( " + elinfo + " )";
00880         ins += ", :ordinates_) ";
00881         ins += " )";
00882 
00883         if(!connection_->executeSDOSTM(ins))
00884         {
00885                 errorMessage_ = "Error inserting in the table " + table + "!"; 
00886                 return false;
00887         }
00888         return true;
00889 }
00890 
00891 bool 
00892 TeOracleSpatial::updatePolygon (const string& table, TePolygon &poly)
00893 {
00894         if(!tableExist(table))
00895         {       
00896                 errorMessage_ = "Table not exist!";
00897                 return false;
00898         }
00899         
00900         string elinfo;
00901 
00902         if(!allocateOrdinatesObject(poly, elinfo))
00903                 return false;
00904                 
00905         string sql;
00906         sql =  "UPDATE " + table + " SET ";
00907         sql += ", object_id = '" + poly.objectId() + "'";
00908         sql += ", spatial_data = ";
00909         sql += " MDSYS.SDO_GEOMETRY(2003, NULL, NULL";
00910         sql += ", MDSYS.SDO_ELEM_INFO_ARRAY( " + elinfo + " )";
00911         sql += ", :ordinates_) ";
00912         sql += " WHERE geom_id = " + poly.geomId();
00913 
00914         //OCI
00915         if(!connection_->executeSDOSTM(sql))
00916         {
00917                 errorMessage_ = "Error updating in the table " + table + "!"; 
00918                 return false;
00919         }
00920         
00921         return true;
00922 }
00923 
00924 bool 
00925 TeOracleSpatial::selectPolygonSet (const string& table, const string& criteria, TePolygonSet &ps)
00926 {
00927         TeOracleSpatialPortal *ocip = (TeOracleSpatialPortal*)getPortal();
00928         if(!ocip)
00929                 return false;
00930 
00931         string sql ="SELECT * FROM " + table;
00932         if (!criteria.empty())
00933                 sql += " WHERE " + criteria;
00934         sql += " ORDER BY object_id ASC ";
00935          
00936         if (!ocip->query(sql) || !ocip->fetchRow())
00937         {
00938                 delete ocip;
00939                 return false;
00940         }
00941         
00942         bool flag = true;
00943         do
00944         {
00945                 TePolygon poly;
00946                 flag = ocip->fetchGeometry(poly);
00947                 ps.add(poly);
00948         }
00949         while (flag);
00950 
00951         delete ocip;
00952         return true;
00953 }
00954 
00955 bool 
00956 TeOracleSpatial::loadPolygonSet (const string& table, const string& geoid, TePolygonSet &ps)
00957 {
00958         TeOracleSpatialPortal *ocip = (TeOracleSpatialPortal*)getPortal();
00959         if(!ocip)
00960                 return false;
00961 
00962         string q ="SELECT * FROM " + table;
00963 
00964         if (geoid != "")
00965                 q += " WHERE object_id = '" + geoid +"'";
00966         
00967         if (!ocip->query(q) || !ocip->fetchRow())
00968         {       
00969                 delete ocip;
00970                 return false;
00971         }
00972 
00973         bool flag = true;
00974         do
00975         {
00976                 TePolygon poly;
00977                 flag = ocip->fetchGeometry(poly);
00978                 ps.add(poly);
00979         }
00980         while (flag);
00981         delete ocip;
00982         return true;
00983 }
00984 
00985 
00986 bool 
00987 TeOracleSpatial::loadPolygonSet (const string& table, TeBox &box, TePolygonSet &ps)
00988 {
00989         TeOracleSpatialPortal *ocip = (TeOracleSpatialPortal*)getPortal();
00990         if(!ocip)
00991                 return false;
00992 
00993         string q = "SELECT * FROM " + table;
00994         q += this->getSQLBoxWhere (box, TePOLYGONS, table);
00995         q += " ORDER BY object_id ";
00996 
00997         if (!ocip->query(q) || !ocip->fetchRow())
00998         {       
00999                 delete ocip;
01000                 return false;
01001         }
01002         bool flag = true;
01003         do
01004         {
01005                 TePolygon poly;
01006                 flag = ocip->fetchGeometry(poly);
01007                 ps.add(poly);
01008         }
01009         while (flag);
01010         delete ocip;
01011         return true;
01012 }
01013 
01014 bool 
01015 TeOracleSpatial::loadPolygonSet(TeTheme* theme, TePolygonSet &ps)
01016 {
01017         string collTable = theme->collectionTable();
01018         if (collTable.empty())
01019                 return false;
01020 
01021         TeLayer* themeLayer = theme->layer();
01022         if (!themeLayer->hasGeometry(TePOLYGONS))
01023                 return false;
01024         
01025         string polygonTable = themeLayer->tableName(TePOLYGONS);
01026         if (polygonTable.empty())
01027                 return false;
01028 
01029         string sql = "SELECT * FROM (" + polygonTable + " RIGHT JOIN " + collTable;
01030         sql += " ON " + polygonTable + ".object_id = " + collTable + ".c_object_id)";
01031         
01032         TeOracleSpatialPortal *ocip = (TeOracleSpatialPortal*)getPortal();
01033         if(!ocip)
01034                 return false;
01035 
01036         if (!ocip->query(sql) || !ocip->fetchRow())
01037         {
01038                 delete ocip;
01039                 return false;
01040         }
01041 
01042         bool flag = true;
01043         do
01044         {
01045                 TePolygon poly;
01046                 flag = ocip->fetchGeometry(poly);
01047                 ps.add ( poly );
01048         }
01049         while (flag);           
01050         delete ocip;
01051         return true;
01052 }
01053 
01054 TeDatabasePortal* 
01055 TeOracleSpatial::loadPolygonSet(const string& table, TeBox &box)
01056 {
01057         TeOracleSpatialPortal *portal = (TeOracleSpatialPortal*)this->getPortal();
01058         if (!portal)
01059                 return 0;
01060 
01061         string q;
01062         q = "SELECT * FROM " + table + " WHERE ";
01063         q += this->getSQLBoxWhere (box, TePOLYGONS, table);
01064         q += " ORDER BY object_id ";
01065 
01066         if (!portal->query(q) || !portal->fetchRow())
01067         {       
01068                 delete portal;
01069                 return 0;
01070         }
01071         else 
01072                 return portal;
01073 }
01074 
01075 
01076 //Spatial query
01077 //retornam um portal
01078 bool 
01079 TeOracleSpatial::spatialRelation(const string& actGeomTable, TeGeomRep /* actRep */, TeKeys& actIdsIn, TeDatabasePortal *portal, int relate, const string& actCollTable)
01080 {
01081         string Ids = getStringIds(actIdsIn);
01082         string actGeomColl = "spatial_data";
01083         
01084         string spatialRel = getOracleSpatialRelation(relate);
01085         
01086         //Montar a sql para passar para o Oracle
01087         string sql = "SELECT geomTable1.* ";
01088         sql += " FROM "+ actGeomTable +" geomTable1,";
01089         sql += actGeomTable + " geomTable2 ";
01090         
01091         if(!actCollTable.empty())
01092         {
01093                 sql += ", "+ actCollTable +" collTable ";
01094                 sql += " WHERE geomTable1.object_id = collTable.c_object_id AND ";
01095         }
01096         else
01097                 sql += " WHERE ";
01098         
01099         sql += " geomTable2.object_id IN (" + Ids + ") AND ";
01100 
01101         if(relate==TeEQUALS)
01102                 sql += " geomTable1.object_id NOT IN (" + Ids + ") AND ";  
01103         
01104         if(relate==TeDISJOINT)
01105                 sql += " NOT ";  // NOT ANYINTERACT
01106 
01107         sql += " SDO_RELATE(geomTable1."+ actGeomColl +", geomTable2."+ actGeomColl +", 'mask= "; 
01108         sql += spatialRel + " querytype=WINDOW') = 'TRUE'";
01109 
01110         portal->freeResult();
01111         if(!((TeOracleSpatialPortal*)portal)->querySDO (sql)) 
01112                 return false;
01113 
01114         return (portal->fetchRow());
01115 }
01116 
01117 bool 
01118 TeOracleSpatial::spatialRelation(const string& actGeomTable, TeGeomRep /* actRep */, TeKeys& actIdsIn, const string& visGeomTable, TeGeomRep /* visRep */, TeDatabasePortal *portal, int relate, const string& visCollTable)
01119 {
01120         string Ids = getStringIds(actIdsIn);
01121         string spatialRel = getOracleSpatialRelation(relate);
01122         string actGeomColl = "spatial_data";
01123         string visGeomColl = "spatial_data";
01124         
01125         //Montar a sql para passar para o Oracle
01126         string sql = "SELECT geomTable1.* ";
01127         sql += " FROM "+ visGeomTable +" geomTable1,";
01128         sql += actGeomTable + " geomTable2 ";
01129         
01130         if(!visCollTable.empty())
01131         {
01132                 sql += ", "+ visCollTable +" collTable";
01133                 sql += " WHERE geomTable1.object_id = collTable.c_object_id AND ";
01134         }
01135         else
01136                 sql += " WHERE ";
01137         
01138         sql += " geomTable2.object_id IN (" + Ids + ") AND ";
01139 
01140         if(relate==TeDISJOINT)
01141                 sql += " NOT ";  // NOT ANYINTERACT
01142 
01143         sql += " SDO_RELATE(geomTable1."+ visGeomColl +", geomTable2."+ actGeomColl +", 'mask= "; 
01144         sql += spatialRel + " querytype=WINDOW') = 'TRUE'";
01145 
01146         portal->freeResult();
01147         if(!((TeOracleSpatialPortal*)portal)->querySDO(sql))
01148                 return false;
01149         
01150         return (portal->fetchRow());
01151                 
01152 }
01153 
01154 bool 
01155 TeOracleSpatial::spatialRelation(const string& actGeomTable, TeGeomRep /* actRep */, TeGeometry* geom, TeDatabasePortal *portal, int relate, const string& actCollTable) 
01156 {
01157         portal->freeResult();
01158         string elinfo, sdo;
01159         
01160         TeOCICursor     *cursor_ = ((TeOracleSpatialPortal*)portal)->getCursor();       
01161         string spatialRel = getOracleSpatialRelation(relate);
01162         string actGeomColl = "spatial_data";
01163 
01164         if(geom->elemType()==TePOLYGONS)
01165         {
01166                 TePolygon poly, *pPoly;
01167                 pPoly = new TePolygon();
01168                 pPoly = (TePolygon*)geom;
01169                 poly = *pPoly;
01170 
01171                 if(!allocateOrdinatesObject(poly, elinfo, cursor_))
01172                 {
01173                         delete cursor_;
01174                         return false;
01175                 }
01176 
01177                 sdo = " MDSYS.SDO_GEOMETRY(2003, NULL, NULL, ";
01178                 sdo += " MDSYS.SDO_ELEM_INFO_ARRAY(" + elinfo + "), ";
01179                 sdo += " :ordinates_)";
01180 
01181                 //delete pPoly;  //delete tambem o geom, talvez deixar para aplicacao
01182         }
01183         
01184         else if (geom->elemType()==TeLINES)
01185         {
01186                 TeLine2D line, *pLine;
01187                 pLine = new TeLine2D();
01188                 pLine = (TeLine2D*)geom;
01189                 line = *pLine;
01190 
01191                 if(!allocateOrdinatesObject(line, cursor_))
01192                 {
01193                         delete cursor_;
01194                         return false;
01195                 }
01196 
01197                 elinfo = "1, 2, 1";
01198 
01199                 sdo = " MDSYS.SDO_GEOMETRY(2002, NULL, NULL, ";
01200                 sdo += " MDSYS.SDO_ELEM_INFO_ARRAY(" + elinfo + "), ";
01201                 sdo += " :ordinates_)";
01202 
01203                 //delete pLine;  //delete tambem o geom
01204         }
01205         
01206         else if (geom->elemType()==TePOINTS)
01207         {
01208                 TePoint point, *pPoint;
01209                 pPoint = new TePoint();
01210                 pPoint = (TePoint*)geom;
01211                 point = *pPoint;
01212                 
01213                 sdo = " MDSYS.SDO_GEOMETRY(2001, NULL, ";
01214                 sdo += " MDSYS.SDO_POINT_TYPE( ";
01215                 sdo += Te2String(point.location().x(),15);
01216                 sdo += ", " + Te2String(point.location().y(),15);
01217                 sdo += ", NULL )";
01218                 sdo += ", NULL, NULL))";
01219                 
01220                 //delete pPoint;
01221         }
01222 
01223         else if (geom->elemType()==TeCELLS)
01224         {
01225                 TeCell cell, *pCell;
01226                 pCell = new TeCell();
01227                 pCell = (TeCell*)geom;
01228                 cell = *pCell;
01229 
01230                 TeBox b = cell.box();
01231                 
01232                 sdo = " MDSYS.SDO_GEOMETRY(2003, NULL, NULL ";
01233                 sdo += ", MDSYS.SDO_ELEM_INFO_ARRAY( 1, 1003, 3 )";
01234                 sdo += ", MDSYS.SDO_ORDINATE_ARRAY( " ;
01235                 sdo += Te2String(b.lowerLeft().x(),15);
01236                 sdo += ", " + Te2String(b.lowerLeft().y(),15);
01237                 sdo += ", " + Te2String(b.upperRight().x(),15);
01238                 sdo += ", " + Te2String(b.upperRight().y(),15);
01239                 sdo += ")) ";
01240 
01241                 //delete pCell;
01242         }
01243         
01244         //Montar a sql para passar para o Oracle
01245         string sql = "SELECT geomTable.* ";
01246         sql += " FROM " + actGeomTable + " geomTable ";
01247 
01248         if(!actCollTable.empty())
01249         {
01250                 sql += ", "+ actCollTable +" collTable ";
01251                 sql += " WHERE geomTable.object_id = collTable.c_object_id AND ";
01252         }
01253         else
01254                 sql += " WHERE ";
01255         
01256         
01257         if(relate==TeDISJOINT)
01258                 sql += " NOT ";  // NOT ANYINTERACT
01259 
01260         sql += " MDSYS.SDO_RELATE(geomTable."+ actGeomColl +", "+ sdo +", 'mask= "; 
01261         sql += spatialRel + " querytype=WINDOW') = 'TRUE'";
01262 
01263         if(!((TeOracleSpatialPortal*)portal)->querySDO(sql))
01264                 return false;
01265 
01266         return (portal->fetchRow());
01267 }
01268 
01269 
01270 //retornam um vetor de object_ids resultantes da consulta
01271 bool 
01272 TeOracleSpatial::spatialRelation(const string& actGeomTable, TeGeomRep /* actRep */, TeKeys& actIdsIn, TeKeys& actIdsOut, int relate, const string& actCollTable)
01273 {
01274         TeOracleSpatialPortal* portal = (TeOracleSpatialPortal*) getPortal(); 
01275                 
01276         string Ids = getStringIds(actIdsIn);
01277         string spatialRel = getOracleSpatialRelation(relate);
01278         string actGeomColl = "spatial_data";
01279 
01280         //Montar a sql para passar para o Oracle
01281         string sql = "SELECT geomTable1.object_id ";
01282         sql += " FROM "+ actGeomTable +" geomTable1,";
01283         sql += actGeomTable + " geomTable2 ";
01284         
01285         if(!actCollTable.empty())
01286         {
01287                 sql += ", "+ actCollTable +" collTable ";
01288                 sql += " WHERE geomTable1.object_id = collTable.c_object_id AND ";
01289         }
01290         else
01291                 sql += " WHERE ";
01292         
01293         sql += " geomTable2.object_id IN (" + Ids + ") AND ";
01294 
01295         if(relate==TeEQUALS)
01296                 sql += " geomTable1.object_id NOT IN (" + Ids + ") AND ";  
01297         
01298         if(relate==TeDISJOINT)
01299                 sql += " NOT ";  // NOT ANYINTERACT
01300 
01301         sql += " SDO_RELATE(geomTable1."+ actGeomColl +", geomTable2."+ actGeomColl +", 'mask= "; 
01302         sql += spatialRel + " querytype=WINDOW') = 'TRUE'";
01303 
01304         if(!portal->query(sql))
01305         {
01306                 delete portal;
01307                 return false;
01308         }
01309         
01310         actIdsOut.clear();
01311         while(portal->fetchRow())
01312         {
01313                 string objId = portal->getData (0);
01314                 actIdsOut.push_back(objId);
01315         }
01316 
01317         sort(actIdsOut.begin(), actIdsOut.end());
01318         unique(actIdsOut.begin(), actIdsOut.end());
01319 
01320         delete portal;
01321         return true;
01322 }
01323 
01324 
01325 bool 
01326 TeOracleSpatial::spatialRelation(const string& actGeomTable, TeGeomRep /* actRep */, TeKeys& actIdsIn, const string& visGeomTable, TeGeomRep /* visRep */, TeKeys& visIdsOut, int relate, const string& visCollTable)
01327 {
01328         TeOracleSpatialPortal* portal = (TeOracleSpatialPortal*) getPortal(); 
01329         
01330         string Ids = getStringIds(actIdsIn);
01331         string spatialRel = getOracleSpatialRelation(relate);
01332         string actGeomColl = "spatial_data";
01333         string visGeomColl = "spatial_data";
01334         
01335         //Montar a sql para passar para o Oracle
01336         string sql = "SELECT geomTable1.object_id ";
01337         sql += " FROM "+ visGeomTable +" geomTable1,";
01338         sql += actGeomTable + " geomTable2 ";
01339         
01340         if(!visCollTable.empty())
01341         {
01342                 sql += ", "+ visCollTable +" collTable";
01343                 sql += " WHERE geomTable1.object_id = collTable.c_object_id AND ";
01344         }
01345         else
01346                 sql += " WHERE ";
01347         
01348         sql += " geomTable2.object_id IN (" + Ids + ") AND ";
01349 
01350         if(relate==TeDISJOINT)
01351                 sql += " NOT ";  // NOT ANYINTERACT
01352 
01353         sql += " SDO_RELATE(geomTable1."+ visGeomColl +", geomTable2."+ actGeomColl +", 'mask= "; 
01354         sql += spatialRel + " querytype=WINDOW') = 'TRUE'";
01355 
01356         if(!portal->query(sql))
01357         {
01358                 delete portal;
01359                 return false;
01360         }
01361         
01362         visIdsOut.clear();
01363         while(portal->fetchRow())
01364         {
01365                 string objId = portal->getData (0);
01366                 visIdsOut.push_back(objId);
01367         }
01368 
01369         sort(visIdsOut.begin(), visIdsOut.end());
01370         unique(visIdsOut.begin(), visIdsOut.end());
01371 
01372         delete portal;
01373         return true;
01374 }
01375 
01376 
01377 bool 
01378 TeOracleSpatial::spatialRelation(const string& actGeomTable, TeGeomRep actRep, TeGeometry* geom, TeKeys& actIdsOut, int relate, const string& actCollTable)
01379 {
01380         TeOracleSpatialPortal* portal = (TeOracleSpatialPortal*) getPortal(); 
01381                 
01382         if(!spatialRelation(actGeomTable, actRep, geom, portal, relate, actCollTable))
01383         {
01384                 delete portal;
01385                 return false;
01386         }
01387                 
01388         actIdsOut.clear();
01389         do
01390         {
01391                 string objId = portal->getData ("object_id");
01392                 actIdsOut.push_back(objId);
01393         }while(portal->fetchRow());
01394 
01395         sort(actIdsOut.begin(), actIdsOut.end());
01396         unique(actIdsOut.begin(), actIdsOut.end());
01397 
01398         delete portal;
01399         return true;
01400 }
01401 
01402 // metric functions
01403 bool
01404 TeOracleSpatial::calculateArea(const string& actGeomTable, TeGeomRep /* actRep */, TeKeys& actIdsIn, double &area)
01405 {
01406         string Ids = getStringIds(actIdsIn);
01407         string actGeomColl = "spatial_data";
01408         
01409         string sql = "SELECT SUM(SDO_GEOM.SDO_AREA(g."+ actGeomColl +", m.diminfo))";
01410         sql += " FROM "+ actGeomTable +" g, USER_SDO_GEOM_METADATA m";
01411         sql += " WHERE m.table_name = '"+ TeConvertToUpperCase(actGeomTable) +"'";
01412         sql += " AND m.column_name = '"+ TeConvertToUpperCase(actGeomColl) +"'"; 
01413         sql += " AND object_id IN ("+ Ids +")";
01414 
01415         TeOracleSpatialPortal* portal = (TeOracleSpatialPortal*)getPortal();
01416         
01417         if(!portal->query(sql) || !portal->fetchRow())
01418         {
01419                 delete portal;
01420                 return false;
01421         }
01422 
01423         area = portal->getDouble(0);
01424         delete portal;
01425         return true;
01426 }
01427 
01428 bool
01429 TeOracleSpatial::calculateLength(const string& actGeomTable, TeGeomRep /* actRep */, TeKeys& actIdsIn, double &length)
01430 {
01431         string Ids = getStringIds(actIdsIn);
01432         string actGeomColl = "spatial_data";
01433         
01434         string sql = "SELECT SUM(SDO_GEOM.SDO_LENGTH(g."+ actGeomColl +", m.diminfo))";
01435         sql += " FROM "+ actGeomTable +" g, USER_SDO_GEOM_METADATA m";
01436         sql += " WHERE m.table_name = '"+ TeConvertToUpperCase(actGeomTable) +"'";
01437         sql += " AND m.column_name = '"+ TeConvertToUpperCase(actGeomColl) +"'"; 
01438         sql += " AND object_id IN ("+ Ids +")";
01439 
01440         TeOracleSpatialPortal* portal = (TeOracleSpatialPortal*)getPortal();
01441         
01442         if(!portal->query(sql) || !portal->fetchRow())
01443         {
01444                 delete portal;
01445                 return false;
01446         }
01447 
01448         length = portal->getDouble(0);
01449         delete portal;
01450         return true;
01451 }
01452 
01453 
01454 //distancia entre objetos de um mesma tabela
01455 bool
01456 TeOracleSpatial::calculateDistance(const string& actGeomTable, TeGeomRep /* actRep */, TeKeys& Ids, double& distance)
01457 {
01458         string Id1 = Ids[0];
01459         string Id2 = Ids[1];
01460         string actGeomColl = "spatial_data";
01461 
01462         string sql = "SELECT MIN(SDO_GEOM.SDO_DISTANCE(g1."+ actGeomColl +", m.diminfo, ";
01463         sql += " g2."+ actGeomColl +", m.diminfo))";
01464         sql += " FROM "+ actGeomTable +" g1,"+ actGeomTable +" g2, USER_SDO_GEOM_METADATA m";
01465         sql += " WHERE m.table_name = '"+ TeConvertToUpperCase(actGeomTable) +"'";
01466         sql += " AND m.column_name = '"+ TeConvertToUpperCase(actGeomColl) +"'"; 
01467         sql += " AND g1.object_id = '"+ Id1 +"'"; 
01468         sql += " AND g2.object_id = '"+ Id2 +"'";
01469 
01470         TeOracleSpatialPortal* portal = (TeOracleSpatialPortal*)getPortal();
01471         
01472         if(!portal->query(sql) || !portal->fetchRow())
01473         {
01474                 delete portal;
01475                 return false;
01476         }
01477 
01478         distance = portal->getDouble(0);
01479         delete portal;
01480         return true;
01481 }
01482 
01483 
01484 //distancia entre objetos de duas tabelas distintas
01485 bool
01486 TeOracleSpatial::calculateDistance(const string& actGeomTable, TeGeomRep /* actRep */, const string& objId1, const string& visGeomTable, TeGeomRep /* visRep */, const string& objId2, double& distance)
01487 {
01488         string actGeomColl = "spatial_data";
01489         string visGeomColl = "spatial_data";
01490         
01491         string sql = "SELECT MIN(SDO_GEOM.SDO_DISTANCE(g1."+ actGeomColl +", m1.diminfo, ";
01492         sql += " g2."+ visGeomColl +", m2.diminfo))";
01493         sql += " FROM "+ actGeomTable +" g1,"+ visGeomTable +" g2, ";
01494         sql += " USER_SDO_GEOM_METADATA m1, USER_SDO_GEOM_METADATA m2 ";
01495         sql += " WHERE m1.table_name = '"+ TeConvertToUpperCase(actGeomTable) +"'";
01496         sql += " AND m1.column_name = '"+ TeConvertToUpperCase(actGeomColl) +"'"; 
01497         sql += " AND m2.table_name = '"+ TeConvertToUpperCase(visGeomTable) +"'";
01498         sql += " AND m2.column_name = '"+ TeConvertToUpperCase(visGeomColl) +"'";
01499         sql += " AND g1.object_id = '"+ objId1 +"'"; 
01500         sql += " AND g2.object_id = '"+ objId2 +"'";
01501 
01502         TeOracleSpatialPortal* portal = (TeOracleSpatialPortal*)getPortal();
01503         
01504         if(!portal->query(sql) || !portal->fetchRow())
01505         {
01506                 delete portal;
01507                 return false;
01508         }
01509 
01510         distance = portal->getDouble(0);
01511         delete portal;
01512         return true;
01513 }
01514 
01515 
01516 
01517 // functions that return a new geometry
01518 
01519 //Euclidean distance value: dist
01520 bool
01521 TeOracleSpatial::buffer(const string& actGeomTable, TeGeomRep /* actRep */, TeKeys& actIds, TePolygonSet& bufferSet, double dist)
01522 {
01523         string Ids = getStringIds(actIds);
01524         string actGeomColl = "spatial_data";
01525 
01526         string sql = "SELECT g.geom_id, g.object_id,";
01527         sql += " SDO_GEOM.SDO_BUFFER(g."+ actGeomColl +", m.diminfo, "+ Te2String(dist, 15) +")";
01528         sql += " FROM "+ actGeomTable +" g, USER_SDO_GEOM_METADATA m";
01529         sql += " WHERE m.table_name = '"+ TeConvertToUpperCase(actGeomTable) +"'";
01530         sql += " AND m.column_name = '"+ TeConvertToUpperCase(actGeomColl) +"'"; 
01531         sql += " AND object_id IN ("+ Ids +")";
01532 
01533         TeOracleSpatialPortal* portal = (TeOracleSpatialPortal*)getPortal();
01534         
01535         if(!portal->query(sql) || !portal->fetchRow())
01536         {
01537                 delete portal;
01538                 return false;
01539         }
01540 
01541         bool flag = true;
01542         do 
01543         {
01544                 TePolygonSet polySet;
01545                 flag = portal->fetchGeometry(polySet);
01546                 //teste c/ buffer com filhos
01547                 for(unsigned int i=0; i<polySet.size(); i++)
01548                         bufferSet.add(polySet[i]);
01549 
01550         }while(flag);
01551 
01552         delete portal;
01553         return true;
01554 }
01555 
01556 bool 
01557 TeOracleSpatial::convexHull(const string& actGeomTable, TeGeomRep /* actRep */, TeKeys& actIds, TePolygonSet& convexHullSet)
01558 {
01559         string Ids = getStringIds(actIds);
01560         string actGeomColl = "spatial_data";
01561         
01562         string sql = "SELECT g.geom_id, g.object_id,";
01563         sql += " SDO_GEOM.SDO_CONVEXHULL(g."+ actGeomColl +", m.diminfo )";
01564         sql += " FROM "+ actGeomTable +" g, USER_SDO_GEOM_METADATA m";
01565         sql += " WHERE m.table_name = '"+ TeConvertToUpperCase(actGeomTable) +"'";
01566         sql += " AND m.column_name = '"+ TeConvertToUpperCase(actGeomColl) +"'"; 
01567         sql += " AND object_id IN ("+ Ids +")";
01568 
01569         TeOracleSpatialPortal* portal = (TeOracleSpatialPortal*)getPortal();
01570         
01571         if(!portal->query(sql) || !portal->fetchRow())
01572         {
01573                 delete portal;
01574                 return false;
01575         }
01576         
01577         bool flag = true;
01578         do 
01579         {
01580                 TePolygon poly;
01581                 flag = portal->fetchGeometry(poly);
01582                 convexHullSet.add(poly);
01583         }while(flag);
01584 
01585         delete portal;
01586         return true;
01587 }
01588 
01589 bool 
01590 TeOracleSpatial::centroid(const string&  actGeomTable , TeGeomRep /* actRep */, TePointSet& centroidSet, TeKeys actIds, const string& /* actCollTable */)
01591 {
01592         string Ids = getStringIds(actIds);
01593         string actGeomColl = "spatial_data";
01594         
01595         string sql = "SELECT g.geom_id, g.object_id,";
01596         sql += " SDO_GEOM.SDO_CENTROID(g."+ actGeomColl +", m.diminfo )";
01597         sql += " FROM "+ actGeomTable +" g, USER_SDO_GEOM_METADATA m";
01598         sql += " WHERE m.table_name = '"+ TeConvertToUpperCase(actGeomTable) +"'";
01599         sql += " AND m.column_name = '"+ TeConvertToUpperCase(actGeomColl) +"'"; 
01600         
01601         //if empty it calculates the centroids to all geometries 
01602         if(!Ids.empty())
01603                 sql += " AND object_id IN ("+ Ids +")";
01604 
01605         TeOracleSpatialPortal* portal = (TeOracleSpatialPortal*)getPortal();
01606         
01607         if(!portal->query(sql) || !portal->fetchRow())
01608         {
01609                 delete portal;
01610                 return false;
01611         }
01612 
01613         bool flag = true;
01614         do 
01615         {
01616                 TePoint point;
01617                 flag = portal->fetchGeometry(point);
01618                 centroidSet.add(point);
01619         }while(flag);
01620 
01621         delete portal;
01622         return true;
01623         
01624 }
01625 
01626 bool 
01627 TeOracleSpatial::nearestNeighbors(const string& actGeomTable, const string& actCollTable, TeGeomRep actRep, const string& objId1, TeKeys& actIdsOut, int numRes)
01628 {
01629         TeOracleSpatialPortal* portal = (TeOracleSpatialPortal*) getPortal(); 
01630         
01631         if(!nearestNeighbors(actGeomTable, actCollTable, actRep, objId1, portal, numRes))
01632         {
01633                 delete portal;
01634                 return false;
01635         }
01636                 
01637         actIdsOut.clear();
01638         while(portal->fetchRow())
01639         {
01640                 string objId = portal->getData ("object_id");
01641                 actIdsOut.push_back(objId);
01642         }
01643 
01644         sort(actIdsOut.begin(), actIdsOut.end());
01645         unique(actIdsOut.begin(), actIdsOut.end());
01646 
01647         delete portal;
01648         return true;
01649 }
01650 
01651 
01652 bool 
01653 TeOracleSpatial::nearestNeighbors(const string& actGeomTable, TeGeomRep actRep, const string& objId1, const string& visGeomTable, const string& visCollTable, TeGeomRep visRep, TeKeys& visIdsOut, int numRes)
01654 {
01655         TeOracleSpatialPortal* portal = (TeOracleSpatialPortal*) getPortal(); 
01656         
01657         if(!nearestNeighbors(actGeomTable, actRep, objId1, visGeomTable, visCollTable, visRep, portal, numRes))
01658         {
01659                 delete portal;
01660                 return false;
01661         }
01662                 
01663         visIdsOut.clear();
01664         while(portal->fetchRow())
01665         {
01666                 string objId = portal->getData ("object_id");
01667                 visIdsOut.push_back(objId);
01668         }
01669 
01670         sort(visIdsOut.begin(), visIdsOut.end());
01671         unique(visIdsOut.begin(), visIdsOut.end());
01672 
01673         delete portal;
01674         return true;
01675 }
01676 
01677 bool 
01678 TeOracleSpatial::nearestNeighbors(const string& actGeomTable, const string& actCollTable, TeGeomRep /* actRep */, const string& objId1, TeDatabasePortal* portal, int numRes)
01679 {
01680         string actGeomColl = "spatial_data";
01681 
01682         //select the spatial index
01683         string index = " SELECT INDEX_NAME FROM USER_SDO_INDEX_INFO";
01684         index += " WHERE TABLE_NAME = '"+ TeConvertToUpperCase(actGeomTable) +"'"; 
01685 
01686         portal->freeResult();
01687         if(!portal->query(index) || !portal->fetchRow())
01688         return false;
01689 
01690         string indexName = string(portal->getData(0));
01691         string perf = "/*+ INDEX("+ TeConvertToUpperCase(actGeomTable) +" "+ indexName +") */ ";
01692         
01693         string sql = "SELECT "+ perf +"  geomTable1.* ";
01694         sql += " FROM "+ actGeomTable +" geomTable1,";
01695         sql += actGeomTable + " geomTable2 ";
01696         
01697         if(!actCollTable.empty())
01698         {
01699                 sql += ", "+ actCollTable +" collTable ";
01700                 sql += " WHERE ";
01701                 sql += " geomTable1.object_id = collTable.c_object_id AND ";
01702         }
01703         else
01704                 sql += " WHERE ";
01705                 
01706         sql += " SDO_NN(geomTable1."+ actGeomColl +", geomTable2."+ actGeomColl +", 'sdo_batch_size=10')='TRUE' AND ";
01707         sql += " geomTable2.object_id = '" + objId1 + "' AND ";
01708         sql += " geomTable1.object_id <> '"+ objId1 +"' AND ";
01709         sql += " ROWNUM <= "+ Te2String(numRes);
01710 
01711         portal->freeResult();
01712         if(!((TeOracleSpatialPortal*)portal)->querySDO(sql))
01713                 return false;
01714         
01715         return true;
01716 }
01717         
01718 bool 
01719 TeOracleSpatial::nearestNeighbors(const string& actGeomTable, TeGeomRep /* actRep */, const string& objId1, const string& visGeomTable, const string& visCollTable, TeGeomRep /* visRep */, TeDatabasePortal* portal, int numRes)
01720 {
01721         string actGeomColl = "spatial_data";
01722         string visGeomColl = "spatial_data";
01723 
01724         //select the spatial index
01725         string index = " SELECT INDEX_NAME FROM USER_SDO_INDEX_INFO";
01726         index += " WHERE TABLE_NAME = '"+ TeConvertToUpperCase(visGeomTable) +"'"; 
01727 
01728         portal->freeResult();
01729         if(!portal->query(index) || !portal->fetchRow())
01730                 return false;
01731 
01732         string indexName = string(portal->getData(0));
01733         string perf = "/*+ INDEX("+ TeConvertToUpperCase(visGeomTable) +" "+ indexName +") */ ";
01734         string nres = " ROWNUM <= "+ numRes;
01735         
01736         string sql = "SELECT "+ perf +"  geomTable1.* ";
01737         sql += " FROM "+ visGeomTable +" geomTable1,";
01738         sql += actGeomTable + " geomTable2 ";
01739         
01740         if(!visCollTable.empty())
01741         {
01742                 sql += ", "+ visCollTable +" collTable";
01743                 sql += " WHERE ";
01744                 sql += " geomTable1.object_id = collTable.c_object_id AND ";
01745         }
01746         else
01747                 sql += " WHERE ";
01748                 
01749         sql += " SDO_NN(geomTable1."+ actGeomColl +", geomTable2."+ actGeomColl;
01750         sql += ", 'sdo_batch_size=10') = 'TRUE' AND ";
01751         sql += " geomTable2.object_id = '" + objId1 + "' AND ";
01752         sql += " ROWNUM <= "+ Te2String(numRes);
01753         
01754         portal->freeResult();
01755         if(!((TeOracleSpatialPortal*)portal)->querySDO(sql))
01756                 return false;
01757         
01758         return true;
01759 }
01760 
01761 
01762 //Intersection entre dois objetos geograficos de uma mesma tabela
01763 
01764 
01765 bool 
01766 TeOracleSpatial::geomIntersection(const string& actGeomTable, TeGeomRep /* actRep */, TeKeys& actIds, TeGeometryVect& geomVect)
01767 {
01768         string actGeomColl = "spatial_data";
01769         string Ids = getStringIds(actIds);
01770 
01771         string sql = "SELECT SDO_GEOM.SDO_INTERSECTION(g1."+ actGeomColl +", m.diminfo, ";
01772         sql += " g2."+ actGeomColl +", m.diminfo)";
01773         sql += " FROM "+ actGeomTable +" g1,"+ actGeomTable +" g2, USER_SDO_GEOM_METADATA m";
01774         sql += " WHERE m.table_name = '"+ TeConvertToUpperCase(actGeomTable) +"'";
01775         sql += " AND m.column_name = '"+ TeConvertToUpperCase(actGeomColl) +"'"; 
01776         sql += " AND object_id IN ("+ Ids +")";
01777 
01778         TeOracleSpatialPortal* portal = (TeOracleSpatialPortal*)getPortal();
01779         
01780         if((!portal->query(sql))||(!portal->fetchRow()))
01781         {
01782                 delete portal;
01783                 return false;
01784         }
01785 
01786         bool flag = true;
01787         do 
01788         {
01789                 bool result=false;
01790                 TeGeometry* geom = new TeGeometry(); 
01791                 TeGeometry* geom2 = geom;
01792                 flag = portal->getGeometry(&geom, result);
01793                 if(result)
01794                         geomVect.push_back (geom);
01795                 delete geom2;
01796         }while(flag);
01797                 
01798         delete portal;
01799         return true;
01800 }
01801 
01802 
01803 bool 
01804 TeOracleSpatial::geomIntersection(const string& actGeomTable, TeGeomRep /* actRep */, const string& objId1, const string& visGeomTable, TeGeomRep /* visRep */, const string& objId2, TeGeometryVect& geomVect)
01805 {
01806         string actGeomColl = "spatial_data";
01807         string visGeomColl = "spatial_data";
01808         
01809         string sql = "SELECT SDO_GEOM.SDO_INTERSECTION(g1."+ actGeomColl +", m1.diminfo, ";
01810         sql += " g2."+ visGeomColl +", m2.diminfo)";
01811         sql += " FROM "+ actGeomTable +" g1,"+ visGeomTable +" g2, ";
01812         sql += " USER_SDO_GEOM_METADATA m1, USER_SDO_GEOM_METADATA m2 ";
01813         sql += " WHERE m1.table_name = '"+ TeConvertToUpperCase(actGeomTable) +"'";
01814         sql += " AND m1.column_name = '"+ TeConvertToUpperCase(actGeomColl) +"'"; 
01815         sql += " AND m2.table_name = '"+ TeConvertToUpperCase(visGeomTable) +"'";
01816         sql += " AND m2.column_name = '"+ TeConvertToUpperCase(visGeomColl) +"'"; 
01817         sql += " AND g1.object_id = '"+ objId1 +"'"; 
01818         sql += " AND g2.object_id = '"+ objId2 +"'";
01819 
01820         TeOracleSpatialPortal* portal = (TeOracleSpatialPortal*)getPortal();
01821         
01822         if(!portal->query(sql) || !portal->fetchRow())
01823         {
01824                 delete portal;
01825                 return false; 
01826         }
01827 
01828         bool flag = true;
01829         do 
01830         {
01831                 bool result=false;
01832                 TeGeometry* geom = new TeGeometry(); 
01833                 TeGeometry* geom2 = geom;
01834                 flag = portal->getGeometry(&geom, result);
01835                 if(result)
01836                         geomVect.push_back (geom);
01837                 delete geom2;
01838         }while(flag);
01839                 
01840         delete portal;
01841         return true;
01842 }
01843 
01844 
01845 bool 
01846 TeOracleSpatial::geomDifference(const string& actGeomTable, TeGeomRep /* actRep */, const string& objId1, const string& visGeomTable, TeGeomRep /* visRep */, const string& objId2, TeGeometryVect& geomVect)
01847 {
01848         string actGeomColl = "spatial_data";
01849         string visGeomColl = "spatial_data";
01850 
01851         string sql = "SELECT SDO_GEOM.SDO_DIFFERENCE(g1."+ actGeomColl +", m1.diminfo, ";
01852         sql += " g2."+ visGeomColl +", m2.diminfo)";
01853         sql += " FROM "+ actGeomTable +" g1,"+ visGeomTable +" g2, ";
01854         sql += " USER_SDO_GEOM_METADATA m1, USER_SDO_GEOM_METADATA m2 ";
01855         sql += " WHERE m1.table_name = '"+ TeConvertToUpperCase(actGeomTable) +"'";
01856         sql += " AND m1.column_name = '"+ TeConvertToUpperCase(actGeomColl) +"'"; 
01857         sql += " AND m2.table_name = '"+ TeConvertToUpperCase(visGeomTable) +"'";
01858         sql += " AND m2.column_name = '"+ TeConvertToUpperCase(visGeomColl) +"'"; 
01859         sql += " AND g1.object_id = '"+ objId1 +"'"; 
01860         sql += " AND g2.object_id = '"+ objId2 +"'";
01861 
01862         TeOracleSpatialPortal* portal = (TeOracleSpatialPortal*)getPortal();
01863         
01864         if(!portal->query(sql) || !portal->fetchRow())
01865         {
01866                 delete portal;
01867                 return false;
01868         }
01869 
01870         bool flag = true;
01871         do 
01872         {
01873                 bool result=false;
01874                 TeGeometry* geom = new TeGeometry(); 
01875                 TeGeometry* geom2 = geom;
01876                 flag = portal->getGeometry(&geom, result);
01877                 if(result)
01878                         geomVect.push_back (geom);
01879                 delete geom2;
01880         }while(flag);
01881                 
01882         delete portal;
01883         return true;
01884 }
01885 
01886 bool 
01887 TeOracleSpatial::geomDifference(const string& actGeomTable, TeGeomRep /* actRep */, const string& objId1, const string& objId2, TeGeometryVect& geomVect)
01888 {
01889         string actGeomColl = "spatial_data";
01890 
01891         string sql = "SELECT SDO_GEOM.SDO_DIFFERENCE(g1."+ actGeomColl +", m.diminfo, ";
01892         sql += " g2."+ actGeomColl +", m.diminfo)";
01893         sql += " FROM "+ actGeomTable +" g1,"+ actGeomTable +" g2, USER_SDO_GEOM_METADATA m";
01894         sql += " WHERE m.table_name = '"+ TeConvertToUpperCase(actGeomTable) +"'";
01895         sql += " AND m.column_name = '"+ TeConvertToUpperCase(actGeomColl) +"'"; 
01896         sql += " AND g1.object_id = '"+ objId1 +"'"; 
01897         sql += " AND g2.object_id = '"+ objId2 +"'";
01898 
01899         TeOracleSpatialPortal* portal = (TeOracleSpatialPortal*)getPortal();
01900         
01901         if(!portal->query(sql) || !portal->fetchRow())
01902         {
01903                 delete portal;
01904                 return false;
01905         }
01906 
01907         bool flag = true;
01908         do 
01909         {
01910                 bool result=false;
01911                 TeGeometry* geom = new TeGeometry(); 
01912                 TeGeometry* geom2 = geom;
01913                 flag = portal->getGeometry(&geom, result);
01914                 if(result)
01915                         geomVect.push_back (geom);
01916                 delete geom2;
01917         }while(flag);
01918                 
01919         delete portal;
01920         return true;
01921 }
01922 
01923 bool 
01924 TeOracleSpatial::geomXOr(const string& actGeomTable, TeGeomRep /* actRep */, const string& objId1, const string& objId2, TeGeometryVect& geomVect)
01925 {
01926         string actGeomColl = "spatial_data";
01927 
01928         string sql = "SELECT SDO_GEOM.SDO_XOR(g1."+ actGeomColl +", m.diminfo, ";
01929         sql += " g2."+ actGeomColl +", m.diminfo)";
01930         sql += " FROM "+ actGeomTable +" g1,"+ actGeomTable +" g2, USER_SDO_GEOM_METADATA m";
01931         sql += " WHERE m.table_name = '"+ TeConvertToUpperCase(actGeomTable) +"'";
01932         sql += " AND m.column_name = '"+ TeConvertToUpperCase(actGeomColl) +"'"; 
01933         sql += " AND g1.object_id = '"+ objId1 +"'"; 
01934         sql += " AND g2.object_id = '"+ objId2 +"'";
01935 
01936         TeOracleSpatialPortal* portal = (TeOracleSpatialPortal*)getPortal();
01937         
01938         if(!portal->query(sql) || !portal->fetchRow())
01939         {
01940                 delete portal;
01941                 return false;
01942         }
01943 
01944         bool flag = true;
01945         do 
01946         {
01947                 bool result=false;
01948                 TeGeometry* geom = new TeGeometry(); 
01949                 TeGeometry* geom2 = geom;
01950                 flag = portal->getGeometry(&geom, result);
01951                 if(result)
01952                         geomVect.push_back (geom);
01953                 delete geom2;
01954         }while(flag);
01955 
01956         delete portal;
01957         return true;
01958 }
01959 
01960 
01961 bool 
01962 TeOracleSpatial::geomXOr(const string& actGeomTable, TeGeomRep /* actRep */, const string& objId1, const string& visGeomTable, TeGeomRep /* visRep */, const string& objId2, TeGeometryVect& geomVect)
01963 {
01964         string actGeomColl = "spatial_data";
01965         string visGeomColl = "spatial_data";
01966 
01967         string sql = "SELECT SDO_GEOM.SDO_XOR(g1."+ actGeomColl +", m1.diminfo, ";
01968         sql += " g2."+ visGeomColl +", m2.diminfo)";
01969         sql += " FROM "+ actGeomTable +" g1,"+ visGeomTable +" g2, ";
01970         sql += " USER_SDO_GEOM_METADATA m1, USER_SDO_GEOM_METADATA m2 ";
01971         sql += " WHERE m1.table_name = '"+ TeConvertToUpperCase(actGeomTable) +"'";
01972         sql += " AND m1.column_name = '"+ TeConvertToUpperCase(actGeomColl) +"'"; 
01973         sql += " AND m2.table_name = '"+ TeConvertToUpperCase(visGeomTable) +"'";
01974         sql += " AND m2.column_name = '"+ TeConvertToUpperCase(visGeomColl) +"'"; 
01975         sql += " AND g1.object_id = '"+ objId1 +"'"; 
01976         sql += " AND g2.object_id = '"+ objId2 +"'";
01977 
01978         TeOracleSpatialPortal* portal = (TeOracleSpatialPortal*)getPortal();
01979         
01980         if(!portal->query(sql) || !portal->fetchRow())
01981         {
01982                 delete portal;
01983                 return false;
01984         }
01985 
01986         bool flag = true;
01987         do 
01988         {
01989                 bool result=false;
01990                 TeGeometry* geom = new TeGeometry(); 
01991                 TeGeometry* geom2 = geom;
01992                 flag = portal->getGeometry(&geom, result);
01993                 if(result)
01994                         geomVect.push_back (geom);
01995                 delete geom2;
01996         }while(flag);
01997                 
01998         delete portal;
01999         return true;
02000 }
02001 
02002 
02003 bool 
02004 TeOracleSpatial::geomUnion(const string& actGeomTable, TeGeomRep /* actRep */, TeKeys& actIds, TeGeometryVect& geomVect)
02005 {
02006         string actGeomColl = "spatial_data";
02007         string Ids = getStringIds(actIds);
02008 
02009         string sql = "SELECT SDO_GEOM.SDO_UNION(g1."+ actGeomColl +", m.diminfo, ";
02010         sql += " g2."+ actGeomColl +", m.diminfo)";
02011         sql += " FROM "+ actGeomTable +" g1,"+ actGeomTable +" g2, USER_SDO_GEOM_METADATA m";
02012         sql += " WHERE m.table_name = '"+ TeConvertToUpperCase(actGeomTable) +"'";
02013         sql += " AND m.column_name = '"+ TeConvertToUpperCase(actGeomColl) +"'"; 
02014         sql += " AND object_id IN ("+ Ids +")";
02015 
02016         TeOracleSpatialPortal* portal = (TeOracleSpatialPortal*)getPortal();
02017         
02018         if(!portal->query(sql) || !portal->fetchRow())
02019         {
02020                 delete portal;
02021                 return false;
02022         }
02023 
02024         bool flag = true;
02025         do 
02026         {
02027                 bool result=false;
02028                 TeGeometry* geom = new TeGeometry(); 
02029                 TeGeometry* geom2 = geom;
02030                 flag = portal->getGeometry(&geom, result);
02031                 if(result)
02032                         geomVect.push_back (geom);
02033                 delete geom2;
02034         }while(flag);
02035                         
02036         delete portal;
02037         return true;
02038 }
02039 
02040 
02041 bool 
02042 TeOracleSpatial::geomUnion(const string& actGeomTable, TeGeomRep /* actRep */, const string& objId1, const string& visGeomTable, TeGeomRep /* visRep */, const string& objId2, TeGeometryVect& geomVect)
02043 {
02044         string actGeomColl = "spatial_data";
02045         string visGeomColl = "spatial_data";
02046 
02047         string sql = "SELECT SDO_GEOM.SDO_UNION(g1."+ actGeomColl +", m1.diminfo, ";
02048         sql += " g2."+ visGeomColl +", m2.diminfo)";
02049         sql += " FROM "+ actGeomTable +" g1,"+ visGeomTable +" g2, ";
02050         sql += " USER_SDO_GEOM_METADATA m1, USER_SDO_GEOM_METADATA m2 ";
02051         sql += " WHERE m1.table_name = '"+ TeConvertToUpperCase(actGeomTable) +"'";
02052         sql += " AND m1.column_name = '"+ TeConvertToUpperCase(actGeomColl) +"'"; 
02053         sql += " AND m2.table_name = '"+ TeConvertToUpperCase(visGeomTable) +"'";
02054         sql += " AND m2.column_name = '"+ TeConvertToUpperCase(visGeomColl) +"'"; 
02055         sql += " AND g1.object_id = '"+ objId1 +"'"; 
02056         sql += " AND g2.object_id = '"+ objId2 +"'";
02057 
02058         TeOracleSpatialPortal* portal = (TeOracleSpatialPortal*)getPortal();
02059         
02060         if(!portal->query(sql) || !portal->fetchRow())
02061         {
02062                 delete portal;
02063                 return false;
02064         }
02065 
02066         bool flag = true;
02067         do 
02068         {
02069                 bool result=false;
02070                 TeGeometry* geom = new TeGeometry(); 
02071                 TeGeometry* geom2 = geom;
02072                 flag = portal->getGeometry(&geom, result);
02073                 if(result)
02074                         geomVect.push_back (geom);
02075                 delete geom2;
02076         }while(flag);
02077                 
02078         delete portal;
02079         return true;
02080 }
02081 
02082 
02083 //End Spatial Query
02084 
02085 bool
02086 TeOracleSpatial::insertLine (const string& table, TeLine2D &line)
02087 {
02088         
02089         string  elinfo = "1, 2, 1";
02090         
02091         if(!allocateOrdinatesObject(line))
02092                 return false;
02093 
02094         string ins = "INSERT INTO " + table + " ( ";
02095         ins += " geom_id, object_id, spatial_data) VALUES ( ";
02096         ins += getNameSequence(table) +".NEXTVAL";
02097         ins += ", '" + escapeSequence(line.objectId()) + "'";
02098         ins += ", MDSYS.SDO_GEOMETRY(2002, NULL, NULL";
02099         ins += ", MDSYS.SDO_ELEM_INFO_ARRAY( " + elinfo + " )";
02100         ins += ", :ordinates_) ";
02101         ins += " )";
02102 
02103         //OCI
02104         if(!connection_->executeSDOSTM(ins))
02105         {
02106                 errorMessage_ = "Error inserting in the table " + table + "!"; 
02107                 return false;
02108         }
02109 
02110         return true;
02111 }
02112 
02113 
02114 bool 
02115 TeOracleSpatial::updateLine(const string& table, TeLine2D &line)
02116 {
02117         string  elinfo = "1, 2, 1";
02118         
02119         if(!allocateOrdinatesObject(line))
02120                 return false;
02121         
02122         string sql;
02123         sql =  "UPDATE " + table + " SET ";
02124         sql += "  object_id= '" + line.objectId() + "'";
02125         sql += ", spatial_data = ";
02126         sql += " MDSYS.SDO_GEOMETRY(2002, NULL, NULL";
02127         sql += ", MDSYS.SDO_ELEM_INFO_ARRAY( " + elinfo + " )";
02128         sql += ", :ordinates_) ";
02129         sql += " WHERE geom_id = " +  line.geomId ();
02130 
02131         //OCI
02132         if(!connection_->executeSDOSTM(sql))
02133         {
02134                 errorMessage_ = "Error updating in the table " + table + "!"; 
02135                 return false;
02136         }
02137         return true;
02138 }
02139 
02140 
02141 bool 
02142 TeOracleSpatial::loadLineSet (const string& table, const string& geoid, TeLineSet &ls)
02143 {
02144         
02145         TeOracleSpatialPortal *ocip = (TeOracleSpatialPortal*)getPortal();
02146         if(!ocip)
02147                 return false;
02148 
02149         string q ="SELECT * FROM " + table;
02150         if (geoid != "")
02151                 q += " WHERE object_id = '" + geoid +"'";
02152         
02153         if (!ocip->query(q) || !ocip->fetchRow())
02154         {       
02155                 delete ocip;
02156                 return false;
02157         }
02158 
02159         bool flag = true;
02160         do 
02161         {
02162                 TeLine2D line;
02163                 flag = ocip->fetchGeometry(line);
02164                 ls.add ( line );
02165         }while(flag);
02166 
02167         delete ocip;
02168         return true;
02169 }
02170 
02171 bool 
02172 TeOracleSpatial::loadLineSet (const string& table, TeBox &bb, TeLineSet &linSet)
02173 {
02174         TeOracleSpatialPortal *portal = (TeOracleSpatialPortal*)getPortal();
02175         if (!portal)
02176                 return false;
02177 
02178         string q;
02179         q = "SELECT * FROM " + table + " WHERE ";
02180         q += this->getSQLBoxWhere (bb, TeLINES, table);
02181         q += " ORDER BY object_id";
02182 
02183         if (!portal->query(q) || !portal->fetchRow())
02184         {       
02185                 delete portal;
02186                 return false;
02187         }
02188         bool flag = true;
02189         do
02190         {
02191                 TeLine2D lin;
02192                 flag = portal->fetchGeometry(lin);
02193                 linSet.add(lin);
02194         }
02195         while (flag);
02196         delete portal;
02197         return true;
02198 }
02199 
02200 TeDatabasePortal* 
02201 TeOracleSpatial::loadLineSet (const string& table, TeBox &box)
02202 {
02203         TeOracleSpatialPortal *portal = (TeOracleSpatialPortal*) getPortal();
02204         if (!portal)
02205                 return 0;
02206 
02207         string q;
02208         q = "SELECT * FROM " + table + " WHERE ";
02209         q += this->getSQLBoxWhere (box, TeLINES, table);
02210         q += " ORDER BY object_id";
02211 
02212         if (!portal->query(q) || !portal->fetchRow())
02213         {       
02214                 delete portal;
02215                 return 0;
02216         }
02217         return portal;
02218 }
02219 
02220 bool 
02221 TeOracleSpatial::insertPoint(const string& table, TePoint &point)
02222 {
02223         
02224         string ins = "INSERT INTO " + table + " ( ";
02225         ins += " geom_id, object_id, spatial_data) ";
02226         ins += " VALUES ( ";
02227         ins += getNameSequence(table) +".NEXTVAL";
02228         ins += ", '" + escapeSequence(point.objectId()) + "'";
02229         ins += ", MDSYS.SDO_GEOMETRY(2001, NULL, ";
02230         ins += "MDSYS.SDO_POINT_TYPE( ";
02231         ins += Te2String(point.location().x(),15);
02232         ins += ", " + Te2String(point.location().y(),15);
02233         ins += ", NULL )";
02234         ins += ", NULL, NULL)";
02235         ins += " ) ";
02236 
02237         if(!execute(ins))
02238         {
02239                 if(errorMessage_.empty())
02240                         errorMessage_ = "Error inserting in the table " + table + "!"; 
02241                 return false;
02242         }
02243         return true;
02244 }
02245 
02246 
02247 bool 
02248 TeOracleSpatial::updatePoint (const string& table, TePoint &point)
02249 {
02250         string sql;
02251         sql =  "UPDATE " + table + " SET ";
02252         sql += "object_id = '" + point.objectId() + "'";
02253         sql += ", spatial_data = ";
02254         sql += " MDSYS.SDO_GEOMETRY(2001, NULL";
02255         sql += ", MDSYS.SDO_POINT_TYPE( ";
02256         sql += Te2String(point.location ().x(),15);
02257         sql += ", " + Te2String(point.location ().y(),15);
02258         sql += ", NULL )";
02259         sql += ", NULL, NULL)";
02260         sql += " WHERE geom_id = " + Te2String(point.geomId());
02261 
02262         if(!execute(sql))
02263         {
02264                 if(errorMessage_.empty())
02265                         errorMessage_ = "Error updating in the table " + table + "!"; 
02266                 return false;
02267         }
02268 
02269         return true;
02270 }
02271         
02272 bool 
02273 TeOracleSpatial::insertText     (const string& table, TeText &text)
02274 {
02275         string ins = "INSERT INTO " + table + " (geom_id, ";
02276         ins += " object_id, x, y, text_value, angle, height, alignment_vert, ";
02277         ins += " alignment_horiz) VALUES ( ";
02278         ins += getNameSequence(table) +".NEXTVAL";
02279         ins += ", '" + escapeSequence(text.objectId()) + "'";
02280         ins += ",  " + Te2String(text.location().x(),15);
02281         ins += ",  " + Te2String(text.location().y(),15);
02282         ins += ", '" + escapeSequence(text.textValue()) + "'";
02283         ins += ",  " + Te2String(text.angle(),15);
02284         ins += ",  " + Te2String(text.height(),15);
02285         ins += ",  " + Te2String(text.alignmentVert(),15);
02286         ins += ",  " + Te2String(text.alignmentHoriz(),15);
02287         ins += " )";
02288         
02289         if(!execute(ins))
02290         {
02291                 if(errorMessage_.empty())
02292                         errorMessage_ = "Error inserting in the table " + table + "!"; 
02293                 return false;
02294         }
02295         return true;
02296 }
02297 
02298 
02299 bool 
02300 TeOracleSpatial::insertArc (const string& table, TeArc &arc)
02301 {
02302         
02303         string ins = "INSERT INTO " + table + " (geom_id, ";
02304         ins += " object_id, from_node, to_node ) ";
02305         ins += " VALUES ( ";
02306         ins += getNameSequence(table) +".NEXTVAL";
02307         ins += ", '" + escapeSequence(arc.objectId()) + "'";
02308         ins += ",  " + Te2String(arc.fromNode().geomId());
02309         ins += ",  " + Te2String(arc.toNode().geomId());
02310         ins += " )";
02311         
02312         if(!execute(ins))
02313         {
02314                 if(errorMessage_.empty())
02315                         errorMessage_ = "Error inserting in the table " + table + "!"; 
02316                 return false;
02317         }
02318 
02319         return true;
02320 }
02321 
02322 bool 
02323 TeOracleSpatial::insertNode (const string& table, TeNode &node)
02324 {       
02325         string ins = "INSERT INTO " + table + " ( ";
02326         ins += " geom_id, object_id, spatial_data) ";
02327         ins += " VALUES ( ";
02328         ins += getNameSequence(table) +".NEXTVAL";
02329         ins += ", '" + escapeSequence(node.objectId()) + "'";
02330         ins += ", MDSYS.SDO_GEOMETRY(2001, NULL, ";
02331         ins += "MDSYS.SDO_POINT_TYPE( ";
02332         ins += Te2String(node.location().x(),15);
02333         ins += ", " + Te2String(node.location ().y(),15);
02334         ins += ", NULL )";
02335         ins += ", NULL, NULL)";
02336         ins += " ) ";
02337 
02338         if(!execute(ins))
02339         {
02340                 if(errorMessage_.empty())
02341                         errorMessage_ = "Error inserting in the table " + table + "!"; 
02342                 return false;
02343         }
02344         return true;
02345 }
02346         
02347 
02348 bool
02349 TeOracleSpatial::updateNode (const string& table, TeNode &node)
02350 {       
02351         string sql;
02352         sql =  "UPDATE " + table + " SET ";
02353         sql += " object_id = '" + node.objectId() + "'";
02354         sql += ", spatial_data = ";
02355         sql += " MDSYS.SDO_GEOMETRY(2001, NULL";
02356         sql += ", MDSYS.SDO_POINT_TYPE( ";
02357         sql += Te2String(node.location ().x(),15);
02358         sql += ", " + Te2String(node.location ().y(),15);
02359         sql += ", NULL )";
02360         sql += ", NULL, NULL)";
02361         sql += " WHERE geom_id = " + Te2String(node.geomId());
02362         
02363         if(!execute(sql))
02364         {
02365                 if(errorMessage_.empty())
02366                         errorMessage_ = "Error updating in the table " + table + "!"; 
02367                 return false;
02368         }
02369         return true;
02370 }
02371 
02372 bool 
02373 TeOracleSpatial::insertCell (const string& table, TeCell &cell )
02374 {
02375         
02376         TeBox b = cell.box();
02377 
02378         string ins = "INSERT INTO " + table + " ( ";
02379         ins += " geom_id, object_id, col_number, row_number, spatial_data) ";
02380         ins += " VALUES ( ";
02381         ins += getNameSequence(table) +".NEXTVAL";
02382         ins += ", '" + escapeSequence(cell.objectId ()) + "'";
02383         ins += ",  " + Te2String(cell.column ());
02384         ins += ",  " + Te2String(cell.line ());
02385         ins += ", MDSYS.SDO_GEOMETRY(2003, NULL, NULL";
02386         ins += ", MDSYS.SDO_ELEM_INFO_ARRAY( 1, 1003, 3 )";
02387         ins += ", MDSYS.SDO_ORDINATE_ARRAY( " ;
02388         ins += Te2String(b.lowerLeft().x(), 15);
02389         ins += ", " + Te2String(b.lowerLeft().y(),15);
02390         ins += ", " + Te2String(b.upperRight().x(),15);
02391         ins += ", " + Te2String(b.upperRight().y(),15);
02392         ins += ")) ";
02393         ins += " )";
02394                 
02395         if(!execute(ins))
02396         {
02397                 if(errorMessage_.empty())
02398                         errorMessage_ = "Error inserting in the table " + table + "!"; 
02399                 return false;
02400         }
02401         return true;
02402 }
02403 
02404 
02405 bool 
02406 TeOracleSpatial::updateCell(const string& table, TeCell &cell)
02407 {
02408         TeBox b = cell.box ();
02409         
02410         string sql;
02411         sql =  "UPDATE " + table + " SET ";
02412         sql += " object_id= '" + cell.objectId() + "'";
02413         sql += " col_number= " + Te2String(cell.column ());
02414         sql += " row_number= " + Te2String(cell.line ());
02415         sql += " spatial_data= ";
02416         sql += " MDSYS.SDO_GEOMETRY(2003, NULL, NULL";
02417         sql += ", MDSYS.SDO_ELEM_INFO_ARRAY( 1, 1003, 3 )";
02418         sql += ", MDSYS.SDO_ORDINATE_ARRAY( " ;
02419         sql += Te2String(b.lowerLeft().x(), 15);
02420         sql += ", " + Te2String(b.lowerLeft().y(),15);
02421         sql += ", " + Te2String(b.upperRight().x(),15);
02422         sql += ", " + Te2String(b.upperRight().y(),15);
02423         sql += ")) ";
02424         sql += " WHERE geom_id = " +  cell.geomId ();
02425         
02426         if(!execute(sql))
02427         {
02428                 if(errorMessage_.empty())
02429                         errorMessage_ = "Error updating in the table " + table + "!"; 
02430                 return false;
02431         }
02432         
02433         return true;
02434 }
02435 
02436 bool
02437 TeOracleSpatial::deleteMetadata(const string &table, const string &column)
02438 {
02439         string del = "DELETE FROM USER_SDO_GEOM_METADATA ";
02440         del += " WHERE TABLE_NAME = '" + TeConvertToUpperCase(table);
02441         del += "' AND COLUMN_NAME = '" + TeConvertToUpperCase(column) + "'";
02442         if(!(execute(del.c_str ())))
02443                 return false;
02444         return true;
02445 }
02446 
02447 
02448 bool
02449 TeOracleSpatial::rebuildSpatialIndex(const string &table)
02450 {
02451         TeOracleSpatialPortal *ocip = (TeOracleSpatialPortal*)getPortal();
02452         if(!ocip)
02453                 return false;
02454 
02455         string tabIndex;
02456 
02457         string ind = "SELECT index_name FROM USER_SDO_INDEX_INFO";
02458         ind += " WHERE table_name = '" + TeConvertToUpperCase(table) + "'";
02459         if (!ocip->query(ind))
02460         {
02461                 delete ocip;
02462                 return false;
02463         }
02464         
02465         if(!ocip->fetchRow())
02466         {
02467                 delete ocip;
02468                 return false;
02469         }
02470 
02471         tabIndex = string(ocip->getData(0));
02472         delete ocip;
02473                 
02474         string reb = "ALTER INDEX ";
02475         reb += tabIndex + " REBUILD";
02476         if(!execute(reb))
02477                 return false;
02478 
02479         return true;
02480 }
02481 
02482 bool
02483 TeOracleSpatial::deleteSpatialIndex(const string &table)
02484 {
02485         TeOracleSpatialPortal   *ocip = (TeOracleSpatialPortal*)getPortal();
02486         if(!ocip)
02487                 return false;
02488 
02489         string tabIndex;
02490 
02491         string ind = "SELECT index_name FROM USER_SDO_INDEX_INFO";
02492         ind += " WHERE table_name = '"+ TeConvertToUpperCase(table) +"'";
02493         if (!ocip->query(ind))
02494         {
02495                 delete ocip;
02496                 return false;
02497         }
02498 
02499         if(!ocip->fetchRow())
02500         {
02501                 delete ocip;
02502                 return false;
02503         }
02504 
02505         tabIndex = string(ocip->getData(0));
02506         ocip->freeResult();
02507         delete ocip;
02508 
02509         string drop = "DROP INDEX "+ tabIndex;
02510         if (!(execute(drop.c_str ())))
02511                 return false;
02512         return true;
02513 }
02514 
02515 bool 
02516 TeOracleSpatial::generateLabelPositions (TeTheme *theme, const std::string& objectId)
02517 {
02518         
02519         string  geomTable, upd;
02520         string  collTable = theme->collectionTable();
02521         
02522         if((collTable.empty()) || (!tableExist(collTable)))
02523                 return false;
02524 
02525         if( theme->layer()->hasGeometry(TeCELLS)    || 
02526                 theme->layer()->hasGeometry(TePOLYGONS) ||
02527                 theme->layer()->hasGeometry(TeLINES) )
02528         {
02529                 geomTable = theme->layer()->tableName(TeCELLS);
02530                 
02531                 if(geomTable.empty())
02532                 {
02533                         geomTable = theme->layer()->tableName(TePOLYGONS);
02534                         if(geomTable.empty())
02535                                 geomTable = theme->layer()->tableName(TeLINES);
02536                 }
02537                 
02538                 upd= " UPDATE " + collTable + " SET ";
02539                 upd += " label_x = (SELECT MAX(SDO_GEOM.SDO_MIN_MBR_ORDINATE( ";
02540                 upd += geomTable + ".spatial_data, 1) + (SDO_GEOM.SDO_MAX_MBR_ORDINATE( ";
02541                 upd += geomTable + ".spatial_data, 1) -  SDO_GEOM.SDO_MIN_MBR_ORDINATE( ";
02542                 upd += geomTable + ".spatial_data, 1))/2) ";
02543                 upd += "FROM " + geomTable + " WHERE object_id = c_object_id), ";
02544                 
02545                 upd += " label_y = (SELECT MAX(SDO_GEOM.SDO_MIN_MBR_ORDINATE( ";
02546                 upd += geomTable + ".spatial_data, 2) + (SDO_GEOM.SDO_MAX_MBR_ORDINATE( ";
02547                 upd += geomTable + ".spatial_data, 2) -  SDO_GEOM.SDO_MIN_MBR_ORDINATE( ";
02548                 upd += geomTable + ".spatial_data, 2))/2) ";
02549                 upd += "FROM " + geomTable + " WHERE object_id = c_object_id) ";
02550 
02551                 upd += " WHERE label_x IS NULL OR label_y IS NULL";
02552 
02553         }
02554         
02555         else if (theme->layer()->hasGeometry(TePOINTS))
02556         {
02557                 geomTable = theme->layer()->tableName(TePOINTS);
02558                 
02559                 upd= " UPDATE "+ collTable +" SET ";
02560                 upd += " label_x = (SELECT MAX(p.spatial_data.SDO_POINT.X) ";
02561                 upd += " FROM " + geomTable + " p WHERE object_id = c_object_id), ";
02562                 
02563                 upd += " label_y = (SELECT MAX(p.spatial_data.SDO_POINT.Y) ";
02564                 upd += " FROM " + geomTable + " p WHERE object_id = c_object_id) ";
02565                 upd += " WHERE label_x IS NULL OR label_y IS NULL";
02566         }
02567 
02568         if (!objectId.empty())
02569                 upd += " AND c_object_id='"+objectId+"'";
02570                 
02571         return execute(upd);
02572 }
02573 
02574 bool 
02575 TeOracleSpatial::locatePolygon (const string& table, TeCoord2D &pt, TePolygon& polygon, const double& /* tol */)
02576 {
02577         TeOracleSpatialPortal  *ocip = (TeOracleSpatialPortal*)getPortal();
02578         if(!ocip)
02579                 return false;
02580 
02581         string q = "SELECT * FROM " + table;
02582         q += " WHERE MDSYS.SDO_RELATE (spatial_data,";
02583         q += "MDSYS.SDO_GEOMETRY(2001,NULL, MDSYS.SDO_POINT_TYPE(";
02584         q += Te2String(pt.x(),15) + ", " + Te2String(pt.y(), 15);
02585         q += ", NULL), NULL, NULL), ";
02586         q += " 'mask=contains querytype = window') = 'TRUE'";
02587 
02588         if (!ocip->query(q) || !ocip->fetchRow())
02589         {       
02590                 delete ocip;
02591                 return false;
02592         }
02593 
02594         ocip->fetchGeometry(polygon);
02595         delete ocip;
02596         return true;
02597 }
02598 
02599 bool 
02600 TeOracleSpatial::locateLine (const string& table, TeCoord2D &pt, TeLine2D& line, const double& tol)
02601 {
02602 
02603         TeOracleSpatialPortal   *ocip = (TeOracleSpatialPortal*)getPortal();
02604         if(!ocip)
02605                 return false;
02606 
02607         TeBox box (pt.x()-tol,pt.y()-tol,pt.x()+tol,pt.y()+tol);
02608         
02609         string sdoGeom = "mdsys.sdo_geometry(2003,null,null,";
02610         sdoGeom += "mdsys.sdo_elem_info_array(1,1003,3),";
02611         sdoGeom += "mdsys.sdo_ordinate_array(";
02612         sdoGeom += Te2String(box.x1(),15) + ", " + Te2String(box.y1(),15);
02613         sdoGeom += ", " + Te2String(box.x2(),15) + ", " + Te2String(box.y2(),15) + "))";
02614                 
02615         string q = "SELECT * FROM " + table;
02616         q += " WHERE MDSYS.SDO_RELATE (spatial_data,";
02617         q += sdoGeom +","; 
02618         q += "'mask=anyinteract querytype = window') = 'TRUE'";
02619 
02620         if (!ocip->query(q) || !ocip->fetchRow())
02621         {
02622                 delete ocip;
02623                 return false;
02624         }
02625 
02626         ocip->fetchGeometry(line);
02627         delete ocip;
02628         return true;
02629 }
02630 
02631 bool 
02632 TeOracleSpatial::locatePoint (const string& table, TeCoord2D &pt, TePoint& point, const double& tol)
02633 {
02634         TeOracleSpatialPortal   *ocip = (TeOracleSpatialPortal*)getPortal();
02635         if(!ocip)
02636                 return false;
02637 
02638         
02639         TeBox box (pt.x()-tol,pt.y()-tol,pt.x()+tol,pt.y()+tol);
02640         
02641         string sdoGeom = "mdsys.sdo_geometry(2003,null,null,";
02642         sdoGeom += "mdsys.sdo_elem_info_array(1,1003,3),";
02643         sdoGeom += "mdsys.sdo_ordinate_array(";
02644         sdoGeom += Te2String(box.x1(),15) + ", " + Te2String(box.y1(),15);
02645         sdoGeom += ", " + Te2String(box.x2(),15) + ", " + Te2String(box.y2(),15) + "))";
02646                 
02647         string q = "SELECT * FROM " + table;
02648         q += " WHERE MDSYS.SDO_RELATE (spatial_data,";
02649         q += sdoGeom +","; 
02650         q += "'mask=anyinteract querytype = window') = 'TRUE'";
02651         
02652         if (!ocip->query(q) || !ocip->fetchRow())
02653         {
02654                 delete ocip;
02655                 return false;
02656         }
02657 
02658         ocip->fetchGeometry(point);
02659         delete ocip;
02660         return true;
02661 }
02662 
02663 bool 
02664 TeOracleSpatial::locateCell(const string& table, TeCoord2D &pt, TeCell& cell, const double& /* tol */)
02665 {
02666         TeOracleSpatialPortal  *ocip = (TeOracleSpatialPortal*)getPortal();
02667         if(!ocip)
02668                 return false;
02669 
02670         string q = "SELECT * FROM " + table;
02671         q += " WHERE MDSYS.SDO_RELATE (spatial_data,";
02672         q += "MDSYS.SDO_GEOMETRY(2001,NULL,MDSYS.SDO_POINT_TYPE(";
02673         q += Te2String(pt.x(),15) + ", " + Te2String(pt.y(), 15);
02674         q += ", NULL), NULL, NULL), ";
02675         q += " 'mask=contains querytype = window') = 'TRUE'";
02676 
02677         if (!ocip->query(q) || !ocip->fetchRow())
02678         {       
02679                 delete ocip;
02680                 return false;
02681         }
02682         
02683         ocip->fetchGeometry(cell);
02684         delete ocip;
02685         return true;
02686 }
02687 
02688 bool TeOracleSpatial::removeGeometry(const string& tableName, const TeGeomRep& rep, const int& geomId)
02689 {
02690         if(tableName.empty())
02691         {
02692                 return false;
02693         }
02694 
02695         std::string remove = "DELETE FROM " + tableName;
02696         remove += " WHERE geom_id = " + geomId;
02697 
02698         return this->execute(remove);
02699 }
02700 
02701 //---------------- TeOracleSpatialPortal
02702 
02703 int 
02704 TeOracleSpatialPortal::getDimArraySize()
02705 {
02706         if(!cursor_)
02707                 return 0;
02708 
02709         return(cursor_->getDimArraySize());
02710 }
02711 
02712 bool 
02713 TeOracleSpatialPortal::getDimElement(int i,int &elem)
02714 {
02715         if(!cursor_)
02716                 return false;
02717 
02718         return (cursor_->getDimElement(i,elem));
02719 }
02720 
02721 int 
02722 TeOracleSpatialPortal::numberOfOrdinates()
02723 {
02724         if(!cursor_)
02725                 return 0;
02726 
02727         return(cursor_->getNumberOrdinates());
02728 }
02729 
02730 bool
02731 TeOracleSpatialPortal::getCoordinates(int i,TeCoord2D& coord)
02732 {
02733         if(!cursor_)
02734                 return false;
02735 
02736         return (cursor_->getCoordinates((i*2)-1,coord));
02737 }
02738 
02739 bool
02740 TeOracleSpatialPortal::getGeometryType(TeSDOGType& gType)
02741 {
02742         if(!cursor_)
02743                 return false;
02744 
02745         int type = cursor_->getGeometryType(); 
02746         
02747         switch(type)
02748         { 
02749                 case 2000:
02750                         gType=TeSDOUNKNOWN;
02751                         break;
02752 
02753                 case 2001:
02754                         gType=TeSDOPOINT;
02755                         break;
02756 
02757                 case 2002:
02758                         gType=TeSDOLINE;
02759                         break;
02760 
02761                 case 2003:
02762                         gType=TeSDOPOLYGON;
02763                         break;
02764 
02765                 case 2004:
02766                         gType=TeSDOCOLLECTION;
02767                         break;
02768 
02769                 case 2005:
02770                         gType=TeSDOMULTIPOINT;
02771                         break;
02772 
02773                 case 2006:
02774                         gType=TeSDOMULTILINE;
02775                         break;
02776                 
02777                 case 2007:
02778                         gType=TeSDOMULTIPOLYGON;
02779                         break;
02780                 default:
02781                         return false;
02782         };
02783         return true;
02784 }
02785 
02786 int
02787 TeOracleSpatialPortal::getSpatialReferenceId()
02788 {
02789         if(!cursor_)
02790                 return -1;
02791 
02792         return (cursor_->getSpatialReferenceId());
02793 }
02794 
02795 bool
02796 TeOracleSpatialPortal::getPointXYZ (double& x,double& y)
02797 {
02798         if(!cursor_)
02799                 return false;
02800 
02801         return (cursor_->getXYZcoord(x,y));
02802 }
02803 
02804 bool TeOracleSpatialPortal::querySDO (const string &q)  
02805 {
02806         errorMessage_.clear ();
02807 
02808         if (!cursor_->isOpen())
02809         {
02810                 if(!cursor_->open())
02811                 {
02812                         numRows_ = 0;
02813                         return false;
02814                 }
02815         }
02816 
02817         if (!cursor_->querySDO(q))
02818         {
02819                 this->errorMessage_ = cursor_->getErrorMessage();
02820                 numRows_ = 0;
02821                 return false;
02822         }
02823         
02824         numFields_= this->cursor_->numCol();
02825         
02826         attList_.clear ();
02827         int i;
02828         for(i = 1; i <= numFields_ ; i++)
02829         {
02830                 TeAttribute attribute;
02831 
02832                 switch (cursor_->colType(i))
02833                 {
02834                         case 3: //INTEGER
02835                         attribute.rep_.type_ = TeINT;
02836                         break;
02837 
02838                         case 2:  //NUMBER
02839                         case 4: //FLOAT DOUBLE
02840                         attribute.rep_.type_ = TeREAL;
02841                         break;
02842 
02843                         case 12: //Date
02844                         attribute.rep_.type_ = TeDATETIME;
02845                         break;
02846 
02847                         case 113: //Blob
02848                         attribute.rep_.type_ = TeBLOB;
02849                         break;
02850 
02851                         case 96: //CHAR
02852                         case 9: //VARCHAR:
02853                         case 1: //VARCHAR2:
02854                         attribute.rep_.type_ = TeSTRING;
02855                         break;
02856 
02857                         case 108: //OBJECT: // SDO_GEOMETRY
02858                         attribute.rep_.type_ = TeOBJECT;
02859                         break;
02860                         default :
02861                         attribute.rep_.type_ = TeUNKNOWN;
02862                         break;
02863                 } 
02864                 
02865                 attribute.rep_.name_ = cursor_->colName(i); 
02866                 attribute.rep_.numChar_ = cursor_->colSize(i);
02867                 attList_.push_back ( attribute );
02868         }
02869 
02870         curRow_=-1;
02871         return true;
02872 }
02873 
02874 bool
02875 TeOracleSpatialPortal::fetchGeometry (TePolygon& poly)
02876 {
02877         int                     elem,elemnext,i,k,elemType, sdoInterp;
02878 
02879         try
02880         {
02881                 int ndim = getDimArraySize();
02882 
02883                 if(ndim==0)
02884                         return false;
02885 
02886                 vector<TeCoord2D> coordinates;
02887                 if(!cursor_->getCoordinates (coordinates))
02888                         return false;
02889 
02890                 //number of the oordinates in the SDO_GEOMETRY
02891                 int noords = numberOfOrdinates();
02892         
02893                 //Indicates the type of the SDO_GEOMETRY
02894                 int sdoEType;  
02895                 getDimElement(2,sdoEType);
02896         
02897                 int geomId = atoi(getData("geom_id"));
02898                 string objId = string(getData("object_id"));
02899                 
02900                 // 1005: compound polygon: Compound polygon with some vertices connected by straight
02901                 //line segments and some by circular arcs.
02902                 if((sdoEType==1005) || (sdoEType==2005))  
02903                 {                                          
02904                         TeLine2D        lin;   
02905                         //nelem: number of elemnts
02906                         int nelem; 
02907                         getDimElement(3,nelem);  
02908                         
02909                         //posinit is initial position in the vector 
02910                         int posinit=0; //1;
02911                         //ipoxmax: second element position in the ordinates array
02912                         int iposmax=7;                  
02913                         
02914                         int posmax;
02915                         bool thisElemArc = false; 
02916 
02917                         //keep the last point of the element
02918                         //TePoint lastPoint;
02919                         
02920                         //for each element
02921                         for(elem=1; elem<=nelem; elem++)  
02922                         {
02923                                 //to catch the last position of the element: iposmax
02924                                 if(elem==nelem)
02925                                         posmax = noords+1;
02926                                 else
02927                                         getDimElement(iposmax, posmax);
02928 
02929                                 //verify if the element is a arc 
02930                                 getDimElement((iposmax-1), elemType);
02931                                 if(elemType == 2)
02932                                         thisElemArc = true;   
02933                                 else 
02934                                         thisElemArc = false;
02935 
02936                                 //ptSet: ordinates of the element
02937                                 TePointSet ptSet;
02938 
02939                                 //to catch all coords of the element
02940                                 int pos=0;
02941                                                                         
02942                                 for(pos=posinit; pos<((posmax-1)/2);++pos)
02943                                 {
02944                                         TePoint pt(coordinates[pos]);
02945                                         ptSet.add(pt);
02946                                 }
02947 
02948                                 if(thisElemArc) 
02949                                 {
02950                                         //pegar o proximo
02951                                         if(elem!=nelem)
02952                                         {
02953                                                 TePoint pt(coordinates[pos]);
02954                                                 ptSet.add(pt);
02955                                         }
02956 
02957                                         int size = ptSet.size();
02958                                         for(int s=1; (s+1)<size; s=s+2)
02959                                         {
02960                                                 TeLine2D arc;
02961                                                 TeGenerateArc (ptSet[s-1], ptSet[s], ptSet[s+1], arc, 10);
02962 
02963                                                 int sz = arc.size();
02964                                                 for(int i=0; i<sz; i++)
02965                                                         lin.add(arc[i]);
02966                                         }
02967                                 }
02968 
02969                                 else //line segment
02970                                 {
02971                                         int size = ptSet.size();
02972                                         for(int s=0; s<size; s++)
02973                                                 lin.add(ptSet[s].location());
02974                                 }
02975 
02976                                 iposmax+=3;
02977                                 posinit=pos;  //skip first coordinate: equal last point of previous element 
02978                         
02979                         } //for each element
02980 
02981                         TeLinearRing rg(lin);
02982                         rg.objectId(objId);
02983                         rg.geomId(geomId);
02984                         poly.add(rg);
02985                 }
02986 
02987                 else if((sdoEType==1003)||(sdoEType==2003))  //no complex
02988                 {
02989                         for(i=1;i<=ndim;i+=3)
02990                         {
02991                                 TeLine2D        line;
02992                                 getDimElement(i,elem);  // oordinate position
02993                                 if((i+3) > ndim)
02994                                         elemnext = noords+1;
02995                                 else
02996                                         getDimElement(i+3,elemnext);
02997 
02998                                 getDimElement(i+2,sdoInterp);   // sdo interpretation 
02999                                 // sdoInterp = 3: Rectangle type
03000                                 // sdoInterp = 2: Polygon made up of a connected sequence of circular arcs 
03001 
03002                                 if(sdoInterp==2)
03003                                 {
03004                                         //para gerar o arco
03005                                         TePoint pt1;
03006                                         TePoint pt2;
03007                                         TePoint pt3;
03008                                                                                 
03009                                         int cont=0;
03010                                         int k = elem/2;
03011                                         while (k<(elemnext/2))
03012                                         {
03013                                                 for(int i=0; i<3; i++)
03014                                                 {
03015                                                         TeCoord2D pt = coordinates[k];
03016                                                 
03017                                                         if(i==0)
03018                                                         {
03019                                                                 if(!cont)
03020                                                                         pt1.add(pt);
03021                                                                 else
03022                                                                 {
03023                                                                         pt1.add(pt3.location());
03024                                                                         pt2.add(pt);
03025                                                                         ++i;
03026                                                                 }
03027                                                         }
03028                                                         else if (i==1)
03029                                                                 pt2.add(pt);
03030                                                         else if (i==2)
03031                                                                 pt3.add(pt);
03032 
03033                                                         ++cont;
03034                                                         ++k;
03035                                                 }
03036 
03037                                                 TeLine2D arc;
03038                                                 TeGenerateArc (pt1, pt2, pt3, arc, 20);
03039                                                 
03040                                                 int s = arc.size();
03041                                                 for(int j=0; j<s; j++)
03042                                                         line.add(arc[j]);
03043                                         }
03044                                 }
03045                                 else
03046                                 {
03047                                         // ler os pontos
03048                                         for(k=(elem/2);k<(elemnext/2);k++)
03049                                         {
03050                                                 TeCoord2D pt = coordinates[k];
03051                                                 line.add(pt);
03052                                         }
03053 
03054                                         if(sdoInterp==3) // rectangle
03055                                         {
03056                                                 double xmin,ymin, xmax, ymax;
03057                                                 xmin = line.box().x1();
03058                                                 ymin = line.box().y1();
03059                                                 xmax = line.box().x2();
03060                                                 ymax = line.box().y2();
03061 
03062                                                 line.clear();
03063                                                 TeCoord2D pt1(xmin,ymin);
03064                                                 line.add(pt1);
03065                                                 TeCoord2D pt2(xmin,ymax);
03066                                                 line.add(pt2);
03067                                                 TeCoord2D pt3(xmax,ymax);
03068                                                 line.add(pt3);
03069                                                 TeCoord2D pt4(xmax,ymin);
03070                                                 line.add(pt4);
03071                                                 line.add(pt1);
03072                                         }
03073                                 }
03074 
03075                                 TeLinearRing ring(line);
03076                                 ring.objectId (objId);
03077                                 ring.geomId(geomId);
03078                                 poly.add(ring);
03079                         }
03080                 }
03081                 poly.objectId(objId);
03082                 poly.geomId(geomId);
03083                 return(this->fetchRow());
03084         }
03085         catch(...)
03086         {
03087                 errorMessage_ = cursor_->getErrorMessage();
03088                 return false;
03089         }
03090 }
03091 
03092 bool
03093 TeOracleSpatialPortal::fetchGeometry (TePolygon& poly, const unsigned int&)
03094 {
03095         return this->fetchGeometry(poly);
03096 }
03097 
03098 
03099 bool
03100 TeOracleSpatialPortal::getGeometry (TeGeometry** geom, bool& result)
03101 {
03102         TeSDOGType gType;
03103         bool flag=true; 
03104         
03105         try
03106         {
03107                 this->getGeometryType(gType);
03108                         
03109                 if(gType==TeSDOPOLYGON)
03110                 {
03111                         TePolygon   poly, *pol;
03112                         flag = this->fetchGeometry(poly); 
03113                         pol = new TePolygon();
03114                         *pol = poly;
03115                         *geom = pol;
03116                         result = true;
03117                         return flag;
03118                 }
03119                 else if(gType==TeSDOLINE)
03120                 {
03121                         TeLine2D        line, *lin;
03122                         flag = this->fetchGeometry(line); 
03123                         lin = new TeLine2D();
03124                         *lin = line;
03125                         *geom = lin;
03126                         result = true;
03127                         return flag;
03128                 }
03129                 else if(gType==TeSDOPOINT)
03130                 {
03131                         TePoint         point, *pnt;
03132                         flag = this->fetchGeometry(point); 
03133                         pnt = new TePoint();
03134                         *pnt = point;
03135                         *geom = pnt;
03136                         result = true;
03137                         return flag;
03138                 }
03139 
03140                 else if(gType==TeSDOMULTIPOLYGON)
03141                 {
03142                         TePolygonSet   polySet, *polSet;
03143                         flag = this->fetchGeometry(polySet); 
03144                         polSet = new TePolygonSet();
03145                         *polSet = polySet;
03146                         *geom = polSet;
03147                         result = true;
03148                         return flag;
03149                 }
03150                 else if(gType==TeSDOMULTILINE)
03151                 {
03152                         TeLineSet       lineSet, *linSet;
03153                         flag = this->fetchGeometry(lineSet); 
03154                         linSet = new TeLineSet();
03155                         *linSet = lineSet;
03156                         *geom = linSet;
03157                         result = true;
03158                         return flag;
03159                 }
03160                 else if(gType==TeSDOMULTIPOINT)
03161                 {
03162                         TePointSet              pointSet, *pntSet;
03163                         flag = this->fetchGeometry(pointSet); 
03164                         pntSet = new TePointSet();
03165                         *pntSet = pointSet;
03166                         *geom = pntSet;
03167                         result = true;
03168                         return flag;
03169                 }
03170                 
03171         }
03172         catch(...)
03173         {
03174                 result = false;
03175                 return false;
03176         }
03177                 
03178         result = false;
03179         return (this->fetchRow());
03180 }
03181 
03182 
03183 bool
03184 TeOracleSpatialPortal::fetchGeometry (TePolygonSet& polySet)
03185 {
03186         int                                     elem,elemnext,i,k,sdoInterp;
03187         vector<TeCoord2D>       coordinates;
03188         
03189         try
03190         {
03191                 int ndim = getDimArraySize();
03192                 if(ndim==0)
03193                         return false;
03194                 
03195                 TePolygonSet polyHoles;
03196                 TeSDOGType gType;
03197                 
03198                 getGeometryType(gType);
03199 
03200                 if(gType==TeSDOPOLYGON)  
03201                 {
03202                         TePolygon poly;
03203                         bool res = fetchGeometry(poly);
03204                         polySet.add(poly);
03205                         return res;
03206                 }
03207 
03208                 if(!cursor_->getCoordinates(coordinates))
03209                         return false;
03210 
03211                 int noords = numberOfOrdinates();
03212         
03213                 int geomId = atoi(getData("geom_id"));
03214                 string objId = string(getData("object_id"));
03215                 
03216                 bool hasHole = false;
03217 
03218                 for(i=1;i<=ndim;i+=3)
03219                 {
03220                         int                     eType;
03221                         TeLine2D        line;
03222                         TePolygon       poly;
03223                         getDimElement(i,elem);  // oordinate position
03224                         if((i+3) > ndim)
03225                                 elemnext = noords+1;
03226                         else
03227                                 getDimElement(i+3,elemnext);
03228 
03229                         getDimElement(i+1, eType);              // sdo_etype do proximo elemento
03230                         getDimElement(i+2,sdoInterp);   // sdo_interpretation
03231                         
03232                         if(eType==2003)
03233                                 poly = polyHoles.last();
03234 
03235                         // ler os pontos
03236                         for(k=(elem/2);k<(elemnext/2);k++)
03237                         {
03238                                 TeCoord2D pt(coordinates[k]);
03239                                 line.add(pt);
03240                         }
03241 
03242                         if(sdoInterp == 3) // rectangle
03243                         {
03244                                 
03245                                 double xmin,ymin, xmax, ymax;
03246                                 xmin = line.box().x1();
03247                                 ymin = line.box().y1();
03248                                 xmax = line.box().x2();
03249                                 ymax = line.box().y2();
03250 
03251                                 line.clear();
03252                                 TeCoord2D pt1(xmin,ymin);
03253                                 line.add(pt1);
03254                                 TeCoord2D pt2(xmin,ymax);
03255                                 line.add(pt2);
03256                                 TeCoord2D pt3(xmax,ymax);
03257                                 line.add(pt3);
03258                                 TeCoord2D pt4(xmax,ymin);
03259                                 line.add(pt4);
03260                                 line.add(pt1);
03261                         }
03262                         TeLinearRing ring(line);
03263                         ring.objectId (objId);
03264                         ring.geomId(geomId);
03265                         
03266                         poly.add(ring);
03267         
03268                         //verificar se o proximo eh hole
03269                         hasHole=false;
03270                         if(i+4<ndim)
03271                         {
03272                                 getDimElement(i+4, eType);              // sdo_etype do proximo elemento
03273                                 if(eType == 2003)
03274                                         hasHole = true;
03275                         }
03276                         poly.objectId(objId);
03277                         poly.geomId(geomId);
03278                         
03279                         if(!hasHole)
03280                                 polySet.add(poly);
03281                         else    
03282                                 polyHoles.add(poly);
03283                 } //for
03284 
03285                 polySet.objectId(objId);
03286                 polySet.geomId(geomId);
03287 
03288                 return(this->fetchRow());
03289         }
03290         catch(...)
03291         {
03292                 errorMessage_ = cursor_->getErrorMessage();
03293                 return false;
03294         }
03295 }
03296 
03297 bool 
03298 TeOracleSpatialPortal::fetchGeometry (TeLine2D& line)
03299 {
03300         int                                     i;
03301         vector<TeCoord2D>       coordinates;
03302 
03303         try
03304         {
03305                 if(!cursor_->getCoordinates(coordinates))
03306                         return false;
03307                 
03308                 int noords = numberOfOrdinates();
03309                 if(noords==0)
03310                         return false;
03311 
03312                 for(i=0;i<noords/2;i++)
03313                         line.add(coordinates[i]);
03314                         
03315                 line.geomId (atoi(getData("geom_id")));
03316                 line.objectId(string(getData("object_id")));
03317                 return (this->fetchRow());
03318         }
03319         catch(...)
03320         {
03321                 errorMessage_ = cursor_->getErrorMessage();
03322                 return false;
03323         }
03324 }
03325 
03326 bool 
03327 TeOracleSpatialPortal::fetchGeometry (TeLine2D& line, const unsigned int& initIndex)
03328 {
03329         int                                     i;
03330         vector<TeCoord2D>       coordinates;
03331 
03332         try
03333         {
03334                 if(!cursor_->getCoordinates(coordinates))
03335                         return false;
03336                 
03337                 int noords = numberOfOrdinates();
03338                 if(noords==0)
03339                         return false;
03340 
03341                 for(i=0;i<noords/2;i++)
03342                         line.add(coordinates[i]);
03343                         
03344                 line.geomId (atoi(getData(initIndex)));
03345                 line.objectId(string(getData(initIndex+1)));
03346                 return (this->fetchRow());
03347         }
03348         catch(...)
03349         {
03350                 errorMessage_ = cursor_->getErrorMessage();
03351                 return false;
03352         }
03353 }
03354 
03355 
03356 bool 
03357 TeOracleSpatialPortal::fetchGeometry (TeLineSet& lineSet)
03358 {
03359         vector<TeCoord2D>       coordinates;
03360         int                                     elem,elemnext,i,k;
03361 
03362         try
03363         {
03364                 int ndim = getDimArraySize();
03365                 if(ndim==0)
03366                         return false;
03367                 
03368                 int noords = numberOfOrdinates();
03369                 int geomId = atoi(getData("geom_id"));
03370                 string objId = string(getData("object_id"));
03371                 
03372                 TeSDOGType gType;
03373                 getGeometryType(gType);
03374 
03375                 if(gType==TeSDOLINE)  
03376                 {
03377                         TeLine2D line;
03378                         fetchGeometry(line);
03379                         lineSet.add(line);
03380                         return true;
03381                 }
03382 
03383                 if(!cursor_->getCoordinates(coordinates))
03384                         return false;
03385 
03386                 for(i=1;i<=ndim;i+=3)
03387                 {
03388                         TeLine2D        line;
03389                         getDimElement(i,elem);          // oordinate position
03390                         if((i+3) > ndim)
03391                                 elemnext = noords+1;
03392                         else
03393                                 getDimElement(i+3,elemnext);
03394                         
03395                         // ler os pontos
03396                         for(k=(elem/2);k<(elemnext/2);k++)
03397                         {
03398                                 TeCoord2D pt(coordinates[k]);
03399                                 line.add(pt);
03400                         }
03401                         line.objectId (objId);
03402                         line.geomId(geomId);
03403                         lineSet.add(line);
03404                 }
03405 
03406                 lineSet.objectId(objId);
03407                 lineSet.geomId(geomId);
03408                 
03409                 return (this->fetchRow());
03410         }
03411         catch(...)
03412         {
03413                 errorMessage_ = cursor_->getErrorMessage();
03414                 return false;
03415         }
03416 }
03417 
03418 bool 
03419 TeOracleSpatialPortal::fetchGeometry(TePoint& p)
03420 {
03421         double          x,y;
03422         
03423         try
03424         {
03425                 if(!getPointXYZ(x,y))
03426                         return false;           //point in SDO_POINT
03427 
03428                 TeCoord2D c(x,y);
03429                 p.add(c);
03430                 p.objectId(string(getData("object_id")));
03431                 p.geomId(atoi(getData("geom_id")));
03432                 return(this->fetchRow());
03433         }
03434         catch(...)
03435         {
03436                 errorMessage_ = cursor_->getErrorMessage();
03437                 return false;
03438         }
03439 }
03440 
03441 bool 
03442 TeOracleSpatialPortal::fetchGeometry(TePoint& p, const unsigned int& initIndex)
03443 {
03444         double          x,y;
03445         
03446         try
03447         {
03448                 if(!getPointXYZ(x,y))
03449                         return false;           //point in SDO_POINT
03450 
03451                 TeCoord2D c(x,y);
03452                 p.add(c);
03453                 p.objectId(string(getData(initIndex+1)));
03454                 p.geomId(atoi(getData(initIndex)));
03455                 return(this->fetchRow());
03456         }
03457         catch(...)
03458         {
03459                 errorMessage_ = cursor_->getErrorMessage();
03460                 return false;
03461         }
03462 }
03463 
03464 bool 
03465 TeOracleSpatialPortal::fetchGeometry(TePointSet& pointSet)
03466 {
03467         double          x,y;
03468         vector<TeCoord2D> coordinates;
03469 
03470         x=y=-99999.;
03471         try
03472         {
03473                 int noords = numberOfOrdinates();
03474                 if(noords==0)
03475                         return false;
03476 
03477                 TeSDOGType gType;
03478                 getGeometryType(gType);
03479 
03480                 if(gType==TeSDOPOINT)  
03481                 {
03482                         TePoint point;
03483                         fetchGeometry(point);
03484                         pointSet.add(point);
03485                         return true;
03486                 }
03487 
03488                 if(!cursor_->getCoordinates(coordinates))
03489                         return false;
03490 
03491                 int geomId = atoi(getData("geom_id"));
03492                 string objId = string(getData("object_id"));
03493                 
03494                 for(int i=1;i<=noords/2;i++)
03495                 {
03496                         TePoint pt(coordinates[i-1]);
03497                         pt.geomId (geomId);
03498                         pt.objectId (objId);
03499                         pointSet.add(pt);
03500                 }
03501                 
03502                 pointSet.objectId(objId);
03503                 pointSet.geomId(geomId);
03504                 
03505                 return(this->fetchRow());
03506         }
03507         catch(...)
03508         {
03509                 errorMessage_ = cursor_->getErrorMessage();
03510                 return false;
03511         }
03512 }
03513 
03514 bool 
03515 TeOracleSpatialPortal::fetchGeometry(TeNode& n)
03516 {
03517         double  x,y;
03518         x=y=-99999.;
03519         try
03520         {
03521                 if(!getPointXYZ(x,y))
03522                         return false;
03523 
03524                 TeCoord2D point(x,y);
03525                 n.add(point);
03526                 n.geomId(atoi(getData("geom_id")));
03527                 n.objectId(string(getData("object_id")));
03528                 return(this->fetchRow());
03529         }
03530         catch(...)
03531         {
03532                 errorMessage_ = cursor_->getErrorMessage();
03533                 return false;
03534         }
03535 }
03536 
03537 bool 
03538 TeOracleSpatialPortal::fetchGeometry(TeNode& n, const unsigned int& initIndex)
03539 {
03540         double  x,y;
03541         x=y=-99999.;
03542         try
03543         {
03544                 if(!getPointXYZ(x,y))
03545                         return false;
03546 
03547                 TeCoord2D point(x,y);
03548                 n.add(point);
03549                 n.geomId(atoi(getData(initIndex)));
03550                 n.objectId(string(getData(initIndex+1)));
03551                 return(this->fetchRow());
03552         }
03553         catch(...)
03554         {
03555                 errorMessage_ = cursor_->getErrorMessage();
03556                 return false;
03557         }
03558 }
03559 
03560 bool    
03561 TeOracleSpatialPortal::fetchGeometry (TeCell& cell)
03562 {
03563         TeCoord2D coord1,coord2;
03564         try
03565         {
03566                 getCoordinates (1, coord1);
03567                 getCoordinates (2, coord2);
03568                 TeBox b(coord1.x(), coord1.y(), coord2.x(), coord2.y());
03569                 
03570                 cell.setBox (b);
03571                 cell.geomId(atoi(getData("geom_id")));
03572                 cell.objectId (string(getData("object_id")));
03573                 cell.column(atoi(getData("col_number")));
03574                 cell.line(atoi(getData("row_number")));
03575                 return(this->fetchRow());
03576         }
03577         catch(...)
03578         {
03579                 errorMessage_ = cursor_->getErrorMessage();
03580                 return false;
03581         }
03582 }
03583 
03584 bool    
03585 TeOracleSpatialPortal::fetchGeometry (TeCell& cell, const unsigned int& initIndex)
03586 {
03587         TeCoord2D coord1,coord2;
03588         try
03589         {
03590                 getCoordinates (1, coord1);
03591                 getCoordinates (2, coord2);
03592                 TeBox b(coord1.x(), coord1.y(), coord2.x(), coord2.y());
03593                 
03594                 cell.setBox (b);
03595                 cell.geomId(atoi(getData(initIndex)));
03596                 cell.objectId (string(getData(initIndex+1)));
03597                 cell.column(atoi(getData(initIndex+2)));
03598                 cell.line(atoi(getData(initIndex+3)));
03599                 return(this->fetchRow());
03600         }
03601         catch(...)
03602         {
03603                 errorMessage_ = cursor_->getErrorMessage();
03604                 return false;
03605         }
03606 }
03607 
03608 
03609 

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