1

I want to generate a GPX file with Python OGR. In particular I want to generate a tracks (layer of features of OGR type wkbMultiLineString)

My script is below. I get the error ERROR 1: No such field: 'time' Also it won't let me create a field time because it is a default field in GPX.

Any ideas how to solve that?

import ogr, osr

in_data_source = ogr.Open('test.geojson')
in_layer = in_data_source.GetLayer()
feature_count = in_layer.GetFeatureCount()

driver = ogr.GetDriverByName("GPX")
out_data_source = driver.CreateDataSource("t1.gpx")
srs = osr.SpatialReference()
srs.ImportFromEPSG(4326)
out_layer = out_data_source.CreateLayer("t1", srs, ogr.wkbMultiLineString)

line = ogr.Geometry(ogr.wkbLineString)

c = 0
while c in range(feature_count-1):
    print c
    f1 = in_layer.GetFeature(c)
    f2 = in_layer.GetFeature(c+1)
    g1 = f1.GetGeometryRef()
    g2 = f2.GetGeometryRef()

    line.AddPoint(g1.GetX(), g1.GetY())
    line.AddPoint(g2.GetX(), g2.GetY())
    time = f1.GetField("datetime")

    feature = ogr.Feature(out_layer.GetLayerDefn())
    # feature.SetField("time", time)
    feature.SetGeometry(line)
    out_layer.CreateFeature(feature)

    c += 1

This problem can also be solved by using the gpxpy library, though I am interested in solving it with OGR.

4
  • I used instead the gpxpy library to solve the problem: github.com/tkrajina/gpxpy
    – ustroetz
    Commented Feb 17, 2015 at 14:33
  • You should post your comment, expanded perhaps to show code or describe what functions in the library allowed you to solve the problem, as an answer or consider deleting the question altogether if you don't think it holds value for future users. It's ok to answer your own question.
    – Chris W
    Commented Feb 17, 2015 at 21:27
  • 1
    The original answer still is not answered. I am looking for a way with OGR. I updated my question.
    – ustroetz
    Commented Feb 18, 2015 at 8:25
  • 1
    Gotcha. The wording of your initial comment made it sound as if you'd found an alternative solution and moved on, no longer pursuing the original direction.
    – Chris W
    Commented Feb 18, 2015 at 20:25

1 Answer 1

1

I'm not an expert regarding python + GDAL/OGR, but looking into something similar I found a possible answer to your question.

import ogr, osr
import fileinput
from datetime import datetime

srs = osr.SpatialReference()
srs.ImportFromEPSG(4326)

driver = ogr.GetDriverByName("GPX")

out = driver.CreateDataSource('/tmp/test.gpx')

# layer creation: if you use 'track_points', points are accepted
oL = out.CreateLayer("track_points", srs, ogr.wkbPoint)

# loop over tab-separated input stream with timestamps + lon + lat
for l in fileinput.input():
    (timest, lon, lat) = l.split('\t')

    # create point
    p = ogr.Geometry(ogr.wkbPoint)
    # initialise point with coordinates
    p.AddPoint(float(lon), float(lat))

    # prepare new "feature" using the layer's "feature definition", 
    # initialize it by setting geometry and necessary field values
    featureDefn = oL.GetLayerDefn()
    oF = ogr.Feature(featureDefn)
    oF.SetGeometry(p)
    oF.SetField("track_fid", "1")
    oF.SetField("track_seg_id", "1")    

    # adapt this according to the timestamp format of your data source
    timest = datetime.strptime(timest, '%m/%d/%Y %H:%M:%S').isoformat()
    oF.SetField("time", timest)
    oL.CreateFeature(oF)    

del(out)

The script reads tab-separated timestamps + longitude + latitude from the commandline or a file and writes to /tmp/test.gpx.

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.