10

I have a point layer, that I'd like to return the Longitude, latitude using the PyQGIS.

How do I use QgsPoint to do that?

Taras
  • 32,823
  • 4
  • 66
  • 137
dassouki
  • 8,563
  • 12
  • 64
  • 115

2 Answers2

9

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

Taras
  • 32,823
  • 4
  • 66
  • 137
DavidF
  • 4,862
  • 1
  • 26
  • 32
8

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

provider = layer.dataProvider()
feat = QgsFeature()

while(provider.nextFeature(feat)): geom = feat.geometry() x = geom.asPoint().x() y = geom.asPoint().y()

P.S. taken from Points2One Plugin by Pavol Kapusta & Goyo Diaz

Taras
  • 32,823
  • 4
  • 66
  • 137
underdark
  • 84,148
  • 21
  • 231
  • 413