示例#1
0
    def _get_layer(self, layer):
        """Analyze style of self._function and return appropriate
            class of the layer.

        :param layer: A layer.
        :type layer:  QgsMapLayer or SAFE layer.

        :returns:   The layer of appropriate type
        :rtype:     SAFE or QgisWrapper

        :raises: InsufficientParametersError if self._function is not set,
                 InvalidParameterError if style of self._function is not
                     in ('old-style', 'qgis2.0')
                 Any exceptions raised by other libraries will be propogated.
        """

        if self._function is None or self._function == '':
            message = self.tr('Error: Function not set.')
            raise InsufficientParametersError(message)

        # Get type of the impact function (old-style or new-style)
        try:
            func_type = get_safe_impact_function_type(self._function)
            if func_type == 'old-style':
                return convert_to_safe_layer(layer)
            elif func_type == 'qgis2.0':
                # convert for new style impact function
                return QgisWrapper(layer)
            else:
                message = self.tr('Error: Function has unknown style.')
                raise InvalidParameterError(message)
        except:
            raise
示例#2
0
    def requires_clipping(self):
        """Check to clip or not to clip layers.

        If self._function is a 'new-style' impact function, then
        return False -- clipping is unnecessary, else return True

        :returns:   To clip or not to clip.
        :rtype:     bool

        :raises: InsufficientParametersError if function parameter is not set.
                 InvalidParameterError if the function has unknown style.
        """
        f = self.function()
        if f is None:
            message = self.tr('Error: Function is not provided.')
            raise InsufficientParametersError(message)

        style = get_safe_impact_function_type(f)
        if style == 'old-style':
            return True
        elif style == 'qgis2.0':
            return False
        else:
            message = self.tr('Error: Function has unknown style.')
            raise InvalidParameterError(message)
    def run(self):
        """ Main function for hazard impact calculation thread.

        Requires three properties to be set before execution
        can take place:

        * Hazard layer - a path to a raster,
        * Exposure layer - a path to a vector points layer.
        * Function - a function that defines how the Hazard assessment
          will be computed.

        After the thread is complete, you can use the filename and
        result accessors to determine what the result of the analysis was::

          calculator = ImpactCalculator()
          rasterPath = os.path.join(TESTDATA, 'xxx.asc')
          vector_path = os.path.join(TESTDATA, 'xxx.shp')
          calculator.setHazardLayer(self.rasterPath)
          calculator.setExposureLayer(self.vector_path)
          calculator.setFunction('Flood Building Impact Function')
          myRunner = calculator.getRunner()
          # wait till completion
          myRunner.join()
          myResult = myRunner.result()
          filename = myRunner.filename()


        :raises: InsufficientParametersError

        .. note:: a done signal is emitted when the analysis is complete.
        """
        if (self._hazardLayer is None) or \
                (self._exposureLayer is None) or \
                (self._function is None):
            message = self.tr(
                'Ensure that hazard, exposure and function are all set before '
                'trying to run the analysis.')
            raise InsufficientParametersError(message)
        try:
            layers = [self._hazardLayer, self._exposureLayer]
            self._impactLayer = calculate_safe_impact(
                layers=layers,
                impact_fcn=self._function,
                extent=self._extent,
                check_integrity=self._check_integrity)
        except MemoryError, e:
            message = self.tr(
                'An error occurred because it appears that your system does '
                'not have sufficient memory. Upgrading your computer so that '
                'it has more memory may help. Alternatively, consider using a '
                'smaller geographical area for your analysis, or using '
                'rasters with a larger cell size.')
            self._exception = e
            self._traceback = traceback.format_tb(sys.exc_info()[2])
            self._result = message
            LOGGER.exception(message)
    def get_runner(self):
        """ Factory to create a new runner thread.

        Requires three parameters to be set before execution can take place:

        * Hazard layer - a path to a raster (string)
        * Exposure layer - a path to a vector hazard layer (string).
        * Function - a function name that defines how the Hazard assessment
          will be computed (string).

        :returns: An impact calculator thread instance.
        :rtype: ImpactCalculatorThread

        :raises: InsufficientParametersError if not all parameters are set.
        """
        self._filename = None
        self._result = None
        if self._hazardLayer is None:
            message = self.tr('Error: Hazard layer not set.')
            raise InsufficientParametersError(message)

        if self._exposureLayer is None:
            message = self.tr('Error: Exposure layer not set.')
            raise InsufficientParametersError(message)

        if self._function is None or self._function == '':
            message = self.tr('Error: Function not set.')
            raise InsufficientParametersError(message)

        # Call impact calculation engine
        hazard_layer = self.hazard_layer()
        exposure_layer = self.exposure_layer()

        functions = get_safe_impact_function(self._function)
        function = functions[0][self._function]
        return ImpactCalculatorThread(
            hazard_layer,
            exposure_layer,
            function,
            extent=self.extent(),
            check_integrity=self.requires_clipping())