class IntegrationTestsSoilData(unittest.TestCase):

    __owm = owm.OWM(os.getenv('OWM_API_KEY', None))

    def test_call_soil_data(self):

        mgr = self.__owm.agro_manager()

        # check if any previous polygon exists on this account
        n_old_polygons = len(mgr.get_polygons())

        # create pol1
        geopol1 = GeoPolygon([[[-121.1958, 37.6683], [-121.1779, 37.6687],
                               [-121.1773, 37.6792], [-121.1958, 37.6792],
                               [-121.1958, 37.6683]]])
        test_pol = mgr.create_polygon(geopol1, 'soil_data_test_pol')

        soil = mgr.soil_data(test_pol)

        self.assertTrue(isinstance(soil, Soil))
        self.assertEqual(test_pol.id, soil.polygon_id)

        # Delete test polygon
        mgr.delete_polygon(test_pol)
        polygons = mgr.get_polygons()
        self.assertEqual(n_old_polygons, len(polygons))
Exemplo n.º 2
0
class TesIntegrationTileManager(unittest.TestCase):

    __owm = owm.OWM(os.getenv('OWM_API_KEY', None))

    def test_tiles_fetch(self):
        mgr = self.__owm.tile_manager(MapLayerEnum.PRECIPITATION)
        tile = mgr.get_tile(3, 6, 7)
        self.assertIsInstance(tile, Tile)
Exemplo n.º 3
0
class IntegrationTestsPollutionAPI30(unittest.TestCase):

    __owm = owm.OWM(os.getenv('OWM_API_KEY', None)).airpollution_manager()

    def test_coindex_around_coords(self):
        """
        Test feature: get CO index around geo-coordinates.
        """
        u = self.__owm.coindex_around_coords(45, 9)
        self.assertIsNotNone(u)
        self.assertIsNotNone(u.co_samples)
        self.assertIsNotNone(u.reception_time())
        self.assertIsNotNone(u.reference_time())
        self.assertIsNone(u.interval)
        self.assertIsNotNone(u.location)

    def test_ozone_around_coords(self):
        """
        Test feature: get ozone around geo-coordinates.
        """
        u = self.__owm.ozone_around_coords(0.0,
                                           10.0,
                                           start='2016-12-31 12:55:55+00:00')
        self.assertIsNotNone(u)
        self.assertIsNotNone(u.du_value)
        self.assertIsNotNone(u.reception_time())
        self.assertIsNotNone(u.reference_time())
        self.assertIsNone(u.interval)
        self.assertIsNotNone(u.location)

    def test_no2index_around_coords(self):
        """
        Test feature: get NO2 index around geo-coordinates.
        """
        u = self.__owm.no2index_around_coords(
            0.0, 10.0, start='2016-12-31 12:55:55+00:00')
        self.assertIsNotNone(u)
        self.assertIsNotNone(u.no2_samples)
        self.assertIsNotNone(u.reception_time())
        self.assertIsNotNone(u.reference_time())
        self.assertIsNone(u.interval)
        self.assertIsNotNone(u.location)

    def test_so2index_around_coords(self):
        """
        Test feature: get SO2 index around geo-coordinates.
        """
        u = self.__owm.so2index_around_coords(
            0.0, 10.0, start='2016-12-31 12:55:55+00:00')
        self.assertIsNotNone(u)
        self.assertIsNotNone(u.so2_samples)
        self.assertIsNotNone(u.reception_time())
        self.assertIsNotNone(u.reference_time())
        self.assertIsNone(u.interval)
        self.assertIsNotNone(u.location)
Exemplo n.º 4
0
class IntegrationTestsPollutionAPI30(unittest.TestCase):

    __owm = owm.OWM(os.getenv('OWM_API_KEY', None)).airpollution_manager()

    def test_air_quality_at_coords(self):
        """
        Test feature: get all air quality data around geo-coordinates.
        """
        airstatus = self.__owm.air_quality_at_coords(45, 9)
        self.assertIsNotNone(airstatus)
        self.assertIsNotNone(airstatus.air_quality_data)
        self.assertIsNotNone(airstatus.reception_time())
        self.assertIsNotNone(airstatus.reference_time())
        self.assertIsNotNone(airstatus.location)

    def test_air_quality_forecast_at_coords(self):
        """
        Test feature: get all forecasted air quality data around geo-coordinates.
        """
        list_of_airstatuses = self.__owm.air_quality_forecast_at_coords(45, 9)
        self.assertTrue(list_of_airstatuses)
        for airstatus in list_of_airstatuses:
            self.assertIsNotNone(airstatus.air_quality_data)
            self.assertIsNotNone(airstatus.reception_time())
            self.assertIsNotNone(airstatus.reference_time())
            self.assertIsNotNone(airstatus.location)

    def test_air_quality_history_at_coords(self):
        """
        Test feature: get historical air quality data around geo-coordinates.
        """
        start = 1606223802  # Tuesday, November 24, 2020

        list_of_airstatuses = self.__owm.air_quality_history_at_coords(
            45, 9, start)
        self.assertIsInstance(list_of_airstatuses, list)
        for airstatus in list_of_airstatuses:
            self.assertIsNotNone(airstatus.air_quality_data)
            self.assertIsNotNone(airstatus.reception_time())
            self.assertIsNotNone(airstatus.reference_time())
            self.assertIsNotNone(airstatus.location)
Exemplo n.º 5
0
class IntegrationTestsGeocodingAPI(unittest.TestCase):

    __owm = owm.OWM(os.getenv('OWM_API_KEY', None))

    def test_geocode(self):
        mgr = self.__owm.geocoding_manager()

        # Geocode all Paris in the United States
        locations = mgr.geocode('Paris', 'US')
        self.assertTrue(isinstance(locations, list))
        self.assertTrue(all([isinstance(l, Location) for l in locations]))
        self.assertTrue(all([l.name == 'Paris' and l.country == 'US' for l in locations]))

    def test_reverse_geocode(self):
        mgr = self.__owm.geocoding_manager()

        # Reverse geocode the geocoords for Florence (Italy)
        locations = mgr.reverse_geocode(43.783731, 11.246603)
        self.assertTrue(isinstance(locations, list))
        self.assertTrue(all([isinstance(l, Location) for l in locations]))
        self.assertTrue(all([l.name == 'Firenze' and l.country == 'IT' for l in locations]))
