Thursday, 31 January 2019

pyqgis - Using QGIS API and Python, to return latitude and longitude of point?



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?



Answer



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()
provider.select()

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

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...