[Qgis-developer] Selecting with On-the-fly enabled (PyQGIS)

fegyi001 fegyi001 at gmail.com
Mon Mar 18 02:17:19 PDT 2013


Hi everyone, 

I have problems with a specific task. I intend to write a new module, during
which the user has to select features from the map canvas. My project is in
EPSG:23700. There are shapefiles from different coordinate systems, such as
EPSG:23700, EPSG:4326, EPSG:32633 and even without projection. On-the-fly
projection is enabled.

Everything works fine when selecting features from the EPSG:23700 and the
projection-less features. But when I try to click on a feature in the other
files, they are not highlighted and therefore not selected. When I disable
the on-the-fly projection, the EPSG:4326 and EPSG:32633 features are
transformed back to their normal place, and I can click on them. But I
INTEND to use on-the-fly projection, so this is a big problem for me. 

When I use the official icons for selecting features, however, the selection
works just fine with features from ALL EPSGs. 

Does anyone has an idea what should I do? I'm quite new to Python
programming and to the QGIS API.

My code looks like this (only the important parts):

class LelohelyBejelento:

    def __init__(self, iface):
        self.iface = iface
        self.canvas = self.iface.mapCanvas()
        self.clickTool = QgsMapToolEmitPoint(self.canvas)
        self.dlg = LelohelyBejelentoDialog()
        self.selectList = []
        self.cLayer = None
        self.provider = None
		
    def initGui(self):
        self.action = QAction(QIcon(":/plugins/lelohelybejelento/icon.png"),
u"Lelőhely-bejelentő készítő", self.iface.mainWindow())
        QObject.connect(self.action, SIGNAL("triggered()"), self.run)
        self.iface.addToolBarIcon(self.action)
        self.iface.addPluginToMenu(u"&Lelőhely-bejelentő", self.action)
        result = QObject.connect(self.iface,
SIGNAL("currentLayerChanged(QgsMapLayer *)"), self.handleLayerChange)

        # connect to the selectFature custom function to the map canvas
click event
        QObject.connect(self.clickTool, SIGNAL("canvasClicked(const QgsPoint
&, Qt::MouseButton)"), self.selectFeature)
    
    def unload(self):
        self.iface.removePluginMenu(u"&Lelőhely-bejelentő", self.action)
        self.iface.removeToolBarIcon(self.action)

    def selectFeature(self, point, button):
        self.selectList = []
        pntGeom = QgsGeometry.fromPoint(point)
        pntBuff = pntGeom.buffer( (self.canvas.mapUnitsPerPixel() * 2),0)
        rect = pntBuff.boundingBox()
        if self.cLayer:
            self.allLayers = self.canvas.layers()
            feat = QgsFeature()
            # create the select statement
            self.provider.select([],rect)
            while self.provider.nextFeature(feat):
                if feat.geometry().intersects(pntBuff):
                    self.selectList.append(feat.id())
					
    def run(self):
        self.cLayer = self.iface.mapCanvas().currentLayer()
        if self.cLayer:
            self.provider = self.cLayer.dataProvider()
        self.canvas.setMapTool(self.clickTool)
        self.dlg.show()
        result = self.dlg.exec_()
        if result == 1:
            pass




--
View this message in context: http://osgeo-org.1560.n6.nabble.com/Selecting-with-On-the-fly-enabled-PyQGIS-tp5041031.html
Sent from the Quantum GIS - Developer mailing list archive at Nabble.com.


More information about the Qgis-developer mailing list