class IntegrationTestsUVIndexAPI30(unittest.TestCase):

    __owm = owm.OWM(os.getenv('OWM_API_KEY', None)).uvindex_manager()

    def test_uvindex_around_coords(self):
        """
        Test feature: get UV index around geo-coordinates.
        """
        u = self.__owm.uvindex_around_coords(45, 9)
        self.assertIsNotNone(u)
        self.assertIsNotNone(u.value)
        self.assertIsNotNone(u.reception_time())
        self.assertIsNotNone(u.location)

    def test_uvindex_forecast_around_coords(self):
        """
        Test feature: get UV index forecast around geo-coordinates.
        """
        uv_list = self.__owm.uvindex_forecast_around_coords(45, 9)
        self.assertIsInstance(uv_list, list)
        for item in uv_list:
            self.assertIsNotNone(item.value)
            self.assertIsNotNone(item.reception_time())
            self.assertIsNotNone(item.location)

    def test_uvindex_history_around_coords(self):
        """
        Test feature: get UV index history around geo-coordinates.
        """
        start = datetime(2017, 6, 21)
        end = datetime(2017, 6, 27)
        uv_list = self.__owm.uvindex_history_around_coords(37.7,
                                                           -122.37,
                                                           start,
                                                           end=end)
        self.assertIsInstance(uv_list, list)
        for item in uv_list:
            self.assertIsNotNone(item.value)
            self.assertIsNotNone(item.reception_time())
            self.assertIsNotNone(item.location)
