1

Does anybody know where i can find implementation of feature called "Zoom to Feature".

To use this function from GUI you have to right click on layer then choose "Open Attribute Table" then right click on feature and there should be option called "Zoom to Feature" in context menu.

It looks like this:

enter image description here

[IMPORTANT]

Feature in Qgis is a set of attributes from table.

enter image description here

4
  • Witch context menu are you referring to ? Also if you are not selecting a feature in the table to witch feature do you want to zoom ?
    – J.R
    Commented Oct 17, 2019 at 13:24
  • Are you wanting the QgsMapCanvas::zoomToSelected() method?
    – Joseph
    Commented Oct 17, 2019 at 13:42
  • @Joseph Nope i dont want this method because it assumes that feature was previously selected. I dont want to select any feature. I just want to zoom my map view to some feature on some layer. Commented Oct 17, 2019 at 13:45
  • @J.R Im reffering to context menu presented in the attached picture. There is a feature selected in the attached picture. As far as i know Feature in Qgis is set of attributes in table. Commented Oct 17, 2019 at 13:48

1 Answer 1

2

It is what i was looking 4.

    void QgsIdentifyResultsDialog::zoomToFeature()
{
  QTreeWidgetItem *item = lstResults->currentItem();

  QgsVectorLayer *vlayer = vectorLayer( item );
  QgsRasterLayer *rlayer = rasterLayer( item );
  if ( !vlayer && !rlayer )
    return;

  QgsMapLayer *layer = nullptr;
  if ( vlayer )
    layer = vlayer;
  else
    layer = rlayer;

  QgsIdentifyResultsFeatureItem *featItem = dynamic_cast<QgsIdentifyResultsFeatureItem *>( featureItem( item ) );
  if ( !featItem )
    return;

  QgsFeature feat = featItem->feature();
  if ( !feat.hasGeometry() )
    return;

  // TODO: verify CRS for raster WMS features
  QgsRectangle rect = mCanvas->mapSettings().layerExtentToOutputExtent( layer, feat.geometry().boundingBox() );

  if ( rect.isEmpty() )
  {
    QgsPointXY c = rect.center();
    rect = mCanvas->extent();
    rect.scale( 0.5, &c );
  }

  mCanvas->setExtent( rect );
  mCanvas->refresh();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.