[Python-modules-commits] [gpxpy] 02/05: Import gpxpy_1.1.2.orig.tar.gz

Dominik George natureshadow-guest at moszumanska.debian.org
Fri Jan 6 14:44:41 UTC 2017


This is an automated email from the git hooks/post-receive script.

natureshadow-guest pushed a commit to branch master
in repository gpxpy.

commit bb62a46ceed1a366f5259a4ac90adc69ea855f3e
Author: Dominik George <nik at naturalnet.de>
Date:   Fri Jan 6 15:39:32 2017 +0100

    Import gpxpy_1.1.2.orig.tar.gz
---
 PKG-INFO          |   2 +-
 gpxinfo           |  17 ++---
 gpxpy/__init__.py |   8 ++-
 gpxpy/geo.py      |   8 +--
 gpxpy/gpx.py      | 187 ++++++++++++++++++++++++++----------------------------
 gpxpy/gpxfield.py |  36 +++++------
 gpxpy/gpxxml.py   |   1 -
 gpxpy/parser.py   |  18 +++---
 setup.py          |   2 +-
 9 files changed, 134 insertions(+), 145 deletions(-)

diff --git a/PKG-INFO b/PKG-INFO
index 672265f..d18c0c6 100644
--- a/PKG-INFO
+++ b/PKG-INFO
@@ -1,6 +1,6 @@
 Metadata-Version: 1.1
 Name: gpxpy
-Version: 1.1.1
+Version: 1.1.2
 Summary: GPX file parser and GPS track manipulation library
 Home-page: http://www.trackprofiler.com/gpxpy/index.html
 Author: Tomo Krajina
diff --git a/gpxinfo b/gpxinfo
index 2a4b2ab..3d3ddcd 100644
--- a/gpxinfo
+++ b/gpxinfo
@@ -53,14 +53,15 @@ def print_gpx_part_info(gpx_part, indentation='    '):
     points_no = len(list(gpx_part.walk(only_points=True)))
     print('%sPoints: %s' % (indentation, points_no))
 
-    distances = []
-    previous_point = None
-    for point in gpx_part.walk(only_points=True):
-        if previous_point:
-            distance = point.distance_2d(previous_point)
-            distances.append(distance)
-        previous_point = point
-    print('{}Avg distance between points: {:.2f}m'.format(indentation, sum(distances) / len(list(gpx_part.walk()))))
+    if points_no > 0:
+        distances = []
+        previous_point = None
+        for point in gpx_part.walk(only_points=True):
+            if previous_point:
+                distance = point.distance_2d(previous_point)
+                distances.append(distance)
+            previous_point = point
+        print('{}Avg distance between points: {:.2f}m'.format(indentation, sum(distances) / len(list(gpx_part.walk()))))
 
     print('')
 
diff --git a/gpxpy/__init__.py b/gpxpy/__init__.py
index 996f229..2e2b313 100644
--- a/gpxpy/__init__.py
+++ b/gpxpy/__init__.py
@@ -15,7 +15,7 @@
 # limitations under the License.
 
 
-def parse(xml_or_file, parser=None):
+def parse(xml_or_file, parser=None, version = None):
     """
     Parse xml (string) or file object. This is just an wrapper for
     GPXParser.parse() function.
@@ -24,11 +24,13 @@ def parse(xml_or_file, parser=None):
     detected, lxml if possible).
 
     xml_or_file must be the xml to parse or a file-object with the XML.
+
+    version may be '1.0', '1.1' or None (then it will be read from the gpx
+    xml node if possible, if not then version 1.0 will be used).
     """
 
-    from . import gpx as mod_gpx
     from . import parser as mod_parser
 
     parser = mod_parser.GPXParser(xml_or_file, parser=parser)
 
-    return parser.parse()
+    return parser.parse(version)
diff --git a/gpxpy/geo.py b/gpxpy/geo.py
index ac55805..c4882dd 100644
--- a/gpxpy/geo.py
+++ b/gpxpy/geo.py
@@ -14,8 +14,6 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-import pdb
-
 import logging as mod_logging
 import math as mod_math
 
