12

Someone mistakenly saved a GPS track as waypoints and then sent them along to me in a .csv file. They want a line back (shapefile). What's the simplest method to convert this to a line? Available tools are Arcmap, gdal/ogr and qgis more or less in order of preference. I'd rather not install an additional tool; an online conversion service would be okay.

Latitude,Longitude,Date,Time,ampm,,,
60.71586,-135.07476,25/07/2010,9:26:15,PM,,,,,
60.71637,-135.07563,25/07/2010,9:26:12,PM,,,,,
60.71648,-135.07612,25/07/2010,9:26:11,PM,,,,,
60.71664,-135.07707,25/07/2010,9:26:09,PM,,,,,
60.71672,-135.07756,25/07/2010,9:26:08,PM,,,,,
1
  • The .csv had a few dozen rows of garbage data (active log start/stop messages etc.) which I got rid of by the expedient of sorting on all columns and then deleting non-data rows which floated to the top. Dumb, I know (must get more sleep!), otherwise the qgis Point2one plugin would likely have worked, and the others as well. Thanks to fmark's python skills both mistakes can be rectified, though I have to wait until I'm back in the office next week to verify. Commented Aug 8, 2010 at 6:52

5 Answers 5

23

It seems like the crucial thing you want here is for the points in the line to be sorted by the time of capture, spread across three column rows. While you could organise the data in a spreadsheet, I often find writing a quick script provides the most flexibility:

import csv
from datetime import datetime
try:
    from osgeo import ogr
except ImportError:
    import ogr

SHP_FILENAME = "output.shp"
CSV_FILENAME = "input.csv"

r = csv.reader(open(CSV_FILENAME, 'r'), delimiter=',', quotechar=None)
header = dict(((str, i) for i, str in enumerate(r.next())))

# load data rows into memory
rows = [row for row in r]

# sort by date and time ascending
rows.sort(key=lambda row: datetime.strptime(
        (row[header['Date']] + ' ' + row[header['Time']] + ' ' + 
         row[header['ampam']]), 
        '%d/%m/%Y %I:%M:%S %p'))

# Create new shapefile
ogr.UseExceptions()
ds = ogr.GetDriverByName('ESRI Shapefile').CreateDataSource(SHP_FILENAME)
layer = ds.CreateLayer("data", None, ogr.wkbLineString)

# Create a new line geometry
line = ogr.Geometry(type=ogr.wkbLineString)

# Add GPS points to line
lon_idx, lat_idx = header['Longitude'], header['Latitude']
for row in rows:
    line.AddPoint(float(row[lon_idx]), float(row[lat_idx]))

# Add line as a new feature to the shapefile
feature = ogr.Feature(feature_def=layer.GetLayerDefn())
feature.SetGeometryDirectly(line)
layer.CreateFeature(feature)

# Cleanup
feature.Destroy()
ds.Destroy()
2
  • Nice! Keeping that one.
    – Nathan W
    Commented Aug 7, 2010 at 8:21
  • this is the best tutorial to OGR, I've ever seen
    – dassouki
    Commented Jan 24, 2011 at 12:46
8

QGIS Plugin "Points2One" should be what you're looking for.

If you don't tick "Sort points by this field", the plugin connects them in the internal point order in the layer. I used your sample, arranged the points in a zigzag order and it worked as expected:

enter image description here

3
  • nope, it matches point to point based on latitude and not time sequence, see imgur.com/d2Ycg.jpg Perhaps with some massage of the csv to turn the timestamps into 24hr it might work better Commented Aug 7, 2010 at 0:38
  • Please tell me where I can download the points2one plugin, if it's still available. I want to make a line composed of GPS points and manually added points (to smooth out the curves).
    – Gray Shaw
    Commented Apr 10, 2012 at 23:15
  • Found it: pyqgis.org/repo/contributed
    – Gray Shaw
    Commented Apr 10, 2012 at 23:24
3

ArcGIS 10.0 has a Points To Line tool.

I recommend reading the ArcGIS 10.2 for Desktop documentation, but in summary:

Creates line features from points.

...

Each feature in the output will be based on unique values in the Line Field.

...

By default, points used to create each output line feature will be used in the order they are found. If a different order is desired, specify a Sort Field.

1
  • This was a useful tool for me. It is important to use the Line Field or the Sort Field to get it right (depends on attributes). In my case I had to use the Line Field option (using only the field with the date attribute and not date + time) for the data to understand that each date created a new line. Commented Aug 28, 2012 at 22:17
1

If you happen to have ETGeoWizards installed on your ArcMap machine, that will handle it, too.

1

The free GME ArcGIS extension provides convert.pointstolines which will create a single line out of a point layer.

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.