Пример #1
0
    def calculate_impact(self):
        if_manager = ImpactFunctionManager()
        function_id = self.function_id
        impact_function = if_manager.get_instance(function_id)

        impact_function.hazard = SafeLayer(self.hazard_layer)
        impact_function.exposure = SafeLayer(self.exposure_layer)

        try:
            self.impact_layer = safe_calculate_impact(impact_function)
            self.set_style()
        except ZeroImpactException as e:
            # in case zero impact, just return
            LOGGER.info('No impact detected')
            LOGGER.info(e.message)
            return

        # copy results of impact to report_path directory
        base_name, _ = os.path.splitext(self.impact_layer.filename)
        dir_name = os.path.dirname(self.impact_layer.filename)
        for (root, dirs, files) in os.walk(dir_name):
            for f in files:
                source_filename = os.path.join(root, f)
                if source_filename.find(base_name) >= 0:
                    extensions = source_filename.replace(base_name, '')
                    new_path = os.path.join(self.report_path,
                                            'impact' + extensions)
                    shutil.copy(source_filename, new_path)

        self.impact_layer = read_layer(self.impact_path)
Пример #2
0
def analysis_execution():

    from safe.test.utilities import get_qgis_app

    # get_qgis_app must be called before importing Analysis
    QGIS_APP, CANVAS, IFACE, PARENT = get_qgis_app()

    from safe.utilities.analysis import Analysis
    from safe.utilities.keyword_io import KeywordIO

    analysis = Analysis()

    arg = AnalysisArguments.read_arguments()

    register_impact_functions()

    registry = ImpactFunctionManager().registry

    function = registry.get_instance(arg.impact_function_name)

    hazard_layer = safe_to_qgis_layer(read_layer(arg.hazard_filename))
    exposure_layer = safe_to_qgis_layer(read_layer(arg.exposure_filename))
    if arg.aggregation_filename:
        aggregation_layer = safe_to_qgis_layer(
            read_layer(arg.aggregation_filename))

    keywords_io = KeywordIO()

    try:
        analysis.map_canvas = IFACE.mapCanvas()
        analysis.hazard_layer = hazard_layer
        analysis.hazard_keyword = keywords_io.read_keywords(hazard_layer)
        analysis.exposure_layer = exposure_layer
        analysis.exposure_keyword = keywords_io.read_keywords(exposure_layer)
        if aggregation_layer:
            analysis.aggregation_layer = aggregation_layer
            analysis.aggregation_keyword = keywords_io.read_keywords(
                aggregation_layer)
        analysis.impact_function = function

        analysis.setup_analysis()
        print 'Setup analysis done'
        analysis.run_analysis()
        print 'Analysis done'
    except Exception as e:
        print e.message

    impact = analysis.impact_layer
    qgis_impact = safe_to_qgis_layer(impact)

    generate_styles(impact, qgis_impact)

    copy_impact_layer(impact, arg.impact_filename)
Пример #3
0
def analysis_execution():

    from safe.test.utilities import get_qgis_app

    # get_qgis_app must be called before importing Analysis
    QGIS_APP, CANVAS, IFACE, PARENT = get_qgis_app()

    from safe.utilities.analysis import Analysis
    from safe.utilities.keyword_io import KeywordIO

    analysis = Analysis()

    arg = AnalysisArguments.read_arguments()

    register_impact_functions()

    registry = ImpactFunctionManager().registry

    function = registry.get_instance(arg.impact_function_name)

    hazard_layer = safe_to_qgis_layer(read_layer(arg.hazard_filename))
    exposure_layer = safe_to_qgis_layer(read_layer(arg.exposure_filename))
    if arg.aggregation_filename:
        aggregation_layer = safe_to_qgis_layer(read_layer(
            arg.aggregation_filename))

    keywords_io = KeywordIO()

    try:
        analysis.map_canvas = IFACE.mapCanvas()
        analysis.hazard_layer = hazard_layer
        analysis.hazard_keyword = keywords_io.read_keywords(hazard_layer)
        analysis.exposure_layer = exposure_layer
        analysis.exposure_keyword = keywords_io.read_keywords(exposure_layer)
        if aggregation_layer:
            analysis.aggregation_layer = aggregation_layer
            analysis.aggregation_keyword = keywords_io.read_keywords(
                aggregation_layer)
        analysis.impact_function = function

        analysis.setup_analysis()
        print 'Setup analysis done'
        analysis.run_analysis()
        print 'Analysis done'
    except Exception as e:
        print e.message

    impact = analysis.impact_layer
    qgis_impact = safe_to_qgis_layer(impact)

    generate_styles(impact, qgis_impact)

    copy_impact_layer(impact, arg.impact_filename)
