Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
tkrajina committed Dec 24, 2016
2 parents 8abebca + 5ea43e5 commit 2d965f3
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 27 deletions.
26 changes: 19 additions & 7 deletions gpxinfo
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#!/bin/env python
#!/usr/bin/env python

"""
Command line utility to extract basic statistics from a gpx file
Command line utility to extract basic statistics from gpx file(s)
"""

import pdb

import sys as mod_sys
import logging as mod_logging
import math as mod_math
import argparse as mod_argparse

import gpxpy as mod_gpxpy

Expand All @@ -21,10 +22,12 @@ import gpxpy as mod_gpxpy
def format_time(time_s):
if not time_s:
return 'n/a'
minutes = mod_math.floor(time_s / 60.)
hours = mod_math.floor(minutes / 60.)

return '%s:%s:%s' % (str(int(hours)).zfill(2), str(int(minutes % 60)).zfill(2), str(int(time_s % 60)).zfill(2))
elif args.seconds:
return str(int(time_s))
else:
minutes = mod_math.floor(time_s / 60.)
hours = mod_math.floor(minutes / 60.)
return '%s:%s:%s' % (str(int(hours)).zfill(2), str(int(minutes % 60)).zfill(2), str(int(time_s % 60)).zfill(2))


def print_gpx_part_info(gpx_part, indentation=' '):
Expand Down Expand Up @@ -99,5 +102,14 @@ def run(gpx_files):
print('Error processing %s' % gpx_file)
mod_sys.exit(1)


def make_parser():
parser = mod_argparse.ArgumentParser(usage='%(prog)s [-s] [file ...]',
description='Command line utility to extract basic statistics from gpx file(s)')
parser.add_argument('-s', '--seconds', action='store_true',
help='print times as N seconds, rather than HH:MM:SS')
return parser

if __name__ == '__main__':
run(mod_sys.argv[1:])
args, gpx_files = make_parser().parse_known_args()
run(gpx_files)
12 changes: 12 additions & 0 deletions gpxpy/gpxfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import inspect as mod_inspect
import datetime as mod_datetime
import re

from . import utils as mod_utils

Expand All @@ -36,6 +37,17 @@ def parse_time(string):
string = string.replace('Z', '')
if '.' in string:
string = string.split('.')[0]
if len(string) > 19:
# 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 = 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 mod_gpx.DATE_FORMATS:
try:
return mod_datetime.datetime.strptime(string, date_format)
Expand Down
8 changes: 1 addition & 7 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,16 @@ test: test-py2 test-py3
echo 'OK'
test-py3:
python3 -m unittest test
SAXCount -v=always -n -s -f validation_gpx10.gpx
SAXCount -v=always -n -s -f validation_gpx11.gpx
rm validation_gpx*gpx
test-py2:
python -m unittest test
SAXCount -v=always -n -s -f validation_gpx10.gpx
SAXCount -v=always -n -s -f validation_gpx11.gpx
rm validation_gpx*gpx
check-all-commited:
if [ -n "$(GIT_PORCELAIN_STATUS)" ]; \
then \
echo 'YOU HAVE UNCOMMITED CHANGES'; \
git status; \
exit 1; \
fi
pypi-upload: test check-all-commited
pypi-upload: check-all-commited test
python setup.py register
python setup.py sdist upload --sign
ctags:
Expand Down
54 changes: 43 additions & 11 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import logging as mod_logging
import os as mod_os
import time as mod_time
import codecs as mod_codecs
import copy as mod_copy
import datetime as mod_datetime
import random as mod_random
Expand Down Expand Up @@ -100,6 +101,14 @@ def equals(object1, object2, ignore=None):
return True


def custom_open(filename, encoding=None):
if PYTHON_VERSION[0] == '3':
return open(filename, encoding=encoding)
elif encoding == 'utf-8':
mod_codecs.open(filename, encoding='utf-7')
return open(filename)


def cca(number1, number2):
return 1 - number1 / number2 < 0.999

Expand Down Expand Up @@ -150,10 +159,7 @@ def get_parser_type(self):
raise Exception('Implement this in subclasses')

def parse(self, file, encoding=None, version = None):
if PYTHON_VERSION[0] == '3':
f = open('test_files/%s' % file, encoding=encoding)
else:
f = open('test_files/%s' % file)
f = custom_open('test_files/%s' % file, encoding=encoding)

parser = mod_parser.GPXParser(f, parser=self.get_parser_type())
gpx = parser.parse(version)
Expand Down Expand Up @@ -182,7 +188,7 @@ def test_parse_with_all_parser_types(self):

def test_simple_parse_function(self):
# Must not throw any exception:
mod_gpxpy.parse(open('test_files/korita-zbevnica.gpx'), parser=self.get_parser_type())
mod_gpxpy.parse(custom_open('test_files/korita-zbevnica.gpx', encoding='utf-8'), parser=self.get_parser_type())

def test_simple_parse_function_invalid_xml(self):
try:
Expand Down Expand Up @@ -316,19 +322,19 @@ def test_unicode_name(self):
self.assertTrue(make_str(name) == 'šđčćž')

