-1

How to get position of QgsPoint objects in pyqgis/ python console using QGIS 3.x?

For example: 1 points in vector layer TEST_P

migeometria = f.geometry()
posicion=geom.asPoint()
print(posicion.x())  

I need current coordinate x, y

Also

Get coordinate the mouse position in map from python console. In status bar show:

Coordinate: |____________|

See: line number 1084 in https://github.com/qgis/QGIS/blob/master/src/plugins/georeferencer/qgsgeorefplugingui.cpp
mCoordsLabel->setToolTip( tr( "Current map coordinate" ) );

0
#active layer is a point layer:
feat = next(iface.activeLayer().getFeatures())#get first feature
geo= QgsGeometry.asPoint(feat.geometry())#get the geometry of the feature
pxy=QgsPointXY(geo)
print (pxy.x(),pxy.y())
0

Run this from console, click anywhere onto the map. To deactivate it, click another tool eg Pan

class MapTool(QgsMapTool):
    def __init__(self, iface, canvas):
        super(MapTool, self).__init__(canvas)

    def canvasReleaseEvent(self, event):
        point = self.toMapCoordinates(event.pos())
        print (point.x(),"/",point.y())

tool=MapTool(iface, iface.mapCanvas())
iface.mapCanvas().setMapTool(tool)

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.