osgEarth 2.1.1
Public Member Functions | Protected Member Functions | Private Attributes

OGRFeatureSource Class Reference

Inheritance diagram for OGRFeatureSource:
Collaboration diagram for OGRFeatureSource:

List of all members.

Public Member Functions

 OGRFeatureSource (const OGRFeatureOptions &options)
virtual ~OGRFeatureSource ()
void initialize (const std::string &referenceURI)
const FeatureProfilecreateFeatureProfile ()
FeatureCursorcreateFeatureCursor (const Symbology::Query &query)
virtual bool deleteFeature (FeatureID fid)
virtual int getFeatureCount () const
virtual FeaturegetFeature (FeatureID fid)
virtual bool isWritable () const
const FeatureSchemagetSchema () const
virtual bool insertFeature (Feature *feature)
virtual
osgEarth::Symbology::Geometry::Type 
getGeometryType () const

Protected Member Functions

Symbology::GeometryparseGeometry (const Config &geomConf)
Symbology::GeometryparseGeometryUrl (const std::string &geomUrl)
void initSchema ()

Private Attributes

std::string _source
OGRDataSourceH _dsHandle
OGRLayerH _layerHandle
OGRSFDriverH _ogrDriverHandle
osg::ref_ptr< Symbology::Geometry_geometry
const OGRFeatureOptions _options
int _featureCount
bool _needsSync
bool _writable
FeatureSchema _schema
Geometry::Type _geometryType

Detailed Description

A FeatureSource that reads features from an OGR driver.

This FeatureSource does NOT support styling.

Definition at line 48 of file FeatureSourceOGR.cpp.


Constructor & Destructor Documentation

OGRFeatureSource::OGRFeatureSource ( const OGRFeatureOptions options) [inline]

Definition at line 51 of file FeatureSourceOGR.cpp.

virtual OGRFeatureSource::~OGRFeatureSource ( ) [inline, virtual]

Destruct the object, cleaning up and OGR handles.

Definition at line 68 of file FeatureSourceOGR.cpp.

    {       
        OGR_SCOPED_LOCK;

        if ( _layerHandle )
        {
            if (_needsSync)
            {
                OGR_L_SyncToDisk( _layerHandle ); // for writing only
                const char* name = OGR_FD_GetName( OGR_L_GetLayerDefn( _layerHandle ) );
                std::stringstream buf;
                buf << "REPACK " << name; 
                std::string bufStr;
                bufStr = buf.str();
                OGR_DS_ExecuteSQL( _dsHandle, bufStr.c_str(), 0L, 0L );
            }
            _layerHandle = 0L;
        }

        if ( _dsHandle )
        {
            OGRReleaseDataSource( _dsHandle );
            _dsHandle = 0L;
        }
    }

Member Function Documentation

FeatureCursor* OGRFeatureSource::createFeatureCursor ( const Symbology::Query query) [inline, virtual]

Creates a cursor that iterates over all the features corresponding to the specified query.

Caller takes ownership of the returned object.

Implements osgEarth::Features::FeatureSource.

Definition at line 260 of file FeatureSourceOGR.cpp.

    {
        if ( _geometry.valid() )
        {
            return new GeometryFeatureCursor(
                _geometry.get(),
                getFeatureProfile(),
                _options.filters() );
                //getFilters() );
        }
        else
        {
            OGR_SCOPED_LOCK;

            // Each cursor requires its own DS handle so that multi-threaded access will work.
            // The cursor impl will dispose of the new DS handle.

                OGRDataSourceH dsHandle = OGROpenShared( _source.c_str(), 0, &_ogrDriverHandle );
                if ( dsHandle )
                {
                OGRLayerH layerHandle = OGR_DS_GetLayer( dsHandle, 0 );

                return new FeatureCursorOGR( 
                    dsHandle,
                    layerHandle, 
                    getFeatureProfile(),
                    query, 
                    _options.filters() );
            }
            else
            {
                return 0L;
            }
        }
    }
const FeatureProfile* OGRFeatureSource::createFeatureProfile ( ) [inline, virtual]

Called once at startup to create the profile for this feature set. Successful profile creation implies that the datasource opened succesfully.

Implements osgEarth::Features::FeatureSource.

