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?
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 communityThe 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.select([])
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
select_all(mylayer)
# coordinates of the vertices/nodes of every feature of a polyline
for i, feature in enumerate(mylayer.selectedFeatures()):
geom= feature.geometry()
xy=geom.asPolyline()
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)
print "i, xy[1]
? And what about the last node?
Oct 16, 2014 at 18:50
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.
kml_to_csv:
#!/bin/bash
# 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.