I add my own road vector data on top of Google Maps using the OpenLayers plugin with QGIS. The vector layer is generated using a PostgreSQL DB.

Is there any way to extract coordinates of a road segment?

5 Answers 5


The easiest solution is to select the road segment(s) on the map or in attribute table and copy-paste (Ctrl-C - Ctrl-V) them into a text editor. You'll get the WKT string of the geometry with all its node coordinates.

If you want to export the whole layer, an easy solution is to use "Save as ..." to CSV and specify OGR creation option "GEOMETRY=AS_XYZ" or "GEOMETRY=AS_XY". The OGR creation option is important. If it is not specified only the attribute table is exported.


In fact, it is very easy in the Python console

def select_all(layer):
    layer.setSelectedFeatures([obj.id() for obj in couche])

# selection of the active layer
mylayer = qgis.utils.iface.activeLayer()

# selection of all the elements (geometry + attributes) of active layer

# coordinates of the vertices/nodes of every feature of a polyline
for i, feature in enumerate(mylayer.selectedFeatures()):
     geom= feature.geometry()
     print i, xy

0 [(206643,125181), (201007,121518), (208616,118700), (199035,115037), (200726,111937), (192835,107428), (192835,107428)]
1 [(196157,123493), (193933,121483), (198411,119320), (195456,117523), (198137,117614)]

# extraction in wkt format
for i, feature in enumerate(mylayer.selectedFeatures()):
    geom= feature.geometry()
    wkt = geom.exportToWkt()
    print i, wkt

0 LINESTRING(206643.215176 125181.180586, 201007.334329 121517.855521, 208615.775876 118699.916872, 199034.777658 115036.590588, 200725.543215 111936.856010, 192835.309877 107428.147942, 192835.309877 107428.147942)
1 LINESTRING(196156.747710 123492.901991, 193933.267396 121482.632118, 198410.686659 119320.069073, 195456.199118 117523.009641, 198136.558949 117614.385545)
  • What would I need to change to select only the first and last nodes? Would it just be print "i, xy[1]? And what about the last node? Commented Oct 16, 2014 at 18:50
  • I'd be happy to turn this into a question of its own if you think it's distinct enough? Commented Oct 16, 2014 at 18:50
  • 1
    Just create a list with xy coordinates -> the first = list[0], the last list[len(list)]
    – gene
    Commented Oct 16, 2014 at 19:41

Yes, in three stages. Firstly, select the lines (or features) of interest and save the selection as a shapefile. Then load the shapefile and go Vector -> Geometry Tools -> Extract nodes. Then load the 'nodes' shapefile layer and add the coordinates for the points to the shapefile's attribute table, Vector -> Geometry Tools -> Export/Add geometry columns.

There must be a simpler way. Nick.


Sure and there are multiple ways. The most coarse is to just hover over the segment and read the coordinates, but that's not exact at all. You can use the Split Feature plugin to split multilinestrings into linestrings and then clicking on the segment with the identify tool to get the coordinates of the start and end point (in the (derived) section). If for some reason you want the segment centers, you could use Polygon Centroids from the vector menu.

Alternatively you could do it with PostGIS (ST_Dump, ST_StartPoint, ST_EndPoint), since you already have the data in there.


Another way would be to save your line to a shapefile in qgis and then save the shapefile as a kml file. That will give you all your coordinates.

After that you might want to remove the kml header and have line of coords arranged into columns (to turn it into a csv file), with a simple bash script.

NOTE: You will need to open your kml with a text editor and make a newline after the word '<coordinates>' to make your qgis generated kml the same as a standard GE kml for this script to work.


# a script for converting from kml to csv

# get the date and time
now=$(date +"%y-%m-%d-%H-%M-%S")

grep '<coordinates>' -A1 test-track.kml | tail -n +2 > tmpfile
tr ' ' '\n' < ./tmpfile >> $now.csv
rm -rf tmpfile
echo 'done'
sleep 2

Where: The kml file is named 'test-track', you will need to edit the script with your own file name or rename your file 'test-track.kml' for the script to work.

  • 1
    It's possible to export directly to CSV (with coordinates) from Shapefile. There is no need to take an extra KML step.
    – underdark
    Commented Jun 25, 2012 at 6:47
  • Well I think that it should be possible providing the coordinates are already present in the attribute table, which in most cases requires the three stage process explained in an earlier post. However, (strangely) qgis seems to be able to provide the coordinates quite readily if we ask for them in a kml file, which means just a two stage process instead of a three stage procedure. So it's not an extra step. :)
    – herman
    Commented Jun 25, 2012 at 8:35
  • 1
    The coordinates don't have to be present in attribute table. It's just necessary to specify that coordinates should be exported using the OGR creation option "GEOMETRY=AS_XYZ".
    – underdark
    Commented Jun 25, 2012 at 8:45
  • Thank you underdark, that works well, only it seems to me it requires creating a points file from the line file first. Vector -> Geometry Tools -> Extract nodes. It's hard to say if that's actually any easier or faster. I guess it's a matter for personal preferences.
    – herman
    Commented Jun 26, 2012 at 10:14
  • No, it's not necessary to extract the nodes. A linestring WKT will be exported just fine.
    – underdark
    Commented Jun 26, 2012 at 11:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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