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.