def calculate_values(*, gpx_track): if not gpx_track.gpx: return gpxpy_instance = gpx_track.get_gpxpy_instance() gpx_track.points_no = gpxpy_instance.get_points_no() gpx_track.length = gpxpy_instance.length_3d() try: ideal_distances_qs = DistanceModel.objects.filter( min_distance_m__lte=gpx_track.length, max_distance_m__gte=gpx_track.length) except DistanceModel.DoesNotExist: pass else: ideal_distance_count = ideal_distances_qs.count() if ideal_distance_count > 1: log.error("Found more the one ideal distances for %i Meters", gpx_track.length) ideal_distances_qs = ideal_distances_qs.order_by("distance_km") ideal_distance = ideal_distances_qs[0] elif ideal_distance_count == 1: ideal_distance = ideal_distances_qs.get() else: ideal_distance = None if ideal_distance: gpx_track.ideal_distance = ideal_distance log.debug("Set ideal distance to %s", gpx_track.ideal_distance) # e.g: GPX without a track return 0 duration = gpxpy_instance.get_duration() if duration: gpx_track.duration_s = duration gpx_track.calc_pace() uphill_downhill = gpxpy_instance.get_uphill_downhill() gpx_track.uphill = uphill_downhill.uphill gpx_track.downhill = uphill_downhill.downhill elevation_extremes = gpxpy_instance.get_elevation_extremes() gpx_track.min_elevation = elevation_extremes.minimum gpx_track.max_elevation = elevation_extremes.maximum identifier = get_identifier(gpxpy_instance) gpx_track.start_time = identifier.start_time gpx_track.finish_time = identifier.finish_time gpx_track.start_latitude = identifier.start_lat gpx_track.start_longitude = identifier.start_lon gpx_track.finish_latitude = identifier.finish_lat gpx_track.finish_longitude = identifier.finish_lon if not gpx_track.start_temperature: try: temperature, weather_state = meta_weather_com.coordinates2weather( gpx_track.start_latitude, gpx_track.start_longitude, date=gpx_track.start_time, max_seconds=gpx_track.duration_s, ) except NoWeatherData: log.error("No weather data for start.") else: gpx_track.start_temperature = temperature gpx_track.start_weather_state = weather_state if not gpx_track.finish_temperature: try: temperature, weather_state = meta_weather_com.coordinates2weather( gpx_track.finish_latitude, gpx_track.finish_longitude, date=gpx_track.finish_time, max_seconds=gpx_track.duration_s, ) except NoWeatherData: log.error("No weather data for finish.") else: gpx_track.finish_temperature = temperature gpx_track.finish_weather_state = weather_state if not gpx_track.full_start_address: try: start_address = reverse_geo(gpx_track.start_latitude, gpx_track.start_longitude) except Exception as err: # e.g.: geopy.exc.GeocoderTimedOut: Service timed out log.error("Can't reverse geo: %s" % err) else: gpx_track.short_start_address = start_address.short gpx_track.full_start_address = start_address.full if not gpx_track.full_finish_address: try: finish_address = reverse_geo(gpx_track.finish_latitude, gpx_track.finish_longitude) except Exception as err: # e.g.: geopy.exc.GeocoderTimedOut: Service timed out log.error("Can't reverse geo: %s" % err) else: gpx_track.short_finish_address = finish_address.short gpx_track.full_finish_address = finish_address.full # TODO: Handle other extensions, too. # Garmin containes also 'cad' extension_data = get_extension_data(gpxpy_instance) if extension_data is not None and "hr" in extension_data: heart_rates = extension_data["hr"] gpx_track.heart_rate_min = min(heart_rates) gpx_track.heart_rate_avg = statistics.median(heart_rates) gpx_track.heart_rate_max = max(heart_rates) if not gpx_track.creator: gpx_track.creator = gpxpy_instance.creator
def calculate_values(self): if not self.gpx: return gpxpy_instance = self.get_gpxpy_instance() self.points_no = gpxpy_instance.get_points_no() self.length = gpxpy_instance.length_3d() try: ideal_distances_qs = DistanceModel.objects.filter( min_distance_m__lte=self.length, max_distance_m__gte=self.length, ) except DistanceModel.DoesNotExist: pass else: ideal_distance_count = ideal_distances_qs.count() if ideal_distance_count>1: log.error("Found more the one ideal distances for %i Meters", self.length) ideal_distances_qs = ideal_distances_qs.order_by("distance_km") ideal_distance = ideal_distances_qs[0] elif ideal_distance_count==1: ideal_distance = ideal_distances_qs.get() else: ideal_distance = None if ideal_distance: self.ideal_distance = ideal_distance log.debug("Set ideal distance to %s", self.ideal_distance) # e.g: GPX without a track return 0 duration = gpxpy_instance.get_duration() if duration: self.duration_s = duration self.calc_pace() uphill_downhill = gpxpy_instance.get_uphill_downhill() self.uphill = uphill_downhill.uphill self.downhill = uphill_downhill.downhill elevation_extremes = gpxpy_instance.get_elevation_extremes() self.min_elevation = elevation_extremes.minimum self.max_elevation = elevation_extremes.maximum identifier = get_identifier(gpxpy_instance) self.start_time = identifier.start_time self.finish_time = identifier.finish_time self.start_latitude = identifier.start_lat self.start_longitude = identifier.start_lon self.finish_latitude = identifier.finish_lat self.finish_longitude = identifier.finish_lon if not self.start_temperature: try: temperature, weather_state = meta_weather_com.coordinates2weather( self.start_latitude, self.start_longitude, date=self.start_time, max_seconds=self.duration_s ) except NoWeatherData: log.error("No weather data for start.") else: self.start_temperature = temperature self.start_weather_state = weather_state if not self.finish_temperature: try: temperature, weather_state = meta_weather_com.coordinates2weather( self.finish_latitude, self.finish_longitude, date=self.finish_time, max_seconds=self.duration_s ) except NoWeatherData: log.error("No weather data for finish.") else: self.finish_temperature = temperature self.finish_weather_state = weather_state if not self.full_start_address: try: start_address = reverse_geo(self.start_latitude, self.start_longitude) except Exception as err: # e.g.: geopy.exc.GeocoderTimedOut: Service timed out log.error("Can't reverse geo: %s" % err) else: self.short_start_address = start_address.short self.full_start_address = start_address.full if not self.full_finish_address: try: finish_address = reverse_geo(self.finish_latitude, self.finish_longitude) except Exception as err: # e.g.: geopy.exc.GeocoderTimedOut: Service timed out log.error("Can't reverse geo: %s" % err) else: self.short_finish_address = finish_address.short self.full_finish_address = finish_address.full if not self.track_svg: log.debug("Create SVG from GPX...") svg_string = gpx2svg_string(gpxpy_instance) content = ContentFile(svg_string) # https://docs.djangoproject.com/en/2.0/ref/models/fields/#django.db.models.fields.files.FieldFile.save self.track_svg.save( name="temp.svg", # real file path will be set in self.get_svg_upload_path() content=content, save=False ) # TODO: Handle other extensions, too. # Garmin containes also 'cad' extension_data = get_extension_data(gpxpy_instance) if extension_data is not None and "hr" in extension_data: heart_rates = extension_data["hr"] self.heart_rate_min = min(heart_rates) self.heart_rate_avg = statistics.median(heart_rates) self.heart_rate_max = max(heart_rates) if not self.creator: self.creator = gpxpy_instance.creator