0

I'm having a Spatialite layer to store photo locations from objects (stored in another Spatialite layer) taken in the field. Therefor I'm using a custom photo form like explained here.

enter image description here

Since now I type the corresponding object id in the "Objekt" field as an attribute of the photo layer. As an improvement I want to have a combo box (see picture of the photo form) were I can choose the object ids from a selection of the 5 nearest objects.

What I need is the coordinates from the point that will be created after I close the form. Or in other the words: the coordinates from the canvas click before the custom form appears. To search the 5 nearest objects (which is simple to achieve) I need the coordinates before the custom form opens, then a function can search for the neighbors and the results can be added to the combo box.

By now I'm using the following map tool (for the use in the Python console) to get click coordinates from the canvas:

from qgis.gui import QgsMapTool
from PyQt4.QtCore import pyqtSignal

class PointTool(QgsMapTool):   
    def __init__(self, canvas):
        QgsMapTool.__init__(self, canvas)
        self.canvas = canvas    

    canvasClicked = pyqtSignal(['QgsPoint'])
    def canvasReleaseEvent(self, event):
        point = self.toMapCoordinates(event.pos())          
        self.canvasClicked.emit(point)

def getCanvasCoordinates(point):
    QgsMessageLog.logMessage(str(point))

mapTool = PointTool(iface.mapCanvas())
iface.mapCanvas().setMapTool(mapTool)
mapTool.canvasClicked.connect(getCanvasCoordinates)

How can I implement this in the custom form to get the coordinates, that I need to make the next steps explained above?

Is there a way to connect the getCanvasCoordinates function to the QAction like:

iface.actionAddFeature().triggered.connect(getRasterValue)
1

Try setting up the custom form to open using the "Load from external file" option of the Python Init function box (under Fields tab of Layer Properties).

in your external file put your code in a function def my_form_open(dialog, layer, feature):and put the name in the "Function name" box on the Fields tab.

Inside that function you now have access to the feature being created so you can use

feature.geometry().asPoint().x()
feature.geometry().asPoint().y()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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