no attribute 'utcoffset'
oe8bck opened this issue · 2 comments
Hi,
I would like to make a track file out of LAT/LON and TIME data. The question is, what I did wrong in the format of the time data.
The original data is in UNIX time stamp and I did it this way:
In [9]: p['time']
Out[9]: '1596957617 '
In [10]: ts=datetime.utcfromtimestamp(int(p['time'])).strftime('%Y-%m-%dT%H:%M:%SZ')
In [11]: ts
Out[11]: '2020-08-09T07:20:17Z'
This works in principle as can be seen e.g. in these statements:
In [13]: gpx_track.get_time_bounds()
Out[13]: TimeBounds(start_time='2020-08-08T13:19:03Z', end_time='2020-08-09T07:20:17Z')
In [14]: gpxpy.gpx.GPXTrackPoint(
...: latitude=p['lat'],
...: longitude=p['lng'],
...: elevation=p['altitude'],
...: time=ts,
...: speed=data[0]['speed'],
...: )
Out[14]: GPXTrackPoint(46.62583, 14.12600, elevation='444.00', time='2020-08-09T07:20:17Z', speed='0')
Then I create a track with this time formats with this kind of loop
for p in data:
ts=datetime.utcfromtimestamp(int(p['time'])).strftime('%Y-%m-%dT%H:%M:%SZ')
gpx_segment.points.append(gpxpy.gpx.GPXTrackPoint(
latitude=p['lat'],
longitude=p['lng'],
elevation=p['altitude'],
time=ts,
speed=p['speed'],
))
But if I want to get the XML string, I get these errors:
In [25]: %run json2gpx.py
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
~/src/json2gpx/json2gpx.py in <module>
47
48 # You can add routes and waypoints, too...
---> 49 print('Created GPX:', gpx.to_xml())
/usr/local/lib/python3.8/dist-packages/gpxpy/gpx.py in to_xml(self, version, prettyprint)
2704 ]
2705
-> 2706 content = mod_gpxfield.gpx_fields_to_xml(
2707 self, 'gpx', version,
2708 custom_attributes={
/usr/local/lib/python3.8/dist-packages/gpxpy/gpxfield.py in gpx_fields_to_xml(instance, tag, version, custom_attributes, nsmap, prettyprint, indent)
547 body.append('>')
548 tag_open = False
--> 549 xml_value = gpx_field.to_xml(value, version, nsmap,
550 prettyprint=prettyprint,
551 indent=indent + ' ')
/usr/local/lib/python3.8/dist-packages/gpxpy/gpxfield.py in to_xml(self, value, version, nsmap, prettyprint, indent)
252 result = []
253 for obj in value:
--> 254 result.append(gpx_fields_to_xml(obj, self.tag, version,
255 nsmap=nsmap,
256 prettyprint=prettyprint,
/usr/local/lib/python3.8/dist-packages/gpxpy/gpxfield.py in gpx_fields_to_xml(instance, tag, version, custom_attributes, nsmap, prettyprint, indent)
547 body.append('>')
548 tag_open = False
--> 549 xml_value = gpx_field.to_xml(value, version, nsmap,
550 prettyprint=prettyprint,
551 indent=indent + ' ')
/usr/local/lib/python3.8/dist-packages/gpxpy/gpxfield.py in to_xml(self, value, version, nsmap, prettyprint, indent)
252 result = []
253 for obj in value:
--> 254 result.append(gpx_fields_to_xml(obj, self.tag, version,
255 nsmap=nsmap,
256 prettyprint=prettyprint,
/usr/local/lib/python3.8/dist-packages/gpxpy/gpxfield.py in gpx_fields_to_xml(instance, tag, version, custom_attributes, nsmap, prettyprint, indent)
547 body.append('>')
548 tag_open = False
--> 549 xml_value = gpx_field.to_xml(value, version, nsmap,
550 prettyprint=prettyprint,
551 indent=indent + ' ')
/usr/local/lib/python3.8/dist-packages/gpxpy/gpxfield.py in to_xml(self, value, version, nsmap, prettyprint, indent)
252 result = []
253 for obj in value:
--> 254 result.append(gpx_fields_to_xml(obj, self.tag, version,
255 nsmap=nsmap,
256 prettyprint=prettyprint,
/usr/local/lib/python3.8/dist-packages/gpxpy/gpxfield.py in gpx_fields_to_xml(instance, tag, version, custom_attributes, nsmap, prettyprint, indent)
547 body.append('>')
548 tag_open = False
--> 549 xml_value = gpx_field.to_xml(value, version, nsmap,
550 prettyprint=prettyprint,
551 indent=indent + ' ')
/usr/local/lib/python3.8/dist-packages/gpxpy/gpxfield.py in to_xml(self, value, version, nsmap, prettyprint, indent)
218 return '{}="{}"'.format(self.attribute, mod_utils.make_str(value))
219 elif self.type_converter:
--> 220 value = self.type_converter.to_string(value)
221 if self.tag:
222 return mod_utils.to_xml(self.tag, content=value, escape=True,
/usr/local/lib/python3.8/dist-packages/gpxpy/gpxfield.py in to_string(self, time)
129 def to_string(self, time: Optional[mod_datetime.datetime]) -> Optional[str]:
130 if time:
--> 131 return format_time(time) if time else None
132 return None
133
/usr/local/lib/python3.8/dist-packages/gpxpy/gpxfield.py in format_time(time)
90
91 def format_time(time: mod_datetime.datetime) -> str:
---> 92 offset = time.utcoffset()
93 if not offset or offset == 0:
94 tz = 'Z'
AttributeError: 'str' object has no attribute 'utcoffset'
So what did I wrong? As this class is exactly what I am looking for, any hint to get rid of this error is very welcome!
(I've added an example as attachment)
test.py.zip
KR,
Christof
note that this line specifies what type should time
be:
def format_time(time: mod_datetime.datetime) -> str:
Your tz
iz a string ('2020-08-09T07:20:17Z'
), it should be a datetime.datetime
.
Thanks a lot for the hint!
In the example I changed the 5th line to
ts=datetime.fromtimestamp(1596892743)
and now it works!
KR,
Christof