def test_unicode_2(self):
parser = mod_parser.GPXParser(open('test_files/unicode2.gpx'), parser=self.get_parser_type())
parser = mod_parser.GPXParser(custom_open('test_files/unicode2.gpx', encoding='utf-8'), parser=self.get_parser_type())
gpx = parser.parse()
gpx.to_xml()

def test_unicode_bom(self):
gpx = self.parse('unicode_with_bom.gpx')
gpx = self.parse('unicode_with_bom.gpx', encoding='utf-8')

name = gpx.waypoints[0].name

self.assertTrue(make_str(name) == 'test')

def test_force_version(self):
gpx = self.parse('unicode_with_bom.gpx', version = '1.1')
gpx = self.parse('unicode_with_bom.gpx', version = '1.1', encoding='utf-8')

security = gpx.waypoints[0].extensions['security']

Expand Down Expand Up @@ -1496,15 +1502,15 @@ def test_distance_from_line(self):
def test_simplify(self):
for gpx_file in mod_os.listdir('test_files'):
print('Parsing:', gpx_file)
gpx = mod_gpxpy.parse(open('test_files/%s' % gpx_file), parser=self.get_parser_type())
gpx = mod_gpxpy.parse(custom_open('test_files/%s' % gpx_file, encoding='utf-8'), parser=self.get_parser_type())

length_2d_original = gpx.length_2d()

gpx = mod_gpxpy.parse(open('test_files/%s' % gpx_file), parser=self.get_parser_type())
gpx = mod_gpxpy.parse(custom_open('test_files/%s' % gpx_file, encoding='utf-8'), parser=self.get_parser_type())
gpx.simplify(max_distance=50)
length_2d_after_distance_50 = gpx.length_2d()

gpx = mod_gpxpy.parse(open('test_files/%s' % gpx_file), parser=self.get_parser_type())
gpx = mod_gpxpy.parse(custom_open('test_files/%s' % gpx_file, encoding='utf-8'), parser=self.get_parser_type())
gpx.simplify(max_distance=10)
length_2d_after_distance_10 = gpx.length_2d()

Expand Down Expand Up @@ -1615,6 +1621,11 @@ def test_adjust_time(self):
self.assertEquals(gpx.tracks[0].segments[1].points[0].time, mod_datetime.datetime(2013, 1, 2, 12, 30, 1))
self.assertEquals(gpx.tracks[0].segments[1].points[1].time, mod_datetime.datetime(2013, 1, 2, 12, 31, 1))

def test_unicode(self):
parser = mod_parser.GPXParser(custom_open('test_files/unicode2.gpx', encoding='utf-8'), parser=self.get_parser_type())
gpx = parser.parse()
gpx.to_xml()

def test_location_delta(self):
location = mod_geo.Location(-20, -50)

Expand Down Expand Up @@ -2790,6 +2801,27 @@ def test_zero_latlng(self):
self.assertEquals(0, gpx2.tracks[0].segments[0].points[0].longitude)
self.assertEquals(0, gpx2.tracks[0].segments[0].points[0].elevation)

def test_remove_timezone_from_timestamp(self):
xml = '<?xml version="1.0" encoding="UTF-8"?>\n'
xml += '<gpx>\n'
xml += '<trk>\n'
xml += '<trkseg>\n'
xml += '<trkpt lat="35.794159" lon="-5.832745"><time>2014-02-02T10:23:18Z+01:00</time></trkpt>\n'
xml += '</trkseg></trk></gpx>\n'
gpx = mod_gpxpy.parse(xml, parser=self.get_parser_type())
self.assertEquals(gpx.tracks[0].segments[0].points[0].time, mod_datetime.datetime(2014, 2, 2, 10, 23, 18))

def test_timestamp_with_single_digits(self):
xml = '<?xml version="1.0" encoding="UTF-8"?>\n'
xml += '<gpx>\n'
xml += '<trk>\n'
xml += '<trkseg>\n'
xml += '<trkpt lat="35.794159" lon="-5.832745"><time>2014-2-2T2:23:18Z-02:00</time></trkpt>\n'
xml += '</trkseg></trk></gpx>\n'
gpx = mod_gpxpy.parse(xml, parser=self.get_parser_type())
self.assertEquals(gpx.tracks[0].segments[0].points[0].time, mod_datetime.datetime(2014, 2, 2, 2, 23, 18))


class LxmlTests(mod_unittest.TestCase, AbstractTests):
def get_parser_type(self):
return 'lxml'
Expand Down
2 changes: 1 addition & 1 deletion validation_gpx10.gpx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?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" xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd" version="1.0" creator="...">
<gpx xmlns="http://www.topografix.com/GPX/1/0" xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0" creator="...">
<name>example name</name>
<desc>example description</desc>
<author>example author</author>
Expand Down
2 changes: 1 addition & 1 deletion validation_gpx11.gpx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<gpx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.topografix.com/GPX/1/1" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" version="1.1" creator="...">
<gpx xmlns="http://www.topografix.com/GPX/1/1" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.1" creator="...">
<metadata>
<name>example name</name>
<desc>example description</desc>
Expand Down

0 comments on commit 2d965f3

Please sign in to comment.