Definition at line 109 of file FeatureSourceOGR.cpp.

    {
        FeatureProfile* result = 0L;

        // see if we have a custom profile.
        osg::ref_ptr<const Profile> profile;
        if ( _options.profile().isSet() )
        {
            profile = Profile::create( *_options.profile() );
        }

        if ( _geometry.valid() )
        {
            // if the user specified explicit geometry/profile, use that:
            GeoExtent ex;
            if ( profile.valid() )
            {
                ex = profile->getExtent();
            }

            if ( !ex.isValid() )
            {
                // default to WGS84 Lat/Long
                ex = osgEarth::Registry::instance()->getGlobalGeodeticProfile()->getExtent();
            }
            result = new FeatureProfile( ex );
        }

        else if ( !_source.empty() )
        {
            // otherwise, assume we're loading from the URL:
            OGR_SCOPED_LOCK;

            // load up the driver, defaulting to shapefile if unspecified.
            std::string driverName = _options.ogrDriver().value();
            if ( driverName.empty() )
                driverName = "ESRI Shapefile";
            _ogrDriverHandle = OGRGetDriverByName( driverName.c_str() );

            // attempt to open the dataset:
            int openMode = _options.openWrite().isSet() && _options.openWrite().value() ? 1 : 0;

                _dsHandle = OGROpenShared( _source.c_str(), openMode, &_ogrDriverHandle );
                if ( _dsHandle )
                {
                if (openMode == 1) _writable = true;

                        _layerHandle = OGR_DS_GetLayer( _dsHandle, 0 ); // default to layer 0 for now
                if ( _layerHandle )
                {                    
                    GeoExtent extent;

                    // if the user provided a profile, user that:
                    if ( profile.valid() )
                    {
                        result = new FeatureProfile( profile->getExtent() );
                    }

                    else
                    {
                        // extract the SRS and Extent:                
                        OGRSpatialReferenceH srHandle = OGR_L_GetSpatialRef( _layerHandle );
                        if ( srHandle )
                        {
                            osg::ref_ptr<SpatialReference> srs = SpatialReference::createFromHandle( srHandle, false );
                            if ( srs.valid() )
                            {
                                // extract the full extent of the layer:
                                OGREnvelope env;
                                if ( OGR_L_GetExtent( _layerHandle, &env, 1 ) == OGRERR_NONE )
                                {
                                    GeoExtent extent( srs.get(), env.MinX, env.MinY, env.MaxX, env.MaxY );
                                    
                                    // got enough info to make the profile!
                                    result = new FeatureProfile( extent );
                                }
                            }
                        }
                    }

                    // assuming we successfully opened the layer, build a spatial index if requested.
                    if ( _options.buildSpatialIndex() == true )
                    {
                        OE_INFO << LC << "Building spatial index for " << getName() << std::endl;
                        std::stringstream buf;
                        const char* name = OGR_FD_GetName( OGR_L_GetLayerDefn( _layerHandle ) );
                        buf << "CREATE SPATIAL INDEX ON " << name; 
                                            std::string bufStr;
                                            bufStr = buf.str();
                        OGR_DS_ExecuteSQL( _dsHandle, bufStr.c_str(), 0L, 0L );
                    }

                    //Get the feature count
                    _featureCount = OGR_L_GetFeatureCount( _layerHandle, 1 );

                    initSchema();

                    OGRwkbGeometryType wkbType = OGR_FD_GetGeomType( OGR_L_GetLayerDefn( _layerHandle ) );
                    if (
                        wkbType == wkbPolygon ||
                        wkbType == wkbPolygon25D )
                    {
                        _geometryType = Geometry::TYPE_POLYGON;
                    }
                    else if (
                        wkbType == wkbLineString ||
                        wkbType == wkbLineString25D )
                    {
                        _geometryType = Geometry::TYPE_LINESTRING;
                    }
                    else if (
                        wkbType == wkbLinearRing )
                    {
                        _geometryType = Geometry::TYPE_RING;
                    }
                    else if ( 
                        wkbType == wkbPoint ||
                        wkbType == wkbPoint25D )
                    {
                        _geometryType = Geometry::TYPE_POINTSET;
                    }
                    else if (
                        wkbType == wkbGeometryCollection ||
                        wkbType == wkbGeometryCollection25D ||
                        wkbType == wkbMultiPoint ||
                        wkbType == wkbMultiPoint25D ||
                        wkbType == wkbMultiLineString ||
                        wkbType == wkbMultiLineString25D ||
                        wkbType == wkbMultiPolygon ||
                        wkbType == wkbMultiPolygon25D )
                    {
                        _geometryType = Geometry::TYPE_MULTI;
                    }
                }
                }
            else
            {
                OE_INFO << LC << "failed to open dataset \"" << _source << "\"" << std::endl;
            }
        }
        else
        {
            OE_INFO << LC 
                << "Feature Source: no valid source data available" << std::endl;
        }

        return result;
    }

Here is the call graph for this function:

