def save(self, *args, **kwargs): ''' Calculate extra values before save ''' # Save first to get id super(Segment, self).save(*args, **kwargs) # Create gpxtrack for this segment if not self.gpx_file: for slope in self.get_slopes(): if slope.exercise.route and slope.exercise.route.gpx_file: trip = slope.exercise tripdetails = trip.get_details().all() if filldistance(tripdetails): i = 0 start, stop= 0, 0 for d in tripdetails: if not start and d.distance >= slope.start*1000: start = i elif start: if d.distance > (slope.start*1000+ slope.length): stop = i break i += 1 tripdetails = tripdetails[start:stop] g = GPXWriter(tripdetails) if g.points: # don't write gpx if not lon/lat-trip filename = 'gpx/segment/%s.gpx' %self.id self.gpx_file.save(filename, ContentFile(g.xml), save=True) break # generate png if it doesn't exist (after save, it uses id for filename) if self.gpx_file: filename = 'svg/segment/%s.png' %self.id if not gpxstore.exists(filename): create_png_from_gpx.delay(self.gpx_file.path, filename) for attr in ('ascent', 'grade', 'length', 'start_lon', 'start_lat', 'end_lon', 'end_lat'): for slope in self.get_slopes(): slopeattr = attr if attr == 'length': # omg slopeattr = 'distance' # so silly if not getattr(self, slopeattr): slopeval = getattr(slope, attr) if slopeval: if slopeattr == 'distance': # this design is so silly, why vary between m and km? slopeval = slopeval/1000 setattr(self, slopeattr, slopeval) self.category = get_category(self.grade, self.distance*1000) super(Segment, self).save(*args, **kwargs)
def save(self, force_insert=False, force_update=False): # If we have gpx file set but not start_lat set, parse gpx and set start and end positions if self.gpx_file: if not self.start_lat or not self.distance or not self.ascent: g = GPXParser(self.gpx_file.file) if g: # set coordinates for route if it doesn't exist if not self.start_lat: self.start_lon = g.start_lon self.start_lat = g.start_lat self.end_lon = g.end_lon self.end_lat = g.end_lat if not self.distance: # distance calculated in meters in parser self.distance = g.distance/1000.0 if not self.ascent: self.ascent = g.ascent self.descent = g.descent # No name, possibly autogenerated route, try and set name if not self.name: self.set_geo_title() # Check for single serving that really are not if self.single_serving and self.exercise_set.count() > 1: self.single_serving = False elif not self.single_serving and self.description == AUTOROUTE_DESCRIPTION and self.exercise_set.count() < 2: self.single_serving = True super(Route, self).save(force_insert, force_update) if self.gpx_file: # generate svg if it doesn't exist (after save, it uses id for filename) filename = 'svg/%s.png' %self.id if not gpxstore.exists(filename): create_png_from_gpx.delay(self.gpx_file.path, filename) # generate simplified gpx to use as layer, for faster loading filename = 'gpx/%s.simpler.gpx' %self.id if not gpxstore.exists(filename): create_simplified_gpx.delay(self.gpx_file.path, filename)
def save(self, *args, **kwargs): ''' Calculate extra values before save ''' # Save first to get id super(Segment, self).save(*args, **kwargs) # Create gpxtrack for this segment if not self.gpx_file: for slope in self.get_slopes(): if slope.exercise.route and slope.exercise.route.gpx_file: trip = slope.exercise tripdetails = trip.get_details().all() if filldistance(tripdetails): i = 0 start, stop= 0, 0 for d in tripdetails: if not start and d.distance >= slope.start*1000: start = i elif start: if d.distance > (slope.start*1000+ slope.length): stop = i break i += 1 tripdetails = tripdetails[start:stop] g = GPXWriter(tripdetails) if g.points: # don't write gpx if not lon/lat-trip filename = 'gpx/segment/%s.gpx' %self.id self.gpx_file.save(filename, ContentFile(g.xml), save=True) break #gradobj = SegmentAltitudeGradient.objects.filter(segment=self.id).exists() #if not gradobj: SegmentAltitudeGradient.objects.filter(segment=self.id).delete() if True: # TODO somehow figure out a way to get the sanest result # No Gradient Found, try and generate for slope in self.get_slopes(): trip = slope.exercise tripdetails = trip.get_details().all() i = 0 start, stop = 0, 0 for d in tripdetails: if not start and d.distance >= slope.start*1000: start = i elif start: if d.distance > (slope.start*1000+ slope.length): stop = i break i += 1 tripdetails = tripdetails[start:stop] lons = [d.lon for d in tripdetails] lats = [d.lat for d in tripdetails] distances, gradients, altitudes = getgradients(tripdetails) if distances: d_offset = distances[0] for i in xrange(0, len(tripdetails)): sag = SegmentAltitudeGradient() sag.segment_id = self.id sag.xaxis = distances[i]-d_offset sag.gradient = gradients[i] sag.altitude = altitudes[i] sag.lon = lons[i] sag.lat = lats[i] sag.save() break # only break if we found exercise with distances # generate png if it doesn't exist (after save, it uses id for filename) if self.gpx_file: filename = 'svg/segment/%s.png' %self.id if not gpxstore.exists(filename): create_png_from_gpx.delay(self.gpx_file.path, filename) for attr in ('ascent', 'grade', 'length', 'start_lon', 'start_lat', 'end_lon', 'end_lat'): for slope in self.get_slopes(): slopeattr = attr if attr == 'length': # omg slopeattr = 'distance' # so silly if not getattr(self, slopeattr): slopeval = getattr(slope, attr) if slopeval: if slopeattr == 'distance': # this design is so silly, why vary between m and km? slopeval = slopeval/1000 setattr(self, slopeattr, slopeval) self.category = get_category(self.grade, self.distance*1000) super(Segment, self).save(*args, **kwargs)