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 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 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 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)