@@ -347,7 +345,7 @@ class LocationDelta:
         """
         Version 1:
             Distance (in meters).
-            angle_from_north *clockwise*. 
+            angle_from_north *clockwise*.
             ...must be given
         Version 2:
             latitude_diff and longitude_diff
@@ -362,8 +360,8 @@ class LocationDelta:
         elif (latitude_diff is not None) and (longitude_diff is not None):
             if (distance is not None) or (angle is not None):
                 raise Exception('No distance/angle if using lat/lon diff!')
-            this.latitude_diff  = latitude_diff
-            this.longitude_diff = longitude_diff
+            self.latitude_diff  = latitude_diff
+            self.longitude_diff = longitude_diff
             self.move_function = self.move_by_lat_lon_diff
 
     def move(self, location):
diff --git a/gpxpy/gpx.py b/gpxpy/gpx.py
index a148cc1..7a7973b 100644
--- a/gpxpy/gpx.py
+++ b/gpxpy/gpx.py
@@ -18,8 +18,6 @@
 GPX related stuff
 """
 
-import pdb
-
 import logging as mod_logging
 import math as mod_math
 import collections as mod_collections
@@ -176,12 +174,12 @@ class GPXWaypoint(mod_geo.Location):
     gpx_10_fields = GPX_10_POINT_FIELDS
     gpx_11_fields = GPX_11_POINT_FIELDS
 
