2015-09-08 16:55:41 +00:00
|
|
|
from django.contrib.gis.db import models
|
2015-09-08 17:09:03 +00:00
|
|
|
from django.contrib.gis import geos
|
|
|
|
from django.db import transaction
|
2015-09-08 16:55:41 +00:00
|
|
|
|
2015-09-01 03:31:25 +00:00
|
|
|
# Create your models here.
|
2015-09-01 04:47:56 +00:00
|
|
|
from django.contrib.auth.models import User
|
2015-09-01 03:31:25 +00:00
|
|
|
|
|
|
|
|
2015-09-08 17:09:03 +00:00
|
|
|
class TrackManager(models.Manager):
|
|
|
|
def create_from_gpx(cls, 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
|
|
|
|
|
|
|
|
with transaction.atomic():
|
|
|
|
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)
|
|
|
|
return track
|
|
|
|
raise Exception("Track not created, rolling back DB")
|
|
|
|
|
|
|
|
|
2015-09-01 03:31:25 +00:00
|
|
|
class Track(models.Model):
|
2015-09-01 04:47:56 +00:00
|
|
|
user = models.ForeignKey(User, blank=False)
|
2015-09-01 03:31:25 +00:00
|
|
|
start = models.DateTimeField()
|
|
|
|
name = models.CharField(max_length=1024, blank=False)
|
|
|
|
description = models.TextField(blank=True)
|
2015-09-08 17:09:03 +00:00
|
|
|
objects = TrackManager()
|
2015-09-01 03:31:25 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def finish(self):
|
2015-09-01 04:47:56 +00:00
|
|
|
return self.segment_set.order_by("-time")[0].finish
|
2015-09-01 03:31:25 +00:00
|
|
|
|
2015-09-01 05:05:34 +00:00
|
|
|
def __str__(self):
|
|
|
|
return "[{user}] '{name}' from {start} to {finish}".format(
|
|
|
|
user=self.user.username,
|
|
|
|
name=self.name,
|
|
|
|
start=self.start,
|
|
|
|
finish=self.finish)
|
|
|
|
|
2015-09-01 04:50:22 +00:00
|
|
|
|
2015-09-01 03:56:40 +00:00
|
|
|
class Segment(models.Model):
|
|
|
|
track = models.ForeignKey(Track)
|
2015-09-01 04:47:56 +00:00
|
|
|
time = models.DateTimeField()
|
|
|
|
number = models.IntegerField(default=1)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def finish(self):
|
|
|
|
return self.point_set.order_by("-time")[0].time
|
2015-09-01 03:31:25 +00:00
|
|
|
|
2015-09-01 05:05:34 +00:00
|
|
|
def __str__(self):
|
|
|
|
return "'{name}' Segment #{number} at {tme}".format(
|
|
|
|
name=self.track.name,
|
|
|
|
number=self.number,
|
|
|
|
tme=self.time)
|
|
|
|
|
2015-09-01 04:50:22 +00:00
|
|
|
|
2015-09-01 03:31:25 +00:00
|
|
|
class Point(models.Model):
|
|
|
|
time = models.DateTimeField()
|
|
|
|
elevation = models.DecimalField(max_digits=20, decimal_places=2)
|
2015-09-01 03:56:40 +00:00
|
|
|
segment = models.ForeignKey(Segment)
|
2015-09-01 05:05:34 +00:00
|
|
|
|
2015-09-08 16:55:41 +00:00
|
|
|
# GeoDjango-specific: a geometry field (MultiPolygonField), and
|
|
|
|
# overriding the default manager with a GeoManager instance.
|
|
|
|
point = models.PointField()
|
|
|
|
objects = models.GeoManager()
|
|
|
|
|
2015-09-01 05:05:34 +00:00
|
|
|
def __str__(self):
|
|
|
|
return "'{name}' ({lat},{lon}) -> {alt} on {time}".format(
|
2015-09-08 16:55:41 +00:00
|
|
|
lat=self.point.x,
|
|
|
|
lon=self.point.y,
|
2015-09-01 05:05:34 +00:00
|
|
|
alt=self.elevation,
|
|
|
|
time=self.time,
|
|
|
|
name=self.segment.track.name)
|