Skip to content

Commit edc0303

Browse files
committed
Properly zoom to feature for single point selected
fix #9160 this will fetch the canvas content and combine selected point with closest point and scale it 5 times
1 parent 0959e9f commit edc0303

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

src/gui/qgsmapcanvas.cpp

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -990,8 +990,39 @@ void QgsMapCanvas::zoomToSelected( QgsVectorLayer *layer )
990990
}
991991

992992
rect = mapSettings().layerExtentToOutputExtent( layer, rect );
993+
994+
// zoom in if point cannot be distinguish from others
995+
// also check that rect is empty, as it might not in case of multi points
996+
if ( layer->geometryType() == QgsWkbTypes::PointGeometry && rect.isEmpty() )
997+
{
998+
QgsPointXY center = mSettings.mapToLayerCoordinates( layer, rect.center() );
999+
QgsRectangle extentRect = mSettings.mapToLayerCoordinates( layer, extent() );
1000+
QgsFeatureRequest req = QgsFeatureRequest().setFilterRect( extentRect ).setLimit( 1000 ).setNoAttributes();
1001+
QgsFeatureIterator fit = layer->getFeatures( req );
1002+
QgsFeature f;
1003+
QgsPointXY closestPoint;
1004+
double closestSquaredDistance = extentRect.width() + extentRect.height();
1005+
bool pointFound = false;
1006+
while ( fit.nextFeature( f ) )
1007+
{
1008+
QgsPointXY point = f.geometry().asPoint();
1009+
double sqrDist = point.sqrDist( center );
1010+
if ( sqrDist > closestSquaredDistance || sqrDist < 4 * std::numeric_limits<double>::epsilon() )
1011+
continue;
1012+
pointFound = true;
1013+
closestPoint = point;
1014+
closestSquaredDistance = sqrDist;
1015+
}
1016+
if ( pointFound )
1017+
{
1018+
// combine selected point with closest point and scale this rect
1019+
rect.combineExtentWith( mSettings.layerToMapCoordinates( layer, closestPoint ) );
1020+
rect.scale( 5, &center );
1021+
}
1022+
}
1023+
9931024
zoomToFeatureExtent( rect );
994-
} // zoomToSelected
1025+
}
9951026

9961027
void QgsMapCanvas::zoomToFeatureExtent( QgsRectangle &rect )
9971028
{

0 commit comments

Comments
 (0)