def test_eventParser(self): """Test eventFilePath works (using cached data)""" myShakeId = '20120726022003' myShakeEvent = ShakeEvent(myShakeId) self.assertEquals(26, myShakeEvent.day) self.assertEquals(7, myShakeEvent.month) self.assertEquals(2012, myShakeEvent.year) self.assertEquals(2, myShakeEvent.hour) self.assertEquals(15, myShakeEvent.minute) self.assertEquals(35, myShakeEvent.second) self.assertEquals('WIB', myShakeEvent.timeZone) self.assertEquals(124.45, myShakeEvent.longitude) self.assertEquals(-0.21, myShakeEvent.latitude) self.assertEquals(11.0, myShakeEvent.depth) self.assertEquals('Southern Molucca Sea', myShakeEvent.location) self.assertEquals(122.45, myShakeEvent.xMinimum) self.assertEquals(126.45, myShakeEvent.xMaximum) self.assertEquals(-2.21, myShakeEvent.yMinimum) self.assertEquals(1.79, myShakeEvent.yMaximum) myGridXmlData = myShakeEvent.mmiData self.assertEquals(25921, len(myGridXmlData)) myDelimitedString = myShakeEvent.mmiDataToDelimitedText() self.assertEqual(578234, len(myDelimitedString))
def testI18n(self): """See if internationalisation is working.""" myShakeId = '20120726022003' myShakeEvent = ShakeEvent(myShakeId, theLocale='id') myShaking = myShakeEvent.mmiShaking(5) myExpectedShaking = 'Sedang' self.assertEqual(myExpectedShaking, myShaking)
def testCalculateFatalities(self): """Test that we can calculate fatalities.""" LOGGER.debug(QGISAPP.showSettings()) myShakeId = '20120726022003' myShakeEvent = ShakeEvent(myShakeId) myResult, myFatalitiesHtml = myShakeEvent.calculateImpacts() myExpectedResult = ('/tmp/inasafe/realtime/shakemaps-extracted' '/20120726022003/impact-nearest.tif') myMessage = 'Got:\n%s\nExpected:\n%s\n' % (myResult, myExpectedResult) assert myResult == myExpectedResult, myMessage myExpectedResult = ('/tmp/inasafe/realtime/shakemaps-extracted' '/20120726022003/impacts.html') myMessage = 'Got:\n%s\nExpected:\n%s\n' % (myFatalitiesHtml, myExpectedResult) assert myFatalitiesHtml == myExpectedResult, myMessage myExpectedFatalities = { 2: 0.0, # rounded from 0.47386375223673427, 3: 0.0, # rounded from 0.024892573693488258, 4: 0.0, 5: 0.0, 6: 0.0, 7: 0.0, 8: 0.0, 9: 0.0 } myMessage = 'Got:\n%s\nExpected:\n%s\n' % (myShakeEvent.fatalityCounts, myExpectedFatalities) assert myShakeEvent.fatalityCounts == myExpectedFatalities, myMessage
def testSortedImpactedCities(self): """Test getting impacted cities sorted by mmi then population.""" myShakeId = '20120726022003' myShakeEvent = ShakeEvent(myShakeId) myTable = myShakeEvent.sortedImpactedCities() myFilePath = unique_filename( prefix='testSortedImpactedCities', suffix='.txt', dir=temp_dir('test')) myFile = file(myFilePath, 'wt') myFile.writelines(str(myTable)) myFile.close() myTable = str(myTable).replace(', \'', ',\n\'') myTable += '\n' myFixturePath = os.path.join( dataDir(), 'tests', 'testSortedImpactedCities.txt') myFile = file(myFixturePath, 'rt') myExpectedString = myFile.read() myFile.close() myExpectedString = myExpectedString.replace(', \'', ',\n\'') self.maxDiff = None self.assertEqual(myExpectedString, myTable)
def testSortedImpactedCities(self): """Test getting impacted cities sorted by mmi then population.""" myShakeId = '20120726022003' myShakeEvent = ShakeEvent(myShakeId) myTable = myShakeEvent.sortedImpactedCities() myExpectedResult = [ {'dir_from': 13.119078636169434, 'dir_to': -166.88092041015625, 'roman': 'II', 'dist_to': 3.036229133605957, 'mmi-int': 2.0, 'name': 'Manado', 'mmi': 1.809999942779541, 'id': 207, 'population': 451893}, {'dir_from': -61.620426177978516, 'dir_to': 118.37957000732422, 'roman': 'II', 'dist_to': 2.4977917671203613, 'mmi-int': 2.0, 'name': 'Gorontalo', 'mmi': 2.25, 'id': 282, 'population': 144195}, {'dir_from': -114.04046630859375, 'dir_to': 65.95953369140625, 'roman': 'II', 'dist_to': 3.3138768672943115, 'mmi-int': 2.0, 'name': 'Luwuk', 'mmi': 1.5299999713897705, 'id': 215, 'population': 47778}, {'dir_from': 16.94407844543457, 'dir_to': -163.05592346191406, 'roman': 'II', 'dist_to': 2.504295825958252, 'mmi-int': 2.0, 'name': 'Tondano', 'mmi': 1.909999966621399, 'id': 57, 'population': 33317}, {'dir_from': 14.14267635345459, 'dir_to': -165.85733032226562, 'roman': 'II', 'dist_to': 2.5372657775878906, 'mmi-int': 2.0, 'name': 'Tomohon', 'mmi': 1.690000057220459, 'id': 58, 'population': 27624}] myMessage = 'Got:\n%s\nExpected:\n%s\n' % (myTable, myExpectedResult) assert myTable == myExpectedResult, myMessage
def testCalculateFatalities(self): """Test that we can calculate fatalities.""" LOGGER.debug(QGISAPP.showSettings()) myShakeId = '20120726022003' myShakeEvent = ShakeEvent(myShakeId) myResult, myFatalitiesHtml = myShakeEvent.calculateImpacts() myExpectedResult = ( '/tmp/inasafe/realtime/shakemaps-extracted' '/20120726022003/impact-nearest.tif') myMessage = 'Got:\n%s\nExpected:\n%s\n' % (myResult, myExpectedResult) assert myResult == myExpectedResult, myMessage myExpectedResult = ( '/tmp/inasafe/realtime/shakemaps-extracted' '/20120726022003/impacts.html') myMessage = 'Got:\n%s\nExpected:\n%s\n' % ( myFatalitiesHtml, myExpectedResult) assert myFatalitiesHtml == myExpectedResult, myMessage myExpectedFatalities = {2: 0.0, # rounded from 0.47386375223673427, 3: 0.0, # rounded from 0.024892573693488258, 4: 0.0, 5: 0.0, 6: 0.0, 7: 0.0, 8: 0.0, 9: 0.0} myMessage = 'Got:\n%s\nExpected:\n%s\n' % ( myShakeEvent.fatalityCounts, myExpectedFatalities) assert myShakeEvent.fatalityCounts == myExpectedFatalities, myMessage
def testBearingToCardinal(self): """Test we can convert a bearing to a cardinal direction.""" myShakeId = '20120726022003' myShakeEvent = ShakeEvent(myShakeId) # Ints should work myExpectedResult = 'SSE' myResult = myShakeEvent.bearingToCardinal(160) myMessage = ('Got:\n%s\nExpected:\n%s\n' % (myResult, myExpectedResult)) assert myResult == myExpectedResult, myMessage # Floats should work myExpectedResult = 'SW' myResult = myShakeEvent.bearingToCardinal(225.4) myMessage = ('Got:\n%s\nExpected:\n%s\n' % (myResult, myExpectedResult)) assert myResult == myExpectedResult, myMessage # non numeric data as input should return None myExpectedResult = None myResult = myShakeEvent.bearingToCardinal('foo') myMessage = ('Got:\n%s\nExpected:\n%s\n' % (myResult, myExpectedResult)) assert myResult == myExpectedResult, myMessage
def processEvent(theEventId=None, theLocale='en'): """Launcher that actually runs the event processing.""" myPopulationPath = os.path.join( dataDir(), 'exposure', 'IDN_mosaic', 'popmap10_all.tif') # Use cached data where available # Whether we should always regenerate the products myForceFlag = False if 'INASAFE_FORCE' in os.environ: myForceString = os.environ['INASAFE_FORCE'] if str(myForceString).capitalize() == 'Y': myForceFlag = True # We always want to generate en products too so we manipulate the locale # list and loop through them: myLocaleList = [theLocale] if 'en' not in myLocaleList: myLocaleList.append('en') # Now generate the products for myLoc in myLocaleList: # Extract the event # noinspection PyBroadException try: if os.path.exists(myPopulationPath): myShakeEvent = ShakeEvent( theEventId=theEventId, theLocale=myLoc, theForceFlag=myForceFlag, thePopulationRasterPath=myPopulationPath) else: myShakeEvent = ShakeEvent( theEventId=theEventId, theLocale=myLoc, theForceFlag=myForceFlag) except (BadZipfile, URLError): # retry with force flag true if os.path.exists(myPopulationPath): myShakeEvent = ShakeEvent( theEventId=theEventId, theLocale=myLoc, theForceFlag=True, thePopulationRasterPath=myPopulationPath) else: myShakeEvent = ShakeEvent( theEventId=theEventId, theLocale=myLoc, theForceFlag=True) except: LOGGER.exception('An error occurred setting up the shake event.') return LOGGER.info('Event Id: %s', myShakeEvent) LOGGER.info('-------------------------------------------') myShakeEvent.renderMap(myForceFlag)
def test_gridXmlFilePath(self): """Test eventFilePath works(using cached data)""" myShakeId = '20120726022003' myExpectedPath = os.path.join(shakemapExtractDir(), myShakeId, 'grid.xml') myShakeEvent = ShakeEvent(myShakeId) myPath = myShakeEvent.gridFilePath() self.assertEquals(myExpectedPath, myPath)
def testSortedImpactedCities(self): """Test getting impacted cities sorted by mmi then population.""" myShakeId = '20120726022003' myShakeEvent = ShakeEvent(myShakeId) myTable = myShakeEvent.sortedImpactedCities() myExpectedResult = [{ 'dir_from': 13.119078636169434, 'dir_to': -166.88092041015625, 'roman': 'II', 'dist_to': 3.036229133605957, 'mmi-int': 2.0, 'name': 'Manado', 'mmi': 1.809999942779541, 'id': 207, 'population': 451893 }, { 'dir_from': -61.620426177978516, 'dir_to': 118.37957000732422, 'roman': 'II', 'dist_to': 2.4977917671203613, 'mmi-int': 2.0, 'name': 'Gorontalo', 'mmi': 2.25, 'id': 282, 'population': 144195 }, { 'dir_from': -114.04046630859375, 'dir_to': 65.95953369140625, 'roman': 'II', 'dist_to': 3.3138768672943115, 'mmi-int': 2.0, 'name': 'Luwuk', 'mmi': 1.5299999713897705, 'id': 215, 'population': 47778 }, { 'dir_from': 16.94407844543457, 'dir_to': -163.05592346191406, 'roman': 'II', 'dist_to': 2.504295825958252, 'mmi-int': 2.0, 'name': 'Tondano', 'mmi': 1.909999966621399, 'id': 57, 'population': 33317 }, { 'dir_from': 14.14267635345459, 'dir_to': -165.85733032226562, 'roman': 'II', 'dist_to': 2.5372657775878906, 'mmi-int': 2.0, 'name': 'Tomohon', 'mmi': 1.690000057220459, 'id': 58, 'population': 27624 }] myMessage = 'Got:\n%s\nExpected:\n%s\n' % (myTable, myExpectedResult) assert myTable == myExpectedResult, myMessage
def testBoundsToRect(self): """Test that we can calculate the event bounds properly""" myShakeId = '20120726022003' myShakeEvent = ShakeEvent(myShakeId) myBounds = myShakeEvent.boundsToRectangle().toString() myExpectedResult = ('122.4500000000000028,-2.2100000000000000 : ' '126.4500000000000028,1.7900000000000000') myMessage = 'Got:\n%s\nExpected:\n%s\n' % (myBounds, myExpectedResult) assert myBounds == myExpectedResult, myMessage
def testEventToShapefile(self): """Check we can convert the shake event to a raster""" myShakeId = '20120726022003' myShakeEvent = ShakeEvent(myShakeId) myPath = myShakeEvent.mmiDataToShapefile(theForceFlag=True) assert os.path.exists(myPath) myExpectedQml = myPath.replace('shp', 'qml') myMessage = '%s not found' % myExpectedQml assert os.path.exists(myExpectedQml), myMessage
def test_eventGridToCsv(self): """Test grid data can be written to csv""" myShakeId = '20120726022003' myShakeEvent = ShakeEvent(myShakeId) myPath = myShakeEvent.mmiDataToDelimitedFile(theForceFlag=True) myFile = file(myPath, 'rt') myString = myFile.readlines() myFile.close() self.assertEqual(25922, len(myString))
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 testRomanize(self): """Test we can convert MMI values to float.""" myShakeId = '20120726022003' myShakeEvent = ShakeEvent(myShakeId) myValues = range(2, 10) myExpectedResult = ['II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX'] myResult = [] for myValue in myValues: myResult.append(myShakeEvent.romanize(myValue)) myMessage = 'Got:\n%s\nExpected:\n%s\n' % (myResult, myExpectedResult) assert myResult == myExpectedResult, myMessage
def test_extractDateTime(self): """Check that we extract date and time correctly.""" myShakeId = '20120726022003' myShakeEvent = ShakeEvent(myShakeId, theLocale='en') myShakeEvent.extractDateTime('2012-08-07T01:55:12WIB') self.assertEqual(1, myShakeEvent.hour) self.assertEqual(55, myShakeEvent.minute) self.assertEqual(12, myShakeEvent.second) myShakeEvent.extractDateTime('2013-02-07T22:22:37WIB') self.assertEqual(22, myShakeEvent.hour) self.assertEqual(22, myShakeEvent.minute) self.assertEqual(37, myShakeEvent.second)
def testEventInfoString(self): """Test we can get a location info string nicely.""" myShakeId = '20120726022003' myShakeEvent = ShakeEvent(myShakeId) myDegreeSymbol = unichr(176) myExpectedResult = ( 'M 5.0 26-7-2012 2:15:35 Latitude: 0%s12\'36.00"S Longitude: ' '124%s27\'0.00"E Depth: 11.0km Located 0.00km n/a of n/a' % (myDegreeSymbol, myDegreeSymbol)) myResult = myShakeEvent.eventInfo() myMessage = ('Got:\n%s\nExpected:\n%s\n' % (myResult, myExpectedResult)) assert myResult == myExpectedResult, myMessage
def testMmiColour(self): """Test that we can get a colour given an mmi number.""" myShakeId = '20120726022003' myShakeEvent = ShakeEvent(myShakeId) myValues = range(0, 12) myExpectedResult = ['#FFFFFF', '#FFFFFF', '#209fff', '#00cfff', '#55ffff', '#aaffff', '#fff000', '#ffa800', '#ff7000', '#ff0000', '#D00', '#800'] myResult = [] for myValue in myValues: myResult.append(myShakeEvent.mmiColour(myValue)) myMessage = 'Got:\n%s\nExpected:\n%s\n' % (myResult, myExpectedResult) assert myResult == myExpectedResult, myMessage
def testMmiColour(self): """Test that we can get a colour given an mmi number.""" myShakeId = '20120726022003' myShakeEvent = ShakeEvent(myShakeId) myValues = range(0, 12) myExpectedResult = [ '#FFFFFF', '#FFFFFF', '#209fff', '#00cfff', '#55ffff', '#aaffff', '#fff000', '#ffa800', '#ff7000', '#ff0000', '#D00', '#800' ] myResult = [] for myValue in myValues: myResult.append(myShakeEvent.mmiColour(myValue)) myMessage = 'Got:\n%s\nExpected:\n%s\n' % (myResult, myExpectedResult) assert myResult == myExpectedResult, myMessage
def testImpactedCitiesTable(self): """Test getting impacted cities table.""" myShakeId = '20120726022003' myShakeEvent = ShakeEvent(myShakeId) myTable, myPath = myShakeEvent.impactedCitiesTable() myExpectedResult = 906 myTable = myTable.toNewlineFreeString() myResult = len(myTable) myMessage = ('Got:\n%s\nExpected:\n%s\nFor rendered table:\n%s' % (myResult, myExpectedResult, myTable)) assert myResult == myExpectedResult, myMessage 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 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() myFeature = QgsFeature() myAttributes = myProvider.attributeIndexes() myProvider.select(myAttributes) myExpectedFeatureCount = 6 self.assertEquals(myProvider.featureCount(), myExpectedFeatureCount) myStrings = [] while myProvider.nextFeature(myFeature): # fetch map of attributes myAttributes = myFeature.attributeMap() for (myKey, myValue) in myAttributes.iteritems(): myStrings.append("%d: %s\n" % (myKey, myValue.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(myStrings, myExpectedString) 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 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 timeZone: WIB xMinimum: 122.45 xMaximum: 126.45 yMinimum: -2.21 yMaximum: 1.79 rows: 161.0 columns: 161.0 mmiData: Populated populationRasterPath: None impactFile: None impactKeywordsFile: None fatalityCounts: None displacedCounts: None affectedCounts: None extentWithCities: Not set zoomFactor: 1.25 searchBoxes: 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 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(myStrings, myExpectedString) 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 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 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 testSortedImpactedCities(self): """Test getting impacted cities sorted by mmi then population.""" myShakeId = '20120726022003' myShakeEvent = ShakeEvent(myShakeId) myTable = myShakeEvent.sortedImpactedCities() myFilePath = unique_filename(prefix='testSortedImpactedCities', suffix='.txt', dir=temp_dir('test')) myFile = file(myFilePath, 'wt') myFile.writelines(str(myTable)) myFile.close() myTable = str(myTable).replace(', \'', ',\n\'') myTable += '\n' myFixturePath = os.path.join(dataDir(), 'tests', 'testSortedImpactedCities.txt') myFile = file(myFixturePath, 'rt') myExpectedString = myFile.read() myFile.close() myExpectedString = myExpectedString.replace(', \'', ',\n\'') self.maxDiff = None self.assertEqual(myExpectedString, myTable)
def process_event(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", "IDN_mosaic", "popmap10_all.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( event_id=event_id, locale=locale, force_flag=force_flag, population_raster_path=population_path ) else: shake_event = ShakeEvent(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( event_id=event_id, locale=locale, force_flag=True, population_raster_path=population_path ) else: shake_event = ShakeEvent(event_id=event_id, locale=locale, force_flag=True) 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 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)
# myShakeEvent = ShakeGridConverter( # theEventId=myId, # theLocale='en', # theForceFlag=False, # theDataIsLocalFlag=True) # myShakeEvent.render_map(theForceFlag=False) # # myId = '20120118231552_se' # myShakeEvent = ShakeGridConverter( # theEventId=myId, # theLocale='en', # theForceFlag=False, # theDataIsLocalFlag=True) # myShakeEvent.render_map(theForceFlag=False) # # # myId = '20120118231562_se' # myShakeEvent = ShakeGridConverter( # theEventId=myId, # theLocale='en', # theForceFlag=False, # theDataIsLocalFlag=True) # myShakeEvent.render_map(theForceFlag=False) myId = '20130224200633' myShakeEvent = ShakeEvent(event_id=myId, locale='en', force_flag=False, data_is_local_flag=True) myShakeEvent.render_map(force_flag=False)
def testEventInfoDict(self): """Test we can get a dictionary of location info nicely.""" myShakeId = '20120726022003' myShakeEvent = ShakeEvent(myShakeId) myResult = myShakeEvent.eventDict() myExpectedDict = {'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'} myResult['elapsed-time'] = u'' myMessage = 'Got:\n%s\nExpected:\n%s\n' % (myResult, myExpectedDict) self.maxDiff = None myDifference = DictDiffer(myResult, myExpectedDict) print myDifference.all() self.assertDictEqual(myExpectedDict, myResult, myMessage)
# coding=utf-8 """Simple helper for when you already have the grid.xml and just want a map.""" #Tim Sutton, April 2013. from shake_event import ShakeEvent if __name__ == '__main__': SHAKE_ID = '20131105060809' # To ensure that realtime is working for not just en locale, use other # locale here to test realtime locales = ['en', 'id'] for locale in locales: shake_event = ShakeEvent( event_id=SHAKE_ID, locale=locale, force_flag=False, data_is_local_flag=True) shake_event.render_map(force_flag=False)
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)
# theEventId=myId, # theLocale='en', # theForceFlag=False, # theDataIsLocalFlag=True) # myShakeEvent.render_map(theForceFlag=False) # # myId = '20120118231552_se' # myShakeEvent = ShakeGridConverter( # theEventId=myId, # theLocale='en', # theForceFlag=False, # theDataIsLocalFlag=True) # myShakeEvent.render_map(theForceFlag=False) # # # myId = '20120118231562_se' # myShakeEvent = ShakeGridConverter( # theEventId=myId, # theLocale='en', # theForceFlag=False, # theDataIsLocalFlag=True) # myShakeEvent.render_map(theForceFlag=False) myId = '20130224200633' myShakeEvent = ShakeEvent( event_id=myId, locale='en', force_flag=False, data_is_local_flag=True) myShakeEvent.render_map(force_flag=False)
def testEventInfoDict(self): """Test we can get a dictionary of location info nicely.""" myShakeId = '20120726022003' myShakeEvent = ShakeEvent(myShakeId) myResult = myShakeEvent.eventDict() myExpectedDict = { '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' } myResult['elapsed-time'] = u'' myMessage = 'Got:\n%s\nExpected:\n%s\n' % (myResult, myExpectedDict) self.maxDiff = None myDifference = DictDiffer(myResult, myExpectedDict) print myDifference.all() self.assertDictEqual(myExpectedDict, myResult, myMessage)
# coding=utf-8 """Simple helper for when you already have the grid.xml and just want a map.""" # Tim Sutton, April 2013. import sys from shake_event import ShakeEvent if __name__ == '__main__': working_dir = sys.argv[1] SHAKE_ID = '20131105060809' # To ensure that realtime is working for not just en locale, use other # locale here to test realtime locales = ['en', 'id'] for locale in locales: shake_event = ShakeEvent( working_dir=working_dir, event_id=SHAKE_ID, locale=locale, force_flag=False, data_is_local_flag=True) shake_event.render_map(force_flag=False)
# Simple helper for when you already have the grid.xml and you just want a # map. # # Tim Sutton, April 2013 from shake_event import ShakeEvent myId = '20120118231542_se' myShakeEvent = ShakeEvent( theEventId=myId, theLocale='id', theForceFlag=False, theDataIsLocalFlag=True) myShakeEvent.renderMap(theForceFlag=False) myId = '20120118231552_se' myShakeEvent = ShakeEvent( theEventId=myId, theLocale='id', theForceFlag=False, theDataIsLocalFlag=True) myShakeEvent.renderMap(theForceFlag=False) myId = '20120118231562_se' myShakeEvent = ShakeEvent( theEventId=myId, theLocale='id', theForceFlag=False, theDataIsLocalFlag=True) myShakeEvent.renderMap(theForceFlag=False)
# myShakeEvent = ShakeEvent( # theEventId=myId, # theLocale='en', # theForceFlag=False, # theDataIsLocalFlag=True) # myShakeEvent.renderMap(theForceFlag=False) # # myId = '20120118231552_se' # myShakeEvent = ShakeEvent( # theEventId=myId, # theLocale='en', # theForceFlag=False, # theDataIsLocalFlag=True) # myShakeEvent.renderMap(theForceFlag=False) # # # myId = '20120118231562_se' # myShakeEvent = ShakeEvent( # theEventId=myId, # theLocale='en', # theForceFlag=False, # theDataIsLocalFlag=True) # myShakeEvent.renderMap(theForceFlag=False) myId = '20130224200633' myShakeEvent = ShakeEvent(theEventId=myId, theLocale='en', theForceFlag=False, theDataIsLocalFlag=True) myShakeEvent.renderMap(theForceFlag=False)
def process_event(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', 'IDN_mosaic', 'popmap10_all.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( event_id=event_id, locale=locale, force_flag=force_flag, population_raster_path=population_path) else: shake_event = ShakeEvent(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( event_id=event_id, locale=locale, force_flag=True, population_raster_path=population_path) else: shake_event = ShakeEvent(event_id=event_id, locale=locale, force_flag=True) 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)