def inasafe_place_value_coefficient(number, feature, parent): """Given a number, it will return the coefficient of the place value name. For instance: * inasafe_place_value_coefficient(10) -> 1 * inasafe_place_value_coefficient(1700) -> 1.7 It needs to be used with inasafe_number_denomination_unit. """ _ = feature, parent # NOQA if number >= 0: rounded_number = round_affected_number( number, use_rounding=True, use_population_rounding=True ) min_number = 1000 value, unit = denomination(rounded_number, min_number) if number < min_number: rounded_number = int(round(value, 1)) else: rounded_number = round(value, 1) return str(rounded_number) else: return None
def inasafe_place_value_name(number, feature, parent): """Given a number, it will return the place value name. For instance: * inasafe_place_value_name(10) -> Ten \n * inasafe_place_value_name(1700) -> Thousand It needs to be used with inasafe_place_value_coefficient. """ _ = feature, parent # NOQA if number is None: return None rounded_number = round_affected_number(number, use_rounding=True, use_population_rounding=True) value, unit = denomination(rounded_number, 1000) if not unit: return None else: return unit['name']
def inasafe_place_value_name(number, feature, parent): """Given a number, it will return the place value name. For instance: * inasafe_place_value_name(10) -> Ten \n * inasafe_place_value_name(1700) -> Thousand It needs to be used with inasafe_place_value_coefficient. """ _ = feature, parent # NOQA if number is None: return None rounded_number = round_affected_number( number, use_rounding=True, use_population_rounding=True ) value, unit = denomination(rounded_number, 1000) if not unit: return None else: return unit['name']
def population_chart_extractor(impact_report, component_metadata): """Creating population donut chart. :param impact_report: the impact report that acts as a proxy to fetch all the data that extractor needed :type impact_report: safe.report.impact_report.ImpactReport :param component_metadata: the component metadata. Used to obtain information about the component we want to render :type component_metadata: safe.report.report_metadata. ReportComponentsMetadata :return: context for rendering phase :rtype: dict .. versionadded:: 4.0 """ context = {} extra_args = component_metadata.extra_args hazard_layer = impact_report.hazard analysis_layer = impact_report.analysis analysis_layer_fields = analysis_layer.keywords['inasafe_fields'] """Generate Donut chart for affected population""" # create context for the donut chart # retrieve hazard classification from hazard layer hazard_classification = layer_hazard_classification(hazard_layer) if not hazard_classification: return context data = [] labels = [] colors = [] for hazard_class in hazard_classification['classes']: # Skip if it is not affected hazard class if not hazard_class['affected']: continue # hazard_count_field is a dynamic field with hazard class # as parameter field_key_name = hazard_count_field['key'] % ( hazard_class['key'],) try: # retrieve dynamic field name from analysis_fields keywords # will cause key error if no hazard count for that particular # class field_name = analysis_layer_fields[field_key_name] # Hazard label taken from translated hazard count field # label, string-formatted with translated hazard class label hazard_value = value_from_field_name(field_name, analysis_layer) hazard_value = round_affected_number( hazard_value, enable_rounding=True, use_population_rounding=True) except KeyError: # in case the field was not found continue data.append(hazard_value) labels.append(hazard_class['name']) colors.append(hazard_class['color'].name()) # add total not affected try: field_name = analysis_layer_fields[total_not_affected_field['key']] hazard_value = value_from_field_name(field_name, analysis_layer) hazard_value = round_affected_number( hazard_value, enable_rounding=True, use_population_rounding=True) data.append(hazard_value) labels.append(total_not_affected_field['name']) colors.append(green.name()) except KeyError: # in case the field is not there pass # add number for total not affected chart_title = resolve_from_dictionary(extra_args, 'chart_title') total_header = resolve_from_dictionary(extra_args, 'total_header') donut_context = DonutChartContext( data=data, labels=labels, colors=colors, inner_radius_ratio=0.5, stroke_color='#fff', title=chart_title, total_header=total_header, as_file=True) context['context'] = donut_context return context
def population_chart_extractor(impact_report, component_metadata): """Creating population donut chart. :param impact_report: the impact report that acts as a proxy to fetch all the data that extractor needed :type impact_report: safe.report.impact_report.ImpactReport :param component_metadata: the component metadata. Used to obtain information about the component we want to render :type component_metadata: safe.report.report_metadata. ReportComponentsMetadata :return: context for rendering phase :rtype: dict .. versionadded:: 4.0 """ context = {} extra_args = component_metadata.extra_args analysis_layer = impact_report.analysis analysis_layer_fields = analysis_layer.keywords['inasafe_fields'] provenance = impact_report.impact_function.provenance hazard_keywords = provenance['hazard_keywords'] exposure_keywords = provenance['exposure_keywords'] """Generate Donut chart for affected population.""" # create context for the donut chart # retrieve hazard classification from hazard layer hazard_classification = definition( active_classification(hazard_keywords, exposure_keywords['exposure'])) if not hazard_classification: return context data = [] labels = [] colors = [] for hazard_class in hazard_classification['classes']: # Skip if it is not affected hazard class if not hazard_class['affected']: continue # hazard_count_field is a dynamic field with hazard class # as parameter field_key_name = hazard_count_field['key'] % (hazard_class['key'], ) try: # retrieve dynamic field name from analysis_fields keywords # will cause key error if no hazard count for that particular # class field_name = analysis_layer_fields[field_key_name] # Hazard label taken from translated hazard count field # label, string-formatted with translated hazard class label hazard_value = value_from_field_name(field_name, analysis_layer) hazard_value = round_affected_number(hazard_value, use_rounding=True, use_population_rounding=True) except KeyError: # in case the field was not found continue data.append(hazard_value) labels.append(hazard_class['name']) colors.append(hazard_class['color'].name()) # add total not affected try: field_name = analysis_layer_fields[total_not_affected_field['key']] hazard_value = value_from_field_name(field_name, analysis_layer) hazard_value = round_affected_number(hazard_value, use_rounding=True, use_population_rounding=True) data.append(hazard_value) labels.append(total_not_affected_field['name']) colors.append(green.name()) except KeyError: # in case the field is not there pass # add number for total not affected chart_title = resolve_from_dictionary(extra_args, 'chart_title') total_header = resolve_from_dictionary(extra_args, 'total_header') donut_context = DonutChartContext(data=data, labels=labels, colors=colors, inner_radius_ratio=0.5, stroke_color='#fff', title=chart_title, total_header=total_header, as_file=True) context['context'] = donut_context return context