osgEarth 2.1.1

/home/cube/sources/osgearth/src/osgEarthFeatures/Feature.cpp

Go to the documentation of this file.
00001 /* -*-c++-*- */
00002 /* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
00003  * Copyright 2008-2010 Pelican Mapping
00004  * http://osgearth.org
00005  *
00006  * osgEarth is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU Lesser General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public License
00017  * along with this program.  If not, see <http://www.gnu.org/licenses/>
00018  */
00019 #include <osgEarthFeatures/Feature>
00020 #include <algorithm>
00021 
00022 using namespace osgEarth;
00023 using namespace osgEarth::Features;
00024 using namespace osgEarth::Symbology;
00025 
00026 static
00027 std::string EMPTY_STRING;
00028 
00029 //----------------------------------------------------------------------------
00030 
00031 FeatureProfile::FeatureProfile( const GeoExtent& extent ) :
00032 _extent( extent ),
00033 _firstLevel(0),
00034 _maxLevel(-1),
00035 _tiled(false)
00036 {
00037     //nop
00038 }
00039 
00040 bool
00041 FeatureProfile::getTiled() const
00042 {
00043     return _tiled;
00044 }
00045 
00046 void
00047 FeatureProfile::setTiled(bool tiled)
00048 {
00049     _tiled = true;
00050 }
00051 
00052 int
00053 FeatureProfile::getFirstLevel() const
00054 {
00055     return _firstLevel;
00056 }
00057 
00058 void
00059 FeatureProfile::setFirstLevel(int firstLevel )
00060 {
00061     _firstLevel = firstLevel;
00062 }
00063 
00064 int
00065 FeatureProfile::getMaxLevel() const
00066 {
00067     return _maxLevel;
00068 }
00069 
00070 void
00071 FeatureProfile::setMaxLevel(int maxLevel)
00072 {
00073     _maxLevel = maxLevel;
00074 }
00075 
00076 const osgEarth::Profile* 
00077 FeatureProfile::getProfile() const
00078 {
00079     return _profile.get();
00080 }
00081 
00082 void
00083 FeatureProfile::setProfile( const osgEarth::Profile* profile )
00084 {
00085     _profile = profile;
00086 }
00087 
00088 //----------------------------------------------------------------------------
00089 
00090 std::string
00091 AttributeValue::getString() const
00092 {
00093     switch( first ) {
00094         case ATTRTYPE_STRING: return second.stringValue;
00095         case ATTRTYPE_DOUBLE: return osgEarth::toString(second.doubleValue);
00096         case ATTRTYPE_INT:    return osgEarth::toString(second.intValue);
00097         case ATTRTYPE_BOOL:   return osgEarth::toString(second.boolValue);
00098     }
00099     return EMPTY_STRING;
00100 }
00101 
00102 double
00103 AttributeValue::getDouble( double defaultValue ) const 
00104 {
00105     switch( first ) {
00106         case ATTRTYPE_STRING: return osgEarth::as<double>(second.stringValue, defaultValue);
00107         case ATTRTYPE_DOUBLE: return second.doubleValue;
00108         case ATTRTYPE_INT:    return (double)second.intValue;
00109         case ATTRTYPE_BOOL:   return second.boolValue? 1.0 : 0.0;
00110     }
00111     return defaultValue;
00112 }
00113 
00114 int
00115 AttributeValue::getInt( int defaultValue ) const 
00116 {
00117     switch( first ) {
00118         case ATTRTYPE_STRING: return osgEarth::as<int>(second.stringValue, defaultValue);
00119         case ATTRTYPE_DOUBLE: return (int)second.doubleValue;
00120         case ATTRTYPE_INT:    return second.intValue;
00121         case ATTRTYPE_BOOL:   return second.boolValue? 1 : 0;
00122     }
00123     return defaultValue;
00124 }
00125 
00126 bool
00127 AttributeValue::getBool( bool defaultValue ) const 
00128 {
00129     switch( first ) {
00130         case ATTRTYPE_STRING: return osgEarth::as<bool>(second.stringValue, defaultValue);
00131         case ATTRTYPE_DOUBLE: return second.doubleValue != 0.0;
00132         case ATTRTYPE_INT:    return second.intValue != 0;
00133         case ATTRTYPE_BOOL:   return second.boolValue;
00134     }
00135     return defaultValue;
00136 }
00137 
00138 //----------------------------------------------------------------------------
00139 
00140 Feature::Feature( FeatureID fid ) :
00141 _fid( fid )
00142 {
00143     //NOP
00144 }
00145 
00146 Feature::Feature( Geometry* geom, const Style& style, FeatureID fid ) :
00147 _geom ( geom ),
00148 _fid  ( fid )
00149 {
00150     if ( !style.empty() )
00151         _style = style;
00152 }
00153 
00154 Feature::Feature( const Feature& rhs, const osg::CopyOp& copyOp ) :
00155 _fid      ( rhs._fid ),
00156 _attrs    ( rhs._attrs ),
00157 _style    ( rhs._style ),
00158 _geoInterp( rhs._geoInterp )
00159 {
00160     if ( rhs._geom.valid() )
00161         //_geom = dynamic_cast<Geometry*>( copyOp( rhs._geom.get() ) );
00162         _geom = rhs._geom->clone();
00163 }
00164 
00165 FeatureID
00166 Feature::getFID() const 
00167 {
00168     return _fid;
00169 }
00170 
00171 void
00172 Feature::set( const std::string& name, const std::string& value )
00173 {
00174     AttributeValue& a = _attrs[name];
00175     a.first = ATTRTYPE_STRING;
00176     a.second.stringValue = value;
00177 }
00178 
00179 void
00180 Feature::set( const std::string& name, double value )
00181 {
00182     AttributeValue& a = _attrs[name];
00183     a.first = ATTRTYPE_DOUBLE;
00184     a.second.doubleValue = value;
00185 }
00186 
00187 void
00188 Feature::set( const std::string& name, int value )
00189 {
00190     AttributeValue& a = _attrs[name];
00191     a.first = ATTRTYPE_INT;
00192     a.second.intValue = value;
00193 }
00194 
00195 void
00196 Feature::set( const std::string& name, bool value )
00197 {
00198     AttributeValue& a = _attrs[name];
00199     a.first = ATTRTYPE_BOOL;
00200     a.second.boolValue = value;
00201 }
00202 
00203 bool
00204 Feature::hasAttr( const std::string& name ) const
00205 {
00206     return _attrs.find(toLower(name)) != _attrs.end();
00207 }
00208 
00209 std::string
00210 Feature::getString( const std::string& name ) const
00211 {
00212     AttributeTable::const_iterator i = _attrs.find(toLower(name));
00213     return i != _attrs.end()? i->second.getString() : EMPTY_STRING;
00214 }
00215 
00216 double
00217 Feature::getDouble( const std::string& name, double defaultValue ) const 
00218 {
00219     AttributeTable::const_iterator i = _attrs.find(toLower(name));
00220     return i != _attrs.end()? i->second.getDouble(defaultValue) : defaultValue;
00221 }
00222 
00223 int
00224 Feature::getInt( const std::string& name, int defaultValue ) const 
00225 {
00226     AttributeTable::const_iterator i = _attrs.find(toLower(name));
00227     return i != _attrs.end()? i->second.getInt(defaultValue) : defaultValue;
00228 }
00229 
00230 bool
00231 Feature::getBool( const std::string& name, bool defaultValue ) const 
00232 {
00233     AttributeTable::const_iterator i = _attrs.find(toLower(name));
00234     return i != _attrs.end()? i->second.getBool(defaultValue) : defaultValue;
00235 }
00236 
00237 double
00238 Feature::eval( NumericExpression& expr ) const
00239 {
00240     const NumericExpression::Variables& vars = expr.variables();
00241     for( NumericExpression::Variables::const_iterator i = vars.begin(); i != vars.end(); ++i )
00242         expr.set( *i, getDouble(i->first, 0.0)); //osgEarth::as<double>(getAttr(i->first),0.0) );
00243     return expr.eval();
00244 }
00245 
00246 const std::string&
00247 Feature::eval( StringExpression& expr ) const
00248 {
00249     const StringExpression::Variables& vars = expr.variables();
00250     for( StringExpression::Variables::const_iterator i = vars.begin(); i != vars.end(); ++i )
00251         expr.set( *i, getString(i->first) ); //getAttr(i->first) );
00252     return expr.eval();
00253 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines