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 community
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
New file, write
vertices as the new file name, and click on
Copy the following code snippet as is, paste it into the
Function Editor text area, and then click on
Run Script and
from qgis.core import qgsfunction @qgsfunction(args="auto", group='Custom') def getStartVertexX(g, feature, parent): return g.vertexAt( 0 ) @qgsfunction(args="auto", group='Custom') def getStartVertexY(g, feature, parent): return g.vertexAt( 0 ) @qgsfunction(args="auto", group='Custom') def getEndVertexX(g, feature, parent): line = g.asPolyline() return line[-1] if line else None @qgsfunction(args="auto", group='Custom') def getEndVertexY(g, feature, parent): line = g.asPolyline() return line[-1] if line else None
Go to tab
Expression and do this:
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
getStartVertexX( $geometry )
Repeat 5.a-c for fields
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.
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 Y
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.