Skip to content

Commit

Permalink
Improve support for analysis extent option
Browse files Browse the repository at this point in the history
  • Loading branch information
lucernae committed Jun 26, 2017
1 parent 215a721 commit 429fb7b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 9 deletions.
31 changes: 25 additions & 6 deletions bin/inasafe.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/env python
# coding=utf-8
"""
InaSAFE Disaster risk assessment tool developed by AusAid -
Expand Down Expand Up @@ -74,7 +74,17 @@ def __init__(self, arguments_=None):
self.report_template = arguments_['--report-template']
# optional arguments
if not arguments_['--extent'] is None:
self.extent = arguments_['--extent'].replace(',', '.').split(':')
# Extent is in the format: xmin,ymin,xmax,ymax
# example: -10.01,10.01,-9.01,11.01
try:
self.extent = arguments_['--extent'].split(',')
# convert to float
self.extent = [float(n) for n in self.extent]
if len(self.extent) != 4:
raise ValueError()
except ValueError as e:
e.message = 'Improperly formatted Extent option'
raise
else:
LOGGER.debug('no extent specified')
if arguments_['--download']:
Expand Down Expand Up @@ -288,15 +298,20 @@ def impact_function_setup(
impact_function.map_canvas = CANVAS
# QSetting context
settings = QSettings()
crs = settings.value('inasafe/analysis_extent_crs', '', type=str)
impact_function.requested_extent_crs = QgsCoordinateReferenceSystem(crs)
# Default extent CRS is EPSG:4326
impact_function.requested_extent_crs = QgsCoordinateReferenceSystem(
'EPSG:4326')
try:
impact_function.requested_extent = QgsRectangle(
float(command_line_arguments.extent[0]),
float(command_line_arguments.extent[1]),
float(command_line_arguments.extent[2]),
float(command_line_arguments.extent[3])
)
# Use HazardExposureBoundingBox settings
settings.setValue(
'inasafe/analysis_extents_mode',
'HazardExposureBoundingBox')
except AttributeError:
print "No extents"
pass
Expand Down Expand Up @@ -349,12 +364,16 @@ def build_report(cli_arguments):
extra_layers = [hazard_layer]
layer_registry.addMapLayer(impact_layer)
layer_registry.addMapLayers(extra_layers)
CANVAS.setExtent(impact_layer.extent())
if cli_arguments.extent:
report_extent = QgsRectangle(*cli_arguments.extent)
else:
report_extent = impact_layer.extent()
CANVAS.setExtent(report_extent)
CANVAS.refresh()
report = ImpactReport(
IFACE, cli_arguments.report_template, impact_layer,
extra_layers=extra_layers)
report.extent = CANVAS.fullExtent()
report.extent = report_extent
LOGGER.debug(os.path.splitext(cli_arguments.output_file)[0] + '.pdf')
map_path = report.print_map_to_pdf(
os.path.splitext(cli_arguments.output_file)[0] + '.pdf')
Expand Down
2 changes: 1 addition & 1 deletion bin/usage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Usage:
inasafe --hazard=HAZARD_FILE (--download --layers=LAYER_NAME [LAYER_NAME...] | --exposure=EXP_FILE | --aggregation=AG_FILE)
--impact-function=IF_ID --report-template=TEMPLATE --output-file=FILE [--extent=XMIN:YMIN:XMAX:YMAX]
inasafe [--hazard=HAZARD_FILE --exposure=EXP_FILE] (--version | --list-functions)
inasafe --download --layers=LAYER_NAME [LAYER_NAME...] --extent=XMIN:YMIN:XMAX:YMAX
inasafe --download --layers=LAYER_NAME [LAYER_NAME...] --extent=XMIN,YMIN,XMAX,YMAX
inasafe report [--report-template=TEMPLATE] --output-file=IMPACT_FILE

Options:
Expand Down
35 changes: 33 additions & 2 deletions headless/tasks/inasafe_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,37 @@ def filter_impact_function(hazard=None, exposure=None):

@app.task(queue='inasafe-headless-analysis')
def run_analysis(hazard, exposure, function, aggregation=None,
generate_report=False):
"""Run analysis"""
generate_report=False,
requested_extent=None):

"""Run analysis with a given combination
Proxy tasks for celery broker. It is not actually implemented here.
It is implemented in InaSAFE headless.tasks package
:param hazard: hazard layer url
:type hazard: str
:param exposure: exposure layer url
:type exposure: str
:param function: Impact Function ID of valid combination of
hazard and exposure
:type function: str
:param aggregation: aggregation layer url
:type aggregation: str
:param generate_report: set True to generate pdf report
:type generate_report: bool
:param requested_extent: An extent of BBOX format list to denote the area
of analysis. In CRS EPSG:4326
:type requested_extent: list(float)
:return: Impact layer url
:rtype: str
"""
hazard_file = download_layer(hazard)
exposure_file = download_layer(exposure)
aggregation_file = None
Expand All @@ -67,6 +96,8 @@ def run_analysis(hazard, exposure, function, aggregation=None,
arguments.exposure = exposure_file
arguments.aggregation = aggregation_file
arguments.impact_function = function
if requested_extent:
arguments.extent = requested_extent

# generate names for impact results
# create date timestamp
Expand Down

0 comments on commit 429fb7b

Please sign in to comment.