/usr/include/terralib/kernel/TeSTEFunctionsDB.h is in libterralib-dev 4.3.0+dfsg.2-10.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 | /************************************************************************************
TerraLib - a library for developing GIS applications.
Copyright © 2001-2007 INPE and Tecgraf/PUC-Rio.
This code is part of the TerraLib library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
You should have received a copy of the GNU Lesser General Public
License along with this library.
The authors reassure the license terms regarding the warranties.
They specifically disclaim any warranties, including, but not limited to,
the implied warranties of merchantability and fitness for a particular purpose.
The library p rovided hereunder is on an "as is" basis, and the authors have no
obligation to provide maintenance, support, updates, enhancements, or modifications.
In no event shall INPE and Tecgraf / PUC-Rio be held liable to any party for direct,
indirect, special, incidental, or consequential damages arising out of the use
of this library and its documentation.
*************************************************************************************/
/*! \file TeSTEFunctionsDB.h
\brief This file contains a set of functions to build spatio-temporal sets from a TerraLib databse
*/
#ifndef __TERRALIB_INTERNAL_STOFUNCTIONS_H
#define __TERRALIB_INTERNAL_STOFUNCTIONS_H
#include "TeDatabase.h"
#include "TeProgress.h"
#include <vector>
#include <string>
//! Builds the spatial object set from database according to the restrictions previously defined
/*!
\param stoset the STOSet that will be filled
\param loadGeometries if the STOSet will be filled with the geometries
\param loadAllAttributes if the STOSet will be filled with all attributes of the tables of the STOSet
\param attrNames a subset of the attribute names that will be loaded, if the param loadAllAttributes is false
*/
template<typename elementSet>
bool TeSTOSetBuildDB(elementSet* stoset, bool loadGeometries = false, bool loadAllAttributes = false, vector<string> attrNames = vector<string>());
//! Builds the spatial object set from database according to the restrictions previously defined
/*!
\param stoset the STOSet that will be filled
\param groupAttr a map from attribute name to statistic type
\param loadGeometries if the STOSet will be filled with the geometries
*/
template<typename elementSet>
bool TeSTOSetBuildDB(elementSet* stoset, TeGroupingAttr& groupAttr, bool loadGeometries = false);
//! Updates or inserts a database table from a spatial object set
/*!
\param elemSet the set of spatial objects that will be inserted or updated into database
\param tableName database table name that will be updated
\param indexes the attribute indexes of the spatial objects that must be updated
*/
template<typename elementSet>
bool TeUpdateDBFromSet (elementSet* elemSet, const string& tableName, vector<int>* indexes = 0);
//! Auxiliary function to insert a row
template<typename Element>
bool insertRow (Element* elem, TeTable& table, const string& uniqueValue, TeDatabase* db, vector<int>* indexes=0);
//! Auxiliary function to update a row
template<typename Element>
bool updateRow (Element* elem, TeTable table, const string& uniqueId, TeDatabase* db, vector<int>* indexes=0);
//--------------- Implementation
template<typename elementSet>
bool TeSTOSetBuildDB(elementSet* stoset, bool loadGeometries, bool loadAllAttributes, vector<string> attrNames)
{
if(!stoset->build(loadGeometries, loadAllAttributes, attrNames))
return false;
return true;
}
template<typename elementSet>
bool TeSTOSetBuildDB(elementSet* stoset, TeGroupingAttr& groupAttr, bool loadGeometries)
{
if(!stoset->build(groupAttr, loadGeometries))
return false;
return true;
}
template<typename elementSet>
bool TeUpdateDBFromSet (elementSet* elemSet, const string& tableName, vector<int>* indexes)
{
TeDatabase* db = 0;
if((elemSet->getTheme()) && (elemSet->theme()->layer()))
db = elemSet->getTheme()->layer()->database();
else if(elemSet->getLayer())
db = elemSet->getLayer()->database();
if(!db)
return false;
//progress bar
int step = 0;
int numSteps = elemSet->numSTInstance();
if(TeProgress::instance())
TeProgress::instance()->setTotalSteps(numSteps);
try
{
TeAttrTableVector attrTables;
if(elemSet->getTheme())
elemSet->getTheme()->getAttTables(attrTables);
else if(elemSet->getLayer())
elemSet->getLayer()->getAttrTables(attrTables);
if(attrTables.empty())
return false;
TeTable table;
int uniqueIndex = -1;
//! verify if the table is in the stoset
for(unsigned int i=0; i<attrTables.size(); i++)
{
if(attrTables[i].name()==tableName)
{
uniqueIndex = i;
table = attrTables[i];
break;
}
}
if((uniqueIndex==-1) || ((table.tableType()!=TeAttrEvent) &&
(table.tableType()!=TeAttrStatic) &&
(table.tableType()!=TeFixedGeomDynAttr) &&
(table.tableType()!=TeAttrExternal)))
return false;
// get some information about the attribute table required
string uniqueIdName = table.uniqueName();
TeDatabasePortal* portal = db->getPortal();
if(!portal)
return false;
map<string, string> uniqueIds;
string sql = "SELECT "+ uniqueIdName +" FROM "+ table.name();
if(!portal->query (sql))
{
delete portal;
return false;
}
while(portal->fetchRow())
uniqueIds[string(portal->getData(0))] = string(portal->getData(0));
delete portal;
//verifies if the attribute column exists in the table
TeAttributeList attList = elemSet->getAttributeList();
for(unsigned int i=0; i<attList.size(); ++i)
{
if(indexes && (find(indexes->begin(), indexes->end(),(int)i) == indexes->end()))
continue;
//verify if the table has this column
string attrName = attList[i].rep_.name_;
size_t pos = attrName.find(".", 0, 1);
if (pos != string::npos)
attList[i].rep_.name_ = attrName.substr(pos+1);
if (!db->columnExist(tableName, attList[i].rep_.name_, attList[i]))
{
if(!db->addColumn (tableName, attList[i].rep_))
return false;
TeAttributeList attrListTable = table.attributeList();
attrListTable.push_back (attList[i]);
table.setAttributeList(attrListTable);
}
}
// Update all the objects
typename elementSet::iterator itObj = elemSet->begin();
while (itObj != elemSet->end())
{
string uniqueId = (*itObj).getUniqueId(uniqueIndex);
if(uniqueIds.find(uniqueId) == uniqueIds.end())
{
if (!insertRow (&(*itObj), table, uniqueId, db, indexes))
return false;
uniqueIds[uniqueId] = uniqueId;
}
else
{
if (!updateRow (&(*itObj), table, uniqueId, db, indexes))
return false;
}
++itObj;
if(TeProgress::instance())
{
if (TeProgress::instance()->wasCancelled())
{
TeProgress::instance()->reset();
return false;
}
else
TeProgress::instance()->setProgress(step);
}
++step;
}
}
catch(...)
{
if (TeProgress::instance())
TeProgress::instance()->reset();
return false;
}
if (TeProgress::instance())
TeProgress::instance()->reset();
return true;
}
template<typename Element>
bool insertRow (Element* elem, TeTable& table, const string& uniqueValue, TeDatabase* db, vector<int>* indexes)
{
vector<string> attrs;
table.attributeNames(attrs);
string ins = " INSERT INTO "+ table.name() +" (";
string values = " VALUES ( ";
TePropertyVector prop = elem->getPropertyVector();
int count=0;
for(unsigned int i=0; i<prop.size(); ++i)
{
if(indexes && (find(indexes->begin(), indexes->end(),(int)i) == indexes->end()))
continue;
string attrName = prop[i].attr_.rep_.name_;
size_t pos = attrName.find(".", 0, 1);
if (pos != string::npos)
attrName = attrName.substr(pos+1);
if( (find(attrs.begin(), attrs.end(), attrName)!=attrs.end()) &&
(TeStringCompare(attrName, table.uniqueName()) == false) &&
(TeStringCompare(attrName,table.linkName()) == false)&&
(TeStringCompare(attrName,table.attInitialTime()) == false)&&
(TeStringCompare(attrName,table.attFinalTime()) == false) )
{
if((prop[i].attr_.rep_.type_!=TeSTRING) && prop[i].value_.empty())
continue;
if(count>0)
{
ins += ",";
values += ",";
}
++count;
ins += attrName;
if(prop[i].attr_.rep_.type_==TeSTRING)
values += "'"+ prop[i].value_ +"'";
else if(prop[i].attr_.rep_.type_==TeREAL)
{
std::string strValue = prop[i].value_;
replace (strValue.begin(), strValue.end(), ',', '.');
values += strValue;
}
else if(prop[i].attr_.rep_.type_==TeDATETIME)
{
TeTime time(prop[i].value_, prop[i].attr_.dateChronon_, prop[i].attr_.dateTimeFormat_, prop[i].attr_.dateSeparator_, prop[i].attr_.timeSeparator_);
values += db->getSQLTime(time);
}
else
values += prop[i].value_;
}
}
// -------- object_id, unique_id and timeInterval
if(count>0)
{
ins += " ,";
values += " ,";
}
ins += table.linkName();
values += "'"+ elem->objectId() +"'";
if(table.linkName() != table.uniqueName())
{
ins += ", "+ table.uniqueName();
values += ", '"+ uniqueValue +"'";
}
if(!table.attInitialTime().empty())
{
TeTime time (elem->timeInterval().getT1());
ins += ", "+ table.attInitialTime();
values += ", "+ db->getSQLTime(time);
}
if((!table.attFinalTime().empty()) && (table.attInitialTime()!=table.attFinalTime()))
{
TeTime time (elem->timeInterval().getT2());
ins += ", "+ table.attFinalTime();
values += ", "+ db->getSQLTime(time);
}
// ----------
ins += ") "+ values +" )";
if(!db->execute (ins))
return false;
return true;
}
template<typename Element>
bool updateRow (Element* elem, TeTable table, const string& uniqueId, TeDatabase* db, vector<int>* indexes)
{
vector<string> attrs;
table.attributeNames(attrs);
string ins = " UPDATE "+ table.name() +" SET ";
TePropertyVector prop = elem->getPropertyVector();
int count = 0;
for(unsigned int i=0; i<prop.size(); ++i)
{
if(indexes && (find(indexes->begin(), indexes->end(),(int)i) == indexes->end()))
continue;
string attrName = prop[i].attr_.rep_.name_;
size_t pos = attrName.find(".", 0, 1);
std::string tableName;
if (pos != string::npos)
{
attrName = attrName.substr(pos+1);
tableName = attrName.substr(0, pos-1);
if (tableName != table.name())
continue;
}
if( (find(attrs.begin(), attrs.end(), attrName)!=attrs.end()) &&
(TeStringCompare(attrName,table.uniqueName()) == false) &&
(TeStringCompare(attrName,table.linkName()) == false) &&
(TeStringCompare(attrName,table.attInitialTime()) == false) &&
(TeStringCompare(attrName,table.attFinalTime()) == false) )
{
if((prop[i].attr_.rep_.type_!=TeSTRING) && (prop[i].value_.empty()))
continue;
if(count>0)
ins += ",";
++count;
ins += attrName +" = ";
if(prop[i].attr_.rep_.type_==TeSTRING)
ins += "'"+ db->escapeSequence(prop[i].value_) +"'";
else if(prop[i].attr_.rep_.type_==TeREAL)
{
std::string strValue = prop[i].value_;
replace (strValue.begin(), strValue.end(), ',', '.');
ins += strValue;
}
else if(prop[i].attr_.rep_.type_==TeDATETIME)
{
TeTime time(prop[i].value_, prop[i].attr_.dateChronon_, prop[i].attr_.dateTimeFormat_, prop[i].attr_.dateSeparator_, prop[i].attr_.timeSeparator_);
ins += db->getSQLTime(time);
}
else
ins += prop[i].value_;
}
}
// -------- timeInterval
if(!table.attInitialTime().empty())
{
TeTime time (elem->timeInterval().getT1());
if(count>0)
ins += ", ";
ins += table.attInitialTime() +" = ";
ins += db->getSQLTime(time);
++count;
}
if((!table.attFinalTime().empty()) && (table.attInitialTime()!=table.attFinalTime()))
{
TeTime time (elem->timeInterval().getT2());
if(count>0)
ins += ", ";
ins += table.attFinalTime() +" = ";
ins += db->getSQLTime(time);
++count;
}
// ----------
if ( count == 0 )
return true;
ins += " WHERE "+ table.uniqueName() +" = '"+ uniqueId +"'";
if(!db->execute (ins))
return false;
return true;
}
/** \example createSTElementSetFromLayer.cpp
Shows how to create a new Spatial Temporal Element Set (STElementSet) from a layer
*/
/** \example createSTElementSetFromTheme.cpp
Shows how to create a new Spatial Temporal Element Set (STElementSet) from a theme
*/
#endif
|