def test_uses_grid_location(self):
        """Test regarding issue #2438
        """
        working_dir = shakemap_extract_dir()
        # population_path =
        shake_event = ShakeEvent(
            working_dir=working_dir,
            event_id=SHAKE_ID_2,
            locale='en',
            force_flag=True,
            data_is_local_flag=True,
            # population_raster_path=population_path
        )
        expected_location = 'Yogyakarta'
        self.assertEqual(
            shake_event.event_dict()['shake-grid-location'],
            expected_location)

        inasafe_django = InaSAFEDjangoREST()

        if inasafe_django.is_configured():
            # generate report
            shake_event.render_map()
            # push to realtime django
            push_shake_event_to_rest(shake_event)
            # check shake event exists
            session = inasafe_django.rest
            response = session.earthquake(SHAKE_ID_2).GET()
            self.assertEqual(response.status_code, requests.codes.ok)

            self.assertEqual(
                response.json()['location_description'],
                shake_event.event_dict()['shake-grid-location'])
Exemple #2
0
    def test_uses_grid_location(self):
        """Test regarding issue #2438
        """
        working_dir = shakemap_extract_dir()
        # population_path =
        shake_event = ShakeEvent(
            working_dir=working_dir,
            event_id=SHAKE_ID_2,
            locale='en',
            force_flag=True,
            data_is_local_flag=True,
            # population_raster_path=population_path
        )
        expected_location = 'Yogyakarta'
        self.assertEqual(
            shake_event.event_dict()['shake-grid-location'],
            expected_location)

        inasafe_django = InaSAFEDjangoREST()

        if inasafe_django.is_configured():
            # generate report
            shake_event.render_map()
            # push to realtime django
            push_shake_event_to_rest(shake_event)
            # check shake event exists
            session = inasafe_django.rest
            response = session.earthquake(SHAKE_ID_2).GET()
            self.assertEqual(response.status_code, requests.codes.ok)

            self.assertEqual(
                response.json()['location_description'],
                shake_event.event_dict()['shake-grid-location'])
    def test_push_to_realtime(self):
        # only do the test if realtime test server is configured
        inasafe_django = InaSAFEDjangoREST()
        if inasafe_django.is_configured():

            working_dir = shakemap_extract_dir()
            shake_event = ShakeEvent(
                working_dir=working_dir,
                event_id=SHAKE_ID,
                locale='en',
                data_is_local_flag=True)
            # generate report
            shake_event.render_map()
            # push to realtime django
            push_shake_event_to_rest(shake_event)
            # check shake event exists
            session = inasafe_django.rest
            response = session.earthquake(SHAKE_ID).GET()
            self.assertEqual(response.status_code, requests.codes.ok)

            event_dict = shake_event.event_dict()
            earthquake_data = {
                'shake_id': shake_event.event_id,
                'magnitude': float(event_dict.get('mmi')),
                'depth': float(event_dict.get('depth-value')),
                'time': shake_event.shake_grid.time,
                'location': {
                    'type': 'Point',
                    'coordinates': [
                        shake_event.shake_grid.longitude,
                        shake_event.shake_grid.latitude
                    ]
                },
                'location_description': event_dict.get('shake-grid-location')
            }

            for key, value in earthquake_data.iteritems():
                if isinstance(value, datetime.datetime):
                    self.assertEqual(
                        datetime.datetime.strptime(
                                response.json()[key], '%Y-%m-%dT%H:%M:%SZ'
                        ).replace(tzinfo=pytz.utc),
                        value
                    )
                else:
                    self.assertEqual(response.json()[key], value)
Exemple #4
0
    def test_push_to_realtime(self):
        # only do the test if realtime test server is configured
        inasafe_django = InaSAFEDjangoREST()
        if inasafe_django.is_configured():

            working_dir = shakemap_extract_dir()
            shake_event = ShakeEvent(
                working_dir=working_dir,
                event_id=SHAKE_ID,
                locale='en',
                data_is_local_flag=True)
            # generate report
            shake_event.render_map()
            # push to realtime django
            push_shake_event_to_rest(shake_event)
            # check shake event exists
            session = inasafe_django.rest
            response = session.earthquake(SHAKE_ID).GET()
            self.assertEqual(response.status_code, requests.codes.ok)

            event_dict = shake_event.event_dict()
            earthquake_data = {
                'shake_id': shake_event.event_id,
                'magnitude': float(event_dict.get('mmi')),
                'depth': float(event_dict.get('depth-value')),
                'time': shake_event.shake_grid.time,
                'location': {
                    'type': 'Point',
                    'coordinates': [
                        shake_event.shake_grid.longitude,
                        shake_event.shake_grid.latitude
                    ]
                },
                'location_description': event_dict.get('shake-grid-location')
            }

            for key, value in earthquake_data.iteritems():
                if isinstance(value, datetime.datetime):
                    self.assertEqual(
                        datetime.datetime.strptime(
                                response.json()[key], '%Y-%m-%dT%H:%M:%SZ'
                        ).replace(tzinfo=pytz.utc),
                        value
                    )
                else:
                    self.assertEqual(response.json()[key], value)