virtual bool OGRFeatureSource::deleteFeature ( FeatureID  fid) [inline, virtual]

Deletes the feature with the given FID

Returns:
True on success; false on failure or if the source is read-only

Reimplemented from osgEarth::Features::FeatureSource.

Definition at line 296 of file FeatureSourceOGR.cpp.

    {
        if (_writable && _layerHandle)
        {
            if (OGR_L_DeleteFeature( _layerHandle, fid ) == OGRERR_NONE)
            {
                _needsSync = true;
                return true;
            }            
        }
        return false;
    }
virtual Feature* OGRFeatureSource::getFeature ( FeatureID  fid) [inline, virtual]

Gets the Feature with the given FID

Returns:
The Feature with the given FID or NULL if not found.

Reimplemented from osgEarth::Features::FeatureSource.

Definition at line 314 of file FeatureSourceOGR.cpp.

    {
        Feature* result = NULL;
        OGRFeatureH handle = OGR_L_GetFeature( _layerHandle, fid);
        if (handle)
        {
            result = OgrUtils::createFeature( handle );
            OGR_F_Destroy( handle );
        }
        return result;
    }

Here is the call graph for this function:

virtual int OGRFeatureSource::getFeatureCount ( ) const [inline, virtual]

Gets the number of features in this FeatureSource

Returns:
The number of features or -1 if the number of features cannot be determined.

Reimplemented from osgEarth::Features::FeatureSource.

Definition at line 309 of file FeatureSourceOGR.cpp.

    {
        return _featureCount;
    }
virtual osgEarth::Symbology::Geometry::Type OGRFeatureSource::getGeometryType ( ) const [inline, virtual]

Gets the Geometry type of the FeatureSource

Returns:
The Geometry type of the FeatureSource

Reimplemented from osgEarth::Features::FeatureSource.

Definition at line 420 of file FeatureSourceOGR.cpp.

    {
        return _geometryType;
    }
const FeatureSchema& OGRFeatureSource::getSchema ( ) const [inline, virtual]

Gets the FeatureSchema for this FeatureSource. If the schema doesn't publish a source, this might be empty.

Reimplemented from osgEarth::Features::FeatureSource.

Definition at line 331 of file FeatureSourceOGR.cpp.

    {
        return _schema;
    } 
void OGRFeatureSource::initialize ( const std::string &  referenceURI) [inline, virtual]

Initialize the FeatureSource. The profile should be computed and set here using setProfile()

Implements osgEarth::Features::FeatureSource.

Definition at line 95 of file FeatureSourceOGR.cpp.

    {
        if ( _options.url().isSet() )
        {
            _source = osgEarth::getFullPath( referenceURI, _options.url()->full() );
        }
        else if ( _options.connection().isSet() )
        {
            _source = _options.connection().value();
        }
    }

Here is the call graph for this function:

void OGRFeatureSource::initSchema ( ) [inline, protected]

Definition at line 445 of file FeatureSourceOGR.cpp.

    {
        OGRFeatureDefnH layerDef =  OGR_L_GetLayerDefn( _layerHandle );
        for (int i = 0; i < OGR_FD_GetFieldCount( layerDef ); i++)
        {
            OGRFieldDefnH fieldDef = OGR_FD_GetFieldDefn( layerDef, i );
            std::string name;
            name = std::string( OGR_Fld_GetNameRef( fieldDef ) );
            OGRFieldType ogrType = OGR_Fld_GetType( fieldDef );
            _schema[ name ] = OgrUtils::getAttributeType( ogrType );
        }
    }

Here is the call graph for this function:

virtual bool OGRFeatureSource::insertFeature ( Feature feature) [inline, virtual]

Inserts the given feature into the FeatureSource

Returns:
True if the feature was inserted, false if not

Reimplemented from osgEarth::Features::FeatureSource.

