tkrajina / gpxpy

gpx-py is a python GPX parser. GPX (GPS eXchange Format) is an XML based file format for GPS tracks.
Apache License 2.0
1.02k stars 223 forks source link

How can I keep the milisecond part of the timestamp? #170

Closed zodiac911 closed 5 years ago

zodiac911 commented 5 years ago

I have sample gpx file like this

<?xml version="1.0" encoding="UTF-8"?>
<gpx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.topografix.com/GPX/1/0" version="1.0" xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd" creator="gpx.py -- https://github.com/tkrajina/gpxpy">
<trk>
<name/>
<desc/>
<trkseg>
<trkpt lat="47.4973805" lon="18.8395458">
<ele>242.0</ele>
<time>2017-08-08T18:35:56.544000Z</time></trkpt>
<trkpt lat="47.4974565" lon="18.8394014">
<ele>242.0</ele>
<time>2017-08-08T18:35:57.617000Z</time></trkpt>
<trkpt lat="47.4974565" lon="18.8394014">
<ele>242.0</ele>
<time>2017-08-08T18:35:57.818000Z</time></trkpt>
<trkpt lat="47.4974566" lon="18.8394013">
<ele>241.8</ele>
<time>2017-08-08T18:35:58.019000Z</time></trkpt>
<trkpt lat="47.4974566" lon="18.8394012">
<ele>241.6</ele>
<time>2017-08-08T18:35:58.218000Z</time></trkpt></trkseg></trk></gpx>

How can I keep the milisecond part if I use gpxpy to parse it?

If I run the code below

import gpxpy
import gpxpy.gpx

# Parsing an existing file:
# -------------------------

gpx_file = open('sample.gpx', '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} Time is {3}'.format(point.latitude, point.longitude, point.elevation, 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('Point at ({0},{1}) -> {2}'.format(point.latitude, point.longitude, point.elevation))

I get those

Point at (47.4973805,18.8395458) -> 242.0 Time is 2017-08-08 18:35:56
Point at (47.4974565,18.8394014) -> 242.0 Time is 2017-08-08 18:35:57
Point at (47.4974565,18.8394014) -> 242.0 Time is 2017-08-08 18:35:57
Point at (47.4974566,18.8394013) -> 241.8 Time is 2017-08-08 18:35:58
Point at (47.4974566,18.8394012) -> 241.6 Time is 2017-08-08 18:35:58

with the milisecond part of the original timestamp lost

tkrajina commented 5 years ago

Should work now in 1.3.5 (just released)

zodiac911 commented 5 years ago

Should work now in 1.3.5 (just released)

Really efficient bro, it's very helpful to me.