Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

PEP8 and other celanups for realtime

  • Loading branch information...
commit 9922dea5aba67b5557a56bbae04c97e326a9326d 1 parent 2e56dbf
Tim Sutton timlinux authored
140 realtime/shake_event.py
View
@@ -127,7 +127,8 @@ def __init__(self, theEventId=None,
# We inherit from QObject for translation support
QObject.__init__(self)
# self.data = ShakeData(theEventId, theForceFlag)
- self.data = SftpShakeData(theEvent=theEventId,
+ self.data = SftpShakeData(
+ theEvent=theEventId,
theForceFlag=theForceFlag)
self.data.extract()
self.latitude = None
@@ -303,12 +304,12 @@ def parseGridXml(self):
self.latitude = float(myEventElement.attributes[
'lat'].nodeValue)
self.location = myEventElement.attributes[
- 'event_description'].nodeValue.strip()
+ 'event_description'].nodeValue.strip()
self.depth = float(myEventElement.attributes['depth'].nodeValue)
# Get the date - its going to look something like this:
# 2012-08-07T01:55:12WIB
myTimeStamp = myEventElement.attributes[
- 'event_timestamp'].nodeValue
+ 'event_timestamp'].nodeValue
self.extractDateTime(myTimeStamp)
# Note the timezone here is inconsistent with YZ from grid.xml
# use the latter
@@ -351,8 +352,9 @@ def parseGridXml(self):
except Exception, e:
LOGGER.exception('Event parse failed')
- raise GridXmlParseError('Failed to parse grid file.\n%s\n%s'
- % (e.__class__, str(e)))
+ raise GridXmlParseError(
+ 'Failed to parse grid file.\n%s\n%s'
+ % (e.__class__, str(e)))
def mmiDataToDelimitedText(self):
"""Return the mmi data as a delimited test string.
@@ -413,9 +415,8 @@ def mmiDataToDelimitedFile(self, theForceFlag=True):
myFile.close()
# Also write the .csv which contains metadata about field types
- myCsvPath = os.path.join(shakemapExtractDir(),
- self.eventId,
- 'mmi.csvt')
+ myCsvPath = os.path.join(
+ shakemapExtractDir(), self.eventId, 'mmi.csvt')
myFile = file(myCsvPath, 'wt')
myFile.write('"Real","Real","Real"')
myFile.close()
@@ -543,11 +544,9 @@ def mmiDataToShapefile(self, theForceFlag=False):
#now generate the tif using default interpoation options
- myCommand = (('ogr2ogr -overwrite -select mmi -a_srs EPSG:4326 '
- '%(shp)s %(vrt)s mmi') % {
- 'shp': myShpPath,
- 'vrt': myVrtPath
- })
+ myCommand = (
+ ('ogr2ogr -overwrite -select mmi -a_srs EPSG:4326 '
+ '%(shp)s %(vrt)s mmi') % {'shp': myShpPath, 'vrt': myVrtPath})
LOGGER.info('Created this gdal command:\n%s' % myCommand)
# Now run GDAL warp scottie...
@@ -626,10 +625,7 @@ def mmiDataToRaster(self, theForceFlag=False, theAlgorithm='nearest'):
else:
myAlgorithm = theAlgorithm
- myCommand = (('gdal_grid -a %(alg)s -zfield "mmi" -txe %(xMin)s '
- '%(xMax)s -tye %(yMin)s %(yMax)s -outsize %(dimX)i '
- '%(dimY)i -of GTiff -ot Float16 -a_srs EPSG:4326 -l mmi '
- '%(vrt)s %(tif)s') % {
+ myOptions = {
'alg': myAlgorithm,
'xMin': self.xMinimum,
'xMax': self.xMaximum,
@@ -638,17 +634,22 @@ def mmiDataToRaster(self, theForceFlag=False, theAlgorithm='nearest'):
'dimX': self.columns,
'dimY': self.rows,
'vrt': myVrtPath,
- 'tif': myTifPath
- })
+ 'tif': myTifPath}
+
+ myCommand = (('gdal_grid -a %(alg)s -zfield "mmi" -txe %(xMin)s '
+ '%(xMax)s -tye %(yMin)s %(yMax)s -outsize %(dimX)i '
+ '%(dimY)i -of GTiff -ot Float16 -a_srs EPSG:4326 -l mmi '
+ '%(vrt)s %(tif)s') % myOptions)
LOGGER.info('Created this gdal command:\n%s' % myCommand)
# Now run GDAL warp scottie...
self._runCommand(myCommand)
# copy the keywords file from fixtures for this layer
- myKeywordPath = os.path.join(shakemapExtractDir(),
- self.eventId,
- 'mmi-%s.keywords' % theAlgorithm)
+ myKeywordPath = os.path.join(
+ shakemapExtractDir(),
+ self.eventId,
+ 'mmi-%s.keywords' % theAlgorithm)
mySourceKeywords = os.path.join(dataDir(), 'mmi.keywords')
shutil.copyfile(mySourceKeywords, myKeywordPath)
# Lastly copy over the standard qml (QGIS Style file) for the mmi.tif
@@ -699,7 +700,8 @@ def mmiDataToContours(self, theForceFlag=True, theAlgorithm='nearest'):
os.remove(myOutputFileBase + 'dbf')
os.remove(myOutputFileBase + 'prj')
except OSError:
- LOGGER.exception('Old contour files not deleted'
+ LOGGER.exception(
+ 'Old contour files not deleted'
' - this may indicate a file permissions issue.')
myTifPath = self.mmiDataToRaster(theForceFlag, theAlgorithm)
@@ -1076,7 +1078,8 @@ def memoryLayerToShapefile(self,
os.remove(myOutputFileBase + 'dbf')
os.remove(myOutputFileBase + 'prj')
except OSError:
- LOGGER.exception('Old shape files not deleted'
+ LOGGER.exception(
+ 'Old shape files not deleted'
' - this may indicate a file permissions issue.')
# Next two lines a workaround for a QGIS bug (lte 1.8)
@@ -1104,8 +1107,7 @@ def memoryLayerToShapefile(self,
myOptions,
myLayerOptions,
mySkipAttributesFlag,
- myActualNewFileName
- )
+ myActualNewFileName)
if myResult == QgsVectorFileWriter.NoError:
LOGGER.debug('Wrote mem layer to shp: %s' % myOutputFile)
@@ -1232,8 +1234,9 @@ def localCityFeatures(self):
# mmi to get a tighter AOI then do a small zoom out.
self.extentWithCities = myRectangle
if not myFoundFlag:
- LOGGER.debug('Could not find %s cities after expanding rect '
- '%s times.' % (myMinimumCityCount, myAttemptsLimit))
+ LOGGER.debug(
+ 'Could not find %s cities after expanding rect '
+ '%s times.' % (myMinimumCityCount, myAttemptsLimit))
# Setup field indexes of our input and out datasets
myCities = []
myLayerIdIndex = myLayerProvider.fieldNameIndex('id')
@@ -1254,8 +1257,8 @@ def localCityFeatures(self):
myEpicenter = QgsPoint(self.longitude, self.latitude)
# Should not need this to be repeated here but not working without it
- myLayer.select(myIndexes, myRectangle,
- myFetchGeometryFlag, myUseIntersectionFlag)
+ myLayer.select(
+ myIndexes, myRectangle, myFetchGeometryFlag, myUseIntersectionFlag)
# Now loop through the db adding selected features to mem layer
while myLayerProvider.nextFeature(myFeature):
if not myFeature.isValid():
@@ -1288,8 +1291,8 @@ def localCityFeatures(self):
# Populate the mmi field by raster lookup
# Get a {int, QVariant} back
- myRasterValues = myRasterLayer.dataProvider().identify(myPoint,
- QgsRasterDataProvider.IdentifyFormatValue)
+ myRasterValues = myRasterLayer.dataProvider().identify(
+ myPoint, QgsRasterDataProvider.IdentifyFormatValue)
myRasterValues = myRasterValues.values()
if not myRasterValues or len(myRasterValues) < 1:
# position not found on raster
@@ -1347,14 +1350,13 @@ def localCitiesMemoryLayer(self):
QgsField('dir_to', QVariant.Double),
QgsField('dir_from', QVariant.Double),
QgsField('roman', QVariant.String),
- QgsField('colour', QVariant.String),
- ])
+ QgsField('colour', QVariant.String)])
myCities = self.localCityFeatures()
myResult = myMemoryProvider.addFeatures(myCities)
if not myResult:
LOGGER.exception('Unable to add features to cities memory layer')
- raise CityMemoryLayerCreationError('Could not add any features'
- 'to cities memory layer.')
+ raise CityMemoryLayerCreationError(
+ 'Could not add any features to cities memory layer.')
myMemoryLayer.commitChanges()
myMemoryLayer.updateExtents()
@@ -1406,8 +1408,8 @@ def citySearchBoxMemoryLayer(self, theForceFlag=False):
if not myResult:
LOGGER.exception('Unable to add features to city search boxes'
'memory layer')
- raise CityMemoryLayerCreationError('Could not add any features'
- 'to city search boxes memory layer.')
+ raise CityMemoryLayerCreationError(
+ 'Could not add any features to city search boxes memory layer')
myMemoryLayer.commitChanges()
myMemoryLayer.updateExtents()
@@ -1652,16 +1654,17 @@ def impactTable(self):
8: 0.0,
9: 0.0}
"""
- myHeader = [TableCell(self.tr('Intensity'),
- header=True)]
- myAffectedRow = [TableCell(self.tr('People Affected (x 1000)'),
- header=True)]
- myImpactRow = [TableCell(self.tr('Perceived Shaking'),
- header=True)]
+ myHeader = [
+ TableCell(self.tr('Intensity'), header=True)]
+ myAffectedRow = [
+ TableCell(self.tr('People Affected (x 1000)'), header=True)]
+ myImpactRow = [
+ TableCell(self.tr('Perceived Shaking'), header=True)]
for myMmi in range(2, 10):
- myHeader.append(TableCell(self.romanize(myMmi),
- cell_class='mmi-%s' % myMmi,
- header=True))
+ myHeader.append(
+ TableCell(self.romanize(myMmi),
+ cell_class='mmi-%s' % myMmi,
+ header=True))
if myMmi in self.affectedCounts:
myAffectedRow.append(
'%i' % round(self.affectedCounts[myMmi] / 1000))
@@ -1670,19 +1673,19 @@ def impactTable(self):
myImpactRow.append(TableCell(self.mmiShaking(myMmi)))
- myTableBody = []
+ myTableBody = list()
myTableBody.append(myAffectedRow)
myTableBody.append(myImpactRow)
myTable = Table(myTableBody, header_row=myHeader,
table_class='table table-striped table-condensed')
myPath = self.writeHtmlTable(theFileName='impacts.html',
- theTable=myTable)
+ theTable=myTable)
return myPath
def calculateImpacts(self,
- thePopulationRasterPath=None,
- theForceFlag=False,
- theAlgorithm='nearest'):
+ thePopulationRasterPath=None,
+ theForceFlag=False,
+ theAlgorithm='nearest'):
"""Use the SAFE ITB earthquake function to calculate impacts.
Args:
@@ -1709,16 +1712,16 @@ def calculateImpacts(self,
"""
if thePopulationRasterPath is None or (
- not os.path.isfile(thePopulationRasterPath) and
- not os.path.islink(thePopulationRasterPath)):
+ not os.path.isfile(thePopulationRasterPath) and not
+ os.path.islink(thePopulationRasterPath)):
myExposurePath = self._getPopulationPath()
else:
myExposurePath = thePopulationRasterPath
myHazardPath = self.mmiDataToRaster(
- theForceFlag=theForceFlag,
- theAlgorithm=theAlgorithm)
+ theForceFlag=theForceFlag,
+ theAlgorithm=theAlgorithm)
myClippedHazardPath, myClippedExposurePath = self.clipLayers(
theShakeRasterPath=myHazardPath,
@@ -1738,8 +1741,9 @@ def calculateImpacts(self,
myDisplaced = myResult.keywords['displaced_per_mmi']
myTotalFatalities = myResult.keywords['total_fatalities']
except:
- LOGGER.exception('fatalities_per_mmi key not found in:\n%s' %
- myResult.keywords)
+ LOGGER.exception(
+ 'Fatalities_per_mmi key not found in:\n%s' %
+ myResult.keywords)
raise
# Copy the impact layer into our extract dir.
myTifPath = os.path.join(shakemapExtractDir(),
@@ -1748,9 +1752,10 @@ def calculateImpacts(self,
shutil.copyfile(myResult.filename, myTifPath)
LOGGER.debug('Copied impact result to:\n%s\n' % myTifPath)
# Copy the impact keywords layer into our extract dir.
- myKeywordsPath = os.path.join(shakemapExtractDir(),
- self.eventId,
- 'impact-%s.keywords' % theAlgorithm)
+ myKeywordsPath = os.path.join(
+ shakemapExtractDir(),
+ self.eventId,
+ 'impact-%s.keywords' % theAlgorithm)
myKeywordsSource = os.path.splitext(myResult.filename)[0]
myKeywordsSource = '%s.keywords' % myKeywordsSource
shutil.copyfile(myKeywordsSource, myKeywordsPath)
@@ -1794,7 +1799,7 @@ def clipLayers(self, theShakeRasterPath, thePopulationRasterPath):
# Reproject all extents to EPSG:4326 if needed
myGeoCrs = QgsCoordinateReferenceSystem()
- myGeoCrs.createFromEpsg(4326)
+ myGeoCrs.createFromId(4326, QgsCoordinateReferenceSystem.EpsgCrsId)
# Get the Hazard extents as an array in EPSG:4326
# Note that we will always clip to this extent regardless of
@@ -1802,8 +1807,9 @@ def clipLayers(self, theShakeRasterPath, thePopulationRasterPath):
# from safe_qgis which takes care to ensure that the two layers
# have coincidental coverage before clipping. The
# clipper function will take care to null padd any missing data.
- myHazardGeoExtent = extentToGeoArray(myHazardLayer.extent(),
- myHazardLayer.crs())
+ myHazardGeoExtent = extentToGeoArray(
+ myHazardLayer.extent(),
+ myHazardLayer.crs())
# Next work out the ideal spatial resolution for rasters
# in the analysis. If layers are not native WGS84, we estimate
@@ -1832,9 +1838,9 @@ def clipLayers(self, theShakeRasterPath, thePopulationRasterPath):
# The extents should already be correct but the cell size may need
# resampling, so we pass the hazard layer to the clipper
myClippedHazardPath = clipLayer(
- theLayer=myHazardLayer,
- theExtent=myHazardGeoExtent,
- theCellSize=myCellSize)
+ theLayer=myHazardLayer,
+ theExtent=myHazardGeoExtent,
+ theCellSize=myCellSize)
myClippedExposurePath = clipLayer(
theLayer=myExposureLayer,
6 realtime/test_sftp_client.py
View
@@ -34,9 +34,9 @@ def test_download_path(self):
my_ssh_client = SFtpClient(the_working_dir='shakemaps')
assert(my_ssh_client is not None)
- remote_path = os.path.join(my_ssh_client.sftp.getcwd(),
- '20130113003746/output/grid.xml')
- local_path = '/home/sunnii/Documents/inasafe/'
+ remote_path = os.path.join(
+ my_ssh_client.sftp.getcwd(), '20130113003746/output/grid.xml')
+ local_path = '/tmp/inasafe/'
print local_path
my_ssh_client.download_path(remote_path, local_path)
30 realtime/test_shake_event.py
View
@@ -257,15 +257,18 @@ def testCalculateFatalities(self):
myShakeEvent = ShakeEvent(myShakeId)
myResult, myFatalitiesHtml = myShakeEvent.calculateImpacts()
- myExpectedResult = ('/tmp/inasafe/realtime/shakemaps-extracted'
- '/20120726022003/impact-nearest.tif')
+ 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')
+ myExpectedResult = (
+ '/tmp/inasafe/realtime/shakemaps-extracted'
+ '/20120726022003/impacts.html')
- myMessage = 'Got:\n%s\nExpected:\n%s\n' % (myFatalitiesHtml,
+ myMessage = 'Got:\n%s\nExpected:\n%s\n' % (
+ myFatalitiesHtml,
myExpectedResult)
assert myFatalitiesHtml == myExpectedResult, myMessage
@@ -279,7 +282,7 @@ def testCalculateFatalities(self):
9: 0.0}
myMessage = 'Got:\n%s\nExpected:\n%s\n' % (
- myShakeEvent.fatalityCounts, myExpectedFatalities)
+ myShakeEvent.fatalityCounts, myExpectedFatalities)
assert myShakeEvent.fatalityCounts == myExpectedFatalities, myMessage
def testBoundsToRect(self):
@@ -287,8 +290,9 @@ def testBoundsToRect(self):
myShakeId = '20120726022003'
myShakeEvent = ShakeEvent(myShakeId)
myBounds = myShakeEvent.boundsToRectangle().toString()
- myExpectedResult = ('122.4500000000000028,-2.2100000000000000 : '
- '126.4500000000000028,1.7900000000000000')
+ myExpectedResult = (
+ '122.4500000000000028,-2.2100000000000000 : '
+ '126.4500000000000028,1.7900000000000000')
myMessage = 'Got:\n%s\nExpected:\n%s\n' % (myBounds, myExpectedResult)
assert myBounds == myExpectedResult, myMessage
@@ -370,8 +374,9 @@ def testImpactedCitiesTable(self):
(myResult, myExpectedResult, myTable))
assert myResult == myExpectedResult, myMessage
- myExpectedPath = ('/tmp/inasafe/realtime/shakemaps-extracted/'
- '20120726022003/affected-cities.html')
+ myExpectedPath = (
+ '/tmp/inasafe/realtime/shakemaps-extracted/'
+ '20120726022003/affected-cities.html')
myMessage = 'Got:\n%s\nExpected:\n%s\n' % (myPath, myExpectedPath)
assert myPath == myExpectedPath, myMessage
@@ -382,8 +387,9 @@ def testFatalitiesTable(self):
myShakeEvent.calculateImpacts()
myResult = myShakeEvent.impactTable()
# TODO compare actual content of impact table...
- myExpectedResult = ('/tmp/inasafe/realtime/shakemaps-extracted/'
- '20120726022003/impacts.html')
+ myExpectedResult = (
+ '/tmp/inasafe/realtime/shakemaps-extracted/'
+ '20120726022003/impacts.html')
myMessage = ('Got:\n%s\nExpected:\n%s' %
(myResult, myExpectedResult))
assert myResult == myExpectedResult, myMessage
Please sign in to comment.
Something went wrong with that request. Please try again.