Definition at line 336 of file FeatureSourceOGR.cpp.

    {
        OGR_SCOPED_LOCK;
        OGRFeatureH feature_handle = OGR_F_Create( OGR_L_GetLayerDefn( _layerHandle ) );
        if ( feature_handle )
        {
            const AttributeTable& attrs = feature->getAttrs();

            // assign the attributes:
            int num_fields = OGR_F_GetFieldCount( feature_handle );
            for( int i=0; i<num_fields; i++ )
            {
                OGRFieldDefnH field_handle_ref = OGR_F_GetFieldDefnRef( feature_handle, i );
                std::string name = OGR_Fld_GetNameRef( field_handle_ref );
                int field_index = OGR_F_GetFieldIndex( feature_handle, name.c_str() );

                AttributeTable::const_iterator a = attrs.find( toLower(name) );
                if ( a != attrs.end() )
                {
                    switch( OGR_Fld_GetType(field_handle_ref) )
                    {
                    case OFTInteger:
                        OGR_F_SetFieldInteger( feature_handle, field_index, a->second.getInt(0) );
                        break;
                    case OFTReal:
                        OGR_F_SetFieldDouble( feature_handle, field_index, a->second.getDouble(0.0) );
                        break;
                    case OFTString:
                        OGR_F_SetFieldString( feature_handle, field_index, a->second.getString().c_str() );
                        break;
                    }
                }
            }

            //    std::string value = feature->getAttr( name );
            //    if (!value.empty())
            //    {
            //        switch( OGR_Fld_GetType( field_handle_ref ) )
            //        {
            //        case OFTInteger:
            //            OGR_F_SetFieldInteger( feature_handle, field_index, as<int>(value, 0) );
            //            break;
            //        case OFTReal:
            //            OGR_F_SetFieldDouble( feature_handle, field_index, as<double>(value, 0.0) );
            //            break;
            //        case OFTString:
            //            OGR_F_SetFieldString( feature_handle, field_index, value.c_str() );
            //            break;                    
            //        }
            //    }
            //}

            // assign the geometry:
            OGRFeatureDefnH def = ::OGR_L_GetLayerDefn( _layerHandle );

            OGRwkbGeometryType reported_type = OGR_FD_GetGeomType( def );

            OGRGeometryH ogr_geometry = OgrUtils::createOgrGeometry( feature->getGeometry(), reported_type );
            if ( OGR_F_SetGeometryDirectly( feature_handle, ogr_geometry ) != OGRERR_NONE )
            {
                OE_WARN << LC << "OGR_F_SetGeometryDirectly failed!" << std::endl;
            }

            if ( OGR_L_CreateFeature( _layerHandle, feature_handle ) != OGRERR_NONE )
            {
                //TODO: handle error better
                OE_WARN << LC << "OGR_L_CreateFeature failed!" << std::endl;
                OGR_F_Destroy( feature_handle );
                return false;
            }

            // clean up the feature
            OGR_F_Destroy( feature_handle );
        }
        else
        {
            //TODO: handle error better
            OE_WARN << LC << "OGR_F_Create failed." << std::endl;
            return false;
        }

        return true;
    }

Here is the call graph for this function:

virtual bool OGRFeatureSource::isWritable ( ) const [inline, virtual]

Whether this FeatureSource supports inserting and deleting features

Reimplemented from osgEarth::Features::FeatureSource.

Definition at line 326 of file FeatureSourceOGR.cpp.

    {
        return _writable;
    }
Symbology::Geometry* OGRFeatureSource::parseGeometry ( const Config geomConf) [inline, protected]

Definition at line 428 of file FeatureSourceOGR.cpp.

    {
        return OgrUtils::createGeometryFromWKT( geomConf.value() );
    }

Here is the call graph for this function:

Symbology::Geometry* OGRFeatureSource::parseGeometryUrl ( const std::string &  geomUrl) [inline, protected]

Definition at line 434 of file FeatureSourceOGR.cpp.

    {
        std::string wkt;
        if ( HTTPClient::readString( geomUrl, wkt ) == HTTPClient::RESULT_OK )
        {
            Config conf( "geometry", wkt );
            return parseGeometry( conf );
        }
        return 0L;
    }

Here is the call graph for this function:


Member Data Documentation

OGRDataSourceH OGRFeatureSource::_dsHandle [private]

Definition at line 464 of file FeatureSourceOGR.cpp.

Definition at line 469 of file FeatureSourceOGR.cpp.

Definition at line 467 of file FeatureSourceOGR.cpp.

Geometry::Type OGRFeatureSource::_geometryType [private]

Definition at line 473 of file FeatureSourceOGR.cpp.

OGRLayerH OGRFeatureSource::_layerHandle [private]

Definition at line 465 of file FeatureSourceOGR.cpp.

Definition at line 470 of file FeatureSourceOGR.cpp.

OGRSFDriverH OGRFeatureSource::_ogrDriverHandle [private]

Definition at line 466 of file FeatureSourceOGR.cpp.

Reimplemented from osgEarth::Features::FeatureSource.

Definition at line 468 of file FeatureSourceOGR.cpp.

Definition at line 472 of file FeatureSourceOGR.cpp.

std::string OGRFeatureSource::_source [private]

Definition at line 463 of file FeatureSourceOGR.cpp.

Definition at line 471 of file FeatureSourceOGR.cpp.


The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines