Exemple #1
0
    def test_resultStyling(self):
        """Test that ouputs from a model are correctly styled (colours and
        opacity. """

        # Push OK with the left mouse button

        print '--------------------'
        print combos_to_string(DOCK)

        result, message = setup_scenario(
            DOCK,
            hazard='A flood in Jakarta like in 2007',
            exposure='People',
            function='Need evacuation',
            function_id='Flood Evacuation Function')
        self.assertTrue(result, message)

        # Enable on-the-fly reprojection
        set_canvas_crs(GEOCRS, True)
        set_jakarta_extent()

        # Run manually so we can get the output layer
        DOCK.clip_parameters = DOCK.get_clip_parameters()
        DOCK.prepare_aggregator()
        DOCK.aggregator.validate_keywords()
        DOCK.setup_calculator()
        test_runner = DOCK.calculator.get_runner()
        test_runner.run()  # Run in same thread
        safe_layer = test_runner.impact_layer()
        qgis_layer = read_impact_layer(safe_layer)
        style = safe_layer.get_style_info()
        setRasterStyle(qgis_layer, style)
        # simple test for now - we could test explicity for style state
        # later if needed.
        message = (
            'Raster layer was not assigned a Singleband pseudocolor'
            ' renderer as expected.')
        self.assertTrue(
            qgis_layer.renderer().type() == 'singlebandpseudocolor', message)
Exemple #2
0
    def test_resultStyling(self):
        """Test that ouputs from a model are correctly styled (colours and
        opacity. """

        # Push OK with the left mouse button

        print '--------------------'
        print combos_to_string(DOCK)

        myResult, myMessage = setup_scenario(
            DOCK,
            hazard='A flood in Jakarta like in 2007',
            exposure='People',
            function='Need evacuation',
            function_id='Flood Evacuation Function')
        assert myResult, myMessage

        # Enable on-the-fly reprojection
        set_canvas_crs(GEOCRS, True)
        set_jakarta_extent()

        # Run manually so we can get the output layer
        DOCK.prepare_aggregator()
        DOCK.aggregator.validate_keywords()
        DOCK.setup_calculator()
        myRunner = DOCK.calculator.get_runner()
        myRunner.run()  # Run in same thread
        myEngineImpactLayer = myRunner.impact_layer()
        myQgisImpactLayer = read_impact_layer(myEngineImpactLayer)
        myStyle = myEngineImpactLayer.get_style_info()
        #print myStyle
        setRasterStyle(myQgisImpactLayer, myStyle)
        # simple test for now - we could test explicity for style state
        # later if needed.
        myMessage = ('Raster layer was not assigned a ColorRampShader'
                     ' as expected.')
        assert myQgisImpactLayer.colorShadingAlgorithm() == QgsRasterLayer. \
            ColorRampShader, myMessage
Exemple #3
0
    def test_resultStyling(self):
        """Test that ouputs from a model are correctly styled (colours and
        opacity. """

        # Push OK with the left mouse button

        print '--------------------'
        print combos_to_string(DOCK)

        myResult, myMessage = setup_scenario(
            DOCK,
            hazard='A flood in Jakarta like in 2007',
            exposure='People',
            function='Need evacuation',
            function_id='Flood Evacuation Function')
        assert myResult, myMessage

        # Enable on-the-fly reprojection
        set_canvas_crs(GEOCRS, True)
        set_jakarta_extent()

        # Run manually so we can get the output layer
        DOCK.prepare_aggregator()
        DOCK.aggregator.validate_keywords()
        DOCK.setup_calculator()
        myRunner = DOCK.calculator.get_runner()
        myRunner.run()  # Run in same thread
        myEngineImpactLayer = myRunner.impact_layer()
        myQgisImpactLayer = read_impact_layer(myEngineImpactLayer)
        myStyle = myEngineImpactLayer.get_style_info()
        #print myStyle
        setRasterStyle(myQgisImpactLayer, myStyle)
        # simple test for now - we could test explicity for style state
        # later if needed.
        myMessage = ('Raster layer was not assigned a ColorRampShader'
                     ' as expected.')
        assert myQgisImpactLayer.colorShadingAlgorithm() == QgsRasterLayer. \
            ColorRampShader, myMessage
Exemple #4
0
    def run_task(self, task_item, status_item, count=0, index=''):
        """Run a single task.

        :param task_item: Table task_item containing task name / details.
        :type task_item: QTableWidgetItem

        :param status_item: Table task_item that holds the task status.
        :type status_item: QTableWidgetItem

        :param count: Count of scenarios that have been run already.
        :type count:

        :param index: The index for the table item that will be run.
        :type index: int

        :returns: Flag indicating if the task succeeded or not.
        :rtype: bool
        """

        self.enable_busy_cursor()
        # set status to 'running'
        status_item.setText(self.tr('Running'))

        # .. see also:: :func:`appendRow` to understand the next 2 lines
        variant = task_item.data(QtCore.Qt.UserRole)
        value = variant[0]

        result = True

        if isinstance(value, str):
            filename = value
            # run script
            try:
                self.run_script(filename)
                # set status to 'OK'
                status_item.setText(self.tr('Script OK'))
            except Exception as e:  # pylint: disable=W0703
                # set status to 'fail'
                status_item.setText(self.tr('Script Fail'))

                LOGGER.exception('Running macro failed. The exception: ' +
                                 str(e))
                result = False
        elif isinstance(value, dict):
            path = str(self.output_directory.text())
            title = str(task_item.text())

            # Its a dict containing files for a scenario
            result = self.run_scenario(value)
            if not result:
                status_item.setText(self.tr('Analysis Fail'))
            else:
                # NOTE(gigih):
                # Usually after analysis is done, the impact layer
                # become the active layer. <--- WRONG
                # noinspection PyUnresolvedReferences
                impact_layer = self.dock.runner.impact_layer()
                # Load impact layer into QGIS
                qgis_layer = read_impact_layer(impact_layer)

                # noinspection PyBroadException
                try:
                    status_item.setText(self.tr('Analysis Ok'))
                    self.create_pdf(
                        title, path, qgis_layer, count, index)
                    status_item.setText(self.tr('Report Ok'))
                except Exception:  # pylint: disable=W0703
                    LOGGER.exception('Unable to render map: "%s"' % value)
                    status_item.setText(self.tr('Report Failed'))
                    result = False
        else:
            LOGGER.exception('Data type not supported: "%s"' % value)
            result = False

        self.disable_busy_cursor()
        return result
Exemple #5
0
    def run_task(self, task_item, status_item, count=0, index=''):
        """Run a single task.

        :param task_item: Table task_item containing task name / details.
        :type task_item: QTableWidgetItem

        :param status_item: Table task_item that holds the task status.
        :type status_item: QTableWidgetItem

        :param count: Count of scenarios that have been run already.
        :type count:

        :param index: The index for the table item that will be run.
        :type index: int

        :returns: Flag indicating if the task succeeded or not.
        :rtype: bool
        """
        self.enable_busy_cursor()
        # set status to 'running'
        status_item.setText(self.tr('Running'))
        # .. see also:: :func:`appendRow` to understand the next 2 lines
        variant = task_item.data(QtCore.Qt.UserRole)
        value = variant[0]

        result = True

        if isinstance(value, str):
            filename = value
            # run script
            try:
                self.run_script(filename)
                # set status to 'OK'
                status_item.setText(self.tr('Script OK'))
            except Exception as e:  # pylint: disable=W0703
                # set status to 'fail'
                status_item.setText(self.tr('Script Fail'))

                LOGGER.exception('Running macro failed. The exception: ' +
                                 str(e))
                result = False
        elif isinstance(value, dict):
            path = str(self.output_directory.text())
            title = str(task_item.text())

            # Its a dict containing files for a scenario
            result = self.run_scenario(value)
            if not result:
                status_item.setText(self.tr('Analysis Fail'))
            else:
                # NOTE(gigih):
                # Usually after analysis is done, the impact layer
                # become the active layer. <--- WRONG
                # noinspection PyUnresolvedReferences
                impact_layer = self.dock.runner.impact_layer()
                # Load impact layer into QGIS
                qgis_layer = read_impact_layer(impact_layer)
                QgsMapLayerRegistry.instance().addMapLayer(qgis_layer,
                                                           addToLegend=False)
                # noinspection PyBroadException
                try:
                    status_item.setText(self.tr('Analysis Ok'))
                    self.create_pdf(title, path, qgis_layer, count, index)
                    status_item.setText(self.tr('Report Ok'))
                except Exception:  # pylint: disable=W0703
                    LOGGER.exception('Unable to render map: "%s"' % value)
                    status_item.setText(self.tr('Report Failed'))
                    result = False
        else:
            LOGGER.exception('Data type not supported: "%s"' % value)
            result = False

        self.disable_busy_cursor()
        return result