class IntegrationTestsWebAPI25(unittest.TestCase):
    __owm = owm.OWM(os.getenv('OWM_API_KEY', None)).weather_manager()

    def test_weather_at_place(self):
        """
        Test feature: get currently observed weather at specific location
        """
        o1 = self.__owm.weather_at_place('London,GB')
        o2 = self.__owm.weather_at_place('Kiev')
        self.assertTrue(o1 is not None)
        self.assertTrue(o1.reception_time() is not None)
        loc = o1.location
        self.assertTrue(loc is not None)
        self.assertTrue(all(v is not None for v in loc.__dict__.values()))
        weat = o1.weather
        self.assertTrue(weat is not None)
        self.assertTrue(o2 is not None)
        self.assertTrue(o2.reception_time() is not None)
        loc = o2.location
        self.assertTrue(loc is not None)
        self.assertTrue(all(v is not None for v in loc.__dict__.values()))
        weat = o2.weather
        self.assertTrue(weat is not None)

    def test_weather_at_coords(self):
        """
        Test feature: get currently observed weather at specific coordinates
        """
        o1 = self.__owm.weather_at_coords(41.896144, 12.484589)  # Rome
        o2 = self.__owm.weather_at_coords(-33.936524, 18.503723)  # Cape Town
        self.assertTrue(o1)
        self.assertTrue(o1.reception_time())
        loc = o1.location
        self.assertTrue(loc)
        self.assertTrue(all(v is not None for v in loc.__dict__.values()))
        weat = o1.weather
        self.assertTrue(weat)
        self.assertTrue(o2)
        self.assertTrue(o2.reception_time())
        loc = o2.location
        self.assertTrue(loc)
        self.assertTrue(all(v is not None for v in loc.__dict__.values()))
        weat = o2.weather
        self.assertTrue(weat)

    def test_weather_at_zipcode(self):
        """
        Test feature: get currently observed weather at specific postcode
        """
        o1 = self.__owm.weather_at_zip_code("94040", "US")
        self.assertTrue(o1)
        self.assertTrue(o1.reception_time())
        loc = o1.location
        self.assertTrue(loc)
        self.assertTrue(all(v is not None for v in loc.__dict__.values()))
        weat = o1.weather
        self.assertTrue(weat)

    def test_weather_at_id(self):
        o1 = self.__owm.weather_at_id(5128581)  # New York
        o2 = self.__owm.weather_at_id(703448)  # Kiev'
        self.assertTrue(o1 is not None)
        self.assertTrue(o1.reception_time() is not None)
        loc = o1.location
        self.assertTrue(loc is not None)
        self.assertTrue(all(v is not None for v in loc.__dict__.values()))
        weat = o1.weather
        self.assertTrue(weat is not None)
        self.assertTrue(o2 is not None)
        self.assertTrue(o2.reception_time() is not None)
        loc = o2.location
        self.assertTrue(loc is not None)
        self.assertTrue(all(v is not None for v in loc.__dict__.values()))
        weat = o2.weather
        self.assertTrue(weat is not None)

    def test_weather_at_ids(self):
        # New York, Kiev
        observations = self.__owm.weather_at_ids([5128581, 703448])
        o1 = observations[0]
        o2 = observations[1]
        self.assertTrue(o1 is not None)
        self.assertTrue(o1.reception_time() is not None)
        loc = o1.location
        self.assertTrue(loc is not None)
        self.assertTrue(all(v is not None for v in loc.__dict__.values()))
        weat = o1.weather
        self.assertTrue(weat is not None)
        self.assertTrue(o2 is not None)
        self.assertTrue(o2.reception_time() is not None)
        loc = o2.location
        self.assertTrue(loc is not None)
        self.assertTrue(all(v is not None for v in loc.__dict__.values()))
        weat = o2.weather
        self.assertTrue(weat is not None)

    def test_weather_at_places(self):
        """
        Test feature: find currently observed weather for locations matching
        the specified text search pattern
        """
        # Test using searchtype=accurate
        o1 = self.__owm.weather_at_places("London", "accurate")
        o2 = self.__owm.weather_at_places("Paris", "accurate", 2)
        self.assertTrue(isinstance(o1, list))
        for item in o1:
            self.assertTrue(item)
            self.assertTrue(item.reception_time())
            loc = item.location
            self.assertTrue(loc is not None)
            self.assertTrue(all(v is not None for v in loc.__dict__.values()))
            weat = item.weather
            self.assertTrue(weat is not None)
        self.assertTrue(isinstance(o2, list))
        self.assertFalse(len(o2) > 2)
        for item in o2:
            self.assertTrue(item)
            self.assertTrue(item.reception_time())
            loc = item.location
            self.assertTrue(loc is not None)
            self.assertTrue(all(v is not None for v in loc.__dict__.values()))
            weat = item.weather
            self.assertTrue(weat is not None)

        # Test using searchtype=like
        o3 = self.__owm.weather_at_places("London", "like")
        o4 = self.__owm.weather_at_places("Paris", "like", 2)
        self.assertTrue(isinstance(o3, list))
        for item in o3:
            self.assertTrue(item)
            self.assertTrue(item.reception_time())
            loc = item.location
            self.assertTrue(loc is not None)
            self.assertTrue(all(v is not None for v in loc.__dict__.values()))
            weat = item.weather
            self.assertTrue(weat is not None)
        self.assertTrue(isinstance(o4, list))
        self.assertFalse(len(o4) > 2)
        for item in o4:
            self.assertTrue(item)
            self.assertTrue(item.reception_time())
            loc = item.location
            self.assertTrue(loc is not None)
            self.assertTrue(all(v is not None for v in loc.__dict__.values()))
            weat = item.weather
            self.assertTrue(weat is not None)

    def test_weather_around_coords(self):
        """
        Test feature: find currently observed weather for locations that are
        nearby the specified coordinates
        """
        o2 = self.__owm.weather_around_coords(57.0, -2.15)  # Scotland
        self.assertTrue(isinstance(o2, list))
        for item in o2:
            self.assertTrue(item is not None)
            self.assertTrue(item.reception_time() is not None)
            loc = item.location
            self.assertTrue(loc is not None)
            self.assertTrue(all(v is not None for v in loc.__dict__.values()))
            weat = item.weather
            self.assertTrue(weat is not None)
        o1 = self.__owm.weather_around_coords(57.0, -2.15, 2)  # Scotland
        self.assertTrue(isinstance(o1, list))
        for item in o1:
            self.assertTrue(item is not None)
            self.assertTrue(item.reception_time() is not None)
            loc = item.location
            self.assertTrue(loc is not None)
            self.assertTrue(all(v is not None for v in loc.__dict__.values()))
            weat = item.weather
            self.assertTrue(weat is not None)

    def test_forecast_at_place_on_3h(self):
        """
        Test feature: get 3 hours forecast for a specific location
        """
        fc1 = self.__owm.forecast_at_place("London,GB", "3h")
        fc2 = self.__owm.forecast_at_place('Kiev', "3h")
        self.assertTrue(fc1)
        f1 = fc1.forecast
        self.assertTrue(f1 is not None)
        self.assertTrue(f1.reception_time() is not None)
        loc = f1.location
        self.assertTrue(loc is not None)
        self.assertTrue(all(v is not None for v in loc.__dict__.values()))
        for weather in f1:
            self.assertTrue(weather is not None)
        self.assertTrue(fc2 is not None)
        f2 = fc2.forecast
        self.assertTrue(f2 is not None)
        self.assertTrue(f2.reception_time() is not None)
        loc = f2.location
        self.assertTrue(loc is not None)
        self.assertTrue(all(v is not None for v in loc.__dict__.values()))
        for weather in f2:
            self.assertTrue(weather is not None)

    def test_forecast_at_coords_on_3h(self):
        """
        Test feature: get 3 hours forecast at a specific geographic coordinate
        """
        # London,uk
        fc1 = self.__owm.forecast_at_coords(51.5073509, -0.1277583, "3h")
        # Kiev
        fc2 = self.__owm.forecast_at_coords(50.4501, 30.5234, "3h")
        self.assertTrue(fc1)
        f1 = fc1.forecast
        self.assertTrue(f1 is not None)
        self.assertTrue(f1.reception_time() is not None)
        loc = f1.location
        self.assertTrue(loc is not None)
        self.assertTrue(all(v is not None for v in loc.__dict__.values()))
        for weather in f1:
            self.assertTrue(weather is not None)
        self.assertTrue(fc2 is not None)
        f2 = fc2.forecast
        self.assertTrue(f2 is not None)
        self.assertTrue(f2.reception_time() is not None)
        loc = f2.location
        self.assertTrue(loc is not None)
        self.assertTrue(all(v is not None for v in loc.__dict__.values()))
        for weather in f2:
            self.assertTrue(weather is not None)
        with self.assertRaises(ValueError):
            self.__owm.forecast_at_coords(199, 199, '3h')

    def test_forecast_at_id_on_3h(self):
        """
        Test feature: get 3 hours forecast for city ID
        """
        # London,uk
        fc1 = self.__owm.forecast_at_id(2643743, '3h')
        # Kiev
        fc2 = self.__owm.forecast_at_id(703448, '3h')
        self.assertTrue(fc1)
        f1 = fc1.forecast
        self.assertTrue(f1 is not None)
        self.assertTrue(f1.reception_time() is not None)
        loc = f1.location
        self.assertTrue(loc is not None)
        self.assertTrue(all(v is not None for v in loc.__dict__.values()))
        for weather in f1:
            self.assertTrue(weather is not None)
        self.assertTrue(fc2 is not None)
        f2 = fc2.forecast
        self.assertTrue(f2 is not None)
        self.assertTrue(f2.reception_time() is not None)
        loc = f2.location
        self.assertTrue(loc is not None)
        self.assertTrue(all(v is not None for v in loc.__dict__.values()))
        for weather in f2:
            self.assertTrue(weather is not None)
        # Unexistent
        try:
            fc3 = self.__owm.forecast_at_id(99999999999999, '3h')
            self.fail()
        except pyowm.commons.exceptions.NotFoundError:
            pass  # ok

    def forecast_at_place_daily(self):
        """
        Test feature: get daily forecast for a specific location
        """
        fc1 = self.__owm.forecast_at_place("London,GB", "daily")
        fc2 = self.__owm.forecast_at_place('Kiev', "daily")
        self.assertTrue(fc1)
        f1 = fc1.forecast
        f1 = fc1.forecast
        self.assertTrue(f1 is not None)
        self.assertTrue(f1.reception_time() is not None)
        loc = f1.location
        self.assertTrue(loc is not None)
        self.assertTrue(all(v is not None for v in loc.__dict__.values()))
        for weather in f1:
            self.assertTrue(weather is not None)
        self.assertTrue(fc2 is not None)
        f2 = fc2.forecast
        self.assertTrue(f2 is not None)
        self.assertTrue(f2.reception_time() is not None)
        loc = f2.location
        self.assertTrue(loc is not None)
        self.assertTrue(all(v is not None for v in loc.__dict__.values()))
        for weather in f2:
            self.assertTrue(weather is not None)

    def test_forecast_at_coords_daily(self):
        """
        Test feature: get daily forecast at a specific geographic coordinate
        """
        fc1 = self.__owm.forecast_at_coords(51.5073509, -0.1277583,
                                            'daily')  # London,uk
        self.assertTrue(fc1)
        f1 = fc1.forecast
        self.assertTrue(f1 is not None)
        self.assertTrue(f1.reception_time() is not None)
        loc = f1.location
        self.assertTrue(loc is not None)
        self.assertTrue(all(v is not None for v in loc.__dict__.values()))
        for weather in f1:
            self.assertTrue(weather is not None)
        with self.assertRaises(ValueError):
            self.__owm.forecast_at_coords(199, 199, 'daily')

    def test_forecast_at_id_daily(self):
        """
        Test feature: get daily forecast for a specific city ID
        """
        # London,uk
        fc1 = self.__owm.forecast_at_id(2643743, 'daily')
        # Kiev
        fc2 = self.__owm.forecast_at_id(703448, 'daily')
        try:
            fc3 = self.__owm.forecast_at_id(99999999, 'daily')
            raise AssertionError("APIRequestError was expected here")
        except pyowm.commons.exceptions.NotFoundError:
            pass  # Ok!
        self.assertTrue(fc1)
        f1 = fc1.forecast
        self.assertTrue(f1 is not None)
        self.assertTrue(f1.reception_time() is not None)
        loc = f1.location
        self.assertTrue(loc is not None)
        self.assertTrue(all(v is not None for v in loc.__dict__.values()))
        for weather in f1:
            self.assertTrue(weather is not None)
        self.assertTrue(fc2 is not None)
        f2 = fc2.forecast
        self.assertTrue(f2 is not None)
        self.assertTrue(f2.reception_time() is not None)
        loc = f2.location
        self.assertTrue(loc is not None)
        self.assertTrue(all(v is not None for v in loc.__dict__.values()))
        for weather in f2:
            self.assertTrue(weather is not None)

    def test_station_tick_history(self):
        """
        Test feature: get station tick weather history for a specific
        meteostation
        """
        try:
            h1 = self.__owm.station_tick_history(39276)
            if h1 is not None:
                sh1 = h1.station_history
                self.assertTrue(sh1 is not None)
                data1 = sh1.measurements
                self.assertTrue(data1 is not None)
                self.assertFalse(0, len(data1))
                h2 = self.__owm.station_tick_history(39276, limit=2)
                self.assertTrue(h2 is not None)
                sh2 = h2.station_history
                self.assertTrue(sh2 is not None)
                data2 = sh2.measurements
                self.assertTrue(data2 is not None)
                self.assertFalse(len(data2) > 2)
                h3 = self.__owm.station_tick_history(987654)  # Shall be None
                self.assertFalse(h3 is not None)
        except pyowm.commons.exceptions.UnauthorizedError:
            pass  # it's a paid-level API feature

    def test_station_hour_history(self):
        """
        Test feature: get station hour weather history for a specific
        meteostation
        """
        try:
            h1 = self.__owm.station_hour_history(123)
            if h1 is not None:
                sh1 = h1.station_history
                self.assertTrue(sh1 is not None)
                data1 = sh1.measurements
                self.assertTrue(data1 is not None)
                self.assertFalse(0, len(data1))
                h2 = self.__owm.station_hour_history(987654)  # Shall be None
                self.assertFalse(h2 is not None)
        except pyowm.commons.exceptions.UnauthorizedError:
            pass  # it's a paid-level API feature

    def test_station_day_history(self):
        """
        Test feature: get station hour weather history for a specific
        meteostation
        """
        try:
            h1 = self.__owm.station_day_history(123)
            if h1 is not None:
                sh1 = h1.station_history
                self.assertTrue(sh1 is not None)
                data1 = sh1.measurements
                self.assertTrue(data1 is not None)
                self.assertFalse(0, len(data1))
                h2 = self.__owm.station_day_history(123, limit=3)
                self.assertTrue(h2 is not None)
                sh2 = h2.station_history
                self.assertTrue(sh2 is not None)
                data2 = sh2.measurements
                self.assertTrue(data2 is not None)
                h3 = self.__owm.station_day_history(987654)  # Shall be None
                self.assertFalse(h3 is not None)
        except pyowm.commons.exceptions.UnauthorizedError:
            pass  # it's a paid-level API feature

    def test_weather_at_places_in_bbox(self):
        o = self.__owm.weather_at_places_in_bbox(0.734720, 38.422663, 1.964651,
                                                 39.397204, 10, False)  # Ibiza
        self.assertTrue(isinstance(o, list))
        for item in o:
            self.assertTrue(item is not None)
            self.assertTrue(item.reception_time() is not None)
            loc = item.location
            self.assertTrue(loc is not None)
            weat = item.weather
            self.assertTrue(weat is not None)

    def test_one_call(self):
        result = self.__owm.one_call(lat=46.49, lon=11.33)
        self.assertTrue(isinstance(result, OneCall))
        self.assertEqual(46.49, result.lat)
        self.assertEqual(11.33, result.lon)
        self.assertEqual("Europe/Rome", result.timezone)
        self.assertTrue(isinstance(result.current, Weather))
        self.assertEqual(48, len(result.forecast_hourly))
        for i, weather in enumerate(result.forecast_hourly):
            self.assertTrue(isinstance(weather, Weather),
                            f"entry {i} of forecast_hourly is invalid")
        self.assertEqual(8, len(result.forecast_daily))
        for i, weather in enumerate(result.forecast_daily):
            self.assertTrue(isinstance(weather, Weather),
                            f"entry {i} of forecast_hourly is invalid")

    def test_one_call_historical(self):
        result = self.__owm.one_call_history(lat=46.49, lon=11.33)
        self.assertTrue(isinstance(result, OneCall))
        self.assertEqual(46.49, result.lat)
        self.assertEqual(11.33, result.lon)
        self.assertEqual("Europe/Rome", result.timezone)
        self.assertTrue(isinstance(result.current, Weather))
        for i, weather in enumerate(result.forecast_hourly):
            self.assertTrue(isinstance(weather, Weather),
                            f"entry {i} of forecast_hourly is invalid")
        self.assertEqual(8, len(result.forecast_daily))
        for i, weather in enumerate(result.forecast_daily):
            self.assertTrue(isinstance(weather, Weather),
                            f"entry {i} of forecast_hourly is invalid")
class IntegrationTestsPolygonsAPISubset(unittest.TestCase):

    __owm = owm.OWM(os.getenv('OWM_API_KEY', None))

    def test_polygons_CRUD(self):

        mgr = self.__owm.agro_manager()

        # check if any previous polygon exists on this account
        n_old_polygons = len(mgr.get_polygons())

        # create pol1
        geopol1 = GeoPolygon([[[-121.1958, 37.6683], [-121.1779, 37.6687],
                               [-121.1773, 37.6792], [-121.1958, 37.6792],
                               [-121.1958, 37.6683]]])
        pol1 = mgr.create_polygon(geopol1, 'polygon_1')

        # create pol2
        geopol2 = GeoPolygon([[[-141.1958, 27.6683], [-141.1779, 27.6687],
                               [-141.1773, 27.6792], [-141.1958, 27.6792],
                               [-141.1958, 27.6683]]])
        pol2 = mgr.create_polygon(geopol2, 'polygon_2')

        # Read all
        polygons = mgr.get_polygons()
        self.assertEqual(n_old_polygons + 2, len(polygons))
        self.assertTrue(all([isinstance(p, Polygon) for p in polygons]))

        # Read one by one
        result = mgr.get_polygon(pol1.id)
        self.assertEqual(pol1.id, result.id)
        self.assertEqual(pol1.name, pol1.name)
        self.assertEqual(pol1.area, result.area)
        self.assertEqual(pol1.user_id, result.user_id)
        self.assertEqual(pol1.center.lon, result.center.lon)
        self.assertEqual(pol1.center.lat, result.center.lat)
        self.assertEqual(pol1.geopolygon.geojson(),
                         result.geopolygon.geojson())

        result = mgr.get_polygon(pol2.id)
        self.assertEqual(pol2.id, result.id)
        self.assertEqual(pol2.name, result.name)
        self.assertEqual(pol2.area, result.area)
        self.assertEqual(pol2.user_id, result.user_id)
        self.assertEqual(pol2.center.lon, result.center.lon)
        self.assertEqual(pol2.center.lat, result.center.lat)
        self.assertEqual(pol2.geopolygon.geojson(),
                         result.geopolygon.geojson())

        # Update a polygon
        pol2.name = 'a better name'
        mgr.update_polygon(pol2)
        result = mgr.get_polygon(pol2.id)
        self.assertEqual(pol2.id, result.id)
        self.assertEqual(pol2.area, result.area)
        self.assertEqual(pol2.user_id, result.user_id)
        self.assertEqual(pol2.center.lon, result.center.lon)
        self.assertEqual(pol2.center.lat, result.center.lat)
        self.assertEqual(pol2.geopolygon.geojson(),
                         result.geopolygon.geojson())
        self.assertNotEqual(pol2.name,
                            pol1.name)  # of course, the name has changed

        # Delete polygons one by one
        mgr.delete_polygon(pol1)
        polygons = mgr.get_polygons()
        self.assertEqual(n_old_polygons + 1, len(polygons))

        mgr.delete_polygon(pol2)
        polygons = mgr.get_polygons()
        self.assertEqual(n_old_polygons, len(polygons))
