/
qgsglobefeatureidentify.cpp
86 lines (75 loc) · 3.04 KB
/
qgsglobefeatureidentify.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/***************************************************************************
qgsglobefeatureidentify.cpp
--------------------------------------
Date : 27.10.2013
Copyright : (C) 2013 Matthias Kuhn
Email : matthias dot kuhn at gmx dot ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsglobefeatureidentify.h"
#include "qgsmapcanvas.h"
#include <qgsmaplayerregistry.h>
#include <qgsrubberband.h>
#include <qgsvectorlayer.h>
#include "featuresource/qgsglobefeaturesource.h"
#include <osg/ValueObject>
#include <osgEarth/Registry>
QgsGlobeFeatureIdentifyCallback::QgsGlobeFeatureIdentifyCallback( QgsMapCanvas* mapCanvas )
: mCanvas( mapCanvas ), mRubberBand( new QgsRubberBand( mapCanvas, QGis::Polygon ) )
{
QColor color( Qt::green );
color.setAlpha( 190 );
mRubberBand->setColor( color );
}
QgsGlobeFeatureIdentifyCallback::~QgsGlobeFeatureIdentifyCallback()
{
mCanvas->scene()->removeItem( mRubberBand );
delete mRubberBand;
}
#if OSGEARTH_VERSION_LESS_THAN(2, 7, 0)
void QgsGlobeFeatureIdentifyCallback::onHit( osgEarth::Features::FeatureSourceIndexNode* index, osgEarth::Features::FeatureID fid, const EventArgs& /*args*/ )
{
QgsGlobeFeatureSource* globeSource = dynamic_cast<QgsGlobeFeatureSource*>( index->getFeatureSource() );
if ( globeSource )
{
QgsVectorLayer* lyr = globeSource->layer();
#else
void QgsGlobeFeatureIdentifyCallback::onHit( osgEarth::ObjectID id )
{
osgEarth::Features::FeatureIndex* index = osgEarth::Registry::objectIndex()->get<osgEarth::Features::FeatureIndex>( id );
osgEarth::Features::Feature* feature = index->getFeature( id );
osgEarth::Features::FeatureID fid = feature->getFID();
std::string layerId;
if ( feature->getUserValue( "qgisLayerId", layerId ) )
{
QgsVectorLayer* lyr = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( QString::fromStdString( layerId ) ) );
#endif
if ( lyr )
{
QgsFeature feat;
lyr->getFeatures( QgsFeatureRequest().setFilterFid( fid ) ).nextFeature( feat );
if ( feat.isValid() )
mRubberBand->setToGeometry( feat.geometry(), lyr );
else
mRubberBand->reset( QGis::Polygon );
}
}
else
{
QgsDebugMsg( "Clicked feature was not on a QGIS layer" );
}
}
#if OSGEARTH_VERSION_LESS_THAN(2, 7, 0)
void QgsGlobeFeatureIdentifyCallback::onMiss( const EventArgs &/*args*/ )
#else
void QgsGlobeFeatureIdentifyCallback::onMiss()
#endif
{
mRubberBand->reset( QGis::Polygon );
}