Exemple #6
0
    def run_task(self, task_item, status_item, count=0, index=''):
        """Run a single task.

        :param task_item: Table task_item containing task name / details.
        :type task_item: QTableWidgetItem

        :param status_item: Table task_item that holds the task status.
        :type status_item: QTableWidgetItem

        :param count: Count of scenarios that have been run already.
        :type count:

        :param index: The index for the table item that will be run.
        :type index: int

        :returns: Flag indicating if the task succeeded or not.
        :rtype: bool
        """

        self.enable_busy_cursor()
        # set status to 'running'
        status_item.setText(self.tr('Running'))

        # .. see also:: :func:`appendRow` to understand the next 2 lines
        myVariant = task_item.data(QtCore.Qt.UserRole)
        myValue = myVariant.toPyObject()[0]

        myResult = True

        if isinstance(myValue, str):
            myFilename = myValue
            # run script
            try:
                self.run_script(myFilename)
                # set status to 'OK'
                status_item.setText(self.tr('Script OK'))
            except Exception as e:  # pylint: disable=W0703
                # set status to 'fail'
                status_item.setText(self.tr('Script Fail'))

                LOGGER.exception('Running macro failed. The exception: ' +
                                 str(e))
                myResult = False
        elif isinstance(myValue, dict):
            myPath = str(self.leOutputDir.text())
            myTitle = str(task_item.text())

            # Its a dict containing files for a scenario
            myResult = self.run_scenario(myValue)
            if not myResult:
                status_item.setText(self.tr('Analysis Fail'))
            else:
                # NOTE(gigih):
                # Usually after analysis is done, the impact layer
                # become the active layer. <--- WRONG
                myImpactLayer = self.dock.runner.impact_layer()
                # Load impact layer into QGIS
                myQGISImpactLayer = read_impact_layer(myImpactLayer)

                # noinspection PyBroadException
                try:
                    status_item.setText(self.tr('Analysis Ok'))
                    self.create_pdf(myTitle, myPath, myQGISImpactLayer, count,
                                    index)
                    status_item.setText(self.tr('Report Ok'))
                except Exception:  # pylint: disable=W0703
                    LOGGER.exception('Unable to render map: "%s"' % myValue)
                    status_item.setText(self.tr('Report Failed'))
                    myResult = False
        else:
            LOGGER.exception('Data type not supported: "%s"' % myValue)
            myResult = False

        self.disable_busy_cursor()
        return myResult
Exemple #7
0
    def runTask(self, theItem, theStatusItem, theCount=0, theIndex=''):
        """Run a single task
        :param theItem:
        :param theStatusItem:
        :param theCount: integer represent count number of scenario has been
        run
        :param theIndex: integer for representing an index when run all
        scenarios
        """

        self.enableBusyCursor()
        # set status to 'running'
        theStatusItem.setText(self.tr('Running'))

        # .. see also:: :func:`appendRow` to understand the next 2 lines
        myVariant = theItem.data(QtCore.Qt.UserRole)
        myValue = myVariant.toPyObject()[0]

        myResult = True

        if isinstance(myValue, str):
            myFilename = myValue
            # run script
            try:
                self.runScriptTask(myFilename)
                # set status to 'OK'
                theStatusItem.setText(self.tr('Script OK'))
            except Exception as e:  # pylint: disable=W0703
                # set status to 'fail'
                theStatusItem.setText(self.tr('Script Fail'))

                LOGGER.exception('Running macro failed. The exception: ' +
                                 str(e))
                myResult = False
        elif isinstance(myValue, dict):
            myPath = str(self.leOutputDir.text())
            myTitle = str(theItem.text())

            # Its a dict containing files for a scenario
            myResult = self.runSimpleTask(myValue)
            if not myResult:
                theStatusItem.setText(self.tr('Analysis Fail'))
            else:
                # NOTE(gigih):
                # Usually after analysis is done, the impact layer
                # become the active layer. <--- WRONG
                myImpactLayer = self.dock.runner.impact_layer()
                # Load impact layer into QGIS
                myQGISImpactLayer = read_impact_layer(myImpactLayer)

                # noinspection PyBroadException
                try:
                    theStatusItem.setText(self.tr('Analysis Ok'))
                    self.createPDFReport(
                        myTitle, myPath, myQGISImpactLayer, theCount, theIndex)
                    theStatusItem.setText(self.tr('Report Ok'))
                except Exception:  # pylint: disable=W0703
                    LOGGER.exception('Unable to render map: "%s"' % myValue)
                    theStatusItem.setText(self.tr('Report Failed'))
                    myResult = False
        else:
            LOGGER.exception('Data type not supported: "%s"' % myValue)
            myResult = False

        self.disableBusyCursor()
        return myResult