No way to write waypoint into gpx output
bosby74 opened this issue · 2 comments
Dear,
I'm a new user of gpxpy (and honestly not a very proficient user of python too) and I'm trying to build a code to read a gpx file, add a waypoint and write it to a new gpx file. The code that I wrote is:
`#` ---------------------------------------------#
#!/usr/bin/env python3.6
# -*- coding: utf-8 -*-
import gpxpy
import gpxpy.gpx
def readGPX(fileGPX):
gpx_file=open(fileGPX,'r')
gpx = gpxpy.parse(gpx_file)
for track in gpx.tracks:
for segment in track.segments:
for point in segment.points:
print('Point at ({0},{1}) -> {2}'.format(point.latitude, point.longitude, point.time))
for waypoint in gpx.waypoints:
print('waypoint {0} -> ({1},{2})'.format(waypoint.name, waypoint.latitude, waypoint.longitude))
for route in gpx.routes:
print('Route:')
for point in route.points:
print('Route Point at ({0},{1}) -> {2}'.format(point.latitude, point.longitude, point.elevation))
return gpx
def add_way_point(gpxFILE, label, lat, lon, t0):
# Create new file GPX
gpx=gpxFILE.clone()
# Add a waypoint
w1 = gpxpy.gpx.GPXWaypoint()
w1.latitude = lat
w1.longitude = lon
w1.name = label
w1.time = t0
gpx.waypoints.append(w1)
for waypoint in gpx.waypoints:
print('waypoint {0} -> ({1},{2},{3})'.format(waypoint.name, waypoint.latitude, waypoint.longitude, waypoint.time))
return gpx
def write_new_gpx(newgpx,output):
with open(output, "w") as f:
f.write(gpx.to_xml(version="1.1"))
return f
# ---------------------------------------------#
filename="./Move_2020_04_26_15_29_35_Trekking.gpx"
output="prova.gpx"
lat_Point="44.1325"
lon_Point="11.090833333333334"
time_Point="2020-04-26 14:38:05" # To convert time to UTC-2
label="Foto1"
gpx=readGPX(filename)
gpx2=add_way_point(gpx, label, lat_Point, lon_Point, time_Point)
write_new_gpx(gpx2,output)
but the output that is produced doesn't contain any waypoints but only track points. Someone have an idea for the reason? Does gpxpy allow to write waypoints? and does gpxpy allow to write gpx output file?
Thanks in advance,
Simone
Adding waypoints to the track works for me:
$ ipython
In [1]: import gpxpy.gpx as g
In [2]: gpx = g.GPX()
In [3]: gpx.waypoints.append(g.GPXWaypoint())
In [5]: gpx.waypoints.append(g.GPXWaypoint(1, 2))
In [7]: print(gpx.to_xml())
<?xml version="1.0" encoding="UTF-8"?>
<gpx xmlns="http://www.topografix.com/GPX/1/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" version="1.1" creator="gpx.py -- https://github.com/tkrajina/gpxpy">
<wpt lat="0" lon="0">
</wpt>
<wpt lat="1" lon="2">
</wpt>
</gpx>
Unfortunately, I don't have the time to debug your example. If you can send me a minimal example (including a minimal GPX file, if needed), I'll revisit the issue.
Dear,
thank you for your answer.
After your suggestion I tried several combinations but it seems that the problem arises trying to write the gpx object as a gpx output file.
In fact the waypoint appears on the gpx object just created but it disappears once use gpx.to_xml() to save the result in the output file.
I send you the python script and the input file that I'm using and the output file produced. Let me know for any suggestion about it.
Thank you,
Simone
waypoint_issue.zip