class IntegrationTestsSatelliteImageryStats(unittest.TestCase):

    __owm = owm.OWM(os.getenv('OWM_API_KEY', None))
    __polygon = None
    __acquired_from = 1500336000  # 18 July 2017
    __acquired_to = 1508976000  # 26 October 2017

    @classmethod
    def setUpClass(cls):
        # create a polygon
        mgr = cls.__owm.agro_manager()
        geopol = GeoPolygon([[[-121.1958, 37.6683], [-121.1779, 37.6687],
                              [-121.1773, 37.6792], [-121.1958, 37.6792],
                              [-121.1958, 37.6683]]])
        cls.__polygon = mgr.create_polygon(geopol, 'stats_test_polygon')

    @classmethod
    def tearDownClass(cls):
        # delete the polygon
        mgr = cls.__owm.agro_manager()
        mgr.delete_polygon(cls.__polygon)

    def test_stats_for_satellite_image(self):
        mgr = self.__owm.agro_manager()

        # search all Landsat 8 images in the specified time frame and with high valid data percentage
        result_set = mgr.search_satellite_imagery(
            self.__polygon.id, self.__acquired_from, self.__acquired_to, None,
            None, None, None, SatelliteEnum.LANDSAT_8.symbol, None, 0.5, 99.5,
            None)
        self.assertIsInstance(result_set, list)
        self.assertTrue(
            all([
                isinstance(i, MetaImage)
                and i.satellite_name == SatelliteEnum.LANDSAT_8.name
                for i in result_set
            ]))

        # only keep EVI and NDVI ones
        ndvi_only = [
            mimg for mimg in result_set if mimg.preset == PresetEnum.NDVI
        ]
        evi_only = [
            mimg for mimg in result_set if mimg.preset == PresetEnum.EVI
        ]

        self.assertTrue(len(ndvi_only) > 1)
        self.assertTrue(len(evi_only) > 1)

        # now search for stats for both types
        stats_ndvi = mgr.stats_for_satellite_image(ndvi_only[0])
        stats_evi = mgr.stats_for_satellite_image(evi_only[0])
        self.assertIsInstance(stats_ndvi, dict)
        self.assertIsInstance(stats_evi, dict)

        # try to search for stats of a non NDVI or EVI image
        falsecolor_only = [
            mimg for mimg in result_set
            if mimg.preset == PresetEnum.FALSE_COLOR
        ]
        with self.assertRaises(ValueError):
            mgr.stats_for_satellite_image(falsecolor_only[0])
