I have a point layer, that I'd like to return the Longitude, latitude using the QGIS API and Python.

How do I use QgsPoint to do that?

closed as off-topic by PolyGeo May 18 at 22:59

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "When seeking help to debug/write/improve code always provide the desired behavior, a specific problem/error and the shortest code (as formatted text, not pictures) needed to reproduce it in the question body. Providing a clear problem statement and a code attempt helps others to help you." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.


This works on the latest release 'Tethys'.

The point layer that I am interested in is the first layer in my layer list. The coordinate values will be in the units of the spatial reference system of the layer. If your layer is in lat/lon, remember that x=lon and y=lat...

Open the Python Console and type this:

from qgis.utils import iface

feat = QgsFeature()
mc = iface.mapCanvas()
layer = mc.layer(0)
provider = layer.dataProvider()

    geometry = feat.geometry()
    print "X Coord %d: " %geometry.asPoint().x()
    print "Y Coord %d: " %geometry.asPoint().y()

This requires your layer to be in a projection using lat/lon to return geographic coordinates:

provider = layer.dataProvider()
feat = QgsFeature()
    geom = feat.geometry()
    x = geom.asPoint().x()
    y = geom.asPoint().y()

(taken from Points2One Plugin by Pavol Kapusta & Goyo Diaz)

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