Skip to content
Permalink
Browse files

move the optimal extent calculation for a point layer to its own method

  • Loading branch information
3nids committed Sep 21, 2020
1 parent dd19040 commit b47346ba20408561cd71016c58b9f21190b22891
Showing with 41 additions and 25 deletions.
  1. +33 −25 src/gui/qgsmapcanvas.cpp
  2. +8 −0 src/gui/qgsmapcanvas.h
@@ -1240,33 +1240,13 @@ void QgsMapCanvas::clearExtentHistory()
emit zoomNextStatusChanged( mLastExtentIndex < mLastExtent.size() - 1 );
}// clearExtentHistory

void QgsMapCanvas::zoomToSelected( QgsVectorLayer *layer )
QgsRectangle QgsMapCanvas::optimalExtentForPointLayer( QgsVectorLayer *layer, const QgsPointXY &center, int scaleFactor )
{
if ( !layer )
{
// use current layer by default
layer = qobject_cast<QgsVectorLayer *>( mCurrentLayer );
}

if ( !layer || !layer->isSpatial() || layer->selectedFeatureCount() == 0 )
return;

QgsRectangle rect = layer->boundingBoxOfSelected();
if ( rect.isNull() )
{
emit messageEmitted( tr( "Cannot zoom to selected feature(s)" ), tr( "No extent could be determined." ), Qgis::Warning );
return;
}

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

// zoom in if point cannot be distinguished from others
// also check that rect is empty, as it might not in case of multi points
if ( layer->geometryType() == QgsWkbTypes::PointGeometry && rect.isEmpty() )
if ( layer->geometryType() == QgsWkbTypes::PointGeometry )
{
int scaleFactor = 5;
QgsPointXY centerMapCoordinates = rect.center();
QgsPointXY centerLayerCoordinates = mSettings.mapToLayerCoordinates( layer, centerMapCoordinates );
QgsPointXY centerLayerCoordinates = mSettings.mapToLayerCoordinates( layer, center );
QgsRectangle extentRect = mSettings.mapToLayerCoordinates( layer, extent() ).scaled( 1.0 / scaleFactor, &centerLayerCoordinates );
QgsFeatureRequest req = QgsFeatureRequest().setFilterRect( extentRect ).setLimit( 1000 ).setNoAttributes();
QgsFeatureIterator fit = layer->getFeatures( req );
@@ -1288,10 +1268,38 @@ void QgsMapCanvas::zoomToSelected( QgsVectorLayer *layer )
{
// combine selected point with closest point and scale this rect
rect.combineExtentWith( mSettings.layerToMapCoordinates( layer, closestPoint ) );
rect.scale( scaleFactor, &centerMapCoordinates );
rect.scale( scaleFactor, &center );
}
}
return rect;
}

void QgsMapCanvas::zoomToSelected( QgsVectorLayer *layer )
{
if ( !layer )
{
// use current layer by default
layer = qobject_cast<QgsVectorLayer *>( mCurrentLayer );
}

if ( !layer || !layer->isSpatial() || layer->selectedFeatureCount() == 0 )
return;

QgsRectangle rect = layer->boundingBoxOfSelected();
if ( rect.isNull() )
{
emit messageEmitted( tr( "Cannot zoom to selected feature(s)" ), tr( "No extent could be determined." ), Qgis::Warning );
return;
}

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

// zoom in if point cannot be distinguished from others
// also check that rect is empty, as it might not in case of multi points
if ( layer->geometryType() == QgsWkbTypes::PointGeometry && rect.isEmpty() )
{
rect = optimalExtentForPointLayer( layer, rect.center() );
}
zoomToFeatureExtent( rect );
}

@@ -1265,6 +1265,14 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
*/
bool boundingBoxOfFeatureIds( const QgsFeatureIds &ids, QgsVectorLayer *layer, QgsRectangle &bbox, QString &errorMsg ) const;

/**
* Rerturns the optimal extent for a point \a layer and a given \a center point in canvas CRS.
* This will return an extent combined of the center and the closest point in the layer.
* The extent can be scaled with a \a scale factor.
* The returned extent might be an empty rect if it cannot be determnined.
*/
QgsRectangle optimalExtentForPointLayer( QgsVectorLayer *layer, const QgsPointXY &center, int scaleFactor = 5 );

void setLayersPrivate( const QList<QgsMapLayer *> &layers );

void startPreviewJobs();

0 comments on commit b47346b

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