1

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
Vince
  • 20,017
  • 15
  • 45
  • 64
TEO
  • 19
  • 1
  • 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 '20 at 08: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 '20 at 08: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 '20 at 09:13
  • Thank for the fast answer unfortunatly i can't change my version of QGIS because my company have some plugin that only work in this version. For the code i didn't started yet, i just wanted to know if it was possible with my actual configuration. – TEO Jun 25 '20 at 09:17
  • 2
    Can't you just export as csv (with the coordinates), and then load into excel? – gvanhavre Jun 25 '20 at 09: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 '20 at 09:51
  • 1
    Please don't ask "Is it possible?" questions, because the answer is always useless -- Just about everything is possible, and even things that aren't possible have work-arounds or alternatives that are, none of which are adequately addressed by a Boolean answer. Instead focus the question on How is it possible? and in GIS SE for a coding question, this must be in the form "Here's my code to try to solve this, but I ran into problem description". – Vince Jun 26 '20 at 03:02

2 Answers2

1

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: print(p) p.transform(tr1) print(p) p.transform(tr2) print(p) break #To just print out first Point

enter image description here

BERA
  • 72,339
  • 13
  • 72
  • 161
1

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

QgsProject.instance().addMapLayer(vlayer)

Export to layer

QgsVectorFileWriter.writeAsVectorFormat(vlayer, '/tmp/out.csv', "utf-8", vlayer.crs(), "CSV")

ThomasG77
  • 30,725
  • 1
  • 53
  • 93