SEXP aRTdb::Print() { stringstream s; if(!Database) error("Invalid object"); TeAttrTableVector atts; Database->getAttrTables(atts, TeAttrExternal); s << artOBJECT("aRTdb") << "\n\n" << "Database: \"" << Database->databaseName() << "\"" << endl << "Layers: "; TeLayerMap& layer_map = Database -> layerMap(); TeLayer* layer; TeLayerMap::iterator lit; if( layer_map.size() > 0 ) { s << endl; for (lit = layer_map.begin(); lit != layer_map.end(); ++lit) { layer = lit -> second; s << " \"" << layer -> name() << "\"" << endl; } } else s << "(none)" << endl; s << "Themes: "; TeThemeMap& theme_map = Database -> themeMap(); TeAbstractTheme* theme; TeThemeMap::iterator tit; if(theme_map.size() > 0) { s << endl; for (tit = theme_map.begin(); tit != theme_map.end(); ++tit) { theme = tit -> second; s << " \"" << theme -> name() << "\"" << endl; } } else s << "(none)" << endl; s << "External tables: "; if( atts.size() ) { s << endl; for(unsigned i = 0; i != atts.size(); i++) s << " \"" << atts[i].name() << "\"" << endl; } else s << "(none)" << endl; s << endl; Rprintf( StreamToChar(s) ); return RNULL; }
SEXP aRTdb::ShowTables() { SEXP vectorTables; TeAttrTableVector atts; TeAttributeList attrList; Database -> getAttrTables(atts, TeAttrExternal); // TODO: it returns false when the database does not have any table //if ( !Database -> getAttrTables(atts, TeAttrExternal) ) // error("Error loading tables from database!"); if( !atts.size() ) return R_NilValue; vectorTables = allocVector(STRSXP, atts.size()); for (unsigned i = 0; i < atts.size(); i++) SET_STRING_ELT(vectorTables, i, mkChar(atts[i].name().c_str())); return vectorTables; }
bool TeExportShapefile(TeLayer* layer, const string& shpFileName, const string& tableName, const string& restriction) { if (!layer || shpFileName.empty()) return false; // check if asked table exist TeAttrTableVector& vTables = layer->attrTables(); TeAttrTableVector::iterator it = vTables.begin(); while (it != vTables.end()) { if (it->name() == tableName) break; ++it; } if (it == vTables.end()) return false; TeAttrTableVector askedTable; askedTable.push_back(*it); TeTheme* tempTheme = new TeTheme(); tempTheme->attributeRest(restriction); tempTheme->layer(layer); tempTheme->collectionTable(""); tempTheme->collectionAuxTable(""); tempTheme->setAttTables(askedTable); TeQuerierParams qPar(true, true); qPar.setParams(tempTheme); TeQuerier* tQuerier = new TeQuerier(qPar); bool res = TeExportQuerierToShapefile(tQuerier, shpFileName); delete tQuerier; delete tempTheme; return res ; }
/// UTILIITARY FUNCTION - Creates a new Theme a TerraLib geographical database /// \param attTable is a copy to the Theme new attriute table being created /// \param outputTable is the new Theme table name /// \param whereClause is a SQL WHERE CLAUSE like string used to querie the TerraLib database /// \param inputThemeName is a string containing the inputTheme that serves as information /// source for the Theme being created /// \param view is a pointer to the TerrraLib TeView object to which Theme will be attached /// \param layer is a pointer to the TerrraLib TeLayer object to which Theme will be attached /// \param db is a pointer to the TerrraLib database into which the Theme will be interted /// \param theme is a pointer to the TeTheme object being added to the geographical database bool createNewTheme( TeTable attTable, char outputTable[], string whereClause, string inputThemeName, TeView *view, TeLayer *layer, TeDatabase *db, TeTheme *theme ) { TeTheme inputTheme( inputThemeName, layer); // Raian /// load the inputTheme properties // loads the existing view if( !db->loadTheme( &inputTheme ) ) { cout << "Error: fail to load theme \"" << inputThemeName << db->errorMessage() << endl; db->close(); return false; } view->add(theme); if( ! whereClause. empty() ) theme->attributeRest(whereClause); // Set a default visual for the geometries of the objects of the layer // Polygons will be set with the blue color TeVisual polygonVisual(TePOLYGONS); TeColor azul(0,0,255); // Raian: polygonVisual.color(TeColor(0,0,255)); polygonVisual.color(azul); // Points will be set with the red color TeVisual pointVisual(TePOINTS); TeColor vermelho(255,0,0); // Raian: pointVisual.color(TeColor(255,0,0)); pointVisual.color(vermelho); pointVisual.style(TePtTypeX); // Set all of the geometrical representations to be visible int allRep = layer->geomRep(); theme->visibleRep(allRep); // Select all the attribute tables of the inputTheme // and add the new attribute table theme->setAttTables( inputTheme.attrTables() ); theme->addThemeTable( attTable ); // Save the theme in the database if (!theme->save()) { cout << "Error: fail to save the theme \"" << outputTable << "\" in the database: " << db->errorMessage() << endl; db->close(); return false; } // Build the collection of objects associated to the theme***** TeAttrTableVector attrsDim; inputTheme.getAttTables(attrsDim, TeFixedGeomDynAttr); string colTable = theme->collectionTable(); string colAuxTable = theme->collectionAuxTable (); // ------------------------ collection string popule; popule = " INSERT INTO "+ colTable +" (c_object_id) "; popule += " SELECT object_id_ FROM "+ string(outputTable); if (!db->execute(popule)) { cout << "Error: fail to build the theme collection\""<< outputTable << "\": " << db->errorMessage() << endl; db->close(); return false; } popule = "UPDATE " + colTable; popule += " SET c_legend_id=0, c_legend_own=0, c_object_status=0 "; if (!db->execute(popule)) { cout << "Error: fail to build the theme collection\""<< outputTable << "\": " << db->errorMessage() << endl; db->close(); return false; } // ------------------------ collection aux if( !attrsDim.empty() && attrsDim[0].name() != "" ){ string ins = "INSERT INTO "+ colAuxTable +" (object_id, aux0, grid_status) "; ins += " SELECT "+ string(outputTable) +".object_id_, "+ attrsDim[0].name() +".attr_id, 0"; ins += " FROM "+ string(outputTable)+" LEFT JOIN "+ attrsDim[0].name() +" ON "; ins += string(outputTable)+".object_id_ = "+ attrsDim[0].name() +".object_id_"; if (!db->execute(ins)) { cout << "Error: fail to build the theme collection\""<< outputTable << "\": " << db->errorMessage() << endl; db->close(); return false; } } else { string ins = "INSERT INTO "+ colAuxTable +" (object_id, grid_status) "; ins += " SELECT "+ string(outputTable) +".object_id_, 0"; ins += " FROM "+ string(outputTable); if (!db->execute(ins)) { cout << "Error: fail to build the theme collection\""<< outputTable << "\": " << db->errorMessage() << endl; db->close(); return false; } }; return true; }
bool updateDB301To302(TeDatabase* db, string& errorMessage) { TeDatabasePortal* portal = db->getPortal(); if(!portal) return false; // ----- valid attribute table TeAttrTableVector attrTableVec; if(db->getAttrTables(attrTableVec)) { for(unsigned int i=0; i<attrTableVec.size(); ++i) { TeTable fromTable = attrTableVec[i]; bool flag = false; if(fromTable.tableType()==TeAttrMedia) continue; //verify if there is another table with the same name for(unsigned int j=0; j<i; ++j) { if(TeConvertToUpperCase(attrTableVec[j].name())==TeConvertToUpperCase(fromTable.name())) { flag = true; break; } } if(flag) continue; if(db->validTable(fromTable)) //fromTable was modified { TeAttributeList newAttrList = fromTable.attributeList(); TeAttributeList oldAttrList = attrTableVec[i].attributeList(); TeAttributeList::iterator newAttIt = newAttrList.begin(); TeAttributeList::iterator oldAttIt = oldAttrList.begin(); bool change = false; while(newAttIt!=newAttrList.end()) { if(((*oldAttIt).rep_.name_) != ((*newAttIt).rep_.name_)) { TeAttributeRep rep = (*newAttIt).rep_; if(db->alterTable(fromTable.name(), rep, (*oldAttIt).rep_.name_)) change = true; } ++newAttIt; ++oldAttIt; } if(change) { // update te_layer_table string upd = " UPDATE te_layer_table "; upd += " SET unique_id = '"+ fromTable.uniqueName() +"'"; upd += ", attr_link = '"+ fromTable.linkName() +"'"; upd += " WHERE attr_table = '"+ fromTable.name() +"'"; if(!db->execute (upd)) { delete portal; errorMessage = "Error updating te_layer_table!\n"; errorMessage += db->errorMessage(); return false; } } }//if }//for portal->freeResult(); } // ----- te_grouping table string sel = " SELECT theme_id, grouping_attr, grouping_attr_type FROM te_grouping "; if(!portal->query(sel)) { delete portal; return false; } while(portal->fetchRow()) { string themeId, gAttr; themeId = portal->getData(0); gAttr = portal->getData(1); int gTypr = portal->getInt(2); if(((gAttr.empty()) || (gAttr=="NONE")) && (gTypr==0)) { string del = " DELETE FROM te_grouping WHERE theme_id = "+ themeId; db->execute(del); } } // ----- te_grouping table delete portal; return true; }
string TeQuerierDB::sqlFrom(string geomTable) { string fromPar = ""; string fromClause = ""; //get collection tables string collAuxTable = params_->theme()->collectionAuxTable(); string collTable = params_->theme()->collectionTable(); if(collAuxTable.empty() || collTable.empty()) return attrTable_.name(); if(attrTable_.name().empty()) return ""; string uniqueIdName = attrTable_.name() +"."+ attrTable_.uniqueName(); string objectIdName = attrTable_.name() +"."+ attrTable_.linkName(); string linkName; if(attrTable_.tableType() != TeAttrExternal) linkName = attrTable_.name() +"."+ attrTable_.linkName(); else linkName = collTable +".c_object_id "; //load geometry table if there is spatial restriction if(geomTable.empty() && params_->hasSpatialRes()) { TeRepresentation* rep = (theme()->layer()->vectRepres())[0]; geomTable = theme()->layer()->tableName(rep->geomRep_); } //get the extern table position int posExtern = -1; TeAttrTableVector attr = params_->theme()->attrTables(); for(unsigned int i=0; i<attr.size(); ++i) { if(attr[i].tableType() == TeAttrExternal) ++posExtern; } //if the table is temporal if((attrTable_.tableType()==TeAttrEvent) || (attrTable_.tableType()==TeFixedGeomDynAttr)) { fromPar += "(("; fromClause = attrTable_.name()+" RIGHT JOIN "+ collAuxTable; if(attrTable_.tableType()==TeFixedGeomDynAttr) { fromClause += " ON "+ uniqueIdName +" = "; fromClause += collAuxTable +".aux0"; fromClause += ")"; } else { fromClause += " ON "+ objectIdName +" = "; fromClause += collAuxTable +".object_id"+ ")"; } fromClause += " LEFT JOIN "+ collTable; fromClause += " ON "+ collAuxTable +".object_id = "+ collTable +".c_object_id )"; if(!geomTable.empty()) { fromPar += "("; fromClause += " LEFT JOIN "+ geomTable +" ON "; fromClause += collAuxTable +".object_id = "+ geomTable +".object_id )"; } if(params_->selectedObjs() != TeAll) //! join with collection table { fromPar += "("; fromClause += " LEFT JOIN "+ collTable +" ON "; fromClause += collAuxTable +".object_id = "+ collTable +".c_object_id )"; } } else if (attrTable_.tableType()==TeAttrStatic) { fromPar += "("; fromClause = attrTable_.name()+" RIGHT JOIN "+ collTable; fromClause += " ON "+ linkName +" = "+ collTable +".c_object_id)"; if(!geomTable.empty()) //! join with geometry table { fromPar += "("; fromClause += " LEFT JOIN "+ geomTable +" ON "; fromClause += collTable +".c_object_id = "+ geomTable +".object_id )"; } if(params_->selectedObjs() != TeAll) //! join with collection table { fromPar += "("; fromClause += " LEFT JOIN "+ collAuxTable +" ON "; fromClause += collTable +".c_object_id = "+ collAuxTable +".object_id )"; } } else if (attrTable_.tableType()==TeAttrExternal) { fromPar += "(("; fromClause = collAuxTable +" RIGHT JOIN "+ collTable; fromClause += " ON "+ collAuxTable +".object_id = "+ collTable +".c_object_id )"; fromClause += " LEFT JOIN "+ attrTable_.name() +" ON "; fromClause += collAuxTable +".aux"+ Te2String(posExtern) +" = "; fromClause += uniqueIdName +" )"; if(!geomTable.empty()) //! join with geometry table { fromPar += "("; fromClause += " LEFT JOIN "+ geomTable +" ON "; fromClause += collTable +".c_object_id = "+ geomTable +".object_id )"; } } return (fromPar+fromClause); }