![]() |
TerraLib 4.1
|
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 *************************************************************************************/ 00028 #ifndef __TERRALIB_INTERNAL_BASESTINSTANCE_H 00029 #define __TERRALIB_INTERNAL_BASESTINSTANCE_H 00030 00031 #include "TeCoord2D.h" 00032 #include "TeGeometryAlgorithms.h" 00033 #include "TeAttribute.h" 00034 #include "TeSharedPtr.h" 00035 00036 #include <string> 00037 #include <map> 00038 #include <vector> 00039 00040 using namespace std; 00041 00042 class TeTheme; 00043 00053 template<typename GeometryType, typename TimeType> 00054 class TeBaseSTInstance 00055 { 00056 00057 protected: 00059 string object_id_; 00061 vector<string> unique_id_; 00063 vector<string> properties_; 00065 GeometryType geometries_; 00067 TimeType time_; 00069 int slice_; 00071 TeSharedPtr<TeAttributeList> attrList_; 00072 00073 public: 00074 00076 TeBaseSTInstance() : object_id_(""), slice_(-1), attrList_(0) 00077 { } 00078 00080 TeBaseSTInstance (const string& object_id, const vector<string>& prop, TeAttributeList* attList = 0, const int& s = -1) : 00081 object_id_(object_id), 00082 properties_(prop), 00083 slice_(s), 00084 attrList_(TeSharedPtr<TeAttributeList>(attList)) 00085 { } 00086 00088 TeBaseSTInstance (const string& object_id, const GeometryType& geometries, const TimeType& time, 00089 const int& s = -1) : 00090 object_id_(object_id), 00091 geometries_(geometries), 00092 time_(time), 00093 slice_(s), 00094 attrList_(0) 00095 { } 00096 00098 TeBaseSTInstance (const string& object_id, const vector<string>& prop, TeAttributeList* attList, 00099 const GeometryType& geometries, const int& slice, const TimeType& time ) : 00100 object_id_(object_id), 00101 properties_(prop), 00102 geometries_(geometries), 00103 time_(time), 00104 slice_(slice), 00105 attrList_(TeSharedPtr<TeAttributeList>(attList)) 00106 { } 00107 00109 virtual ~TeBaseSTInstance() 00110 { } 00111 00113 virtual bool operator== (const TeBaseSTInstance<GeometryType, TimeType>& other); 00114 00116 virtual string getObjectId () 00117 { return object_id_; } 00118 00120 virtual void setObjectId (const string& id) 00121 { object_id_ = id; } 00122 00124 virtual string objectId () 00125 { return getObjectId(); } 00126 00128 virtual void objectId (const string& id) 00129 { setObjectId(id); } 00130 00132 virtual vector<string>& getUniqueId() 00133 { return unique_id_; } 00134 00136 virtual string getUniqueId(const int& i); 00137 00139 virtual void setUniqueId (const vector<string>& id) 00140 { unique_id_ = id; } 00141 00143 virtual void addUniqueId (const string& id) 00144 { unique_id_.push_back(id); } 00145 00147 virtual vector<string>& uniqueId () 00148 { return getUniqueId(); } 00149 00151 virtual string uniqueId (int index) 00152 { return getUniqueId(index); } 00153 00155 virtual void uniqueId (const vector<string>& ids) 00156 { setUniqueId(ids); } 00157 00159 virtual TimeType getTime() 00160 { return time_; } 00161 00163 virtual void setTime (const TimeType& t) 00164 { time_ = t; } 00165 00167 virtual void setProperties(const vector<string>& p) 00168 { properties_ = p; } 00169 00171 virtual void setProperties(TePropertyVector& p); 00172 00174 virtual bool setPropertyValue (const int& i, const string& val); 00175 00177 virtual bool setPropertyValue (const string& name, const string& val); 00178 00180 virtual void addPropertyValue (const string& val) 00181 { properties_.push_back (val); } 00182 00184 virtual bool removePropertyValue (const int& i); 00185 00187 00192 virtual bool addProperty(TeProperty& prop); 00193 00195 virtual bool addProperty(TeAttribute& rep); 00196 00198 virtual vector<string>& getProperties() 00199 { return properties_; } 00200 00202 virtual void getPropertyVector(TePropertyVector& propVec); 00203 00205 TePropertyVector getPropertyVector(); 00206 00208 virtual bool getProperty (TeProperty& prop, unsigned int i = 0); 00209 00211 virtual bool getProperty (TeProperty& prop, string name); 00212 00214 virtual bool getPropertyValue (string& val, const int& i = 0); 00215 00217 virtual bool getPropertyValue (const string& name, string& val); 00218 00220 virtual double operator[](int i); 00221 00223 virtual void properties(TePropertyVector& p) 00224 { setProperties(p); } 00225 00227 virtual GeometryType& getGeometries() 00228 { return geometries_;} 00229 00231 virtual void setGeometry(const GeometryType& g) 00232 { geometries_ = g; } 00233 00235 virtual GeometryType& geometries() 00236 { return getGeometries();} 00237 00239 virtual void setSlice (int s) 00240 { slice_ = s; } 00241 00243 virtual int getSlice() 00244 { return slice_; } 00245 00247 virtual void slice (int s) 00248 { setSlice(s); } 00249 00251 virtual int slice() 00252 { return getSlice(); } 00253 00255 const TeAttributeList* getAttrList() 00256 { return attrList_.nakedPointer(); } 00257 00259 void setAttrList(const TeAttributeList& attList); 00260 00262 void setAttrList(TeSharedPtr<TeAttributeList>& attrList); 00263 00265 virtual void clear(); 00266 00268 virtual TeCoord2D getCentroid(); 00269 00271 virtual double getArea(); 00272 00274 virtual bool isTimeValid() 00275 { return false; } 00276 00278 virtual TeTheme* theme() 00279 { return 0; } 00280 00282 virtual void theme(TeTheme*) 00283 { } 00284 }; 00285 00286 template<typename GeometryType, typename TimeType> bool 00287 TeBaseSTInstance<GeometryType, TimeType>::operator== (const TeBaseSTInstance<GeometryType, TimeType>& other) 00288 { 00289 return ( (object_id_ == other.object_id_) && 00290 (time_ == other.time_) && 00291 (slice_ == other.slice_)); 00292 } 00293 00294 template<typename GeometryType, typename TimeType> string 00295 TeBaseSTInstance<GeometryType, TimeType>::getUniqueId(const int& i) 00296 { 00297 if(i<(int)unique_id_.size()) 00298 return unique_id_[i]; 00299 return ""; 00300 } 00301 00302 template<typename GeometryType, typename TimeType> void 00303 TeBaseSTInstance<GeometryType, TimeType>::setAttrList(const TeAttributeList& attList) 00304 { 00305 attrList_.reset(new TeAttributeList()); 00306 *attrList_ = attList; 00307 } 00308 00309 template<typename GeometryType, typename TimeType> void 00310 TeBaseSTInstance<GeometryType, TimeType>::setAttrList(TeSharedPtr<TeAttributeList>& attList) 00311 { 00312 attrList_ = attList; 00313 } 00314 00315 template<typename GeometryType, typename TimeType> void 00316 TeBaseSTInstance<GeometryType, TimeType>::setProperties(TePropertyVector& p) 00317 { 00318 properties_.clear(); 00319 TePropertyVector::iterator it = p.begin(); 00320 while(it!=p.end()) 00321 { 00322 addPropertyValue(it->value_); 00323 ++it; 00324 } 00325 } 00326 00327 template<typename GeometryType, typename TimeType> bool 00328 TeBaseSTInstance<GeometryType, TimeType>::setPropertyValue (const int& i, const string& val) 00329 { 00330 if(i<0 || i>=(int)properties_.size()) 00331 return false; 00332 properties_[i] = val; 00333 return true; 00334 } 00335 00336 template<typename GeometryType, typename TimeType> bool 00337 TeBaseSTInstance<GeometryType, TimeType>::setPropertyValue (const string& name, const string& val) 00338 { 00339 if(!attrList_.isActive()) 00340 return false; 00341 00342 string newName = TeConvertToUpperCase(name); 00343 size_t pos = name.find(".", 0, 1); 00344 if (pos != string::npos) 00345 newName = TeConvertToUpperCase((name.substr(pos+1))); 00346 00347 for(unsigned int i=0; i<attrList_->size(); ++i) 00348 { 00349 string s = TeConvertToUpperCase((attrList_->operator[](i)).rep_.name_); 00350 if( (s == TeConvertToUpperCase(name)) || (s == newName)) 00351 { 00352 if(i>=properties_.size()) 00353 return false; 00354 properties_[i] = val; 00355 return true; 00356 } 00357 } 00358 return false; 00359 } 00360 00361 template<typename GeometryType, typename TimeType> void 00362 TeBaseSTInstance<GeometryType, TimeType>::getPropertyVector(TePropertyVector& propVec) 00363 { 00364 if(!attrList_.isActive()) 00365 return; 00366 if(properties_.size()!=attrList_->size()) 00367 return; 00368 propVec.clear(); 00369 for(unsigned int i=0; i<properties_.size(); ++i) 00370 { 00371 TeProperty p; 00372 p.value_ = properties_[i]; 00373 p.attr_ = attrList_->operator[](i); 00374 propVec.push_back(p); 00375 } 00376 } 00377 00378 template<typename GeometryType, typename TimeType> TePropertyVector 00379 TeBaseSTInstance<GeometryType, TimeType>::getPropertyVector() 00380 { 00381 TePropertyVector vec; 00382 this->getPropertyVector(vec); 00383 return vec; 00384 } 00385 00386 template<typename GeometryType, typename TimeType> bool 00387 TeBaseSTInstance<GeometryType, TimeType>::getProperty (TeProperty& prop, unsigned int i) 00388 { 00389 if(!attrList_.isActive()) 00390 return false; 00391 00392 if(i>=properties_.size() || i>=attrList_->size()) 00393 return false; 00394 prop.value_ = properties_[i]; 00395 prop.attr_ = (*attrList_)[i]; 00396 return true; 00397 } 00398 00399 template<typename GeometryType, typename TimeType> bool 00400 TeBaseSTInstance<GeometryType, TimeType>::getProperty (TeProperty& prop, string name) 00401 { 00402 if(!attrList_.isActive()) 00403 return false; 00404 if(properties_.size()!=attrList_->size()) 00405 return false; 00406 00407 string newName = TeConvertToUpperCase(name); 00408 size_t pos = name.find(".", 0, 1); 00409 if (pos != string::npos) 00410 newName = TeConvertToUpperCase(name.substr(pos+1)); 00411 00412 for(unsigned int i=0; i<attrList_->size(); ++i) 00413 { 00414 string s = TeConvertToUpperCase((attrList_->operator[](i)).rep_.name_); 00415 if((s == TeConvertToUpperCase(name)) || (s == newName)) 00416 { 00417 prop.value_ = properties_[i]; 00418 prop.attr_ = attrList_->operator[](i); 00419 return true; 00420 } 00421 } 00422 return false; 00423 } 00424 00425 template<typename GeometryType, typename TimeType> bool 00426 TeBaseSTInstance<GeometryType, TimeType>::getPropertyValue (string& val, const int& i) 00427 { 00428 if(i<0 || i>= (int)properties_.size()) 00429 return false; 00430 val = properties_[i]; 00431 return true; 00432 } 00433 00434 template<typename GeometryType, typename TimeType> bool 00435 TeBaseSTInstance<GeometryType, TimeType>::getPropertyValue (const string& name, string& val) 00436 { 00437 if(!attrList_.isActive()) 00438 return false; 00439 00440 if(properties_.size()!=attrList_->size()) 00441 return false; 00442 00443 string newName = TeConvertToUpperCase(name); 00444 size_t pos = name.find(".", 0, 1); 00445 if (pos != string::npos) 00446 newName = TeConvertToUpperCase(name.substr(pos+1)); 00447 00448 for(unsigned int i=0; i<attrList_->size(); ++i) 00449 { 00450 string s = TeConvertToUpperCase((attrList_->operator[](i)).rep_.name_); 00451 if((s == TeConvertToUpperCase(name)) || (s == newName)) 00452 { 00453 val = properties_[i]; 00454 return true; 00455 } 00456 } 00457 return false; 00458 } 00459 00460 template<typename GeometryType, typename TimeType> bool 00461 TeBaseSTInstance<GeometryType, TimeType>::addProperty(TeProperty& prop) 00462 { 00463 if(!attrList_.isActive()) 00464 return false; 00465 00466 string newName = TeConvertToUpperCase(prop.attr_.rep_.name_); 00467 size_t pos = (prop.attr_.rep_.name_).find(".", 0, 1); 00468 if (pos != string::npos) 00469 newName = TeConvertToUpperCase(prop.attr_.rep_.name_.substr(pos+1)); 00470 00471 for(unsigned int i=0; i<attrList_->size(); ++i) 00472 { 00473 string s = TeConvertToUpperCase((attrList_->operator[](i)).rep_.name_); 00474 if((s == TeConvertToUpperCase(prop.attr_.rep_.name_)) || (s == newName)) 00475 { 00476 //the property already exists 00477 if(i>=properties_.size()) 00478 properties_.push_back (prop.value_); 00479 else 00480 properties_[i] = prop.value_; 00481 return true; 00482 } 00483 } 00484 00485 // Adds a new property 00486 attrList_->push_back(prop.attr_); 00487 properties_.push_back (prop.value_); 00488 return true; 00489 } 00490 00491 template<typename GeometryType, typename TimeType> bool 00492 TeBaseSTInstance<GeometryType, TimeType>::addProperty(TeAttribute& attr) 00493 { 00494 if(!attrList_.isActive()) 00495 return false; 00496 attrList_->push_back(attr); 00497 return true; 00498 } 00499 00500 template<typename GeometryType, typename TimeType> bool 00501 TeBaseSTInstance<GeometryType, TimeType>::removePropertyValue(const int& i) 00502 { 00503 int index = 0; 00504 vector<string>::iterator it = properties_.begin(); 00505 while(it!=properties_.end()) 00506 { 00507 if(index==i) 00508 { 00509 properties_.erase(it); 00510 return true; 00511 } 00512 ++it; 00513 ++index; 00514 } 00515 return false; 00516 } 00517 00518 template<typename GeometryType, typename TimeType> double 00519 TeBaseSTInstance<GeometryType, TimeType>::operator[](int i) 00520 { 00521 double val = TeMAXFLOAT; 00522 if(i<0 || i>=(int)properties_.size() || properties_[i].empty()) 00523 return val; 00524 val = atof(properties_[i].c_str()); 00525 return val; 00526 } 00527 00528 template<typename GeometryType, typename TimeType> void 00529 TeBaseSTInstance<GeometryType, TimeType>::clear() 00530 { 00531 object_id_ = ""; 00532 slice_ = -1; 00533 properties_.clear(); 00534 unique_id_.clear(); 00535 attrList_.reset(0); 00536 } 00537 00538 00539 template<typename GeometryType, typename TimeType> TeCoord2D 00540 TeBaseSTInstance<GeometryType, TimeType>::getCentroid() 00541 { 00542 return TeFindCentroid(this->getGeometries()); 00543 } 00544 00545 template<typename GeometryType, typename TimeType> double 00546 TeBaseSTInstance<GeometryType, TimeType>::getArea() 00547 { 00548 return TeGeometryArea(this->getGeometries()); 00549 } 00550 00551 00552 #endif