Skip to content

Commit 673d494

Browse files
committed
[quick] Fix identify: give priority to points (and lines)
Without priority for points (and lines) it is otherwise very difficult to identify them on top of polygons or difficult to identify a point on top of a line. Also lowered the default radius from 8mm (which seemed excessively large) to 5mm.
1 parent f95d323 commit 673d494

File tree

2 files changed

+39
-15
lines changed

2 files changed

+39
-15
lines changed

src/quickgui/qgsquickidentifykit.cpp

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

136160
QgsQuickFeatureLayerPair 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

142166
QgsFeatureList QgsQuickIdentifyKit::identifyVectorLayer( QgsVectorLayer *layer, const QgsPointXY &point ) const

src/quickgui/qgsquickidentifykit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ class QUICK_EXPORT QgsQuickIdentifyKit : public QObject
166166
QgsRectangle toLayerCoordinates( QgsMapLayer *layer, const QgsRectangle &rect ) const;
167167
QgsFeatureList identifyVectorLayer( QgsVectorLayer *layer, const QgsPointXY &point ) const;
168168

169-
double mSearchRadiusMm = 8;
169+
double mSearchRadiusMm = 5;
170170
int mFeaturesLimit = 100;
171171
IdentifyMode mIdentifyMode = IdentifyMode::TopDownAll;
172172
};

0 commit comments

Comments
 (0)