5

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?

2
  • 1
    Reading the comments it seems you don't want to export the coordinate system but coordinates, please make sure your question reflects what you actually intend to do, otherwise it is tedious for people to answer properly. Feb 23 '15 at 14:50
  • Yes, You are right. Sorry! I want the coordinates.
    – Po Po
    Feb 24 '15 at 9:40
12

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:

  1. Load your line layer to QGIS and activate it.

  2. Open the field calculator and go to tab Function Editor.

  3. Click on New file, write vertices as the new file name, and click on Save file.

    enter image description here

  4. 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
    

    enter image description here

  5. 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 )
    

    enter image description here

  6. Repeat 5.a-c for fields startY, endX, and endY, with the following expressions, respectively:

    getStartVertexY( $geometry )
    getEndVertexX( $geometry )
    getEndVertexY( $geometry )
    
  7. Save your edits.

    Now your line layer has field values for start and end coordinates from each corresponding line.

    enter image description here

  8. 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?.

    enter image description here

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
6
  • In fact, I am working with Valmira 2.2.0. So the code that you gave me isn't correct in Valmira 2.2.0 . But still, Thanks a lot!
    – Po Po
    Feb 24 '15 at 10:47
  • Why not upgrading? Feb 24 '15 at 12:05
  • It is my boss that want to work with that programe? I cann't do nothing! Anyway I find what i need so, thanks a lot from you.
    – Po Po
    Feb 24 '15 at 14:10
  • @gcarrillo this is what I need. Only issue is that getEndVertexY($geometry) gives me an EvalError:Bad Index:2. Any ideas?
    – user25976
    May 13 '15 at 20:31
  • 1
    @gcarrillo Ah, I followed the screen capture and notice now that it's actually return line[-1][1] and not line[-1][2]
    – user25976
    May 13 '15 at 20:37
3

With 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 ))
2

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.

4
  • thats was a great idea because thats create all the point that I have in my layer. In my layer, the line can have many ponts. But problem is that still that doesn't issue the values of coordinate. Do I need any tools?
    – Po Po
    Feb 24 '15 at 10:28
  • The coordinates should be the first and seccond columns, are you sure is not there?
    – exg
    Feb 24 '15 at 21:30
  • Yes exg, the coordinate are in excel file. I didn't see in attribute table, thats why i wrote you. Still, do you know any way how to show the coordianates in the attribute table?
    – Po Po
    Feb 25 '15 at 10:24
  • In the window openned when you right click "save as" in the layer to save as csv, select the option "add saved file to map". The layer created will have the coordinates in the atribute table.
    – exg
    Feb 26 '15 at 3:20
2

Export table to excel:

  1. Right click layer in table of contents select Save As
  2. Under Format drop down select Comma Separated Value (CSV) option
  3. Open excel and browse for saved .csv to open
3
  • Yes, but I want also the Coordinate system. I have the line in layer, and I want also the coordina for the begins and the ends points.
    – Po Po
    Feb 23 '15 at 14:24
  • Please update your question to make "begins and the ends points" more clear.
    – artwork21
    Feb 23 '15 at 14:40
  • Sorry! I want the X,Y coordinate for the firs point of the line, and I Want the X,Y coordinate for the ends points of the line.
    – Po Po
    Feb 23 '15 at 14:46
2

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

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.

1

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).

Field calculator showing expression to obtain start/end points on line

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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