class IntegrationTestsSatelliteImagerySearch(unittest.TestCase):

    __owm = owm.OWM(os.getenv('OWM_API_KEY', None))
    __polygon = None
    __acquired_from = 1500336000  # 18 July 2017
    __acquired_to = 1508976000  # 26 October 2017

    @classmethod
    def setUpClass(cls):
        # create a polygon
        mgr = cls.__owm.agro_manager()
        geopol = GeoPolygon([[[-121.1958, 37.6683], [-121.1779, 37.6687],
                              [-121.1773, 37.6792], [-121.1958, 37.6792],
                              [-121.1958, 37.6683]]])
        cls.__polygon = mgr.create_polygon(geopol, 'search_test_polygon')

    @classmethod
    def tearDownClass(cls):
        # delete the polygon
        mgr = cls.__owm.agro_manager()
        mgr.delete_polygon(cls.__polygon)

    # Test methods

    def test_search_all(self):
        mgr = self.__owm.agro_manager()

        # search all images in the specified time frame
        result_set = mgr.search_satellite_imagery(self.__polygon.id,
                                                  self.__acquired_from,
                                                  self.__acquired_to)
        self.assertIsInstance(result_set, list)
        self.assertEqual(len(result_set), 360)
        self.assertTrue(all([isinstance(i, MetaImage) for i in result_set]))

    def test_search_for_one_satellite(self):
        mgr = self.__owm.agro_manager()

        # search all Landsat 8 images in the specified time frame
        result_set = mgr.search_satellite_imagery(
            self.__polygon.id, self.__acquired_from, self.__acquired_to, None,
            None, None, None, SatelliteEnum.LANDSAT_8.symbol, None, None, None,
            None)
        self.assertIsInstance(result_set, list)
        self.assertEqual(len(result_set), 132)
        self.assertTrue(
            all([
                isinstance(i, MetaImage)
                and i.satellite_name == SatelliteEnum.LANDSAT_8.name
                for i in result_set
            ]))

    def test_search_for_geotiff_type_only(self):
        mgr = self.__owm.agro_manager()

        # search all geotiff images in the specified time frame
        result_set = mgr.search_satellite_imagery(self.__polygon.id,
                                                  self.__acquired_from,
                                                  self.__acquired_to,
                                                  ImageTypeEnum.GEOTIFF, None,
                                                  None, None, None, None, None,
                                                  None, None)
        self.assertIsInstance(result_set, list)
        self.assertEqual(len(result_set), 120)
        self.assertTrue(
            all([
                isinstance(i, MetaImage)
                and i.image_type == ImageTypeEnum.GEOTIFF for i in result_set
            ]))

    def test_search_for_ndvi_preset_only(self):
        mgr = self.__owm.agro_manager()

        # search all NDVI images in the specified time frame
        result_set = mgr.search_satellite_imagery(self.__polygon.id,
                                                  self.__acquired_from,
                                                  self.__acquired_to, None,
                                                  PresetEnum.NDVI, None, None,
                                                  None, None, None, None, None)
        self.assertIsInstance(result_set, list)
        self.assertEqual(len(result_set), 90)
        self.assertTrue(
            all([
                isinstance(i, MetaImage) and i.preset == PresetEnum.NDVI
                for i in result_set
            ]))

    def test_search_for_falsecolor_png_only(self):
        mgr = self.__owm.agro_manager()

        # search all PNG images in falsecolor in the specified time frame
        result_set = mgr.search_satellite_imagery(
            self.__polygon.id, self.__acquired_from, self.__acquired_to,
            ImageTypeEnum.PNG, PresetEnum.FALSE_COLOR, None, None, None, None,
            None, None, None)
        self.assertIsInstance(result_set, list)
        self.assertEqual(len(result_set), 60)
        self.assertTrue(
            all([
                isinstance(i, MetaImage) and i.preset == PresetEnum.FALSE_COLOR
                and i.image_type == ImageTypeEnum.PNG for i in result_set
            ]))

    def test_detailed_search(self):
        mgr = self.__owm.agro_manager()

        # in the specified time frame, search all PNG images in truecolor acquired by Sentinel 2
        # and with a max cloud coverage of 5% and at least 90% of valid data coverage
        result_set = mgr.search_satellite_imagery(
            self.__polygon.id, self.__acquired_from, self.__acquired_to,
            ImageTypeEnum.PNG, PresetEnum.TRUE_COLOR, None, None,
            SatelliteEnum.SENTINEL_2.symbol, None, 5, 90, None)
        self.assertIsInstance(result_set, list)
        self.assertEqual(len(result_set), 30)
        self.assertTrue(
            all([
                isinstance(i, MetaImage) and i.preset == PresetEnum.TRUE_COLOR
                and i.image_type == ImageTypeEnum.PNG
                and i.satellite_name == SatelliteEnum.SENTINEL_2.name
                and i.cloud_coverage_percentage <= 5
                and i.valid_data_percentage >= 90 for i in result_set
            ]))
