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

FeatureCursorOGR Class Reference

Inheritance diagram for FeatureCursorOGR:
Collaboration diagram for FeatureCursorOGR:

List of all members.

Public Member Functions

 FeatureCursorOGR (OGRLayerH dsHandle, OGRLayerH layerHandle, const FeatureProfile *profile, const Symbology::Query &query, const FeatureFilterList &filters)
bool hasMore () const
FeaturenextFeature ()

Protected Member Functions

virtual ~FeatureCursorOGR ()

Private Member Functions

void readChunk ()

Private Attributes

OGRDataSourceH _dsHandle
OGRLayerH _layerHandle
OGRLayerH _resultSetHandle
OGRGeometryH _spatialFilter
Symbology::Query _query
int _chunkSize
OGRFeatureH _nextHandleToQueue
osg::ref_ptr< const
FeatureProfile
_profile
std::queue< osg::ref_ptr
< Feature > > 
_queue
osg::ref_ptr< Feature_lastFeatureReturned
const FeatureFilterList_filters

Detailed Description

Definition at line 32 of file FeatureCursorOGR.


Constructor & Destructor Documentation

FeatureCursorOGR::FeatureCursorOGR ( OGRLayerH  dsHandle,
OGRLayerH  layerHandle,
const FeatureProfile profile,
const Symbology::Query query,
const FeatureFilterList filters 
)

Creates a new feature cursor that iterates over an OGR layer.

Parameters:
dsHandleHandle on the OGR data source to which the results layer belongs
layerHandleHandle to the OGR layer containing the features
profileProfile of the feature layer corresponding to the feature data
queryThe the query from which this cursor was created.

Definition at line 31 of file FeatureCursorOGR.cpp.

                                                                      :
_dsHandle( dsHandle ),
_layerHandle( layerHandle ),
_resultSetHandle( 0L ),
_spatialFilter( 0L ),
_query( query ),
_chunkSize( 500 ),
_nextHandleToQueue( 0L ),
_profile( profile ),
_filters( filters )
{
    //_resultSetHandle = _layerHandle;
    {
        OGR_SCOPED_LOCK;

        std::string expr;
        std::string from = OGR_FD_GetName( OGR_L_GetLayerDefn( _layerHandle ));
        from = std::string("'") + from + std::string("'");

        if ( query.expression().isSet() )
        {
            // build the SQL: allow the Query to include either a full SQL statement or
            // just the WHERE clause.
            expr = query.expression().value();

            // if the expression is just a where clause, expand it into a complete SQL expression.
            std::string temp = expr;
            std::transform( temp.begin(), temp.end(), temp.begin(), ::tolower );
            //bool complete = temp.find( "select" ) == 0;
            if ( temp.find( "select" ) != 0 )
            {
                std::stringstream buf;
                buf << "SELECT * FROM " << from << " WHERE " << expr;
                std::string bufStr;
                bufStr = buf.str();
                expr = bufStr;
            }
        }
        else
        {
            std::stringstream buf;
            buf << "SELECT * FROM " << from;
            expr = buf.str();
        }

        // if there's a spatial extent in the query, build the spatial filter:
        if ( query.bounds().isSet() )
        {
            OGRGeometryH ring = OGR_G_CreateGeometry( wkbLinearRing );
            OGR_G_AddPoint(ring, query.bounds()->xMin(), query.bounds()->yMin(), 0 );
            OGR_G_AddPoint(ring, query.bounds()->xMin(), query.bounds()->yMax(), 0 );
            OGR_G_AddPoint(ring, query.bounds()->xMax(), query.bounds()->yMax(), 0 );
            OGR_G_AddPoint(ring, query.bounds()->xMax(), query.bounds()->yMin(), 0 );
            OGR_G_AddPoint(ring, query.bounds()->xMin(), query.bounds()->yMin(), 0 );

            _spatialFilter = OGR_G_CreateGeometry( wkbPolygon );
            OGR_G_AddGeometryDirectly( _spatialFilter, ring ); 
            // note: "Directly" above means _spatialFilter takes ownership if ring handle
        }


        _resultSetHandle = OGR_DS_ExecuteSQL( _dsHandle, expr.c_str(), _spatialFilter, 0L );

        if ( _resultSetHandle )
        {
            OGR_L_ResetReading( _resultSetHandle );
        }
    }

    readChunk();
}

Here is the call graph for this function:

FeatureCursorOGR::~FeatureCursorOGR ( ) [protected, virtual]

