How to use the gpxpy.gpx.GPXException function in gpxpy

To help you get started, we’ve selected a few gpxpy examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github tkrajina / gpxpy / gpxpy / gpx.py View on Github external
If the GPX file currently contains time data, it will be overwritten, unless the force flag is set to False, in
        which case the function will return a GPXException error.

        Parameters
        ----------
        start_time: datetime.datetime object
            Start time of the GPX file (corresponds to the time of the first point)
        time_delta: datetime.timedelta object
            Time interval between two points in the GPX file
        end_time: datetime.datetime object
            End time of the GPX file (corresponds to the time of the last point)
        force: bool
            Overwrite current data if the GPX file currently contains time data
        """
        if not (start_time and end_time) and not (start_time and time_delta) and not (time_delta and end_time):
            raise GPXException('You must provide at least two parameters among start_time, time_step, and end_time')

        if self.has_times() and not force:
            raise GPXException('GPX file currently contains time data. Use force=True to overwrite.')

        point_no = self.get_points_no()

        if start_time and end_time:
            if start_time > end_time:
                raise GPXException('Invalid parameters: end_time must occur after start_time')
            time_delta = (end_time - start_time) / (point_no - 1)
        elif not start_time:
            start_time = end_time - (point_no - 1) * time_delta

        self.time = start_time

        i = 0
github tkrajina / gpxpy / gpxpy / gpx.py View on Github external
mod_gpxfield.GPXField('max_longitude', attribute='maxlon', type=mod_gpxfield.FLOAT_TYPE),
    ]

    __slots__ = ('min_latitude', 'max_latitude', 'min_longitude', 'max_longitude')

    def __init__(self, min_latitude=None, max_latitude=None, min_longitude=None, max_longitude=None):
        self.min_latitude = min_latitude
        self.max_latitude = max_latitude
        self.min_longitude = min_longitude
        self.max_longitude = max_longitude

    def __iter__(self):
        return (self.min_latitude, self.max_latitude, self.min_longitude, self.max_longitude,).__iter__()


class GPXXMLSyntaxException(GPXException):
    """
    Exception used when the XML syntax is invalid.

    The __cause__ can be a minidom or lxml exception (See http://www.python.org/dev/peps/pep-3134/).
    """
    def __init__(self, message, original_exception):
        GPXException.__init__(self, message)
        self.__cause__ = original_exception


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',
github tkrajina / gpxpy / gpxpy / gpx.py View on Github external
def to_xml(self, version=None, prettyprint=True):
        """
        FIXME: Note, this method will change self.version
        """
        if not version:
            if self.version:
                version = self.version
            else:
                version = '1.1'

        if version != '1.0' and version != '1.1':
            raise GPXException('Invalid version %s' % version)

        self.version = version
        if not self.creator:
            self.creator = 'gpx.py -- https://github.com/tkrajina/gpxpy'

        self.nsmap['xsi'] = 'http://www.w3.org/2001/XMLSchema-instance'

        version_path = version.replace('.', '/')

        self.nsmap['defaultns'] = 'http://www.topografix.com/GPX/{0}'.format(
            version_path
        )

        if not self.schema_locations:
            self.schema_locations = [
                p.format(version_path) for p in (
github tkrajina / gpxpy / gpxpy / gpxfield.py View on Github external
def __init__(self, name, tag=None, attribute=None, type=None,
                 possible=None, mandatory=None):
        AbstractGPXField.__init__(self)
        self.name = name
        if tag and attribute:
            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
        elif tag:
            self.tag = name if tag is True else tag
            self.attribute = None
        else:
            self.tag = name
            self.attribute = None
        self.type_converter = type
        self.possible = possible
        self.mandatory = mandatory
github tkrajina / gpxpy / gpxpy / parser.py View on Github external
except Exception as e:
            # The exception here can be a lxml or ElementTree exception.
            log.debug('Error in:\n%s\n-----------\n' % self.xml, exc_info=True)

            # The library should work in the same way regardless of the
            # underlying XML parser that's why the exception thrown
            # here is GPXXMLSyntaxException (instead of simply throwing the
            # original ElementTree or lxml exception e).
            #
            # But, if the user needs the original exception (lxml or ElementTree)
            # it is available with GPXXMLSyntaxException.original_exception:
            raise mod_gpx.GPXXMLSyntaxException('Error parsing XML: %s' % str(e), e)

        if root is None:
            raise mod_gpx.GPXException('Document must have a `gpx` root node.')

        if version is None:
            version = root.get('version')

        mod_gpxfield.gpx_fields_from_xml(self.gpx, root, version)
        return self.gpx
github tkrajina / gpxpy / gpxpy / gpxfield.py View on Github external
def parse_time(string):
    from . import gpx as mod_gpx
    if not string:
        return None
    m = RE_TIMESTAMP.match(string)
    if m:
        dt = [int(m.group(i)) for i in range(1, 7)]
        if m.group(7):
            f = m.group(7)[1:7]
            dt.append(int(f + "0" * (6 - len(f))))
        else:
            dt.append(0)
        dt.append(SimpleTZ(m.group(8)))
        return mod_datetime.datetime(*dt)
    raise mod_gpx.GPXException('Invalid time: {0}'.format(string))
github tkrajina / gpxpy / gpxpy / gpx.py View on Github external
def __init__(self, message, original_exception):
        GPXException.__init__(self, message)
        self.__cause__ = original_exception
github tkrajina / gpxpy / gpxpy / timestamps.py View on Github external
# remove the timezone part
        d = max(string.rfind('+'), string.rfind('-'))
        string = string[0:d]
    if len(string) < 19:
        # string has some single digits
        p = '^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2}).*$'
        s = mod_re.findall(p, string)
        if len(s) > 0:
            string = '{0}-{1:02d}-{2:02d} {3:02d}:{4:02d}:{5:02d}'\
                .format(*[int(x) for x in s[0]])
    for date_format in DATE_FORMATS:
        try:
            return mod_datetime.datetime.strptime(string, date_format)
        except ValueError:
            pass
    raise mod_gpx.GPXException('Invalid time: %s' % string)