class IntegrationTestsAlertAPI30(unittest.TestCase):

    __owm = owm.OWM(os.getenv('OWM_API_KEY', None))

    cond1 = Condition(WeatherParametersEnum.HUMIDITY, OperatorsEnum.LESS_THAN, 10)
    cond2 = Condition(WeatherParametersEnum.CLOUDS, OperatorsEnum.LESS_THAN, 90)
    start = '2019-07-01 14:17:00+00'
    end = '2019-07-02 14:17:00+00'
    # a rectangle around the city of Moscow
    area1 = geo.Polygon.from_dict({
        "type": "Polygon",
        "coordinates": [
            [
                [
                    36.826171875,
                    55.17259379606185
                ],
                [
                    39.012451171875,
                    55.17259379606185
                ],
                [
                    39.012451171875,
                    56.15778819063682
                ],
                [
                    36.826171875,
                    56.15778819063682
                ],
                [
                    36.826171875,
                    55.17259379606185
                ]
            ]
        ]})
    # somewhere in Dubai

    area2 = geo.Point.from_dict({
        "type": "Point",
        "coordinates": [
            55.29693603515625,
            25.186301620540558
    ]})

    def test_triggers_CRUD(self):

        mgr = self.__owm.alert_manager()

        # check if any previous triggers exist on this account
        n_old_triggers = len(mgr.get_triggers())

        # create trigger1
        trigger1 = mgr.create_trigger(self.start, self.end, conditions=[self.cond1], area=[self.area1])

        # create trigger2
        trigger2 = mgr.create_trigger(self.start, self.end, conditions=[self.cond2], area=[self.area2])

        # Read all created triggers
        triggers = mgr.get_triggers()
        self.assertEqual(n_old_triggers + 2, len(triggers))

        # Read one by one
        result = mgr.get_trigger(trigger1.id)
        self.assertEqual(trigger1.id, result.id)
        self.assertEqual(trigger1.start_after_millis, result.start_after_millis)
        self.assertEqual(trigger1.end_after_millis, result.end_after_millis)
        self.assertEqual(len(trigger1.conditions), len(result.conditions))
        self.assertEqual(len(trigger1.area), len(result.area))

        result = mgr.get_trigger(trigger2.id)
        self.assertEqual(trigger2.id, result.id)
        self.assertEqual(trigger2.start_after_millis, result.start_after_millis)
        self.assertEqual(trigger2.end_after_millis, result.end_after_millis)
        self.assertEqual(len(trigger2.conditions), len(result.conditions))
        self.assertEqual(len(trigger2.area), len(result.area))

        # Update a trigger
        modified_trigger2 = copy.deepcopy(trigger2)
        modified_trigger2.conditions = [self.cond1, self.cond2]

        mgr.update_trigger(modified_trigger2)
        result = mgr.get_trigger(modified_trigger2.id)

        self.assertEqual(modified_trigger2.id, result.id)
        self.assertEqual(modified_trigger2.start_after_millis, result.start_after_millis)
        self.assertEqual(modified_trigger2.end_after_millis, result.end_after_millis)
        self.assertEqual(len(modified_trigger2.area), len(result.area))
        # of course, conditions have been modified with respect to former trigger 2
        self.assertNotEqual(len(trigger2.conditions), len(result.conditions))
        self.assertEqual(len(modified_trigger2.conditions), len(result.conditions))

        # Delete triggers one by one
        mgr.delete_trigger(trigger1)
        triggers = mgr.get_triggers()
        self.assertEqual(n_old_triggers + 1, len(triggers))

        mgr.delete_trigger(modified_trigger2)
        triggers = mgr.get_triggers()
        self.assertEqual(n_old_triggers, len(triggers))
class IntegrationTestsStationsAPI30(unittest.TestCase):

    __owm = owm.OWM(os.getenv('OWM_API_KEY', None))

    def test_stations_CRUD(self):

        mgr = self.__owm.stations_manager()

        # check if any previous station exists on this account
        n_old_stations = len(mgr.get_stations())

        # create stat1
        stat1 = mgr.create_station('PYOWM1', 'pyowm_test_station_1',
                                   45.0, 9.0, 189.0)

        # create stat2
        stat2 = mgr.create_station('PYOWM2', 'pyowm_test_station_2',
                                   46.0, 18.0, 50.0)

        # Read all
        stations = mgr.get_stations()
        self.assertEqual(n_old_stations + 2, len(stations))

        # Read one by one
        result = mgr.get_station(stat1.id)
        self.assertEqual(stat1.id, result.id)
        self.assertEqual(stat1.external_id, result.external_id)
        self.assertEqual(stat1.name, result.name)
        self.assertEqual(stat1.lat, result.lat)
        self.assertEqual(stat1.lon, result.lon)
        self.assertEqual(stat1.alt, result.alt)

        result = mgr.get_station(stat2.id)
        self.assertEqual(stat2.id, result.id)
        self.assertEqual(stat2.external_id, result.external_id)
        self.assertEqual(stat2.name, result.name)
        self.assertEqual(stat2.lat, result.lat)
        self.assertEqual(stat2.lon, result.lon)
        self.assertEqual(stat2.alt, result.alt)

        # Update a station
        modified_stat2 = copy.deepcopy(stat2)
        modified_stat2.eternal = 'modified_pyowm_test_station_2'
        modified_stat2.lat = 30.6
        mgr.update_station(modified_stat2)
        result = mgr.get_station(modified_stat2.id)
        self.assertEqual(modified_stat2.id, result.id)
        self.assertEqual(modified_stat2.external_id, result.external_id)
        self.assertEqual(modified_stat2.name, result.name)
        # of course, lat had been modified
        self.assertEqual(modified_stat2.lon, result.lon)
        self.assertEqual(modified_stat2.alt, result.alt)

        # Delete stations one by one
        mgr.delete_station(stat1)
        stations = mgr.get_stations()
        self.assertEqual(n_old_stations + 1, len(stations))

        mgr.delete_station(modified_stat2)
        stations = mgr.get_stations()
        self.assertEqual(n_old_stations, len(stations))

    def test_measurements_and_buffers(self):
        mgr = self.__owm.stations_manager()

        # check if any previous station exists on this account
        n_old_stations = len(mgr.get_stations())

        # create station
        test_station = mgr.create_station('PYOWM_TEST_BUFFERS', 'pyowm_test_buffers', 45.0, 9.0, 189.0)

        # create and bufferize some measurements for the test station
        buf = Buffer(test_station.id)
        buf.append_from_dict(dict(station_id=test_station.id, timestamp=1505231630,
                        temperature=100, wind_speed=2.1,
                        wind_gust=67, humidex=77))
        buf.append_from_dict(dict(station_id=test_station.id, timestamp=1505429694,
                        temperature=100, wind_speed=2.1,
                        wind_gust=67, humidex=77))
        mgr.send_buffer(buf)

        # now directly send measurements
        measurement = Measurement.from_dict(dict(station_id=test_station.id, timestamp=1505415230,
                        temperature=100, wind_speed=2.1,
                        wind_gust=67, humidex=77))
        measurements_list = [
            Measurement.from_dict(dict(station_id=test_station.id, timestamp=1505315230,
                        temperature=100, wind_speed=2.1,
                        wind_gust=67, humidex=77))
        ]
        mgr.send_measurement(measurement)
        mgr.send_measurements(measurements_list)

        # read the measurements for station
        msmts = mgr.get_measurements(test_station.id, 'd', 1505200000, 1505430000)
        for m in msmts:
            self.assertEqual(test_station.id, m.station_id)
            self.assertEqual('d', m.aggregated_on)

        # Delete stations one by one
        mgr.delete_station(test_station)
        stations = mgr.get_stations()
        self.assertEqual(n_old_stations, len(stations))