Пример #4
0
    def calculate_specified_impact(self, function_id, hazard_layer,
                                   exposure_layer, output_basename):
        LOGGER.info('Calculate %s' % function_id)
        if_manager = ImpactFunctionManager()
        impact_function = if_manager.get_instance(function_id)

        impact_function.hazard = hazard_layer

        extent = impact_function.hazard.extent()
        hazard_extent = [
            extent.xMinimum(),
            extent.yMinimum(),
            extent.xMaximum(),
            extent.yMaximum()
        ]

        # clip exposure if required (if it is too large)
        if isinstance(exposure_layer, QgsRasterLayer):
            cell_size, _ = get_wgs84_resolution(exposure_layer)
        else:
            cell_size = None
        clipped_exposure = clip_layer(layer=exposure_layer,
                                      extent=hazard_extent,
                                      cell_size=cell_size)
        exposure_layer = clipped_exposure

        impact_function.exposure = exposure_layer
        impact_function.requested_extent = hazard_extent
        impact_function.requested_extent_crs = impact_function.hazard.crs()
        impact_function.force_memory = True

        try:
            impact_function.run_analysis()
            impact_layer = impact_function.impact

            if impact_layer:
                self.set_impact_style(impact_layer)

                # copy results of impact to report_path directory
                self.copy_layer(impact_layer, output_basename)
        except ZeroImpactException as e:
            # in case zero impact, just return
            LOGGER.info('No impact detected')
            LOGGER.info(e.message)
            return False
        except Exception as e:
            LOGGER.info('Calculation error')
            LOGGER.exception(e)
            return False
        LOGGER.info('Calculation completed.')
        return True
Пример #5
0
def direct_execution():

    arg = AnalysisArguments.read_arguments()

    register_impact_functions()

    registry = ImpactFunctionManager().registry

    function = registry.get_instance(arg.impact_function_name)
    function.hazard = SafeLayer(read_layer(arg.hazard_filename))
    function.exposure = SafeLayer(read_layer(arg.exposure_filename))

    impact = calculate_impact(function)
    qgis_impact = safe_to_qgis_layer(impact)

    generate_styles(impact, qgis_impact)

    copy_impact_layer(impact, arg.impact_filename)
Пример #6
0
def direct_execution():

    arg = AnalysisArguments.read_arguments()

    register_impact_functions()

    registry = ImpactFunctionManager().registry

    function = registry.get_instance(arg.impact_function_name)
    function.hazard = SafeLayer(read_layer(arg.hazard_filename))
    function.exposure = SafeLayer(read_layer(arg.exposure_filename))

    impact = calculate_impact(function)
    qgis_impact = safe_to_qgis_layer(impact)

    generate_styles(impact, qgis_impact)

    copy_impact_layer(impact, arg.impact_filename)
Пример #7
0
    def calculate_specified_impact(self, function_id, hazard_layer, exposure_layer, output_basename):
        LOGGER.info("Calculate %s" % function_id)
        if_manager = ImpactFunctionManager()
        impact_function = if_manager.get_instance(function_id)

        impact_function.hazard = hazard_layer

        extent = impact_function.hazard.extent()
        hazard_extent = [extent.xMinimum(), extent.yMinimum(), extent.xMaximum(), extent.yMaximum()]

        # clip exposure if required (if it is too large)
        if isinstance(exposure_layer, QgsRasterLayer):
            cell_size, _ = get_wgs84_resolution(exposure_layer)
        else:
            cell_size = None
        clipped_exposure = clip_layer(layer=exposure_layer, extent=hazard_extent, cell_size=cell_size)
        exposure_layer = clipped_exposure

        impact_function.exposure = exposure_layer
        impact_function.requested_extent = hazard_extent
        impact_function.requested_extent_crs = impact_function.hazard.crs()
        impact_function.force_memory = True

        try:
            impact_function.run_analysis()
            impact_layer = impact_function.impact

            if impact_layer:
                self.set_impact_style(impact_layer)

                # copy results of impact to report_path directory
                self.copy_layer(impact_layer, output_basename)
        except ZeroImpactException as e:
            # in case zero impact, just return
            LOGGER.info("No impact detected")
            LOGGER.info(e.message)
            return False
        except Exception as e:
            LOGGER.info("Calculation error")
            LOGGER.exception(e)
            return False
        LOGGER.info("Calculation completed.")
        return True
