Exemple #1
0
    void onHit(ObjectID id)
    {
        // First see whether it's a feature:
        FeatureIndex* index = Registry::objectIndex()->get<FeatureIndex>( id );
        Feature* feature = index ? index->getFeature( id ) : 0L;

        if ( feature )
        {
            s_fidLabel->setText( Stringify() << "Feature ID = " << feature->getFID() << " (oid = " << id << ")" );
            s_nameLabel->setText( Stringify() << "Name = " << feature->getString("name") );
        }

        else
        {
            // Check whether it's an annotation:
            AnnotationNode* anno = Registry::objectIndex()->get<AnnotationNode>( id );
            if ( anno )
            {
                s_fidLabel->setText( Stringify() << "ObjectID = " << id );
                s_nameLabel->setName( Stringify() << "Name = " << anno->getName() );
            }

            // None of the above.. clear.
            else
            {
                s_fidLabel->setText( Stringify() << "oid = " << id );
                s_nameLabel->setText( "Name = " );
            }
        }

        s_highlightUniform->set( id );
    }
Exemple #2
0
osg::Group*
createLabels( Map* map )
{
    osg::ref_ptr<osg::Group> labels = new osg::Group();

    // first, open up the source shapefile
    OGRFeatureOptions fo;
    fo.url() = g_featureFile;

    osg::ref_ptr<FeatureSource> features = FeatureSourceFactory::create( fo );
    if ( !features.valid() )
    {
        OE_WARN << LC << "Unable to load features!" << std::endl;
        return 0L;
    }

    features->initialize( "" );
    const FeatureProfile* featureProfile = features->getFeatureProfile();
    if ( !featureProfile || !featureProfile->getSRS() )
    {
        OE_WARN << LC << "Feature data has no spatial reference!" << std::endl;
        return 0L;
    }

    osg::ref_ptr<FeatureCursor> cursor = features->createFeatureCursor();
    if ( !cursor.valid() )
    {
        OE_WARN << LC << "Failed to query the feature source!" << std::endl;
        return 0L;
    }

    //SceneControlBin* priorityBin = canvas->getSceneControls();

    unsigned count = 0;

    std::set<std::string> antiDupeSet;

    while( cursor->hasMore() )
    {
        Feature* feature = cursor->nextFeature();
        Geometry* geom = feature->getGeometry();

        if ( !geom )
            continue;

        // we will display the country name:
        std::string text = feature->getString( g_labelAttr );
        if ( text.empty() )
            continue;

        // and use the population to prioritize labels:
        float population = feature->getDouble(g_priorityAttr, 0.0);

        // remove duplicate labels:
        if ( g_removeDupes )
        {
            if ( antiDupeSet.find(text) != antiDupeSet.end() )
                continue;
            antiDupeSet.insert(text);
        }

        // calculate the world location of the label:
        osg::Vec3d centerPoint = geom->getBounds().center();

        osg::Vec3d mapPoint;
        if ( !map->toMapPoint( centerPoint, featureProfile->getSRS(), mapPoint ) )
            continue;

        osg::Vec3d worldPoint;
        if ( !map->mapPointToGeocentricPoint( mapPoint, worldPoint ) )
            continue;

        // create the label and place it:
        osg::MatrixTransform* xform = new osg::MatrixTransform( osg::Matrix::translate(worldPoint) );
        xform->setCullCallback( new CullNodeByNormal(worldPoint) );
        xform->addChild( new ControlNode(new LabelControl(text)) );
        labels->addChild( xform );

        ++count;

        //OE_NOTICE << LC << "Added: " << text << std::endl;
    }

    OE_NOTICE << LC << "Found " << count << " features. " << std::endl;

    return labels.release();
}