Exemplo n.º 13
0
class IntegrationTestsSatelliteImageryDownload(unittest.TestCase):

    __owm = owm.OWM(os.getenv('OWM_API_KEY', None))
    __polygon = None
    __acquired_from = 1500336000  # 18 July 2017
    __acquired_to = 1508976000  # 26 October 2017

    @classmethod
    def setUpClass(cls):
        # create a polygon
        mgr = cls.__owm.agro_manager()
        geopol = GeoPolygon([[[-121.1958, 37.6683], [-121.1779, 37.6687],
                              [-121.1773, 37.6792], [-121.1958, 37.6792],
                              [-121.1958, 37.6683]]])
        cls.__polygon = mgr.create_polygon(geopol, 'search_test_polygon')

    @classmethod
    def tearDownClass(cls):
        # delete the polygon
        mgr = cls.__owm.agro_manager()
        mgr.delete_polygon(cls.__polygon)

    # Test methods

    def test_download_png(self):
        mgr = self.__owm.agro_manager()

        # search PNG, truecolor, non-tile images acquired by Landsat 8
        result_set = mgr.search_satellite_imagery(
            self.__polygon.id, self.__acquired_from, self.__acquired_to,
            ImageTypeEnum.PNG, PresetEnum.TRUE_COLOR, None, None,
            SatelliteEnum.LANDSAT_8.symbol, None, None, None, None)
        self.assertIsInstance(result_set, list)
        self.assertEqual(len(result_set), 22)

        # just keep the non-tile images
        non_tile_pngs = [
            mimg for mimg in result_set if isinstance(mimg, MetaPNGImage)
        ]
        self.assertEqual(len(non_tile_pngs), 11)

        # download one
        result = mgr.download_satellite_image(non_tile_pngs[0])

        self.assertIsInstance(result, SatelliteImage)
        self.assertIsInstance(result.metadata, MetaPNGImage)
        img = result.data
        self.assertIsInstance(img, Image)
        self.assertEqual(img.image_type, ImageTypeEnum.PNG)
        self.assertNotEqual(len(img.data), 0)

    def test_download_geotiff(self):
        mgr = self.__owm.agro_manager()

        # search GeoTiff, EVIimages acquired by Landsat 8
        result_set = mgr.search_satellite_imagery(
            self.__polygon.id, self.__acquired_from, self.__acquired_to,
            ImageTypeEnum.GEOTIFF, PresetEnum.EVI, None, None,
            SatelliteEnum.LANDSAT_8.symbol, None, None, None, None)
        self.assertIsInstance(result_set, list)
        self.assertEqual(len(result_set), 11)

        # download one
        result = mgr.download_satellite_image(
            result_set[0], palette=PaletteEnum.CONTRAST_SHIFTED)

        self.assertIsInstance(result, SatelliteImage)
        self.assertIsInstance(result.metadata, MetaGeoTiffImage)
        img = result.data
        self.assertIsInstance(img, Image)
        self.assertEqual(img.image_type, ImageTypeEnum.GEOTIFF)
        self.assertNotEqual(len(img.data), 0)

    def test_download_tile(self):
        mgr = self.__owm.agro_manager()

        # search PNG, truecolor, tile images acquired by Landsat 8
        result_set = mgr.search_satellite_imagery(
            self.__polygon.id, self.__acquired_from, self.__acquired_to,
            ImageTypeEnum.PNG, PresetEnum.TRUE_COLOR, None, None,
            SatelliteEnum.LANDSAT_8.symbol, None, None, None, None)
        self.assertIsInstance(result_set, list)
        self.assertEqual(len(result_set), 22)

        # just keep the tiles
        tile_pngs = [mimg for mimg in result_set if isinstance(mimg, MetaTile)]
        self.assertEqual(len(tile_pngs), 11)

        # try to download one without specifying x, y and zoom
        with self.assertRaises(AssertionError):
            mgr.download_satellite_image(tile_pngs[0])
        with self.assertRaises(AssertionError):
            mgr.download_satellite_image(tile_pngs[0], x=1)
        with self.assertRaises(AssertionError):
            mgr.download_satellite_image(tile_pngs[0], x=1, y=2)

        # download one
        result = mgr.download_satellite_image(tile_pngs[1], x=1, y=2, zoom=5)

        self.assertIsInstance(result, SatelliteImage)
        self.assertIsInstance(result.metadata, MetaTile)
        img = result.data
        self.assertIsInstance(img, Image)
        self.assertEqual(img.image_type, ImageTypeEnum.PNG)
        self.assertNotEqual(len(img.data), 0)

    def test_persisting_to_disk(self):
        path = '%s.tif' % uuid.uuid4()
        mgr = self.__owm.agro_manager()

        # search GeoTiff, EVIimages acquired by Landsat 8
        result_set = mgr.search_satellite_imagery(
            self.__polygon.id, self.__acquired_from, self.__acquired_to,
            ImageTypeEnum.GEOTIFF, PresetEnum.EVI, None, None,
            SatelliteEnum.LANDSAT_8.symbol, None, None, None, None)
        self.assertTrue(len(result_set) > 1)
        metaimg = result_set[0]
        sat_img = mgr.download_satellite_image(metaimg)
        try:
            self.assertFalse(os.path.isfile(path))
            sat_img.persist(path)
            self.assertTrue(os.path.isfile(path))
        except:
            self.fail()
        finally:
            os.remove(path)