67 lines
2.3 KiB
Python
67 lines
2.3 KiB
Python
__author__ = 'tyrel'
|
|
from django.core.management.base import BaseCommand, CommandError
|
|
from django.contrib.auth.models import User
|
|
from tracking.models import Track, Segment, Point
|
|
from django.contrib.gis import geos
|
|
import xmltodict
|
|
|
|
import os
|
|
|
|
|
|
class Command(BaseCommand):
|
|
help = 'Import a gpx file'
|
|
|
|
def add_arguments(self, parser):
|
|
parser.add_argument('username', help="Username to import to.")
|
|
parser.add_argument('filepath', help="File or directory to import a gpx file.")
|
|
|
|
def handle(self, *args, **options):
|
|
self.username = options['username']
|
|
filepath = options['filepath']
|
|
|
|
try:
|
|
self.user = User.objects.get(username=self.username)
|
|
except User.DoesNotExist:
|
|
raise CommandError("User {0} does not exist".format(self.username))
|
|
|
|
if os.path.isfile(filepath):
|
|
self.import_file(filepath)
|
|
else:
|
|
raise CommandError("{0} is neither an existing file, nor a path.".format(filepath))
|
|
|
|
def import_file(self, filename):
|
|
with open(filename) as fd:
|
|
obj = dict(xmltodict.parse(fd.read()))['gpx']
|
|
user = self.user
|
|
|
|
create_points_segments_tracks(obj, user)
|
|
|
|
|
|
def create_points_segments_tracks(obj, user):
|
|
# Get some Meta data from the gpx file.
|
|
metadata = obj['metadata']
|
|
start_time = metadata['time']
|
|
# Get the the track and segments from the gpx file.
|
|
tracks = obj['trk']
|
|
name = tracks['name']
|
|
segments = tracks['trkseg']
|
|
# Check unimplemented features
|
|
if isinstance(segments, list):
|
|
raise NotImplemented(
|
|
"Haven't run across one that has multiple segments. "
|
|
"Contribute a patch if you have.")
|
|
elif isinstance(segments, dict):
|
|
track = Track.objects.create(user=user,
|
|
start=start_time,
|
|
name=name)
|
|
points = segments['trkpt']
|
|
segment = Segment(track=track, time=points[0]['time'])
|
|
segment.save()
|
|
for pt in points:
|
|
lat, lon = float(pt['@lat']), float(pt['@lon'])
|
|
poly_pt = geos.Point(lon, lat)
|
|
Point.objects.create(segment=segment,
|
|
time=pt['time'],
|
|
elevation=pt['ele'],
|
|
point=poly_pt)
|