def minimum_needs(self, input_layer): """Compute minimum needs given a layer and a column containing pop. :param input_layer: Vector layer assumed to contain population counts. :type input_layer: QgsVectorLayer :returns: A tuple containing True and the vector layer if post processor success. Or False and an error message if something went wrong. :rtype: tuple(bool,QgsVectorLayer or basetring) """ # Create a new layer for output layer output_layer = self.prepare_new_layer(input_layer) # count each minimum needs for every features for needs in minimum_needs_post_processors: is_success, message = run_single_post_processor( output_layer, needs) # check if post processor not running successfully if not is_success: LOGGER.debug(message) display_critical_message_box( title=self.tr('Error while running post processor'), message=message) return False, None return True, output_layer
def minimum_needs(self, input_layer): """Compute minimum needs given a layer and a column containing pop. :param input_layer: Vector layer assumed to contain population counts. :type input_layer: QgsVectorLayer :returns: A tuple containing True and the vector layer if post processor success. Or False and an error message if something went wrong. :rtype: tuple(bool,QgsVectorLayer or basetring) """ # Create a new layer for output layer output_layer = self.prepare_new_layer(input_layer) # count each minimum needs for every features for needs in minimum_needs_post_processors: is_success, message = run_single_post_processor( output_layer, needs) # check if post processor not running successfully if not is_success: LOGGER.debug(message) display_critical_message_box( title=self.tr('Error while running post processor'), message=message) return False, None return True, output_layer
def accept(self): """Process the layer and field and generate a new layer. .. note:: This is called on OK click. """ # run minimum needs calculator try: success, self.result_layer = ( self.minimum_needs(self.layer.currentLayer())) if not success: return except Exception as e: error_name, traceback = humanise_exception(e) message = ( 'Problem(s) occured. \n%s \nDiagnosis: \n%s' % ( error_name, traceback)) display_critical_message_box( title=self.tr('Error while calculating minimum needs'), message=message) return # remove monkey patching keywords del self.result_layer.keywords # write memory layer to file system settings = QSettings() default_user_directory = settings.value( 'inasafe/defaultUserDirectory', defaultValue='') if default_user_directory: output_directory = os.path.join( default_user_directory, 'minimum_needs_calculator') if not os.path.exists(output_directory): os.makedirs(output_directory) else: output_directory = temp_dir(sub_dir='minimum_needs_calculator') output_layer_name = os.path.split(self.result_layer.name())[1] # If normal filename doesn't exist, then use normal filename random_string_length = len(output_layer_name.split('_')[-1]) normal_filename = output_layer_name[:-(random_string_length + 1)] if not os.path.exists(os.path.join(output_directory, normal_filename)): output_layer_name = normal_filename data_store = Folder(output_directory) data_store.default_vector_format = 'geojson' data_store.add_layer(self.result_layer, output_layer_name) self.result_layer = data_store.layer(output_layer_name) # noinspection PyArgumentList QgsMapLayerRegistry.instance().addMapLayers( [data_store.layer(self.result_layer.name())]) self.done(QtGui.QDialog.Accepted)
def accept(self): """Process the layer and field and generate a new layer. .. note:: This is called on OK click. """ # run minimum needs calculator try: success, self.result_layer = ( self.minimum_needs(self.layer.currentLayer())) if not success: return except Exception as e: error_name, traceback = humanise_exception(e) message = ( 'Problem(s) occured. \n%s \nDiagnosis: \n%s' % ( error_name, traceback)) display_critical_message_box( title=self.tr('Error while calculating minimum needs'), message=message) return # remove monkey patching keywords del self.result_layer.keywords # write memory layer to file system settings = QSettings() default_user_directory = settings.value( 'inasafe/defaultUserDirectory', defaultValue='') if default_user_directory: output_directory = os.path.join( default_user_directory, 'minimum_needs_calculator') if not os.path.exists(output_directory): os.makedirs(output_directory) else: output_directory = temp_dir(sub_dir='minimum_needs_calculator') output_layer_name = os.path.split(self.result_layer.name())[1] # If normal filename doesn't exist, then use normal filename random_string_length = len(output_layer_name.split('_')[-1]) normal_filename = output_layer_name[:-(random_string_length + 1)] if not os.path.exists(os.path.join(output_directory, normal_filename)): output_layer_name = normal_filename data_store = Folder(output_directory) data_store.default_vector_format = 'geojson' data_store.add_layer(self.result_layer, output_layer_name) self.result_layer = data_store.layer(output_layer_name) # noinspection PyArgumentList QgsProject.instance().addMapLayers( [data_store.layer(self.result_layer.name())]) self.done(QtWidgets.QDialog.Accepted)
def accept(self): """Process the layer and field and generate a new layer. .. note:: This is called on OK click. """ # run minimum needs calculator try: success, self.result_layer = ( self.minimum_needs(self.layer.currentLayer())) if not success: return except Exception as e: error_name, traceback = humanise_exception(e) message = ( 'Problem(s) occured. \n%s \nDiagnosis: \n%s' % ( error_name, traceback)) display_critical_message_box( title=self.tr('Error while calculating minimum needs'), message=message) return # remove monkey patching keywords del self.result_layer.keywords # write memory layer to file system settings = QSettings() default_user_directory = settings.value( 'inasafe/defaultUserDirectory', defaultValue='') if default_user_directory: path = os.path.join( default_user_directory, self.result_layer.name()) if not os.path.exists(path): os.makedirs(path) data_store = Folder(path) else: data_store = Folder(temp_dir(sub_dir=self.result_layer.name())) data_store.default_vector_format = 'geojson' data_store.add_layer(self.result_layer, self.result_layer.name()) self.result_layer = data_store.layer(self.result_layer.name()) # noinspection PyArgumentList QgsMapLayerRegistry.instance().addMapLayers( [data_store.layer(self.result_layer.name())]) self.done(QtGui.QDialog.Accepted)
def prepare_task(self, items): """Prepare scenario for impact function variable. :param items: Dictionary containing settings for impact function. :type items: Python dictionary. :return: A tuple containing True and dictionary containing parameters if post processor success. Or False and an error message if something went wrong. """ status = True message = '' # get hazard if 'hazard' in items: hazard_path = items['hazard'] hazard = self.define_layer(hazard_path) if not hazard: status = False message = self.tr( 'Unable to find {hazard_path}').format( hazard_path=hazard_path) else: hazard = None LOGGER.warning('Scenario does not contain hazard path') # get exposure if 'exposure' in items: exposure_path = items['exposure'] exposure = self.define_layer(exposure_path) if not exposure: status = False if message: message += '\n' message += self.tr( 'Unable to find {exposure_path}').format( exposure_path=exposure_path) else: exposure = None LOGGER.warning('Scenario does not contain hazard path') # get aggregation if 'aggregation' in items: aggregation_path = items['aggregation'] aggregation = self.define_layer(aggregation_path) else: aggregation = None LOGGER.info('Scenario does not contain aggregation path') # get extent if 'extent' in items: LOGGER.info('Extent coordinate is found') coordinates = items['extent'] array_coord = extent_string_to_array(coordinates) extent = QgsRectangle(*array_coord) else: extent = None LOGGER.info('Scenario does not contain extent coordinates') # get extent crs id if 'extent_crs' in items: LOGGER.info('Extent CRS is found') crs = items['extent_crs'] extent_crs = QgsCoordinateReferenceSystem(crs) else: LOGGER.info('Extent crs is not found, assuming crs to EPSG:4326') extent_crs = QgsCoordinateReferenceSystem('EPSG:4326') # make sure at least hazard and exposure data are available in # scenario. Aggregation and extent checking will be done when # assigning layer to impact_function if status: parameters = { layer_purpose_hazard['key']: hazard, layer_purpose_exposure['key']: exposure, layer_purpose_aggregation['key']: aggregation, 'extent': extent, 'crs': extent_crs } return True, parameters else: LOGGER.warning(message) display_critical_message_box( title=self.tr('Error while preparing scenario'), message=message) return False, None
def prepare_task(self, items): """Prepare scenario for impact function variable. :param items: Dictionary containing settings for impact function. :type items: Python dictionary. :return: A tuple containing True and dictionary containing parameters if post processor success. Or False and an error message if something went wrong. """ status = True message = '' # get hazard if 'hazard' in items: hazard_path = items['hazard'] hazard = self.define_layer(hazard_path) if not hazard: status = False message = self.tr( 'Unable to find {hazard_path}').format( hazard_path=hazard_path) else: hazard = None LOGGER.warning('Scenario does not contain hazard path') # get exposure if 'exposure' in items: exposure_path = items['exposure'] exposure = self.define_layer(exposure_path) if not exposure: status = False if message: message += '\n' message += self.tr( 'Unable to find {exposure_path}').format( exposure_path=exposure_path) else: exposure = None LOGGER.warning('Scenario does not contain hazard path') # get aggregation if 'aggregation' in items: aggregation_path = items['aggregation'] aggregation = self.define_layer(aggregation_path) else: aggregation = None LOGGER.info('Scenario does not contain aggregation path') # get extent if 'extent' in items: LOGGER.info('Extent coordinate is found') coordinates = items['extent'] array_coord = extent_string_to_array(coordinates) extent = QgsRectangle(*array_coord) else: extent = None LOGGER.info('Scenario does not contain extent coordinates') # get extent crs id if 'extent_crs' in items: LOGGER.info('Extent CRS is found') crs = items['extent_crs'] extent_crs = QgsCoordinateReferenceSystem(crs) else: LOGGER.info('Extent crs is not found, assuming crs to EPSG:4326') extent_crs = QgsCoordinateReferenceSystem('EPSG:4326') # make sure at least hazard and exposure data are available in # scenario. Aggregation and extent checking will be done when # assigning layer to impact_function if status: parameters = { layer_purpose_hazard['key']: hazard, layer_purpose_exposure['key']: exposure, layer_purpose_aggregation['key']: aggregation, 'extent': extent, 'crs': extent_crs } return True, parameters else: LOGGER.warning(message) display_critical_message_box( title=self.tr('Error while preparing scenario'), message=message) return False, None