Пример #8
0
    def calculate_impact(self):
        if_manager = ImpactFunctionManager()
        function_id = self.function_id
        impact_function = if_manager.get_instance(function_id)

        impact_function.hazard = self.hazard_layer.as_qgis_native()
        impact_function.exposure = self.exposure_layer.as_qgis_native()
        extent = impact_function.hazard.extent()
        impact_function.requested_extent = [
            extent.xMinimum(),
            extent.yMinimum(),
            extent.xMaximum(),
            extent.yMaximum()
        ]
        impact_function.requested_extent_crs = impact_function.hazard.crs()
        # impact_function.setup_aggregator()
        # impact_function.aggregator.validate_keywords()

        try:
            # skip process if hazard not contain significant flood
            skip_process = True
            qgis_hazard_layer = self.hazard_layer.as_qgis_native()
            keyword_io = KeywordIO()
            hazard_attribute_key = keyword_io.read_keywords(
                qgis_hazard_layer, 'field')

            # Do not skip if there are significant hazard class (2,3,4)
            for f in qgis_hazard_layer.getFeatures():
                try:
                    # try cast to int
                    hazard_state = f[hazard_attribute_key]
                    hazard_state = int(str(hazard_state))
                    if hazard_state >= 2:
                        skip_process = False
                        break
                except ValueError:
                    # this is expected
                    pass

            if skip_process:
                return

            impact_function.run_analysis()
            self.impact_layer = impact_function.impact
            # impact_function.aggregator.set_layers(
            #     self.hazard_layer.as_qgis_native(),
            #     self.exposure_layer.as_qgis_native())
            self.calculate_aggregate_impact(impact_function)
            # impact_function.run_aggregator()
            # impact_function.run_post_processor()
            # self.generate_aggregation(impact_function)
            self.generate_population_aggregation()
            self.set_style()
        except ZeroImpactException as e:
            # in case zero impact, just return
            LOGGER.info('No impact detected')
            LOGGER.info(e.message)
            return

        # copy results of impact to report_path directory
        base_name, _ = os.path.splitext(self.impact_layer.filename)
        dir_name = os.path.dirname(self.impact_layer.filename)
        for (root, dirs, files) in os.walk(dir_name):
            for f in files:
                source_filename = os.path.join(root, f)
                if source_filename.find(base_name) >= 0:
                    extensions = source_filename.replace(base_name, '')
                    new_path = os.path.join(self.report_path,
                                            'impact' + extensions)
                    shutil.copy(source_filename, new_path)

        self.impact_layer = read_layer(self.impact_path)
Пример #9
0
    def calculate_impact(self):
        if_manager = ImpactFunctionManager()
        function_id = self.function_id
        impact_function = if_manager.get_instance(function_id)

        impact_function.hazard = self.hazard_layer.as_qgis_native()
        impact_function.exposure = self.exposure_layer.as_qgis_native()
        extent = impact_function.hazard.extent()
        impact_function.requested_extent = [
            extent.xMinimum(), extent.yMinimum(),
            extent.xMaximum(), extent.yMaximum()]
        impact_function.requested_extent_crs = impact_function.hazard.crs()
        # impact_function.setup_aggregator()
        # impact_function.aggregator.validate_keywords()

        try:
            # skip process if hazard not contain significant flood
            skip_process = True
            qgis_hazard_layer = self.hazard_layer.as_qgis_native()
            keyword_io = KeywordIO()
            hazard_attribute_key = keyword_io.read_keywords(
                qgis_hazard_layer, 'field')

            # Do not skip if there are significant hazard class (2,3,4)
            for f in qgis_hazard_layer.getFeatures():
                try:
                    # try cast to int
                    hazard_state = f[hazard_attribute_key]
                    hazard_state = int(str(hazard_state))
                    if hazard_state >= 2:
                        skip_process = False
                        break
                except ValueError:
                    # this is expected
                    pass

            if skip_process:
                return

            impact_function.run_analysis()
            self.impact_layer = impact_function.impact
            # impact_function.aggregator.set_layers(
            #     self.hazard_layer.as_qgis_native(),
            #     self.exposure_layer.as_qgis_native())
            self.calculate_aggregate_impact(impact_function)
            # impact_function.run_aggregator()
            # impact_function.run_post_processor()
            # self.generate_aggregation(impact_function)
            self.generate_population_aggregation()
            self.set_style()
        except ZeroImpactException as e:
            # in case zero impact, just return
            LOGGER.info('No impact detected')
            LOGGER.info(e.message)
            return

        # copy results of impact to report_path directory
        base_name, _ = os.path.splitext(self.impact_layer.filename)
        dir_name = os.path.dirname(self.impact_layer.filename)
        for (root, dirs, files) in os.walk(dir_name):
            for f in files:
                source_filename = os.path.join(root, f)
                if source_filename.find(base_name) >= 0:
                    extensions = source_filename.replace(base_name, '')
                    new_path = os.path.join(
                        self.report_path, 'impact' + extensions)
                    shutil.copy(source_filename, new_path)

        self.impact_layer = read_layer(self.impact_path)