osgEarth 2.1.1
Public Types | Public Member Functions | Protected Attributes

osgEarth::Features::CropFilter Class Reference

Inheritance diagram for osgEarth::Features::CropFilter:
Collaboration diagram for osgEarth::Features::CropFilter:

List of all members.

Public Types

enum  Method { METHOD_CENTROID, METHOD_CROPPING }

Public Member Functions

 CropFilter (Method method=METHOD_CENTROID)
optional< Method > & method ()
const optional< Method > & method () const
virtual FilterContext push (FeatureList &input, FilterContext &context)

Protected Attributes

optional< Method_method

Detailed Description

Crops feature geometry to an extent, either by centroid or by actually cutting the geometry.

Definition at line 38 of file CropFilter.


Member Enumeration Documentation

Enumerator:
METHOD_CENTROID 
METHOD_CROPPING 

Definition at line 41 of file CropFilter.

        {
            METHOD_CENTROID,    // include a feature if its centroid is included
            METHOD_CROPPING     // crop a feature's geometry to the target extent
        };

Constructor & Destructor Documentation

CropFilter::CropFilter ( CropFilter::Method  method = METHOD_CENTROID)

Definition at line 27 of file CropFilter.cpp.

                                                :
_method( method )
{
    //nop
}

Member Function Documentation

optional<Method>& osgEarth::Features::CropFilter::method ( ) [inline]

Definition at line 50 of file CropFilter.

{ return _method; }
const optional<Method>& osgEarth::Features::CropFilter::method ( ) const [inline]

Definition at line 51 of file CropFilter.

{ return _method; }
FilterContext CropFilter::push ( FeatureList input,
FilterContext context 
) [virtual]

Implements osgEarth::Features::FeatureFilter.

Definition at line 34 of file CropFilter.cpp.

{
    if ( !context.extent().isSet() )
    {
        OE_WARN << LC << "Extent is not set (and is required)" << std::endl;
        return context;
    }

    const GeoExtent& extent = *context.extent();

    GeoExtent newExtent( extent.getSRS() );

    if ( _method == METHOD_CENTROID )
    {
        for( FeatureList::iterator i = input.begin(); i != input.end();  )
        {
            bool keepFeature = false;

            Feature* feature = i->get();
            Geometry* featureGeom = feature->getGeometry();

            if ( featureGeom && featureGeom->isValid() )
            {
                Bounds bounds = featureGeom->getBounds();
                if ( bounds.isValid() )
                {
                    osg::Vec3d centroid = bounds.center();
                    if ( extent.contains( centroid.x(), centroid.y() ) )
                    {
                        keepFeature = true;
                        newExtent.expandToInclude( bounds );
                    }
                }
            }

            if ( keepFeature )
                ++i;
            else
                i = input.erase( i );
        }
    }

    else // METHOD_CROPPING (requires GEOS)
    {
#ifdef OSGEARTH_HAVE_GEOS

        // create the intersection polygon:
        osg::ref_ptr<Symbology::Polygon> poly;
        
        for( FeatureList::iterator i = input.begin(); i != input.end();  )
        {
            bool keepFeature = false;

            Feature* feature = i->get();
            Symbology::Geometry* featureGeom = feature->getGeometry();
            if ( featureGeom && featureGeom->isValid() )
            {
                // test for trivial acceptance:
                const Bounds bounds = featureGeom->getBounds();
                if ( !bounds.isValid() )
                {
                    //nop
                }

                else if ( extent.contains( bounds ) )
                {
                    keepFeature = true;
                    newExtent.expandToInclude( bounds );
                }

                // then move on to the cropping operation:
                else
                {
                    if ( !poly.valid() )
                    {
                        poly = new Symbology::Polygon();
                        poly->push_back( osg::Vec3d( extent.xMin(), extent.yMin(), 0 ));
                        poly->push_back( osg::Vec3d( extent.xMax(), extent.yMin(), 0 ));
                        poly->push_back( osg::Vec3d( extent.xMax(), extent.yMax(), 0 ));
                        poly->push_back( osg::Vec3d( extent.xMin(), extent.yMax(), 0 ));
                    }

                    osg::ref_ptr<Geometry> croppedGeometry;
                    if ( featureGeom->crop( poly.get(), croppedGeometry ) )
                    {
                        if ( croppedGeometry->isValid() )
                        {
                            feature->setGeometry( croppedGeometry.get() );
                            keepFeature = true;
                            newExtent.expandToInclude( croppedGeometry->getBounds() );
                        }
                    }
                }
            }

            if ( keepFeature )
                ++i;
            else
                i = input.erase( i );
        }  

#else // OSGEARTH_HAVE_GEOS

        OE_WARN << "CropFilter - METHOD_CROPPING not available - please compile osgEarth with GEOS" << std::endl;
        return context;

#endif
    }

    FilterContext newContext = context;
    newContext.extent() = newExtent;

    return newContext;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 57 of file CropFilter.


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