Exemple #5
0
def process_event(working_dir=None, event_id=None, locale='en'):
    """Launcher that actually runs the event processing.

    :param event_id: The event id to process. If None the latest event will
       be downloaded and processed.
    :type event_id: str

    :param locale: The locale that will be used. Default to en.
    :type locale: str

    :return: Return True if succeeded
    :rtype: bool
    """
    population_path = os.path.join(data_dir(), 'exposure', 'population.tif')

    # Use cached data where available
    # Whether we should always regenerate the products
    force_flag = False
    if 'INASAFE_FORCE' in os.environ:
        force_string = os.environ['INASAFE_FORCE']
        if str(force_string).capitalize() == 'Y':
            force_flag = True

    # We always want to generate en products too so we manipulate the locale
    # list and loop through them:
    locale_list = [locale]
    if 'en' not in locale_list:
        locale_list.append('en')

    # Now generate the products
    for locale in locale_list:
        # Extract the event
        # noinspection PyBroadException
        try:
            shake_events = create_shake_events(event_id=event_id,
                                               force_flag=force_flag,
                                               locale=locale,
                                               population_path=population_path,
                                               working_dir=working_dir)
        except (BadZipfile, URLError):
            # retry with force flag true
            shake_events = create_shake_events(event_id=event_id,
                                               force_flag=True,
                                               locale=locale,
                                               population_path=population_path,
                                               working_dir=working_dir)
        except EmptyShakeDirectoryError as ex:
            LOGGER.info(ex)
            return
        except Exception:  # pylint: disable=broad-except
            LOGGER.exception('An error occurred setting up the shake event.')
            return

        LOGGER.info('Event Id: %s', [s.event_id for s in shake_events])
        LOGGER.info('-------------------------------------------')

        for shake_event in shake_events:
            shake_event.render_map(force_flag)
            # push the shakemap to realtime server
            ret = push_shake_event_to_rest(shake_event)
            LOGGER.info('Is Push successful? %s' % bool(ret))

    return True
def process_event(working_dir=None, event_id=None, locale="en"):
    """Launcher that actually runs the event processing.

    :param event_id: The event id to process. If None the latest event will
       be downloaded and processed.
    :type event_id: str

    :param locale: The locale that will be used. Default to en.
    :type locale: str

    :return: Return True if succeeded
    :rtype: bool
    """
    population_path = os.path.join(data_dir(), "exposure", "population.tif")

    # Use cached data where available
    # Whether we should always regenerate the products
    force_flag = False
    if "INASAFE_FORCE" in os.environ:
        force_string = os.environ["INASAFE_FORCE"]
        if str(force_string).capitalize() == "Y":
            force_flag = True

    # We always want to generate en products too so we manipulate the locale
    # list and loop through them:
    locale_list = [locale]
    if "en" not in locale_list:
        locale_list.append("en")

    # Now generate the products
    for locale in locale_list:
        # Extract the event
        # noinspection PyBroadException
        try:
            shake_events = create_shake_events(
                event_id=event_id,
                force_flag=force_flag,
                locale=locale,
                population_path=population_path,
                working_dir=working_dir,
            )
        except (BadZipfile, URLError):
            # retry with force flag true
            shake_events = create_shake_events(
                event_id=event_id,
                force_flag=True,
                locale=locale,
                population_path=population_path,
                working_dir=working_dir,
            )
        except EmptyShakeDirectoryError as ex:
            LOGGER.info(ex)
            return
        except Exception:  # pylint: disable=broad-except
            LOGGER.exception("An error occurred setting up the shake event.")
            return

        LOGGER.info("Event Id: %s", [s.event_id for s in shake_events])
        LOGGER.info("-------------------------------------------")

        for shake_event in shake_events:
            shake_event.render_map(force_flag)
            # push the shakemap to realtime server
            ret = push_shake_event_to_rest(shake_event)
            LOGGER.info("Is Push successful? %s" % bool(ret))

    return True