-    __slots__ = ('latitude', 'longitude', 'elevation', 'time', 
-                 'magnetic_variation', 'geoid_height', 'name', 'comment', 
-                 'description', 'source', 'link', 'link_text', 'symbol', 
-                 'type', 'type_of_gpx_fix', 'satellites', 
-                 'horizontal_dilution', 'vertical_dilution', 
-                 'position_dilution', 'age_of_dgps_data', 'dgps_id', 
+    __slots__ = ('latitude', 'longitude', 'elevation', 'time',
+                 'magnetic_variation', 'geoid_height', 'name', 'comment',
+                 'description', 'source', 'link', 'link_text', 'symbol',
+                 'type', 'type_of_gpx_fix', 'satellites',
+                 'horizontal_dilution', 'vertical_dilution',
+                 'position_dilution', 'age_of_dgps_data', 'dgps_id',
                  'link_type', 'extensions')
 
     def __init__(self, latitude=None, longitude=None, elevation=None, time=None,
@@ -240,12 +238,12 @@ class GPXRoutePoint(mod_geo.Location):
     gpx_10_fields = GPX_10_POINT_FIELDS
     gpx_11_fields = GPX_11_POINT_FIELDS
 
-    __slots__ = ('latitude', 'longitude', 'elevation', 'time', 
-                 'magnetic_variation', 'geoid_height', 'name', 'comment', 
-                 'description', 'source', 'link', 'link_text', 'symbol', 
-                 'type', 'type_of_gpx_fix', 'satellites', 
-                 'horizontal_dilution', 'vertical_dilution', 
-                 'position_dilution', 'age_of_dgps_data', 'dgps_id', 
+    __slots__ = ('latitude', 'longitude', 'elevation', 'time',
+                 'magnetic_variation', 'geoid_height', 'name', 'comment',
+                 'description', 'source', 'link', 'link_text', 'symbol',
+                 'type', 'type_of_gpx_fix', 'satellites',
+                 'horizontal_dilution', 'vertical_dilution',
+                 'position_dilution', 'age_of_dgps_data', 'dgps_id',
                  'link_type', 'extensions')
 
     def __init__(self, latitude=None, longitude=None, elevation=None, time=None, name=None,
@@ -321,8 +319,8 @@ class GPXRoute:
             mod_gpxfield.GPXComplexField('points', tag='rtept', classs=GPXRoutePoint, is_list=True),
     ]
 
-    __slots__ = ('name', 'comment', 'description', 'source', 'link', 
-                 'link_text', 'number', 'points', 'link_type', 'type', 
+    __slots__ = ('name', 'comment', 'description', 'source', 'link',
+                 'link_text', 'number', 'points', 'link_type', 'type',
                  'extensions')
 
     def __init__(self, name=None, description=None, number=None):
@@ -344,9 +342,9 @@ class GPXRoute:
             point.remove_elevation()
 
     def length(self):
-        """ 
-        Computes length (2-dimensional) of route. 
-         
+        """
+        Computes length (2-dimensional) of route.
+
         Returns:
         -----------
         length: float
@@ -398,7 +396,7 @@ class GPXRoute:
         ------
         point: GPXRoutePoint
             A point in the GPXRoute
-        point_no: int 
+        point_no: int
             Not included in yield if only_points is true
         """
         for point_no, point in enumerate(self.points):
@@ -447,12 +445,12 @@ class GPXTrackPoint(mod_geo.Location):
     gpx_10_fields = GPX_TRACK_POINT_FIELDS
     gpx_11_fields = GPX_11_POINT_FIELDS
 
-    __slots__ = ('latitude', 'longitude', 'elevation', 'time', 'course', 
-                 'speed', 'magnetic_variation', 'geoid_height', 'name', 
-                 'comment', 'description', 'source', 'link', 'link_text', 
-                 'symbol', 'type', 'type_of_gpx_fix', 'satellites', 
-                 'horizontal_dilution', 'vertical_dilution', 
-                 'position_dilution', 'age_of_dgps_data', 'dgps_id', 
+    __slots__ = ('latitude', 'longitude', 'elevation', 'time', 'course',
+                 'speed', 'magnetic_variation', 'geoid_height', 'name',
+                 'comment', 'description', 'source', 'link', 'link_text',
+                 'symbol', 'type', 'type_of_gpx_fix', 'satellites',
+                 'horizontal_dilution', 'vertical_dilution',
+                 'position_dilution', 'age_of_dgps_data', 'dgps_id',
                  'link_type', 'extensions')
 
     def __init__(self, latitude=None, longitude=None, elevation=None, time=None, symbol=None, comment=None,
@@ -656,7 +654,7 @@ class GPXTrackSegment:
             track_point.remove_elevation()
 
     def length_2d(self):
-        """ 
+        """
         Computes 2-dimensional length (meters) of segment (only latitude and
         longitude, no elevation).
 
@@ -668,8 +666,8 @@ class GPXTrackSegment:
         return mod_geo.length_2d(self.points)
 
     def length_3d(self):
-        """ 
-        Computes 3-dimensional length of segment (latitude, longitude, and 
+        """
+        Computes 3-dimensional length of segment (latitude, longitude, and
         elevation).
 
         Returns
@@ -686,7 +684,7 @@ class GPXTrackSegment:
         Parameters
         ----------
         location_delta: LocationDelta object
-            Delta (distance/angle or lat/lon offset to apply each point in the 
+            Delta (distance/angle or lat/lon offset to apply each point in the
             segment
         """
         for track_point in self.points:
@@ -705,7 +703,7 @@ class GPXTrackSegment:
         ------
         point: GPXTrackPoint
             A point in the sement
-        point_no: int 
+        point_no: int
             Not included in yield if only_points is true
         """
         for point_no, point in enumerate(self.points):
@@ -728,9 +726,9 @@ class GPXTrackSegment:
         return len(self.points)
 
     def split(self, point_no):
-        """ 
-        Splits the segment into two parts. If one of the split segments is 
-        empty it will not be added in the result. The segments will be split 
+        """
+        Splits the segment into two parts. If one of the split segments is
+        empty it will not be added in the result. The segments will be split
         in place.
 
         Parameters
@@ -758,12 +756,12 @@ class GPXTrackSegment:
 
     def get_moving_data(self, stopped_speed_threshold=None):
         """
-        Return a tuple of (moving_time, stopped_time, moving_distance, 
-        stopped_distance, max_speed) that may be used for detecting the time 
-        stopped, and max speed. Not that those values are not absolutely true, 
+        Return a tuple of (moving_time, stopped_time, moving_distance,
+        stopped_distance, max_speed) that may be used for detecting the time
+        stopped, and max speed. Not that those values are not absolutely true,
         because the "stopped" or "moving" information aren't saved in the segment.
 
-        Because of errors in the GPS recording, it may be good to calculate 
+        Because of errors in the GPS recording, it may be good to calculate
         them on a reduced and smoothed version of the track.
 
         Parameters
@@ -805,7 +803,6 @@ class GPXTrackSegment:
             # Won't compute max_speed for first and last because of common GPS
             # recording errors, and because smoothing don't work well for those
             # points:
-            first_or_last = i in [0, 1, len(self.points) - 1]
             if point.time and previous.time:
                 timedelta = point.time - previous.time
 
@@ -867,7 +864,7 @@ class GPXTrackSegment:
 
         Returns
         ----------
-        bounds : Bounds named tuple 
+        bounds : Bounds named tuple
             min_latitude : float
                 Minimum latitude of segment in decimal degrees [-90, 90]
             max_latitude : float
@@ -897,7 +894,7 @@ class GPXTrackSegment:
     def get_speed(self, point_no):
         """
         Computes the speed at the specified point index.
- 
+
         Parameters
         ----------
         point_no : integer
@@ -905,7 +902,7 @@ class GPXTrackSegment:
 
         Returns
         ----------
-        speed : float 
+        speed : float
             Speed returned in m/s
         """
         point = self.points[point_no]
@@ -1020,7 +1017,7 @@ class GPXTrackSegment:
     def get_duration(self):
         """
         Calculates duration or track segment
-        
+
         Returns
         -------
         duration: float
@@ -1071,7 +1068,7 @@ class GPXTrackSegment:
         return UphillDownhill(uphill, downhill)
 
     def get_elevation_extremes(self):
-        """ 
+        """
         Calculate elevation extremes of track segment
 
         Returns
@@ -1096,9 +1093,9 @@ class GPXTrackSegment:
 
     def get_location_at(self, time):
         """
-        Gets approx. location at given time. Note that, at the moment this 
+        Gets approx. location at given time. Note that, at the moment this
         method returns an instance of GPXTrackPoint in the future -- this may
-        be a mod_geo.Location instance with approximated latitude, longitude 
+        be a mod_geo.Location instance with approximated latitude, longitude
         and elevation!
         """
         if not self.points:
@@ -1262,7 +1259,7 @@ class GPXTrackSegment:
         """
         Returns if points in this segment contains timestamps.
 
-        The first point, the last point, and 75% of the points must have times 
+        The first point, the last point, and 75% of the points must have times
         for this method to return true.
         """
         if not self.points:
@@ -1281,7 +1278,7 @@ class GPXTrackSegment:
         """
         Returns if points in this segment contains elevation.
 
-        The first point, the last point, and at least 75% of the points must 
+        The first point, the last point, and at least 75% of the points must
         have elevation for this method to return true.
         """
         if not self.points:
@@ -1333,18 +1330,18 @@ class GPXTrack:
             mod_gpxfield.GPXComplexField('segments', tag='trkseg', classs=GPXTrackSegment, is_list=True),
     ]
 
-    __slots__ = ('name', 'comment', 'description', 'source', 'link', 
-                 'link_text', 'number', 'segments', 'link_type', 'type', 
+    __slots__ = ('name', 'comment', 'description', 'source', 'link',
+                 'link_text', 'number', 'segments', 'link_type', 'type',
                  'extensions')
 
     def __init__(self, name=None, description=None, number=None):
-        self.name = None
+        self.name = name
         self.comment = None
-        self.description = None
+        self.description = description
         self.source = None
         self.link = None
         self.link_text = None
-        self.number = None
+        self.number = number
         self.segments = []
         self.link_type = None
         self.type = None
@@ -1359,7 +1356,7 @@ class GPXTrack:
 
     def reduce_points(self, min_distance):
         """
-        Reduces the number of points in the track. Segment points will be 
+        Reduces the number of points in the track. Segment points will be
         updated in place.
 
         Parameters
@@ -1404,7 +1401,7 @@ class GPXTrack:
         self.segments = result
 
     def length_2d(self):
-        """ 
+        """
         Computes 2-dimensional length (meters) of track (only latitude and
         longitude, no elevation). This is the sum of the 2D length of all
         segments.
@@ -1451,7 +1448,7 @@ class GPXTrack:
 
         Returns
         ----------
-        bounds : Bounds named tuple 
+        bounds : Bounds named tuple
             min_latitude : float
                 Minimum latitude of track in decimal degrees [-90, 90]
             max_latitude : float
@@ -1522,7 +1519,7 @@ class GPXTrack:
         return result
 
     def length_3d(self):
-        """ 
+        """
         Computes 3-dimensional length of track (latitude, longitude, and
         elevation). This is the sum of the 3D length of all segments.
 
@@ -1539,9 +1536,9 @@ class GPXTrack:
         return length
 
     def split(self, track_segment_no, track_point_no):
-        """ 
-        Splits one of the segments in the track in two parts. If one of the 
-        split segments is empty it will not be added in the result. The 
+        """
+        Splits one of the segments in the track in two parts. If one of the
+        split segments is empty it will not be added in the result. The
         segments will be split in place.
 
         Parameters
@@ -1565,7 +1562,7 @@ class GPXTrack:
         self.segments = new_segments
 
     def join(self, track_segment_no, track_segment_no_2=None):
-        """ 
+        """
         Joins two segments of this track. The segments will be split in place.
 
         Parameters
@@ -1573,8 +1570,8 @@ class GPXTrack:
         track_segment_no : integer
             The index of the first segment to join
         track_segment_no_2 : integer
-            The index of second segment to join. If track_segment_no_2 is not 
-            provided,the join will be with the next segment after 
+            The index of second segment to join. If track_segment_no_2 is not
+            provided,the join will be with the next segment after
             track_segment_no.
         """
         if not track_segment_no_2:
@@ -1600,12 +1597,12 @@ class GPXTrack:
 
     def get_moving_data(self, stopped_speed_threshold=None):
         """
-        Return a tuple of (moving_time, stopped_time, moving_distance, 
-        stopped_distance, max_speed) that may be used for detecting the time 
-        stopped, and max speed. Not that those values are not absolutely true, 
+        Return a tuple of (moving_time, stopped_time, moving_distance,
+        stopped_distance, max_speed) that may be used for detecting the time
+        stopped, and max speed. Not that those values are not absolutely true,
         because the "stopped" or "moving" information aren't saved in the track.
 
-        Because of errors in the GPS recording, it may be good to calculate 
+        Because of errors in the GPS recording, it may be good to calculate
         them on a reduced and smoothed version of the track.
 
         Parameters
@@ -1671,7 +1668,7 @@ class GPXTrack:
         Parameters
         ----------
         location_delta: LocationDelta object
-            Delta (distance/angle or lat/lon offset to apply each point in each 
+            Delta (distance/angle or lat/lon offset to apply each point in each
             segment of the track
         """
         for track_segment in self.segments:
@@ -1680,7 +1677,7 @@ class GPXTrack:
     def get_duration(self):
         """
         Calculates duration or track
-        
+
         Returns
         -------
         duration: float
@@ -1701,7 +1698,7 @@ class GPXTrack:
 
     def get_uphill_downhill(self):
         """
-        Calculates the uphill and downhill elevation climbs for the track. 
+        Calculates the uphill and downhill elevation climbs for the track.
         If elevation for some points is not found those are simply ignored.
 
         Returns
@@ -1728,9 +1725,9 @@ class GPXTrack:
 
     def get_location_at(self, time):
         """
-        Gets approx. location at given time. Note that, at the moment this 
+        Gets approx. location at given time. Note that, at the moment this
         method returns an instance of GPXTrackPoint in the future -- this may
-        be a mod_geo.Location instance with approximated latitude, longitude 
+        be a mod_geo.Location instance with approximated latitude, longitude
         and elevation!
         """
         result = []
@@ -1742,7 +1739,7 @@ class GPXTrack:
         return result
 
     def get_elevation_extremes(self):
-        """ 
+        """
         Calculate elevation extremes of track
 
         Returns
@@ -1919,11 +1916,11 @@ class GPX:
             mod_gpxfield.GPXExtensionsField('extensions'),
     ]
 
-    __slots__ = ('version', 'creator', 'name', 'description', 'author_name', 
-                 'author_email', 'link', 'link_text', 'time', 'keywords', 
-                 'bounds', 'waypoints', 'routes', 'tracks', 'author_link', 
-                 'author_link_text', 'author_link_type', 'copyright_author', 
-                 'copyright_year', 'copyright_license', 'link_type', 
+    __slots__ = ('version', 'creator', 'name', 'description', 'author_name',
+                 'author_email', 'link', 'link_text', 'time', 'keywords',
+                 'bounds', 'waypoints', 'routes', 'tracks', 'author_link',
+                 'author_link_text', 'author_link_type', 'copyright_author',
+                 'copyright_year', 'copyright_license', 'link_type',
                  'metadata_extensions', 'extensions')
 
     def __init__(self):
@@ -1997,7 +1994,7 @@ class GPX:
 
     def adjust_time(self, delta):
         """
-        Adjusts the time of all points in all of the segments of all tracks by 
+        Adjusts the time of all points in all of the segments of all tracks by
         the specified delta.
 
         Parameters
@@ -2058,7 +2055,7 @@ class GPX:
 
         Returns
         ----------
-        bounds : Bounds named tuple 
+        bounds : Bounds named tuple
             min_latitude : float
                 Minimum latitude of track in decimal degrees [-90, 90]
             max_latitude : float
@@ -2102,7 +2099,7 @@ class GPX:
 
     def refresh_bounds(self):
         """
-        Compute bounds and reload min_latitude, max_latitude, min_longitude 
+        Compute bounds and reload min_latitude, max_latitude, min_longitude
         and max_longitude properties of this object
         """
 
@@ -2170,9 +2167,9 @@ class GPX:
         return MovingData(moving_time, stopped_time, moving_distance, stopped_distance, max_speed)
 
     def split(self, track_no, track_segment_no, track_point_no):
-        """ 
-        Splits one of the segments of a track in two parts. If one of the 
-        split segments is empty it will not be added in the result. The 
+        """
+        Splits one of the segments of a track in two parts. If one of the
+        split segments is empty it will not be added in the result. The
         segments will be split in place.
 
         Parameters
@@ -2189,8 +2186,8 @@ class GPX:
         track.split(track_segment_no=track_segment_no, track_point_no=track_point_no)
 
     def length_2d(self):
-        """ 
-        Computes 2-dimensional length of the GPX file (only latitude and 
+        """
+        Computes 2-dimensional length of the GPX file (only latitude and
         longitude, no elevation). This is the sum of 3D length of all segments
         in all tracks.
 
@@ -2207,8 +2204,8 @@ class GPX:
         return result
 
     def length_3d(self):
-        """ 
-        Computes 3-dimensional length of the GPX file (latitude, longitude, and 
+        """
+        Computes 3-dimensional length of the GPX file (latitude, longitude, and
         elevation). This is the sum of 3D length of all segments in all tracks.
 
         Returns
@@ -2266,7 +2263,7 @@ class GPX:
     def get_duration(self):
         """
         Calculates duration of GPX file
-        
+
         Returns
         -------
         duration: float
@@ -2287,7 +2284,7 @@ class GPX:
 
     def get_uphill_downhill(self):
         """
-        Calculates the uphill and downhill elevation climbs for the gpx file. 
+        Calculates the uphill and downhill elevation climbs for the gpx file.
         If elevation for some points is not found those are simply ignored.
 
         Returns
@@ -2314,9 +2311,9 @@ class GPX:
 
     def get_location_at(self, time):
         """
-        Gets approx. location at given time. Note that, at the moment this 
+        Gets approx. location at given time. Note that, at the moment this
         method returns an instance of GPXTrackPoint in the future -- this may
-        be a mod_geo.Location instance with approximated latitude, longitude 
+        be a mod_geo.Location instance with approximated latitude, longitude
         and elevation!
         """
         result = []
@@ -2328,7 +2325,7 @@ class GPX:
         return result
 
     def get_elevation_extremes(self):
-        """ 
+        """
         Calculate elevation extremes of GPX file
 
         Returns
@@ -2595,10 +2592,6 @@ class GPX:
 
         return '<?xml version="1.0" encoding="UTF-8"?>\n' + content.strip()
 
-    def smooth(self, vertical=True, horizontal=False, remove_extremes=False):
-        for track in self.tracks:
-            track.smooth(vertical, horizontal, remove_extremes)
-
     def has_times(self):
         """ See GPXTrackSegment.has_times() """
         if not self.tracks:
diff --git a/gpxpy/gpxfield.py b/gpxpy/gpxfield.py
index b5d2671..e35b18a 100644
--- a/gpxpy/gpxfield.py
+++ b/gpxpy/gpxfield.py
@@ -34,12 +34,14 @@ def parse_time(string):
         string = string.replace('T', ' ')
     if 'Z' in string:
         string = string.replace('Z', '')
+    if '.' in string:
+        string = string.split('.')[0]
     for date_format in mod_gpx.DATE_FORMATS:
         try:
             return mod_datetime.datetime.strptime(string, date_format)
-        except ValueError as e:
+        except ValueError:
             pass
-    raise GPXException('Invalid time: %s' % string)
+    raise mod_gpx.GPXException('Invalid time: %s' % string)
 
 
 # ----------------------------------------------------------------------------------------------------
@@ -61,19 +63,10 @@ class IntConverter:
 
 class TimeConverter:
     def from_string(self, string):
-        from . import gpx as mod_gpx
-        if not string:
+        try:
+            return parse_time(string)
+        except:
             return None
-        if 'T' in string:
-            string = string.replace('T', ' ')
-        if 'Z' in string:
-            string = string.replace('Z', '')
-        for date_format in mod_gpx.DATE_FORMATS:
-            try:
-                return mod_datetime.datetime.strptime(string, date_format)
-            except ValueError as e:
-                pass
-        return None
     def to_string(self, time):
         from . import gpx as mod_gpx
         return time.strftime(mod_gpx.DATE_FORMAT) if time else None
@@ -110,7 +103,8 @@ class GPXField(AbstractGPXField):
         AbstractGPXField.__init__(self)
         self.name = name
         if tag and attribute:
-            raise GPXException('Only tag *or* attribute may be given!')
+            from . import gpx as mod_gpx
+            raise mod_gpx.GPXException('Only tag *or* attribute may be given!')
         if attribute:
             self.tag = None
             self.attribute = name if attribute is True else attribute
@@ -134,7 +128,7 @@ class GPXField(AbstractGPXField):
         if result is None:
             if self.mandatory:
                 from . import gpx as mod_gpx
-                raise mod_gpx.GPXException('%s is mandatory in %s' % (self.name, self.tag))
+                raise mod_gpx.GPXException('%s is mandatory in %s (got %s)' % (self.name, self.tag, result))
             return None
 
         if self.type_converter:
@@ -152,7 +146,7 @@ class GPXField(AbstractGPXField):
         return result
 
     def to_xml(self, value, version):
-        if not value:
+        if value is None:
             return ''
 
         if self.attribute:
@@ -262,7 +256,7 @@ class GPXExtensionsField(AbstractGPXField):
         return result
 
     def to_xml(self, value, version):
-        if value is None or not value:
+        if not value:
             return ''
 
         result = '\n<' + self.tag + '>'
@@ -305,7 +299,7 @@ def gpx_fields_to_xml(instance, tag, version, custom_attributes=None):
             value = getattr(instance, gpx_field.name)
             if gpx_field.attribute:
                 body += ' ' + gpx_field.to_xml(value, version)
-            elif value:
+            elif value is not None:
                 if tag_open:
                     body += '>'
                     tag_open = False
@@ -356,10 +350,10 @@ def gpx_fields_from_xml(class_or_instance, parser, node, version):
 
 def gpx_check_slots_and_default_values(classs):
     """
-    Will fill the default values for this class. Instances will inherit those 
+    Will fill the default values for this class. Instances will inherit those
     values so we don't need to fill default values for every instance.
 
-    This method will also fill the attribute gpx_field_names with a list of 
+    This method will also fill the attribute gpx_field_names with a list of
     gpx field names. This can be used
     """
     fields = classs.gpx_10_fields + classs.gpx_11_fields
diff --git a/gpxpy/gpxxml.py b/gpxpy/gpxxml.py
index dd00127..2794fb8 100644
--- a/gpxpy/gpxxml.py
+++ b/gpxpy/gpxxml.py
@@ -1,7 +1,6 @@
 # -*- coding: utf-8 -*-
 
 import xml.dom.minidom as mod_minidom
-import gpxpy as mod_gpxpy
 
 def split_gpxs(xml):
     """
diff --git a/gpxpy/parser.py b/gpxpy/parser.py
index 278e3f7..b34f3e2 100644
--- a/gpxpy/parser.py
+++ b/gpxpy/parser.py
@@ -16,11 +16,7 @@
 
 from __future__ import print_function
 
-import pdb
-
-import re as mod_re
 import logging as mod_logging
-import datetime as mod_datetime
 import xml.dom.minidom as mod_minidom
 
 try:
@@ -169,13 +165,18 @@ class GPXParser:
 
     def init(self, xml_or_file):
         text = xml_or_file.read() if hasattr(xml_or_file, 'read') else xml_or_file
+        if text[:3] == "\xEF\xBB\xBF": #Remove utf-8 Byte Order Mark (BOM) if present
+            text = text[3:]
         self.xml = mod_utils.make_str(text)
         self.gpx = mod_gpx.GPX()
 
-    def parse(self):
+    def parse(self, version = None):
         """
         Parses the XML file and returns a GPX object.
 
+        version may be '1.0', '1.1' or None (then it will be read from the gpx
+        xml node if possible, if not then version 1.0 will be used).
+
         It will throw GPXXMLSyntaxException if the XML file is invalid or
         GPXException if the XML file is valid but something is wrong with the
         GPX data.
@@ -193,7 +194,7 @@ class GPXParser:
             else:
                 raise mod_gpx.GPXException('Invalid parser type: %s' % self.xml_parser_type)
 
-            self.__parse_dom()
+            self.__parse_dom(version)
 
             return self.gpx
         except Exception as e:
@@ -210,12 +211,13 @@ class GPXParser:
             # it is available with GPXXMLSyntaxException.original_exception:
             raise mod_gpx.GPXXMLSyntaxException('Error parsing XML: %s' % str(e), e)
 
-    def __parse_dom(self):
+    def __parse_dom(self, version = None):
         node = self.xml_parser.get_first_child(name='gpx')
 
         if node is None:
             raise mod_gpx.GPXException('Document must have a `gpx` root node.')
 
-        version = self.xml_parser.get_node_attribute(node, 'version')
+        if version is None:
+            version = self.xml_parser.get_node_attribute(node, 'version')
 
         mod_gpxfield.gpx_fields_from_xml(self.gpx, self.xml_parser, node, version)
diff --git a/setup.py b/setup.py
index d8bdbd1..ddf0fce 100755
--- a/setup.py
+++ b/setup.py
@@ -18,7 +18,7 @@ import distutils.core as mod_distutilscore
 
 mod_distutilscore.setup(
     name='gpxpy',
-    version='1.1.1',
+    version='1.1.2',
     description='GPX file parser and GPS track manipulation library',
     license='Apache License, Version 2.0',
     author='Tomo Krajina',

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/gpxpy.git



More information about the Python-modules-commits mailing list