Example #1
0
    def test_no_json_data(self):
        """
        Request weather for Essen City on 11.02.2017 will raise into 500 - Server error:
            https://www.metaweather.com/de/648820/2017/2/11/

        The API Request will return []:
            https://www.metaweather.com/api/location/648820/2017/2/11/

        MetaWeatherCom().location_day() will raise NoWeatherData
        """
        lat, lon = (51.4109, 6.7828)  # Duisburg -> WOEID: 648820 (Essen, city)
        date = datetime.datetime(year=2017,
                                 month=2,
                                 day=10,
                                 hour=12,
                                 minute=00)

        with requests_mock.mock() as m:
            m.get(
                'https://www.metaweather.com/api/location/search/?lattlong=51.41,6.78',
                headers={'Content-Type': 'application/json'},
                content=fixture_content('metaweather_5141_678.json'))
            m.get('https://www.metaweather.com/api/location/648820/2017/2/10/',
                  headers={'Content-Type': 'application/json'},
                  content=b'[]')
            with self.assertRaises(NoWeatherData):
                temperature, weather_state = meta_weather_com.coordinates2weather(
                    lat, lon, date=date)
Example #2
0
    def test_small_max_seconds(self):
        lat, lon = (51.4109, 6.7828)  # Duisburg -> WOEID: 648820 (Essen, city)
        date = datetime.datetime(year=2018,
                                 month=6,
                                 day=20,
                                 hour=20,
                                 minute=30)

        # Essen City on 21.06.2018
        # https://www.metaweather.com/de/648820/2018/6/20/
        temperature, weather_state = meta_weather_com.coordinates2weather(
            lat, lon, date=date, max_seconds=0.1)

        self.assert_equal_rounded(temperature, 27.94, decimal_places=2)
        self.assertEqual(weather_state, "Light Cloud")
Example #3
0
    def test_no_json_data(self):
        """
        Request weather for Essen City on 11.02.2017 will raise into 500 - Server error:
            https://www.metaweather.com/de/648820/2017/2/11/

        The API Request will return []:
            https://www.metaweather.com/api/location/648820/2017/2/11/

        MetaWeatherCom().location_day() will raise NoWeatherData
        """
        lat, lon = (51.4109, 6.7828)  # Duisburg -> WOEID: 648820 (Essen, city)
        date = datetime.datetime(year=2017,
                                 month=2,
                                 day=10,
                                 hour=12,
                                 minute=00)

        with self.assertRaises(NoWeatherData):
            temperature, weather_state = meta_weather_com.coordinates2weather(
                lat, lon, date=date)
Example #4
0
    def test_small_max_seconds(self):
        lat, lon = (51.4109, 6.7828)  # Duisburg -> WOEID: 648820 (Essen, city)
        date = datetime.datetime(year=2018,
                                 month=6,
                                 day=20,
                                 hour=20,
                                 minute=30)

        with requests_mock.mock() as m:
            m.get(
                'https://www.metaweather.com/api/location/search/?lattlong=51.41,6.78',
                headers={'Content-Type': 'application/json'},
                content=fixture_content('metaweather_5141_678.json'))
            m.get('https://www.metaweather.com/api/location/648820/2018/6/20/',
                  headers={'Content-Type': 'application/json'},
                  content=fixture_content(
                      'metaweather_location_648820_2018_6_20.json'))
            temperature, weather_state = meta_weather_com.coordinates2weather(
                lat, lon, date=date, max_seconds=0.1)

            self.assert_equal_rounded(temperature, 27.94, decimal_places=2)
            self.assertEqual(weather_state, "Light Cloud")
Example #5
0
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
Example #6
0
    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