Skip to content
Permalink
Browse files

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
  • Loading branch information
3nids committed Oct 25, 2018
1 parent 0959e9f commit edc03030efc9e5286df14e4b79629d98701421c2
Showing with 32 additions and 1 deletion.
  1. +32 −1 src/gui/qgsmapcanvas.cpp
@@ -990,8 +990,39 @@ void QgsMapCanvas::zoomToSelected( QgsVectorLayer *layer )
}

rect = mapSettings().layerExtentToOutputExtent( layer, rect );

// zoom in if point cannot be distinguish from others
// also check that rect is empty, as it might not in case of multi points
if ( layer->geometryType() == QgsWkbTypes::PointGeometry && rect.isEmpty() )
{
QgsPointXY center = mSettings.mapToLayerCoordinates( layer, rect.center() );
QgsRectangle extentRect = mSettings.mapToLayerCoordinates( layer, extent() );
QgsFeatureRequest req = QgsFeatureRequest().setFilterRect( extentRect ).setLimit( 1000 ).setNoAttributes();
QgsFeatureIterator fit = layer->getFeatures( req );
QgsFeature f;
QgsPointXY closestPoint;
double closestSquaredDistance = extentRect.width() + extentRect.height();
bool pointFound = false;
while ( fit.nextFeature( f ) )
{
QgsPointXY point = f.geometry().asPoint();
double sqrDist = point.sqrDist( center );
if ( sqrDist > closestSquaredDistance || sqrDist < 4 * std::numeric_limits<double>::epsilon() )
continue;
pointFound = true;
closestPoint = point;
closestSquaredDistance = sqrDist;
}
if ( pointFound )
{
// combine selected point with closest point and scale this rect
rect.combineExtentWith( mSettings.layerToMapCoordinates( layer, closestPoint ) );
rect.scale( 5, &center );
}
}

zoomToFeatureExtent( rect );
} // zoomToSelected
}

void QgsMapCanvas::zoomToFeatureExtent( QgsRectangle &rect )
{

0 comments on commit edc0303

Please sign in to comment.
You can’t perform that action at this time.