I am working in QGIS. I have a georeferenced line layer and I want to export the attribute table and the coordinates of start and end points of my lines to Excel.
Can I do this with QGIS?
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It only takes a minute to sign up.
Sign up to join this communityI am working in QGIS. I have a georeferenced line layer and I want to export the attribute table and the coordinates of start and end points of my lines to Excel.
Can I do this with QGIS?
Extracting coordinates of start/end vertices (not all line vertices) from lines is a nice use case for new functionality in QGIS v.2.8, namely, the function editor.
This is the workflow:
Load your line layer to QGIS and activate it.
Open the field calculator and go to tab Function Editor
.
Click on New file
, write vertices
as the new file name, and click on Save file
.
Copy the following code snippet as is, paste it into the Function Editor
text area, and then click on Run Script
and Save file
:
from qgis.core import qgsfunction
@qgsfunction(args="auto", group='Custom')
def getStartVertexX(g, feature, parent):
return g.vertexAt( 0 )[0]
@qgsfunction(args="auto", group='Custom')
def getStartVertexY(g, feature, parent):
return g.vertexAt( 0 )[1]
@qgsfunction(args="auto", group='Custom')
def getEndVertexX(g, feature, parent):
line = g.asPolyline()
return line[-1][0] if line else None
@qgsfunction(args="auto", group='Custom')
def getEndVertexY(g, feature, parent):
line = g.asPolyline()
return line[-1][1] if line else None
Go to tab Expression
and do this:
a. Check Create a new field
.
b. Set the output field name to startX
, output field type to real
, output field width to 12
, and precision to 6
(you may adjust these values if you consider it).
c. Copy the following line into the Expression
text area and then click on OK
:
getStartVertexX( $geometry )
Repeat 5.a-c for fields startY
, endX
, and endY
, with the following expressions, respectively:
getStartVertexY( $geometry )
getEndVertexX( $geometry )
getEndVertexY( $geometry )
Save your edits.
Now your line layer has field values for start and end coordinates from each corresponding line.
Finally, as your requirement is to export the table to Excel, use the XY Tools plugin as indicated in How to export attribute table to Excel from QGIS?.
That's it! Tell me if you face any issue.
EDIT: Steps 2 to 7 (!) can be avoided by using the following built-in QGIS functions in the Field Calculator:
$x_at(0)
For start X $y_at(0)
For start Y$x_at(-1)
For end X$y_at(-1)
For end YWith QGIS 3 you can do for your respective fields:
x( start_point( $geometry ))
y( start_point( $geometry ))
x( end_point( $geometry ))
y( end_point( $geometry ))
Click on Vector -> Geometry Tools -> Extract Nodes
A window will appears, choose a file to save and mark the option "Add result to canvas".
Right click in the new layer created and click on "save as".
Choose a name to your file and open it with a text editor to check the result.
Export table to excel:
For exporting the coordinates too, I assume you will have to add new columns for x and y coordinates and calculate these using the fieldcalculator with the appropriate operators.
1) Click in Vector --> Geometry Tools --> Extract nodes 2) In the new window a)check "Add result to canvas" b)Browse your locate and save with a name
3) Whait a moment, in the layer Right click and choose "Save as" 4) In "Format" choose the "Comma Separated Values (.csv)" 5) And open the .csv file in to Excel and you have all that you need.
I'm adding a figure further to miln40's solution above for QGIS 3. New users may find the solution easier to understand with a figure and more detail in the explanation.
Select the vector layer that has the line features. Open the field calculator from the data table. Check Create New Field
, enter the name of the Field (e.g., Start_Lat), select Decimal number (real)
.
You can vary your output field lengths and precision according to the type of coordinate system you are using. Output field length 12, and precision 6 was used in the example above, but I could have reduced these to length 7 and precision 1 in my example because I am working with UTM's. The 12 length and precision 6 will be needed if you are working in Lat/Long.