Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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,,,,,
share|improve this question
    
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. –  matt wilkie Aug 8 '10 at 6:52

5 Answers 5

up vote 23 down vote accepted

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()
share|improve this answer
    
Nice! Keeping that one. –  Nathan W Aug 7 '10 at 8:21
    
+1 very elegant solution –  radek Aug 7 '10 at 12:45
    
+1 For elegantly using Pythonic tools. –  Adam Matan Aug 7 '10 at 19:37
    
+1 thanks for going the distance, fmark –  scw Aug 7 '10 at 20:51
    
this is the best tutorial to OGR, I've ever seen –  dassouki Jan 24 '11 at 12:46

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.

share|improve this answer
    
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. –  Sue Deforest Aug 28 '12 at 22:17

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

share|improve this answer
    
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 –  matt wilkie Aug 7 '10 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 Apr 10 '12 at 23:15
    
Found it: pyqgis.org/repo/contributed –  Gray Shaw Apr 10 '12 at 23:24

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

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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