def testFatalitiesTable(self): """Test rendering a fatalities table.""" myShakeId = '20120726022003' myShakeEvent = ShakeEvent(myShakeId) myShakeEvent.calculateImpacts() myResult = myShakeEvent.impactTable() # TODO compare actual content of impact table... myExpectedResult = ( '/tmp/inasafe/realtime/shakemaps-extracted/' '20120726022003/impacts.html') myMessage = ('Got:\n%s\nExpected:\n%s' % (myResult, myExpectedResult)) assert myResult == myExpectedResult, myMessage
def test_event_info_string(self): """Test we can get a location info string nicely.""" working_dir = shakemap_extract_dir() shake_event = ShakeEvent(working_dir=working_dir, event_id=SHAKE_ID, data_is_local_flag=True) expected_result = (u"M 3.6 5-11-2013 6:8:9 Latitude: 2°25′48.00" u'″S Longitude: 140°37' u"′" u'12.00″E Depth: 10.0km Located 0.00km n/a of n/a') result = shake_event.event_info() message = ('Got:\n%s\nExpected:\n%s\n' % (result, expected_result)) self.assertEqual(result, expected_result, message)
def test_local_cities(self): """Test that we can retrieve the cities local to the event""" working_dir = shakemap_extract_dir() shake_event = ShakeEvent( working_dir=working_dir, event_id=SHAKE_ID, data_is_local_flag=True) # Get teh mem layer cities_layer = shake_event.local_cities_memory_layer() provider = cities_layer.dataProvider() expected_feature_count = 2 self.assertEquals(provider.featureCount(), expected_feature_count) strings = [] request = QgsFeatureRequest() for feature in cities_layer.getFeatures(request): # fetch map of attributes attributes = cities_layer.dataProvider().attributeIndexes() for attribute_key in attributes: strings.append("%d: %s\n" % ( attribute_key, feature[attribute_key])) strings.append('------------------\n') LOGGER.debug('Mem table:\n %s' % strings) file_path = unique_filename(prefix='test_local_cities', suffix='.txt', dir=temp_dir('test')) cities_file = file(file_path, 'w') cities_file.writelines(strings) cities_file.close() fixture_path = os.path.join(data_dir(), 'tests', 'test_local_cities.txt') cities_file = file(fixture_path) expected_string = cities_file.readlines() cities_file.close() diff = difflib.unified_diff(expected_string, strings) diff_list = list(diff) diff_string = '' for _, myLine in enumerate(diff_list): diff_string += myLine message = ('Diff is not zero length:\n' 'Control file: %s\n' 'Test file: %s\n' 'Diff:\n%s' % (fixture_path, file_path, diff_string)) self.assertEqual(diff_string, '', message)
def test_fatalities_table(self): """Test rendering a fatalities table.""" shake_event = ShakeEvent(SHAKE_ID, data_is_local_flag=True) shake_event.calculate_impacts() result = shake_event.impact_table() # TODO compare actual content of impact table... # Get the os environment INASAFE_WORK_DIR if it exists inasafe_work_dir = base_data_dir() expected_result = ( '%s/shakemaps-extracted/20131105060809/impacts.html' % inasafe_work_dir) message = 'Got:\n%s\nExpected:\n%s' % (result, expected_result) self.assertEqual(result, expected_result, message)
def test_mmi_potential_damage(self): """Test mmi_potential_damage function.""" working_dir = shakemap_extract_dir() shake_event = ShakeEvent( working_dir=working_dir, event_id=SHAKE_ID, data_is_local_flag=True) values = range(1, 11) expected_result = ['None', 'None', 'None', 'None', 'Very light', 'Light', 'Moderate', 'Mod/Heavy', 'Heavy', 'Very heavy'] result = [] for value in values: result.append(shake_event.mmi_potential_damage(value)) message = 'Got:\n%s\nExpected:\n%s\n' % (result, expected_result) self.assertEqual(result, expected_result, message)
def testLocalCities(self): """Test that we can retrieve the cities local to the event""" myShakeId = '20120726022003' myShakeEvent = ShakeEvent(myShakeId) # Get teh mem layer myCitiesLayer = myShakeEvent.localCitiesMemoryLayer() myProvider = myCitiesLayer.dataProvider() myExpectedFeatureCount = 6 self.assertEquals(myProvider.featureCount(), myExpectedFeatureCount) myStrings = [] myRequest = QgsFeatureRequest() for myFeature in myCitiesLayer.getFeatures(myRequest): # fetch map of attributes myAttributes = myCitiesLayer.dataProvider().attributeIndexes() for myKey in myAttributes: myStrings.append("%d: %s\n" % ( myKey, myFeature[myKey].toString())) myStrings.append('------------------\n') LOGGER.debug('Mem table:\n %s' % myStrings) myFilePath = unique_filename(prefix='testLocalCities', suffix='.txt', dir=temp_dir('test')) myFile = file(myFilePath, 'wt') myFile.writelines(myStrings) myFile.close() myFixturePath = os.path.join(dataDir(), 'tests', 'testLocalCities.txt') myFile = file(myFixturePath, 'rt') myExpectedString = myFile.readlines() myFile.close() myDiff = difflib.unified_diff(myExpectedString, myStrings) myDiffList = list(myDiff) myDiffString = '' for _, myLine in enumerate(myDiffList): myDiffString += myLine myMessage = ('Diff is not zero length:\n' 'Control file: %s\n' 'Test file: %s\n' 'Diff:\n%s' % (myFixturePath, myFilePath, myDiffString)) self.assertEqual(myDiffString, '', myMessage)
def test_event_to_contours(self): """Check we can extract contours from the event""" shake_id = '20120726022003' shake_event = ShakeEvent(shake_id) file_path = shake_event.mmi_data_to_contours(force_flag=True, algorithm='invdist') assert self.check_feature_count(file_path, 16) assert os.path.exists(file_path) expected_qml = file_path.replace('shp', 'qml') message = '%s not found' % expected_qml assert os.path.exists(expected_qml), message file_path = shake_event.mmi_data_to_contours(force_flag=True, algorithm='nearest') assert self.check_feature_count(file_path, 132) file_path = shake_event.mmi_data_to_contours(force_flag=True, algorithm='average') assert self.check_feature_count(file_path, 132)
def testEventToContours(self): """Check we can extract contours from the event""" myShakeId = '20120726022003' myShakeEvent = ShakeEvent(myShakeId) myPath = myShakeEvent.mmiDataToContours(theForceFlag=True, theAlgorithm='invdist') assert self.checkFeatureCount(myPath, 16) assert os.path.exists(myPath) myExpectedQml = myPath.replace('shp', 'qml') myMessage = '%s not found' % myExpectedQml assert os.path.exists(myExpectedQml), myMessage myPath = myShakeEvent.mmiDataToContours(theForceFlag=True, theAlgorithm='nearest') assert self.checkFeatureCount(myPath, 132) myPath = myShakeEvent.mmiDataToContours(theForceFlag=True, theAlgorithm='average') assert self.checkFeatureCount(myPath, 132)
def testEventToRaster(self): """Check we can convert the shake event to a raster""" myShakeId = '20120726022003' myShakeEvent = ShakeEvent(myShakeId) myExpectedState = """latitude: -0.21 longitude: 124.45 eventId: 20120726022003 magnitude: 5.0 depth: 11.0 description: None location: Southern Molucca Sea day: 26 month: 7 year: 2012 time: None time_zone: WIB x_minimum: 122.45 x_maximum: 126.45 y_minimum: -2.21 y_maximum: 1.79 rows: 161.0 columns: 161.0 mmi_data: Populated populationRasterPath: None impact_file: None impact_keywords_file: None fatality_counts: None displaced_counts: None affected_counts: None extent_with_cities: Not set zoom_factor: 1.25 search_boxes: None """ myState = str(myShakeEvent) myMessage = (('Expected:\n----------------\n%s' '\n\nGot\n------------------\n%s\n') % (myExpectedState, myState)) assert myState == myExpectedState, myMessage myPath = myShakeEvent.mmiDataToRaster(theForceFlag=True) assert os.path.exists(myPath) myExpectedQml = myPath.replace('tif', 'qml') assert os.path.exists(myExpectedQml) myExpectedKeywords = myPath.replace('tif', 'keywords') assert os.path.exists(myExpectedKeywords)
def test_push_to_realtime(self): # only do the test if realtime test server is configured if is_realtime_rest_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 = get_realtime_session() earthquake_url = generate_earthquake_detail_url(SHAKE_ID) response = session.get(earthquake_url) 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.strftime( INASAFE_REALTIME_DATETIME_FORMAT), 'location': { 'type': 'Point', 'coordinates': [ shake_event.shake_grid.longitude, shake_event.shake_grid.latitude ] }, 'location_description': event_dict.get('place-name') } for key, value in earthquake_data.iteritems(): self.assertEqual(response.json()[key], value)
def test_event_to_raster(self): """Check we can convert the shake event to a raster""" shake_id = '20120726022003' shake_event = ShakeEvent(shake_id) expected_state = """latitude: -0.21 longitude: 124.45 event_id: 20120726022003 magnitude: 5.0 depth: 11.0 description: None location: Southern Molucca Sea day: 26 month: 7 year: 2012 time: None time_zone: WIB x_minimum: 122.45 x_maximum: 126.45 y_minimum: -2.21 y_maximum: 1.79 rows: 161.0 columns: 161.0 mmi_data: Populated population_raster_path: None impact_file: None impact_keywords_file: None fatality_counts: None displaced_counts: None affected_counts: None extent_with_cities: Not set zoom_factor: 1.25 search_boxes: None """ state = str(shake_event) message = (('Expected:\n----------------\n%s' '\n\nGot\n------------------\n%s\n') % (expected_state, state)) assert state == expected_state, message raster_path = shake_event.mmi_data_to_raster(force_flag=True) assert os.path.exists(raster_path) expected_qml = raster_path.replace('tif', 'qml') assert os.path.exists(expected_qml) expected_keywords = raster_path.replace('tif', 'keywords') assert os.path.exists(expected_keywords)
def test_to_string(self): """Test __str__ works properly.""" working_dir = shakemap_extract_dir() shake_event = ShakeEvent( working_dir=working_dir, event_id=SHAKE_ID, data_is_local_flag=True) expected_state = ( 'latitude: -2.43\n' 'longitude: 140.62\n' 'event_id: 20131105060809\n' 'magnitude: 3.6\n' 'depth: 10.0\n' 'description: None\n' 'location: Papua\n' 'day: 5\n' 'month: 11\n' 'year: 2013\n' 'time: None\n' 'time_zone: WIB\n' 'x_minimum: 139.37\n' 'x_maximum: 141.87\n' 'y_minimum: -3.67875\n' 'y_maximum: -1.18125\n' 'rows: 101.0\n' 'columns: 101.0\n' 'mmi_data: Populated\n' 'population_raster_path: None\n' 'impact_file: None\n' 'impact_keywords_file: None\n' 'fatality_counts: None\n' 'displaced_counts: None\n' 'affected_counts: None\n' 'extent_with_cities: Not set\n' 'zoom_factor: 1.25\n' 'search_boxes: None\n') state = str(shake_event) message = (('Expected:\n----------------\n%s' '\n\nGot\n------------------\n%s\n') % (expected_state, state)) self.assertEqual(state, expected_state, message)
def test_impacted_cities_table(self): """Test getting impacted cities table.""" shake_event = ShakeEvent(SHAKE_ID, data_is_local_flag=True) table, path = shake_event.impacted_cities_table() expected_string = [ '<td>Jayapura</td><td>134</td><td>I</td>', '<td>Abepura</td><td>62</td><td>I</td>'] table = table.toNewlineFreeString().replace(' ', '') for string in expected_string: self.assertIn(string, table) self.max_diff = None # Get the os environment INASAFE_WORK_DIR if it exists inasafe_work_dir = base_data_dir() expected_path = ( '%s/shakemaps-extracted/20131105060809/affected-cities.html' % inasafe_work_dir) message = 'Got:\n%s\nExpected:\n%s\n' % (path, expected_path) self.assertEqual(path, expected_path, message)
def testImpactedCitiesTable(self): """Test getting impacted cities table.""" myShakeId = '20120726022003' myShakeEvent = ShakeEvent(myShakeId) myTable, myPath = myShakeEvent.impactedCitiesTable() myExpectedStrings = [ '<td>Tondano</td><td>33</td><td>I</td>', '<td>Luwuk</td><td>47</td><td>I</td>', '<td>Bitung</td><td>137</td><td>I</td>', '<td>Manado</td><td>451</td><td>I</td>', '<td>Gorontalo</td><td>144</td><td>II</td>'] myTable = myTable.toNewlineFreeString().replace(' ', '') for myString in myExpectedStrings: self.assertIn(myString, myTable) self.maxDiff = None myExpectedPath = ( '/tmp/inasafe/realtime/shakemaps-extracted/' '20120726022003/affected-cities.html') myMessage = 'Got:\n%s\nExpected:\n%s\n' % (myPath, myExpectedPath) assert myPath == myExpectedPath, myMessage
def test_impacted_cities_table(self): """Test getting impacted cities table.""" shake_id = '20120726022003' shake_event = ShakeEvent(shake_id) table, path = shake_event.impacted_cities_table() expected_string = [ '<td>Tondano</td><td>33</td><td>I</td>', '<td>Luwuk</td><td>47</td><td>I</td>', '<td>Bitung</td><td>137</td><td>I</td>', '<td>Manado</td><td>451</td><td>I</td>', '<td>Gorontalo</td><td>144</td><td>II</td>'] table = table.toNewlineFreeString().replace(' ', '') for myString in expected_string: self.assertIn(myString, table) self.max_diff = None expected_path = ( '/tmp/inasafe/realtime/shakemaps-extracted/' '20120726022003/affected-cities.html') message = 'Got:\n%s\nExpected:\n%s\n' % (path, expected_path) assert path == expected_path, message
def test_calculate_fatalities(self): """Test that we can calculate fatalities.""" LOGGER.debug(QGIS_APP.showSettings()) working_dir = shakemap_extract_dir() shake_event = ShakeEvent(working_dir=working_dir, event_id=SHAKE_ID, data_is_local_flag=True) result, fatalities_html = shake_event.calculate_impacts() # Get the os environment INASAFE_WORK_DIR if it exists inasafe_work_dir = base_data_dir() expected_result = ('%s/shakemaps-extracted/20131105060809/impact' '-nearest.tif') % inasafe_work_dir message = 'Got: %s, Expected: %s' % (result, expected_result) self.assertEqual(result, expected_result, message) expected_result = ('%s/shakemaps-extracted/20131105060809/impacts' '.html') % inasafe_work_dir message = 'Got: %s, Expected: %s' % (fatalities_html, expected_result) self.assertEqual(fatalities_html, expected_result, message) expected_fatalities = { 2: 0.0, 3: 0.0, 4: 3.6387775168847936e-05, 5: 0.0, 6: 0.0, 7: 0.0, 8: 0.0, 9: 0.0 } message = 'Got: %s, Expected: %s' % (shake_event.fatality_counts, expected_fatalities) self.assertEqual(shake_event.fatality_counts, expected_fatalities, message)
def test_bearing_to_cardinal(self): """Test we can convert a bearing to a cardinal direction.""" shake_event = ShakeEvent(SHAKE_ID, data_is_local_flag=True) # Ints should work expected_result = 'SSE' result = shake_event.bearing_to_cardinal(160) message = ('Got:\n%s\nExpected:\n%s\n' % (result, expected_result)) self.assertEqual(result, expected_result, message) # Floats should work expected_result = 'SW' result = shake_event.bearing_to_cardinal(225.4) message = ('Got:\n%s\nExpected:\n%s\n' % (result, expected_result)) self.assertEqual(result, expected_result, message) # non numeric data as input should return None expected_result = None result = shake_event.bearing_to_cardinal('foo') message = ('Got:\n%s\nExpected:\n%s\n' % (result, expected_result)) self.assertEqual(result, expected_result, message)
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 """ 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: if os.path.exists(population_path): shake_event = ShakeEvent( working_dir=working_dir, event_id=event_id, locale=locale, force_flag=force_flag, population_raster_path=population_path) else: shake_event = ShakeEvent(working_dir=working_dir, event_id=event_id, locale=locale, force_flag=force_flag) except (BadZipfile, URLError): # retry with force flag true if os.path.exists(population_path): shake_event = ShakeEvent( working_dir=working_dir, event_id=event_id, locale=locale, force_flag=True, population_raster_path=population_path) else: shake_event = ShakeEvent(working_dir=working_dir, event_id=event_id, locale=locale, force_flag=True) 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', shake_event) LOGGER.info('-------------------------------------------') shake_event.render_map(force_flag)
def test_cities_to_shape(self): """Test that we can retrieve the cities local to the event.""" shake_event = ShakeEvent(SHAKE_ID, data_is_local_flag=True) file_path = shake_event.cities_to_shapefile() self.assertTrue(os.path.exists(file_path))
def test_render_map(self): """Test render_map function in shake_event.""" working_dir = shakemap_extract_dir() shake_event = ShakeEvent( working_dir=working_dir, event_id=SHAKE_ID, data_is_local_flag=True) # Render Map shake_event.render_map() # There should be exist: # 1. SHAKE_ID-en.pdf # 2. SHAKE_ID-en.png # 3. SHAKE_ID-thumb-en.png # 4. SHAKE_ID-metadata-en.pickle # 5. mmi-cities.shp, shx, dbf, prj, qml # 6. mmi-contours-nearest.shp, shx, dbf, prj, qml # 7. city-search-boxes.shp, shx, dbf, prj, qml # 8. composer-template.qpt # 9. project.qgs target_dir = os.path.join(shakemap_extract_dir(), SHAKE_ID) shapefile_extension = ['shp', 'shx', 'dbf', 'prj', 'qml'] # 1 pdf_path = os.path.join(target_dir, '%s-en.pdf' % SHAKE_ID) message = 'PDF Report is not generated successfully in %s' % pdf_path self.assertTrue(os.path.exists(pdf_path), message) # 2 png_path = os.path.join(target_dir, '%s-en.png' % SHAKE_ID) message = 'PNG Report is not generated successfully in %s' % png_path self.assertTrue(os.path.exists(png_path), message) # 3 thumbnail_path = os.path.join(target_dir, '%s-thumb-en.png' % SHAKE_ID) message = 'PNG Thumbnail is not generated successfully in %s' % ( thumbnail_path) self.assertTrue(os.path.exists(thumbnail_path), message) # 4 metadata_path = os.path.join( target_dir, '%s-metadata-en.pickle' % SHAKE_ID) message = 'Metadata file is not generated successfully in %s' % ( metadata_path) self.assertTrue(os.path.exists(metadata_path), message) # 5. mmi-cities.shp, shx, dbf, prj, qml mmi_cities_path = os.path.join(target_dir, 'mmi-cities.shp') for extension in shapefile_extension: file_path = mmi_cities_path.replace('shp', extension) message = 'mmi-cities.%s is not generated successfully in %s' % ( extension, file_path) self.assertTrue(os.path.exists(file_path), message) # 6. mmi-contours-nearest.shp, shx, dbf, prj, qml mmi_contours_path = os.path.join( target_dir, 'mmi-contours-nearest.shp') for extension in shapefile_extension: file_path = mmi_contours_path.replace('shp', extension) message = ( 'mmi-contours-nearest.%s is not generated successfully in ' '%s') % (extension, file_path) self.assertTrue(os.path.exists(file_path), message) # 7. city-search-boxes.shp, shx, dbf, prj, qml city_search_boxes_path = os.path.join( target_dir, 'city-search-boxes.shp') for extension in shapefile_extension: file_path = city_search_boxes_path.replace('shp', extension) message = ( 'city-search-boxes.%s is not generated successfully in ' '%s') % (extension, file_path) self.assertTrue(os.path.exists(file_path), message) # 8 composer_template_path = os.path.join( target_dir, 'composer-template.qpt') message = ( 'Composer template file is not generated successfully in %s' % composer_template_path) self.assertTrue(os.path.exists(composer_template_path), message) # 9 qgs_project_path = os.path.join( target_dir, 'project.qgs') message = 'QGIS Project file is not generated successfully in %s' % ( qgs_project_path) self.assertTrue(os.path.exists(qgs_project_path), message)
def test_event_info_dict(self): """Test we can get a dictionary of location info nicely.""" working_dir = shakemap_extract_dir() shake_event = ShakeEvent( working_dir=working_dir, event_id=SHAKE_ID, data_is_local_flag=True) result = shake_event.event_dict() software_tag = ('This report was created using InaSAFE version %s. ' 'Visit http://inasafe.org for more information.' % get_version()) # noinspection PyUnresolvedReferences expected_dict = { 'place-name': u'n/a', 'depth-name': u'Depth', 'fatalities-name': u'Estimated fatalities', 'fatalities-count': u'0', # 44 only after render 'elapsed-time': u'', # empty as it will change 'legend-name': u'Population count per grid cell', 'fatalities-range': '0 - 100', 'longitude-name': u'Longitude', 'located-label': u'Located', 'distance-unit': u'km', 'bearing-compass': u'n/a', 'elapsed-time-name': u'Elapsed time since event', 'exposure-table-name': u'Estimated number of people ' u'affected by each MMI level', 'longitude-value': u'140\xb037\u203212.00\u2033E', 'city-table-name': u'Nearby Places', 'bearing-text': u'bearing', 'limitations': ( u'This impact estimation is automatically generated and only ' u'takes into account the population and cities affected by ' u'different levels of ground shaking. The estimate is based ' u'on ground shaking data from BMKG, population count data ' u'derived by AIFDR from worldpop.org.uk, place information ' u'from geonames.org and software developed by BNPB. ' u'Limitations in the estimates of ground shaking, population ' u'and place names datasets may result in significant ' u'misrepresentation of the on-the-ground situation in the ' u'figures shown here. Consequently decisions should not be ' u'made solely on the information presented here and should ' u'always be verified by ground truthing and other reliable ' u'information sources. The fatality calculation assumes that ' u'no fatalities occur for shake levels below MMI 4. Fatality ' u'counts of less than 50 are disregarded.'), 'depth-unit': u'km', 'latitude-name': u'Latitude', 'mmi': '3.6', 'map-name': u'Estimated Earthquake Impact', 'date': '5-11-2013', 'bearing-degrees': '0.00\xb0', 'formatted-date-time': '05-Nov-13 06:08:09 ', 'distance': '0.00', 'direction-relation': u'of', 'software-tag': software_tag, 'credits': ( u'Supported by the Australia-Indonesia Facility for Disaster ' u'Reduction, Geoscience Australia and the World Bank-GFDRR.'), 'latitude-value': u'2\xb025\u203248.00\u2033S', 'time': '6:8:9', 'depth-value': '10.0'} result['elapsed-time'] = u'' message = 'Got:\n%s\nExpected:\n%s\n' % (result, expected_dict) self.max_diff = None difference = DictDiffer(result, expected_dict) print difference.all() self.assertDictEqual(expected_dict, result, message)
def test_cities_search_boxes_to_shape(self): """Test that we can retrieve the search boxes used to find cities.""" shake_event = ShakeEvent(SHAKE_ID, data_is_local_flag=True) file_path = shake_event.city_search_boxes_to_shapefile() self.assertTrue(os.path.exists(file_path))
def create_shake_events( event_id=None, population_path=None, working_dir=None, locale='en', force_flag=False): """ :param working_dir: The locale working dir where all the shakemaps are located. :type working_dir: str :param event_id: (Optional) Id of the event. Will be used to fetch the ShakeData for this event. The grid.xml file in the unpacked event will be used to initialise the state of the a ShakeGrid instance. If no event id is supplied, the most recent event recorded on working dir will be used. :type event_id: str :param locale:(Optional) string for iso locale to use for outputs. Defaults to en. Can also use 'id' or possibly more as translations are added. :type locale: str :param force_flag: Whether to force retrieval of the dataset. :type force_flag: bool :return: Shake Events to process :rtype: list[ShakeEvent] """ shake_events = [] if not os.path.exists(population_path): population_path = None # cron job executed this script minutely, so it is possible in one # minute that we have more than one shake_event. We can resolve this # by only retrieveng the shake id for that particular minute. # retrieve all the shake ids shake_ids = ShakeData.get_list_event_ids_from_folder(working_dir) shake_ids.sort() shake_ids.reverse() if not shake_ids: return [] if event_id: shake_events.append( ShakeEvent( working_dir=working_dir, event_id=event_id, locale=locale, force_flag=force_flag, population_raster_path=population_path) ) else: last_int = int(shake_ids[0]) # sort descending for shake_id in shake_ids: if last_int - int(shake_id) < 100: shake_event = ShakeEvent( working_dir=working_dir, event_id=shake_id, locale=locale, force_flag=force_flag, population_raster_path=population_path) shake_events.append(shake_event) else: break return shake_events
def test_event_info_dict(self): """Test we can get a dictionary of location info nicely.""" shake_id = '20120726022003' shake_event = ShakeEvent(shake_id) result = shake_event.event_dict() #noinspection PyUnresolvedReferences expected_dict = {'place-name': PyQt4.QtCore.QString(u'n/a'), 'depth-name': PyQt4.QtCore.QString(u'Depth'), 'fatalities-name': PyQt4.QtCore.QString( u'Estimated fatalities'), 'fatalities-count': u'0', # 44 only after render 'elapsed-time': u'', # empty as it will change 'legend-name': PyQt4.QtCore.QString( u'Population density'), 'fatalities-range': '0 - 100', 'longitude-name': PyQt4.QtCore.QString(u'Longitude'), 'located-label': PyQt4.QtCore.QString(u'Located'), 'distance-unit': PyQt4.QtCore.QString(u'km'), 'bearing-compass': u'n/a', 'elapsed-time-name': PyQt4.QtCore.QString( u'Elapsed time since event'), 'exposure-table-name': PyQt4.QtCore.QString( u'Estimated number of people affected by each ' u'MMI level'), 'longitude-value': u'124\xb027\'0.00"E', 'city-table-name': PyQt4.QtCore.QString( u'Places Affected'), 'bearing-text': PyQt4.QtCore.QString(u'bearing'), 'limitations': PyQt4.QtCore.QString( u'This impact estimation is automatically ' u'generated and only takes into account the ' u'population and cities affected by different ' u'levels of ground shaking. The estimate is ' u'based on ground shaking data from BMKG, ' u'population density data from asiapop.org, ' u'place information from geonames.org and ' u'software developed by BNPB. Limitations in ' u'the estimates of ground shaking, ' u'population data and place names datasets may' u' result in significant misrepresentation of ' u'the on-the-ground situation in the figures ' u'shown here. Consequently decisions should not' u' be made solely on the information presented ' u'here and should always be verified by ground ' u'truthing and other reliable information ' u'sources. The fatality calculation assumes ' u'that no fatalities occur for shake levels ' u'below MMI 4. Fatality counts of less than 50 ' u'are disregarded.'), 'depth-unit': PyQt4.QtCore.QString(u'km'), 'latitude-name': PyQt4.QtCore.QString(u'Latitude'), 'mmi': '5.0', 'map-name': PyQt4.QtCore.QString( u'Estimated Earthquake Impact'), 'date': '26-7-2012', 'bearing-degrees': '0.00\xb0', 'formatted-date-time': '26-Jul-12 02:15:35 ', 'distance': '0.00', 'direction-relation': PyQt4.QtCore.QString(u'of'), 'credits': PyQt4.QtCore.QString( u'Supported by the Australia-Indonesia Facility' u' for Disaster Reduction, Geoscience Australia ' u'and the World Bank-GFDRR.'), 'latitude-value': u'0\xb012\'36.00"S', 'time': '2:15:35', 'depth-value': '11.0'} result['elapsed-time'] = u'' message = 'Got:\n%s\nExpected:\n%s\n' % (result, expected_dict) self.max_diff = None difference = DictDiffer(result, expected_dict) print difference.all() self.assertDictEqual(expected_dict, result, message)
def testCitiesToShape(self): """Test that we can retrieve the cities local to the event""" myShakeId = '20120726022003' myShakeEvent = ShakeEvent(myShakeId) myPath = myShakeEvent.citiesToShapefile() assert os.path.exists(myPath)
def test_cities_search_boxes_to_shape(self): """Test that we can retrieve the search boxes used to find cities.""" shake_id = '20120726022003' shake_event = ShakeEvent(shake_id) file_path = shake_event.city_search_boxes_to_shapefile() assert os.path.exists(file_path)
def test_cities_to_shape(self): """Test that we can retrieve the cities local to the event.""" shake_id = '20120726022003' shake_event = ShakeEvent(shake_id) file_path = shake_event.cities_to_shapefile() assert os.path.exists(file_path)
def testCitiesSearchBoxesToShape(self): """Test that we can retrieve the search boxes used to find cities.""" myShakeId = '20120726022003' myShakeEvent = ShakeEvent(myShakeId) myPath = myShakeEvent.citySearchBoxesToShapefile() assert os.path.exists(myPath)