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
987 stars 223 forks source link

GPXTrackPoint(latitude=0, longitude=0) serialize/deserialize raises exception #61

Closed augustjd closed 8 years ago

augustjd commented 8 years ago

I was writing some test cases where a GPX file was written and then read from disk, so I presumed 0,0 would be a safe pair of coordinates to use, but it raised an exception:

root: ERROR: latitude is mandatory in None
Traceback (most recent call last):
  File "/home/august/autologic/api/env/lib/python3.5/site-packages/gpxpy/parser.py", line 196, in parse
    self.__parse_dom()
  File "/home/august/autologic/api/env/lib/python3.5/site-packages/gpxpy/parser.py", line 221, in __parse_dom
    mod_gpxfield.gpx_fields_from_xml(self.gpx, self.xml_parser, node, version)
  File "/home/august/autologic/api/env/lib/python3.5/site-packages/gpxpy/gpxfield.py", line 348, in gpx_fields_from_xml
    value = gpx_field.from_xml(parser, current_node, version)
  File "/home/august/autologic/api/env/lib/python3.5/site-packages/gpxpy/gpxfield.py", line 180, in from_xml
    result.append(gpx_fields_from_xml(self.classs, parser, child_node, version))
  File "/home/august/autologic/api/env/lib/python3.5/site-packages/gpxpy/gpxfield.py", line 348, in gpx_fields_from_xml
    value = gpx_field.from_xml(parser, current_node, version)
  File "/home/august/autologic/api/env/lib/python3.5/site-packages/gpxpy/gpxfield.py", line 180, in from_xml
    result.append(gpx_fields_from_xml(self.classs, parser, child_node, version))
  File "/home/august/autologic/api/env/lib/python3.5/site-packages/gpxpy/gpxfield.py", line 348, in gpx_fields_from_xml
    value = gpx_field.from_xml(parser, current_node, version)
  File "/home/august/autologic/api/env/lib/python3.5/site-packages/gpxpy/gpxfield.py", line 180, in from_xml
    result.append(gpx_fields_from_xml(self.classs, parser, child_node, version))
  File "/home/august/autologic/api/env/lib/python3.5/site-packages/gpxpy/gpxfield.py", line 348, in gpx_fields_from_xml
    value = gpx_field.from_xml(parser, current_node, version)
  File "/home/august/autologic/api/env/lib/python3.5/site-packages/gpxpy/gpxfield.py", line 137, in from_xml
    raise mod_gpx.GPXException('%s is mandatory in %s' % (self.name, self.tag))
gpxpy.gpx.GPXException: latitude is mandatory in None

The latitude/longitudes were not written to the file if they were equal to 0. I'm going to guess that some bit of code in the serialization step is using if not latitude: when it should be if latitude is None:, or, perhaps more generally if attribute.value: write_attribute(attribute) instead of if attribute.value is not None: write_attribute(attribute)

tkrajina commented 8 years ago

Yes, you're right. I just added a test and a quickfix in https://github.com/tkrajina/gpxpy/tree/zero-latlng but I need more testing before merging that in master. Can you try if that branch work properly with your test cases?

augustjd commented 8 years ago

That solves it! Thanks.