GPX File Reader - initial requirements.

Yeah so as I've announced at Github Pages and Travis auto Deployment, I'm starting work on GPX File reader.

Let's first create a requirements on this task.

What's our assumptions ?

1. We will not invent wheel again

I'm not going to create another library that handles reading gpx files them-selves, since It's not the main activity for the project.

I'll focus on reading information that maybe of great interest for this particular project.

2. GPX Reader will support different gpx-formats

Reader will need to support different type of gpx-file formats. Starting from Endomondo/Strava/ and alternatives-to-endomondo stopping at (if needed) my own gpx-format created by Android-sub project.

I do hope, that library it-self will support it, but taking into account that gpx files are just an xml files, I have high-hopes.

3. GPX Reader needs to be extensible via plugins-engine

Project assumes that this reader will not be "universal", and being so- I'll maybe need to use it someday (even in different type of project) and then I'll just create a plugin to already working solution.

Besides I haven't yet done something like plugin-engine extensions - and it looks like something very interesting.

4. What data this project needs ?

For this project I'll read information like gpx locations, timings between each location, routes as a bunch of locations. That data will be very important and can be used for statistics analysis and - story-telling - for gathering endorphine-badges.

What are the engines to read gpx files?

1. Manual reading information from xml

GPX files in it's core are just xml files, hence you can read them as usual xml files.

Check this libraries that handles xml read:

But BE-AWARE! - this maybe harmfull to your neuronic-system and cause some neurotic issues :) since you'll need to scrap all valid information i.e. like all coordinates for route by yourself manually with just xml library.

2. GPXPY library

Some of the features ( i.e. gathering routes not by using XPATH search )that this particular library contains, makes it almost perfect match for this particular job.

GPXPY

So I'll stick to it. As I do recall properly I've been using this library for tests in my previous 'abandoned' project - the test file.

Prerequisites - How to use GPXPY ?

1. Installation of gpxpy

First of all you need to install python library:

pip install gpxpy

Then JUST USE IT .

Oups sorry, I've meant this link - with example

as a Filipe Fernandes uses it.

2. Find gpx file examples to work with - or train with endomondo and create your-own.

I've searched web and finally I've found nice site that gives some gpx files with routes other users went that you also could upload to your Endomondo/Strava application and "go with this route".

Using this GPX file

3. Example code to check functionality of gpxpy.

import gpxpy
import gpxpy.gpx
gpx_file = open('15212277.gpx', 'r')


# Parsing an existing file:
# -------------------------

gpx = gpxpy.parse(gpx_file)

for track in gpx.tracks:
    for segment in track.segments:
        for point in segment.points:
            print 'Point at ({0},{1}) -> {2}'.format(point.latitude, point.longitude, point.elevation)

for waypoint in gpx.waypoints:
    print 'waypoint {0} -> ({1},{2})'.format(waypoint.name, waypoint.latitude, waypoint.longitude)

for route in gpx.routes:
    print 'Route:'
    for point in route.points:
        print 'Point at ({0},{1}) -> {2}'.format(point.latitude, point.longitude, point.elevation)

# gpx_parsed = gpxpy.parse(open(gpx_file)) # This will fail with error of coercing which means that you already have file opened for reading and trying to open it again, those failing.

Source-Code.Py

GPX-example-file

4. Where to put your business logic code for importing data from gpx files at Django Framework?

That was the hardest question I could not find clear answer - Then I've finally found out why - there is no good "answer for all business logics"

It all depends on what your business logic will do. If it has any type of connections with Model and will reuse of model-api than you should put your business logic there. The same applies to Views.

The only Rule of Thumb you should follow is not to overload model/view or controller.

Since our importing methods will only read data from gpx files, I've decided to move to utils.py file as suggested here -> business-logic-in-django-at-stackoverflow.

Geo-Location and other possibilities out-there.

While investigating python gpx libraries I've found library that I might use for GeoCoding.

It's a GeoPy - a Geocoding Library for Python - quoting authors.

I've also found that there is an Geo-framework for Django called GeoDjango

Code commits done for this post:

Unfortunatelly None. I didn't have proper time to sit and code with gpxpy. That's why I introduce "Things I've found worth looking into" :)

Tools and applications used:

  • Vi/Vim
  • VimFX plugin for firefox
  • Docker

Things I've found worth looking into.

1. PyTrainer

Wow I've just found some interesting project called PyTrainer - quoting official github repo info: Pytrainer is a tool to log all your sport excursion coming from GPS devices (with a focus on ForeRunner) or GPX (http://www.topografix.com) files. Pytrainer supports GPS track files and displays it in graphs, maps...

So that's definatelly something to look deeply into someday :)

2. IPython Notebook Viewer.

I've known about this "jupyter" python thing for awhile, but I've forgot about it, and Filipe Fernandes helped me to remind myself about this cool feature!

BTW I think you should definitely check his blog.

3. IPython Notebook are available at github natively.

Basically you can since December 2015 use IPython Notebooks with native github UI, that will render your ipybn files.

Check this cool stuff about Monsoon and cosmic-ray

LL (Lessons Learned)

1. Trying to read gpx files twice will fail with coercing issue.

As I've described it in a sample file with comment, if you try to do something like this:

file_to_open = open("file.txt")

# bla bla bla you do something with it

# and then again:

reading_file = open(file_to_open)

But this time it will not be the same "coercing" issue error in traceback, but still it's the same principle of reusing an open'ed connector to file, to reopen file with it.

Solution:

Find line in which you have the failure and change it to this:

file_to_open = open("file.txt")

# bla bla bla you do something with it

# and then again:

reading_file = open("file.txt")

2. Did not make my commitments

Why I've failed to accomplish creating code ? Well... I'm in a learning curve on working alone in project and still failing at planning part.

Solution:

Start new post-blogs with plan and then stick with it.

Acknowledgements

How do I parse xml in python at stackoverflow

Pytrainer

Visualizing Strava Tracks with python

VimFX - vim plugin for firefox

What's next

1. Implementation of GPX Reader with plugin-engine.

2. Check my Issue list for Biking Endorphines project.



Comments

comments powered by Disqus