Definition at line 107 of file FeatureCursorOGR.cpp.

{
    OGR_SCOPED_LOCK;

    if ( _nextHandleToQueue )
        OGR_F_Destroy( _nextHandleToQueue );

    if ( _resultSetHandle != _layerHandle )
        OGR_DS_ReleaseResultSet( _dsHandle, _resultSetHandle );

    if ( _spatialFilter )
        OGR_G_DestroyGeometry( _spatialFilter );

    if ( _dsHandle )
        OGRReleaseDataSource( _dsHandle );
}

Member Function Documentation

bool FeatureCursorOGR::hasMore ( ) const [virtual]

Implements osgEarth::Features::FeatureCursor.

Definition at line 125 of file FeatureCursorOGR.cpp.

{
    return _resultSetHandle && ( _queue.size() > 0 || _nextHandleToQueue != 0L );
}

Here is the caller graph for this function:

Feature * FeatureCursorOGR::nextFeature ( ) [virtual]

Implements osgEarth::Features::FeatureCursor.

Definition at line 131 of file FeatureCursorOGR.cpp.

{
    if ( !hasMore() )
        return 0L;

    if ( _queue.size() == 0 && _nextHandleToQueue )
        readChunk();

    // do this in order to hold a reference to the feature we return, so the caller
    // doesn't have to. This lets us avoid requiring the caller to use a ref_ptr when 
    // simply iterating over the cursor, making the cursor move conventient to use.
    _lastFeatureReturned = _queue.front();
    _queue.pop();

    return _lastFeatureReturned.get();
}

Here is the call graph for this function:

void FeatureCursorOGR::readChunk ( ) [private]

Definition at line 152 of file FeatureCursorOGR.cpp.

{
    if ( !_resultSetHandle )
        return;
    
    FeatureList preProcessList;
    
    OGR_SCOPED_LOCK;

    if ( _nextHandleToQueue )
    {
        Feature* f = OgrUtils::createFeature( _nextHandleToQueue );
        if ( f ) 
        {
            _queue.push( f );
            
            if ( _filters.size() > 0 )
                preProcessList.push_back( f );
        }
        OGR_F_Destroy( _nextHandleToQueue );
        _nextHandleToQueue = 0L;
    }

    int handlesToQueue = _chunkSize - _queue.size();

    for( int i=0; i<handlesToQueue; i++ )
    {
        OGRFeatureH handle = OGR_L_GetNextFeature( _resultSetHandle );
        if ( handle )
        {
            Feature* f = OgrUtils::createFeature( handle );
            if ( f ) 
            {
                _queue.push( f );

                if ( _filters.size() > 0 )
                    preProcessList.push_back( f );
            }
            OGR_F_Destroy( handle );
        }
        else
            break;
    }

    // preprocess the features using the filter list:
    if ( preProcessList.size() > 0 )
    {
        FilterContext cx;
        cx.profile() = _profile.get();

        for( FeatureFilterList::const_iterator i = _filters.begin(); i != _filters.end(); ++i )
        {
            FeatureFilter* filter = i->get();
            cx = filter->push( preProcessList, cx );
        }
    }

    // read one more for "more" detection:
    _nextHandleToQueue = OGR_L_GetNextFeature( _resultSetHandle );

    //OE_NOTICE << "read " << _queue.size() << " features ... " << std::endl;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 68 of file FeatureCursorOGR.

OGRDataSourceH FeatureCursorOGR::_dsHandle [private]

Definition at line 63 of file FeatureCursorOGR.

Definition at line 73 of file FeatureCursorOGR.

Definition at line 72 of file FeatureCursorOGR.

OGRLayerH FeatureCursorOGR::_layerHandle [private]

Definition at line 64 of file FeatureCursorOGR.

OGRFeatureH FeatureCursorOGR::_nextHandleToQueue [private]

Definition at line 69 of file FeatureCursorOGR.

osg::ref_ptr<const FeatureProfile> FeatureCursorOGR::_profile [private]

Definition at line 70 of file FeatureCursorOGR.

Definition at line 67 of file FeatureCursorOGR.

std::queue< osg::ref_ptr<Feature> > FeatureCursorOGR::_queue [private]

Definition at line 71 of file FeatureCursorOGR.

Definition at line 65 of file FeatureCursorOGR.

OGRGeometryH FeatureCursorOGR::_spatialFilter [private]

Definition at line 66 of file FeatureCursorOGR.


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