简体   繁体   English

使用PyQgis选择和缩放图层的要素

[英]Select and zoom features of a layer using PyQgis

I want to select features and to zoom on them and do all these steps using PyQgis. 我想选择功能并放大它们,并使用PyQgis完成所有这些步骤。

And I'm able to do both of them separatly but it doesn't seems to work when I try to mix the two of them. 而且我能够分别完成这两项操作,但是当我尝试将两者混合使用时,它似乎不起作用。

Both of the codes I use for them are from the internet. 我为它们使用的两个代码均来自互联网。 Here's what I use to select features of a layer : 这是我用来选择图层特征的方法:

from qgis.core import *
import qgis.utils
lyrMap = QgsVectorLayer('C:/someplace', 'MapName', 'ogr')
QgsMapLayerRegistry.instance().addMapLayer(lyrMap)

expr = QgsExpression("'Attribute' IS NOT NULL")
it = lyrMap.getFeatures(QgsFeatureRequest(expr))
ids = [i.id() for i in it] #select only the features for which the expression is true
lyrMap.setSelectedFeatures(ids)

And it seems to do the trick as features appear selected on QGis. 当功能在QGis上被选中时,似乎可以解决问题。

In order to zoom the code is much more simple, it's just : 为了缩放代码要简单得多,它只是:

canvas = qgis.utils.iface.mapCanvas()
canvas.zoomToSelected(lyrMap)

But it seems that canvas doesn't consider that there's a selection on lyrMap and simply do nothing. 但是似乎canvas并不认为lyrMap上有一个选择并且什么也不做。 I've tried to do the selection manually in QGis, and then zoom using zoomToSelected, and it worked. 我尝试在QGis中手动进行选择,然后使用zoomToSelected进行缩放,并且可以正常工作。

But my objective is to do it without needing to do the selection manually... 但我的目标是做到这一点,而无需手动进行选择...

Note : I don't think that's the issue, but the attribute I'm doing the selection on is from a join between lyrMap and another layer (I didn't put the code here because I don't think it's linked). 注意:我不认为这是问题所在,但是我正在选择的属性来自lyrMap和另一层之间的连接(我没有在这里放代码,因为我认为它没有链接)。

Thanks in advances for answers, clues or anything really :) ! 在此先感谢您的答案,线索或任何真正的:)!

This is working for my plugin. 这适用于我的插件。 I am using python 2.7 and QGIS 1.8 and 2.0.1.You can use this code after including using vector file and adding it to the registry. 我正在使用python 2.7和QGIS 1.8和2.0.1。您可以在包括使用矢量文件并将其添加到注册表之后使用此代码。

self.rubberBand = None
#create vertex marker for point..older versons..
self.vMarker = None
#add rubberbands 
self.crossRb = QgsRubberBand(iface.mapCanvas(),QGis.Line)
self.crossRb.setColor(Qt.black)


def pan(self):
        print "pan button clicked!"
        x = self.dlg.ui.mTxtX.text()
        y = self.dlg.ui.mTxtY.text()
        if not x:
            return
        if not y:
            return
        print x + "," + y
        canvas = self.canvas
        currExt = canvas.extent()
        canvasCenter = currExt.center()
        dx = float(x) - canvasCenter.x()
        dy = float(y) - canvasCenter.y()
        xMin = currExt.xMinimum() + dx
        xMax = currExt.xMaximum() + dx
        yMin = currExt.yMinimum() + dy
        yMax = currExt.yMaximum() + dy
        newRect = QgsRectangle(xMin,yMin,xMax,yMax)
        canvas.setExtent(newRect)
        pt = QgsPoint(float(x),float(y))
        self.zoom(pt)
        canvas.refresh()

def zoom(self,point):
        canvas = self.canvas
        currExt = canvas.extent()
        leftPt = QgsPoint(currExt.xMinimum(),point.y())
        rightPt = QgsPoint(currExt.xMaximum(),point.y())
        topPt = QgsPoint(point.x(),currExt.yMaximum())
        bottomPt = QgsPoint(point.x(),currExt.yMinimum())
        horizLine = QgsGeometry.fromPolyline( [ leftPt , rightPt ] )
        vertLine = QgsGeometry.fromPolyline( [ topPt , bottomPt ] )
        self.crossRb.reset(QGis.Line)
        self.crossRb.addGeometry(horizLine,None)
        self.crossRb.addGeometry(vertLine,None)
        if QGis.QGIS_VERSION_INT >= 10900:
            rb = self.rubberBand
            rb.reset(QGis.Point)
            rb.addPoint(point)
        else:
            self.vMarker = QgsVertexMarker(self.canvas)
            self.vMarker.setIconSize(10)
            self.vMarker.setCenter(point)
            self.vMarker.show()

        # wait .5 seconds to simulate a flashing effect
        QTimer.singleShot(500,self.resetRubberbands)

    def resetRubberbands(self):
        print "resetting rubberbands.."
        canvas = self.canvas
        if QGis.QGIS_VERSION_INT >= 10900:
            self.rubberBand.reset()
        else:
            self.vMarker.hide()
            canvas.scene().removeItem(self.vMarker)
        self.crossRb.reset()
        print "completed resetting.."

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM