@@ -93,13 +93,13 @@ QgsQuickFeatureLayerPairs QgsQuickIdentifyKit::identify( const QPointF &point, Q
9393 return results;
9494}
9595
96- static QgsQuickFeatureLayerPair _closestFeature ( const QgsQuickFeatureLayerPairs &results, const QgsMapSettings &mapSettings, const QPointF &point )
96+ static QgsQuickFeatureLayerPair _closestFeature ( const QgsQuickFeatureLayerPairs &results, const QgsMapSettings &mapSettings, const QPointF &point, double searchRadius )
9797{
9898 QgsPointXY mapPoint = mapSettings.mapToPixel ().toMapCoordinates ( point.toPoint () );
9999 QgsGeometry mapPointGeom ( QgsGeometry::fromPointXY ( mapPoint ) );
100100
101- double distMin = 1e10 ;
102- int iMin = -1 ;
101+ double distMinPoint = 1e10 , distMinLine = 1e10 , distMinPolygon = 1e10 ;
102+ int iMinPoint = - 1 , iMinLine = - 1 , iMinPolygon = -1 ;
103103 for ( int i = 0 ; i < results.count (); ++i )
104104 {
105105 const QgsQuickFeatureLayerPair &res = results.at ( i );
@@ -116,27 +116,51 @@ static QgsQuickFeatureLayerPair _closestFeature( const QgsQuickFeatureLayerPairs
116116 }
117117
118118 double dist = geom.distance ( mapPointGeom );
119- if ( dist < distMin )
119+ QgsWkbTypes::GeometryType type = QgsWkbTypes::geometryType ( geom.wkbType () );
120+ if ( type == QgsWkbTypes::PointGeometry )
120121 {
121- iMin = i;
122- distMin = dist;
122+ if ( dist < distMinPoint )
123+ {
124+ iMinPoint = i;
125+ distMinPoint = dist;
126+ }
127+ }
128+ else if ( type == QgsWkbTypes::LineGeometry )
129+ {
130+ if ( dist < distMinLine )
131+ {
132+ iMinLine = i;
133+ distMinLine = dist;
134+ }
135+ }
136+ else // polygons
137+ {
138+ if ( dist < distMinPolygon )
139+ {
140+ iMinPolygon = i;
141+ distMinPolygon = dist;
142+ }
123143 }
124144 }
125145
126- if ( results.empty () )
127- {
128- return QgsQuickFeatureLayerPair ();
129- }
146+ // we give priority to points, then lines, then polygons
147+ // the rationale is that points in polygon (or on a line) would have nearly
148+ // always non-zero distance while polygon surrounding it has zero distance,
149+ // so it would be difficult to identify it
150+ if ( iMinPoint != -1 && distMinPoint <= searchRadius )
151+ return results.at ( iMinPoint );
152+ else if ( iMinLine != -1 && distMinLine <= searchRadius )
153+ return results.at ( iMinLine );
154+ else if ( iMinPolygon != -1 )
155+ return results.at ( iMinPolygon );
130156 else
131- {
132- return results.at ( iMin );
133- }
157+ return QgsQuickFeatureLayerPair ();
134158}
135159
136160QgsQuickFeatureLayerPair QgsQuickIdentifyKit::identifyOne ( const QPointF &point, QgsVectorLayer *layer )
137161{
138162 QgsQuickFeatureLayerPairs results = identify ( point, layer );
139- return _closestFeature ( results, mMapSettings ->mapSettings (), point );
163+ return _closestFeature ( results, mMapSettings ->mapSettings (), point, searchRadiusMU () );
140164}
141165
142166QgsFeatureList QgsQuickIdentifyKit::identifyVectorLayer ( QgsVectorLayer *layer, const QgsPointXY &point ) const
0 commit comments