I was formerly working on ArcGIS but now I'm working on QGIS 2.14.7 and Python 3.8 so I'm a bit confused with the QGIS script.

I have to extract coordinate of point layer (CRS:Lambert 93 and WGS84) and put them into Excel, the problem is that there are a lot of points, so I want to make it easier by automating it.

Is it possible to create a code that:

  • Takes point layer in entry
  • Adds coordinate X and Y (CRS Lambert 93) in the attributes table
  • Adds coordinate °, m, s (CRS WGS 84) in the attributes table
  • Exports the attribute table to Excel
  • 2
    Welcome to Geographic Information Systems! Welcome to GIS SE! We're a little different from other sites; this isn't a discussion forum but a Q&A site. Your questions should as much as possible describe not just what you want to do, but precisely what you have tried and where you are stuck trying that. Please check out our short tour for more about how the site works. – Ian Turton Jun 25 at 8:47
  • 1
    to start with switch to a modern version of QGIS (there is no upgrade fee) as QGIS 2.x won't work with Python 3.x – Ian Turton Jun 25 at 8:49
  • 2
    It is very much possible to create that code. But add some kind of code attempt to prevent your question from being closed (as it is very likely to be now). – BERA Jun 25 at 9:13
  • 2
    Can't you just export as csv (with the coordinates), and then load into excel? – gvanhavre Jun 25 at 9:49
  • 1
    @gvanhavre - does sound as if field calculator and export to csv is the way to go - why not make that an answer – Ian Turton Jun 25 at 9:51

Just an example to get you started. I have QGIS3, dont know if this will work in QGIS2:

lyr = iface.activeLayer() #Click layer in tree
gs = [f.geometry() for f in lyr.getFeatures()]

#From: https://gis.stackexchange.com/questions/163645/transforming-single-qgsgeometry-object-from-one-crs-to-another-using-pyqgis
sourceCrs = QgsCoordinateReferenceSystem(int(lyr.crs().authid().split(':')[1]))

destCrs1 = QgsCoordinateReferenceSystem(2154)
tr1 = QgsCoordinateTransform(sourceCrs, destCrs1, QgsProject.instance())

destCrs2 = QgsCoordinateReferenceSystem(4326)
tr2 = QgsCoordinateTransform(destCrs1, destCrs2, QgsProject.instance())

#(You would write results to fields instead)
for p in gs:
    break #To just print out first Point

enter image description here

| improve this answer | |

You can use Virtual Layers e.g

from qgis.core import QgsVectorLayer, QgsProject

query = 'SELECT *, x(st_transform(geometry, 2154)) AS x_l93, y(st_transform(geometry, 2154)) AS y_l93, LongLatToDMS(x(geometry), y(geometry)) AS dms FROM "Your Layer name"'
vlayer = QgsVectorLayer( "?query={}".format(query), "vlayer", "virtual" )
# Good way to confirm it's fine

# Export to layer
QgsVectorFileWriter.writeAsVectorFormat(vlayer, '/tmp/out.csv', "utf-8", vlayer.crs(), "CSV")
| improve this answer | |

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