def test_row_span(self): """Testing row spanning""" table_cell_aa = TableCell('aa spanned', row_span=2) table_row1 = TableRow([ table_cell_aa, self.table_cell_b, self.table_cell_c, self.table_cell_d ]) table_row2 = TableRow( [self.table_cell_b, self.table_cell_c, self.table_cell_d]) self.html += ' <h2>Spanning Table Columns</h2>\n' body = (' <tbody>\n' ' <tr>\n' ' <td rowspan="2">aa spanned</td>\n' ' <td>b</td>\n' ' <td>c</td>\n' ' <td>d</td>\n' ' </tr>\n' ' <tr>\n' ' <td>b</td>\n' ' <td>c</td>\n' ' <td>d</td>\n' ' </tr>\n' ' </tbody>\n') expected_result = ('%s%s%s' % (self.html_table_start, body, self.html_table_end)) actual_result = Table([table_row1, table_row2]) message = 'Expected: %s\n\nGot: %s' % (expected_result, actual_result) assert expected_result.strip() == str(actual_result).strip(), message self.html += str(actual_result) self.writeHtml('table_rowspanning')
def _tabulate_notes(self, minimum_needs, table_body, total, total_impact, no_data_warning): # Extend impact report for on-screen display table_body.extend([ TableRow(tr('Notes'), header=True), tr('Map shows population count in high, medium, and low hazard ' 'area.'), tr('Total population: %s') % format_int(total), TableRow( tr('Table below shows the minimum needs for all ' 'affected people')) ]) if no_data_warning: table_body.extend([ tr('The layers contained `no data`. This missing data was ' 'carried through to the impact layer.'), tr('`No data` values in the impact layer were treated as 0 ' 'when counting the affected or total population.') ]) total_needs = evacuated_population_needs(total_impact, minimum_needs) for frequency, needs in total_needs.items(): table_body.append( TableRow([ tr('Needs should be provided %s' % frequency), tr('Total') ], header=True)) for resource in needs: table_body.append( TableRow([ tr(resource['table name']), format_int(resource['amount']) ])) return table_body, total_needs
def _tabulate_action_checklist(self, table_body, total, nan_warning): # Action Checklist table_body.append(TableRow(tr('Action Checklist:'), header=True)) table_body.append(TableRow(tr('How will warnings be disseminated?'))) table_body.append(TableRow(tr('How will we reach stranded people?'))) table_body.append(TableRow(tr('Do we have enough relief items?'))) table_body.append( TableRow( 'If yes, where are they located and how will we distribute ' 'them?')) table_body.append( TableRow( 'If no, where can we obtain additional relief items from and ' 'how will we transport them to here?')) # Notes table_body.append(TableRow(tr('Notes'), header=True)) table_body.append( TableRow(tr('Total population: %s') % format_int(total))) table_body.append(TableRow(self.parameters['provenance'])) if nan_warning: table_body.extend([ tr('The population layer contained `no data`. This missing ' 'data was carried through to the impact layer.'), tr('`No data` values in the impact layer were treated as 0 ' 'when counting the affected or total population.') ])
def get_plugins_as_table(name=None): """Retrieve a table listing all plugins and their requirements. Or just a single plugin if name is passed. Args: name str optional name of a specific plugin. Returns: table instance containing plugin descriptive data Raises: None """ table_body = [] header = TableRow([_('Title'), _('ID'), _('Requirements')], header=True) table_body.append(header) plugins_dict = dict([(pretty_function_name(p), p) for p in FunctionProvider.plugins]) if name is not None: if isinstance(name, basestring): # Add the names plugins_dict.update( dict([(p.__name__, p) for p in FunctionProvider.plugins])) msg = ('No plugin named "%s" was found. ' 'List of available plugins is: %s' % (name, ', '.join(plugins_dict.keys()))) if name not in plugins_dict: raise RuntimeError(msg) plugins_dict = {name: plugins_dict[name]} else: msg = ('get_plugins expects either no parameters or a string ' 'with the name of the plugin, you passed: ' '%s which is a %s' % (name, type(name))) raise Exception(msg) # Now loop through the plugins adding them to the table for key, func in plugins_dict.iteritems(): for requirement in requirements_collect(func): row = [] row.append(TableCell(get_function_title(func), header=True)) row.append(key) row.append(requirement) table_body.append(TableRow(row)) table = Table(table_body) table.caption = _('Available Impact Functions') return table
def _tabulate(self, flooded_len, question, road_len, roads_by_type): table_body = [ question, TableRow( [tr('Road Type'), tr('Temporarily closed (m)'), tr('Total (m)')], header=True), TableRow([tr('All'), int(flooded_len), int(road_len)]), TableRow(tr('Breakdown by road type'), header=True)] for road_type, value in roads_by_type.iteritems(): table_body.append( TableRow([ road_type, int(value['flooded']), int(value['total'])]) ) return table_body
def test_row_from_string(self): """Test row from string - it should span to the table width too""" table_row1 = TableRow([ self.table_cell_a, self.table_cell_b, self.table_cell_c, self.table_cell_d ]) self.html += ' <h2>Table row from string</h2>\n' body = (' <tbody>\n' ' <tr>\n' ' <td>a</td>\n' ' <td>b</td>\n' ' <td>c</td>\n' ' <td>d</td>\n' ' </tr>\n' ' <tr>\n' ' <td colspan="100%">foobar</td>\n' ' </tr>\n' ' <tr>\n' ' <td colspan="100%">Piet Pompies</td>\n' ' </tr>\n' ' </tbody>\n') expected_result = ('%s%s%s' % (self.html_table_start, body, self.html_table_end)) actual_result = Table([table_row1, 'foobar', 'Piet Pompies']) message = 'Expected: %s\n\nGot: %s' % (expected_result, actual_result) self.html += str(actual_result) self.writeHtml('table_row_from_string') assert expected_result.strip() == str(actual_result).strip(), message
def test_table_with_colalign(self): """Table columns can be right justified""" # First with default alignment actual_result = Table(['12', '3000', '5']) expected_strings = ['<td colspan="100%">12</td>', '<td colspan="100%">3000</td>', '<td colspan="100%">5</td>'] for s in expected_strings: message = ('Did not find expected string "%s" in result: %s' % (s, actual_result)) assert s in str(actual_result).strip(), message # Then using explicit alignment (all right justified) # FIXME (Ole): This does not work if e.g. col_align has # different strings: col_align = ['right', 'left', 'center'] actual_result = Table(['12', '3000', '5'], col_align=['right', 'right', 'right']) expected_strings = [ ('<td colspan="100%" align="right" style="text-align: ' 'right;">12</td>'), ('<td colspan="100%" align="right" style="text-align: ' 'right;">3000</td>'), ('<td colspan="100%" align="right" style="text-align: ' 'right;">5</td>')] for s in expected_strings: message = ('Did not find expected string "%s" in result: %s' % (s, actual_result)) assert s in str(actual_result).strip(), message # Now try at the TableRow level # FIXME (Ole): Breaks tables! # row = TableRow(['12', '3000', '5'], # col_align=['right', 'right', 'right']) # actual_result = Table(row) # print actual_result # This breaks too - what's going on? # row = TableRow(['12', '3000', '5']) # actual_result = Table(row) # print actual_result # Try at the cell level cell_1 = TableCell('12', align='right') cell_2 = TableCell('3000', align='right') cell_3 = TableCell('5', align='right') row = TableRow([cell_1, cell_2, cell_3]) # print row # OK # This is OK for cell in [cell_1, cell_2, cell_3]: msg = 'Wrong cell alignment %s' % cell assert 'align="right"' in str(cell), msg table = Table(row) self.html += str(table) self.writeHtml('table_column_alignment')
def _tabulate_zero_impact(self, evacuated, question, table_body, thresholds): table_body = [ question, TableRow([(tr('People in %.1f m of water') % thresholds[-1]), '%s' % format_int(evacuated)], header=True)] return table_body
def _tabulate_action_checklist(self, table_body, total, no_data_warning): table_body.append(TableRow(tr('Action Checklist:'), header=True)) table_body.append(TableRow(tr('How will warnings be disseminated?'))) table_body.append(TableRow(tr('How will we reach stranded people?'))) table_body.append(TableRow(tr('Do we have enough relief items?'))) table_body.append( TableRow( tr('If yes, where are they located and how will we distribute ' 'them?'))) table_body.append( TableRow( tr('If no, where can we obtain additional relief items from ' 'and how will we transport them to here?'))) # Extend impact report for on-screen display table_body.extend([ TableRow(tr('Notes'), header=True), tr('Map shows the numbers of people in high, medium, and low ' 'hazard class areas'), tr('Total population: %s') % format_int(total) ]) if no_data_warning: table_body.extend([ tr('The layers contained `no data`. This missing data was ' 'carried through to the impact layer.'), tr('`No data` values in the impact layer were treated as 0 ' 'when counting the affected or total population.') ]) return table_body
def parse_to_html(report): """Convert a json compatible list of results to a tabulated version. :param report: A json compatible report :type report: list :returns: Returns a tabulated version of the report :rtype: basestring """ tabulated_report = [] for row in report: row_template = { 'content': '', 'condition': True, 'arguments': (), 'header': False } row_template.update(row) if not row_template['condition']: continue content = row_template['content'] if row_template['arguments']: arguments = row_template['arguments'] if hasattr(content, '__iter__'): message = ( 'Problem formatting arguments into content.' 'The element count of the arguments must equal ' 'the element count of the content.') assert len(content) == len(arguments), message # pylint: disable=bad-builtin # pylint: disable=deprecated-lambda content = map(lambda c, a: c % a, content, arguments) # pylint: enable=deprecated-lambda # pylint: enable=bad-builtin else: content = row_template['content'] % arguments if row_template['header']: table_row = TableRow(content, header=True) else: table_row = TableRow(content) tabulated_report.append(table_row) html_tabulated_report = Table(tabulated_report).toNewlineFreeString() return html_tabulated_report
def _tabulate(self, flooded_len, question, road_len, roads_by_type): table_body = [ question, TableRow([ tr('Road Type'), tr('Flooded in the threshold (m)'), tr('Total (m)') ], header=True), TableRow([tr('All'), int(flooded_len), int(road_len)]), TableRow(tr('Breakdown by road type'), header=True) ] for t, v in roads_by_type.iteritems(): table_body.append(TableRow([t, int(v['flooded']), int(v['total'])])) return table_body
def test_column(self): """Test to retrieve all element in a column. """ table_body = [] header = TableRow(['header1', 'header2', 'header3', 'header4'], header=True) table_body.append(header) table_body.append(TableRow([1, 2, 3, 4])) table_body.append(TableRow(['a', 'b', 'c', 'd'])) table_body.append(TableRow(['x', 'y', 'z', 't'])) html_table = Table(table_body) expected_result1 = ['header1', 1, 'a', 'x'] expected_result2 = [2, 'b', 'y'] real_result1 = html_table.column(0, True) real_result2 = html_table.column(1) message1 = "Expected %s but got %s" % (expected_result1, real_result1) message2 = "Expected %s but got %s" % (expected_result2, real_result2) assert expected_result1 == real_result1, message1 assert expected_result2 == real_result2, message2
def _tabulate(self, high, low, medium, question, total_impact): # Generate impact report for the pdf map table_body = [ question, TableRow([tr('People impacted '), '%s' % format_int(total_impact)], header=True), TableRow( [tr('People in high hazard area '), '%s' % format_int(high)], header=True), TableRow([ tr('People in medium hazard area '), '%s' % format_int(medium) ], header=True), TableRow([tr('People in low hazard area'), '%s' % format_int(low)], header=True) ] return table_body
def _tabulate(self, high, low, medium, minimum_needs, no_impact, question, total_impact): # Generate impact report for the pdf map table_body = [ question, TableRow([ tr('Total Population Affected '), '%s' % format_int(total_impact) ], header=True), TableRow([ tr('Population in High hazard class areas '), '%s' % format_int(high) ]), TableRow([ tr('Population in Medium hazard class areas '), '%s' % format_int(medium) ]), TableRow([ tr('Population in Low hazard class areas '), '%s' % format_int(low) ]), TableRow( [tr('Population Not Affected'), '%s' % format_int(no_impact)]), TableRow( tr('Table below shows the minimum needs for all ' 'evacuated people')) ] total_needs = evacuated_population_needs(total_impact, minimum_needs) for frequency, needs in total_needs.items(): table_body.append( TableRow([ tr('Needs should be provided %s' % frequency), tr('Total') ], header=True)) for resource in needs: table_body.append( TableRow([ tr(resource['table name']), format_int(resource['amount']) ])) return table_body, total_needs
def test_cell_link(self): """Test cell links work""" table_cell_link = Link('InaSAFE', 'http://inasafe.org') table_row = TableRow([ TableCell(table_cell_link), self.table_cell_b, self.table_cell_c, self.table_cell_d ]) self.html += ' <h2>Link Cell Columns</h2>\n' body = (' <tbody>\n' ' <tr>\n' ' <td><a href="http://inasafe.org">InaSAFE</a></td>\n' ' <td>b</td>\n' ' <td>c</td>\n' ' <td>d</td>\n' ' </tr>\n' ' </tbody>\n') expected_result = ('%s%s%s' % (self.html_table_start, body, self.html_table_end)) actual_result = Table([table_row]) message = 'Expected: %s\n\nGot: %s' % (expected_result, actual_result) assert expected_result.strip() == str(actual_result).strip(), message self.html += str(actual_result) self.writeHtml('table_colspanning')
def run(self, layers): """Risk plugin for flood population evacuation Input layers: List of layers expected to contain my_hazard: Raster layer of flood depth my_exposure: Raster layer of population data on the same grid as my_hazard Counts number of people exposed to flood levels exceeding specified threshold. Return Map of population exposed to flood levels exceeding the threshold Table with number of people evacuated and supplies required """ # Identify hazard and exposure layers my_hazard = get_hazard_layer(layers) # Flood inundation [m] my_exposure = get_exposure_layer(layers) question = get_question(my_hazard.get_name(), my_exposure.get_name(), self) # Determine depths above which people are regarded affected [m] # Use thresholds from inundation layer if specified thresholds = self.parameters['thresholds [m]'] verify(isinstance(thresholds, list), 'Expected thresholds to be a list. Got %s' % str(thresholds)) # Extract data as numeric arrays D = my_hazard.get_data(nan=0.0) # Depth # Calculate impact as population exposed to depths > max threshold P = my_exposure.get_data(nan=0.0, scaling=True) # Calculate impact to intermediate thresholds counts = [] # merely initialize my_impact = None for i, lo in enumerate(thresholds): if i == len(thresholds) - 1: # The last threshold my_impact = M = numpy.where(D >= lo, P, 0) else: # Intermediate thresholds hi = thresholds[i + 1] M = numpy.where((D >= lo) * (D < hi), P, 0) # Count val = int(numpy.sum(M)) # Don't show digits less than a 1000 val = round_thousand(val) counts.append(val) # Count totals evacuated = counts[-1] total = int(numpy.sum(P)) # Don't show digits less than a 1000 total = round_thousand(total) # Calculate estimated minimum needs # The default value of each logistic is based on BNPB Perka 7/2008 # minimum bantuan minimum_needs = self.parameters['minimum needs'] mn_rice = minimum_needs['Rice'] mn_drinking_water = minimum_needs['Drinking Water'] mn_water = minimum_needs['Water'] mn_family_kits = minimum_needs['Family Kits'] mn_toilets = minimum_needs['Toilets'] rice = int(evacuated * mn_rice) drinking_water = int(evacuated * mn_drinking_water) water = int(evacuated * mn_water) family_kits = int(evacuated * mn_family_kits) toilets = int(evacuated * mn_toilets) # Generate impact report for the pdf map table_body = [ question, TableRow([(tr('People in %.1f m of water') % thresholds[-1]), '%s*' % format_int(evacuated)], header=True), TableRow(tr('* Number is rounded to the nearest 1000'), header=False), TableRow(tr('Map shows population density needing evacuation')), TableRow([tr('Needs per week'), tr('Total')], header=True), [tr('Rice [kg]'), format_int(rice)], [tr('Drinking Water [l]'), format_int(drinking_water)], [tr('Clean Water [l]'), format_int(water)], [tr('Family Kits'), format_int(family_kits)], [tr('Toilets'), format_int(toilets)] ] table_body.append(TableRow(tr('Action Checklist:'), header=True)) table_body.append(TableRow(tr('How will warnings be disseminated?'))) table_body.append(TableRow(tr('How will we reach stranded people?'))) table_body.append(TableRow(tr('Do we have enough relief items?'))) table_body.append( TableRow( tr('If yes, where are they located and how ' 'will we distribute them?'))) table_body.append( TableRow( tr('If no, where can we obtain additional relief items from and how ' 'will we transport them to here?'))) # Extend impact report for on-screen display table_body.extend([ TableRow(tr('Notes'), header=True), tr('Total population: %s') % format_int(total), tr('People need evacuation if flood levels exceed %(eps).1f m') % { 'eps': thresholds[-1] }, tr('Minimum needs are defined in BNPB regulation 7/2008'), tr('All values are rounded up to the nearest integer in order to ' 'avoid representing human lives as fractionals.') ]) if len(counts) > 1: table_body.append(TableRow(tr('Detailed breakdown'), header=True)) for i, val in enumerate(counts[:-1]): s = (tr('People in %(lo).1f m to %(hi).1f m of water: %(val)i') % { 'lo': thresholds[i], 'hi': thresholds[i + 1], 'val': format_int(val) }) table_body.append(TableRow(s, header=False)) # Result impact_summary = Table(table_body).toNewlineFreeString() impact_table = impact_summary # check for zero impact if numpy.nanmax(my_impact) == 0 == numpy.nanmin(my_impact): table_body = [ question, TableRow([(tr('People in %.1f m of water') % thresholds[-1]), '%s' % format_int(evacuated)], header=True) ] my_message = Table(table_body).toNewlineFreeString() raise ZeroImpactException(my_message) # Create style colours = [ '#FFFFFF', '#38A800', '#79C900', '#CEED00', '#FFCC00', '#FF6600', '#FF0000', '#7A0000' ] classes = create_classes(my_impact.flat[:], len(colours)) interval_classes = humanize_class(classes) style_classes = [] for i in xrange(len(colours)): style_class = dict() if i == 1: label = create_label(interval_classes[i], 'Low') elif i == 4: label = create_label(interval_classes[i], 'Medium') elif i == 7: label = create_label(interval_classes[i], 'High') else: label = create_label(interval_classes[i]) style_class['label'] = label style_class['quantity'] = classes[i] if i == 0: transparency = 100 else: transparency = 0 style_class['transparency'] = transparency style_class['colour'] = colours[i] style_classes.append(style_class) style_info = dict(target_field=None, style_classes=style_classes, style_type='rasterStyle') # For printing map purpose map_title = tr('People in need of evacuation') legend_notes = tr('Thousand separator is represented by %s' % get_thousand_separator()) legend_units = tr('(people per cell)') legend_title = tr('Population density') # Create raster object and return R = Raster(my_impact, projection=my_hazard.get_projection(), geotransform=my_hazard.get_geotransform(), name=tr('Population which %s') % get_function_title(self), keywords={ 'impact_summary': impact_summary, 'impact_table': impact_table, 'map_title': map_title, 'legend_notes': legend_notes, 'legend_units': legend_units, 'legend_title': legend_title }, style_info=style_info) return R
def run(self, layers): """Experimental impact function for flood polygons on roads. :param layers: List of layers expected to contain H: Polygon layer of inundation areas E: Vector layer of roads """ target_field = self.parameters['target_field'] road_type_field = self.parameters['road_type_field'] affected_field = self.parameters['affected_field'] affected_value = self.parameters['affected_value'] # Extract data hazard = get_hazard_layer(layers) # Flood exposure = get_exposure_layer(layers) # Roads question = get_question(hazard.get_name(), exposure.get_name(), self) hazard = hazard.get_layer() hazard_provider = hazard.dataProvider() affected_field_index = hazard_provider.fieldNameIndex(affected_field) # see #818: should still work if there is no valid attribute if affected_field_index == -1: pass # message = tr('''Parameter "Affected Field"(='%s') # is not present in the attribute table of the hazard layer. # ''' % (affected_field, )) # raise GetDataError(message) LOGGER.info('Affected field: %s' % affected_field) LOGGER.info('Affected field index: %s' % affected_field_index) exposure = exposure.get_layer() # Filter geometry and data using the extent extent = QgsRectangle(*self.extent) request = QgsFeatureRequest() request.setFilterRect(extent) # Split line_layer by hazard and save as result: # 1) Filter from hazard inundated features # 2) Mark roads as inundated (1) or not inundated (0) if affected_field_index != -1: affected_field_type = hazard_provider.fields( )[affected_field_index].typeName() if affected_field_type in ['Real', 'Integer']: affected_value = float(affected_value) ################################# # REMARK 1 # In qgis 2.2 we can use request to filter inundated # polygons directly (it allows QgsExpression). Then # we can delete the lines and call # # request = .... # hazard_poly = union_geometry(H, request) # ################################ hazard_features = hazard.getFeatures(request) hazard_poly = None for feature in hazard_features: attributes = feature.attributes() if affected_field_index != -1: if attributes[affected_field_index] != affected_value: continue if hazard_poly is None: hazard_poly = QgsGeometry(feature.geometry()) else: # Make geometry union of inundated polygons # But some feature.geometry() could be invalid, skip them tmp_geometry = hazard_poly.combine(feature.geometry()) try: if tmp_geometry.isGeosValid(): hazard_poly = tmp_geometry except AttributeError: pass ############################################### # END REMARK 1 ############################################### if hazard_poly is None: message = tr( '''There are no objects in the hazard layer with "Affected value"='%s'. Please check the value or use a different extent.''' % (affected_value, )) raise GetDataError(message) # Clip exposure by the extent extent_as_polygon = QgsGeometry().fromRect(extent) line_layer = clip_by_polygon(exposure, extent_as_polygon) # Find inundated roads, mark them line_layer = split_by_polygon(line_layer, hazard_poly, request, mark_value=(target_field, 1)) # Generate simple impact report epsg = get_utm_epsg(self.extent[0], self.extent[1]) destination_crs = QgsCoordinateReferenceSystem(epsg) transform = QgsCoordinateTransform(exposure.crs(), destination_crs) road_len = flooded_len = 0 # Length of roads roads_by_type = dict() # Length of flooded roads by types roads_data = line_layer.getFeatures() road_type_field_index = line_layer.fieldNameIndex(road_type_field) target_field_index = line_layer.fieldNameIndex(target_field) for road in roads_data: attributes = road.attributes() road_type = attributes[road_type_field_index] if road_type.__class__.__name__ == 'QPyNullVariant': road_type = tr('Other') geom = road.geometry() geom.transform(transform) length = geom.length() road_len += length if road_type not in roads_by_type: roads_by_type[road_type] = {'flooded': 0, 'total': 0} roads_by_type[road_type]['total'] += length if attributes[target_field_index] == 1: flooded_len += length roads_by_type[road_type]['flooded'] += length table_body = [ question, TableRow([ tr('Road Type'), tr('Temporarily closed (m)'), tr('Total (m)') ], header=True), TableRow([tr('All'), int(flooded_len), int(road_len)]), TableRow(tr('Breakdown by road type'), header=True) ] for road_type, value in roads_by_type.iteritems(): table_body.append( TableRow( [road_type, int(value['flooded']), int(value['total'])])) impact_summary = Table(table_body).toNewlineFreeString() map_title = tr('Roads inundated') style_classes = [ dict(label=tr('Not Inundated'), value=0, colour='#1EFC7C', transparency=0, size=0.5), dict(label=tr('Inundated'), value=1, colour='#F31A1C', transparency=0, size=0.5) ] style_info = dict(target_field=target_field, style_classes=style_classes, style_type='categorizedSymbol') # Convert QgsVectorLayer to inasafe layer and return it line_layer = Vector(data=line_layer, name=tr('Flooded roads'), keywords={ 'impact_summary': impact_summary, 'map_title': map_title, 'target_field': target_field }, style_info=style_info) return line_layer
def run(self, layers): """Risk plugin for flood population evacuation Input layers: List of layers expected to contain H: Raster layer of flood depth P: Raster layer of population data on the same grid as H Counts number of people exposed to flood levels exceeding specified threshold. Return Map of population exposed to flood levels exceeding the threshold Table with number of people evacuated and supplies required """ # Identify hazard and exposure layers H = get_hazard_layer(layers) # Flood inundation E = get_exposure_layer(layers) question = get_question(H.get_name(), E.get_name(), self) # Check that hazard is polygon type if not H.is_vector: msg = ('Input hazard %s was not a vector layer as expected ' % H.get_name()) raise Exception(msg) msg = ('Input hazard must be a polygon layer. I got %s with layer ' 'type %s' % (H.get_name(), H.get_geometry_name())) if not H.is_polygon_data: raise Exception(msg) # Run interpolation function for polygon2raster P = assign_hazard_values_to_exposure_data(H, E, attribute_name='population') # Initialise attributes of output dataset with all attributes # from input polygon and a population count of zero new_attributes = H.get_data() category_title = 'FLOODPRONE' # FIXME: Should come from keywords categories = {} for attr in new_attributes: attr[self.target_field] = 0 cat = attr[category_title] categories[cat] = 0 # Count affected population per polygon, per category and total evacuated = 0 for attr in P.get_data(): # Get population at this location pop = float(attr['population']) # Update population count for associated polygon poly_id = attr['polygon_id'] new_attributes[poly_id][self.target_field] += pop # Update population count for each category cat = new_attributes[poly_id][category_title] categories[cat] += pop # Update total evacuated += pop # Count totals total = int(numpy.sum(E.get_data(nan=0, scaling=False))) # Don't show digits less than a 1000 if total > 1000: total = total // 1000 * 1000 if evacuated > 1000: evacuated = evacuated // 1000 * 1000 # Calculate estimated needs based on BNPB Perka 7/2008 minimum bantuan rice = evacuated * 2.8 drinking_water = evacuated * 17.5 water = evacuated * 67 family_kits = evacuated / 5 toilets = evacuated / 20 # Generate impact report for the pdf map table_body = [question, TableRow([_('People needing evacuation'), '%i' % evacuated], header=True), TableRow(_('Map shows population affected in each flood ' 'prone area ')), TableRow([_('Needs per week'), _('Total')], header=True), [_('Rice [kg]'), int(rice)], [_('Drinking Water [l]'), int(drinking_water)], [_('Clean Water [l]'), int(water)], [_('Family Kits'), int(family_kits)], [_('Toilets'), int(toilets)]] impact_table = Table(table_body).toNewlineFreeString() # Extend impact report for on-screen display table_body.extend([TableRow(_('Notes'), header=True), _('Total population: %i') % total, _('People need evacuation if in area identified ' 'as "Flood Prone"'), _('Minimum needs are defined in BNPB ' 'regulation 7/2008')]) impact_summary = Table(table_body).toNewlineFreeString() map_title = _('People affected by flood prone areas') # Define classes for legend for flooded population counts colours = ['#FFFFFF', '#38A800', '#79C900', '#CEED00', '#FFCC00', '#FF6600', '#FF0000', '#7A0000'] population_counts = [x['population'] for x in new_attributes] cls = [0] + numpy.linspace(1, max(population_counts), len(colours)).tolist() # Define style info for output polygons showing population counts style_classes = [] for i, colour in enumerate(colours): lo = cls[i] hi = cls[i + 1] if i == 0: label = _('0') else: label = _('%i - %i') % (lo, hi) entry = dict(label=label, colour=colour, min=lo, max=hi, transparency=0, size=1) style_classes.append(entry) # Override style info with new classes and name style_info = dict(target_field=self.target_field, style_classes=style_classes, legend_title=_('Population Count')) # Create vector layer and return V = Vector(data=new_attributes, projection=H.get_projection(), geometry=H.get_geometry(), name=_('Population affected by flood prone areas'), keywords={'impact_summary': impact_summary, 'impact_table': impact_table, 'map_title': map_title}, style_info=style_info) return V
def run(self, layers): """Plugin for impact of population as derived by categorised hazard Input layers: List of layers expected to contain my_hazard: Raster layer of categorised hazard my_exposure: Raster layer of population data Counts number of people exposed to each category of the hazard Return Map of population exposed to high category Table with number of people in each category """ # The 3 category high_t = 1 medium_t = 0.66 low_t = 0.34 # Identify hazard and exposure layers my_hazard = get_hazard_layer(layers) # Categorised Hazard my_exposure = get_exposure_layer(layers) # Population Raster question = get_question(my_hazard.get_name(), my_exposure.get_name(), self) # Extract data as numeric arrays C = my_hazard.get_data(nan=0.0) # Category # Calculate impact as population exposed to each category P = my_exposure.get_data(nan=0.0, scaling=True) H = numpy.where(C == high_t, P, 0) M = numpy.where(C > medium_t, P, 0) L = numpy.where(C < low_t, P, 0) # Count totals total = int(numpy.sum(P)) high = int(numpy.sum(H)) medium = int(numpy.sum(M)) - int(numpy.sum(H)) low = int(numpy.sum(L)) - int(numpy.sum(M)) total_impact = high + medium + low # Don't show digits less than a 1000 total = round_thousand(total) total_impact = round_thousand(total_impact) high = round_thousand(high) medium = round_thousand(medium) low = round_thousand(low) # Generate impact report for the pdf map table_body = [ question, TableRow([tr('People impacted '), '%s' % format_int(total_impact)], header=True), TableRow( [tr('People in high hazard area '), '%s' % format_int(high)], header=True), TableRow([ tr('People in medium hazard area '), '%s' % format_int(medium) ], header=True), TableRow([tr('People in low hazard area'), '%s' % format_int(low)], header=True) ] impact_table = Table(table_body).toNewlineFreeString() # Extend impact report for on-screen display table_body.extend([ TableRow(tr('Notes'), header=True), tr('Map shows population density in high or medium ' 'hazard area'), tr('Total population: %s') % format_int(total) ]) impact_summary = Table(table_body).toNewlineFreeString() map_title = tr('People in high hazard areas') # Generate 8 equidistant classes across the range of flooded population # 8 is the number of classes in the predefined flood population style # as imported # noinspection PyTypeChecker classes = numpy.linspace(numpy.nanmin(M.flat[:]), numpy.nanmax(M.flat[:]), 8) # Modify labels in existing flood style to show quantities style_classes = style_info['style_classes'] style_classes[1]['label'] = tr('Low [%i people/cell]') % classes[1] style_classes[4]['label'] = tr('Medium [%i people/cell]') % classes[4] style_classes[7]['label'] = tr('High [%i people/cell]') % classes[7] style_info['legend_title'] = tr('Population Density') # Create raster object and return R = Raster(M, projection=my_hazard.get_projection(), geotransform=my_hazard.get_geotransform(), name=tr('Population which %s') % (get_function_title(self).lower()), keywords={ 'impact_summary': impact_summary, 'impact_table': impact_table, 'map_title': map_title }, style_info=style_info) return R
class ITBFatalityFunction(FunctionProvider): """Indonesian Earthquake Fatality Model This model was developed by Institut Teknologi Bandung (ITB) and implemented by Dr. Hadi Ghasemi, Geoscience Australia. Reference: Indonesian Earthquake Building-Damage and Fatality Models and Post Disaster Survey Guidelines Development, Bali, 27-28 February 2012, 54pp. Algorithm: In this study, the same functional form as Allen (2009) is adopted to express fatality rate as a function of intensity (see Eq. 10 in the report). The Matlab built-in function (fminsearch) for Nelder-Mead algorithm was used to estimate the model parameters. The objective function (L2G norm) that is minimised during the optimisation is the same as the one used by Jaiswal et al. (2010). The coefficients used in the indonesian model are x=0.62275231, y=8.03314466, zeta=2.15 Allen, T. I., Wald, D. J., Earle, P. S., Marano, K. D., Hotovec, A. J., Lin, K., and Hearne, M., 2009. An Atlas of ShakeMaps and population exposure catalog for earthquake loss modeling, Bull. Earthq. Eng. 7, 701-718. Jaiswal, K., and Wald, D., 2010. An empirical model for global earthquake fatality estimation, Earthq. Spectra 26, 1017-1037. Caveats and limitations: The current model is the result of the above mentioned workshop and reflects the best available information. However, the current model has a number of issues listed below and is expected to evolve further over time. 1 - The model is based on limited number of observed fatality rates during 4 past fatal events. 2 - The model clearly over-predicts the fatality rates at intensities higher than VIII. 3 - The model only estimates the expected fatality rate for a given intensity level; however the associated uncertainty for the proposed model is not addressed. 4 - There are few known mistakes in developing the current model: - rounding MMI values to the nearest 0.5, - Implementing Finite-Fault models of candidate events, and - consistency between selected GMPEs with those in use by BMKG. These issues will be addressed by ITB team in the final report. Note: Because of these caveats, decisions should not be made solely on the information presented here and should always be verified by ground truthing and other reliable information sources. :author Hadi Ghasemi :rating 3 :param requires category=='hazard' and \ subcategory=='earthquake' and \ layertype=='raster' and \ unit=='MMI' :param requires category=='exposure' and \ subcategory=='population' and \ layertype=='raster' """ title = tr('Die or be displaced') synopsis = tr( 'To asses the impact of earthquake on population based on earthquake ' 'model developed by ITB') citations = tr( ' * Indonesian Earthquake Building-Damage and Fatality Models and ' ' Post Disaster Survey Guidelines Development Bali, 27-28 ' ' February 2012, 54pp.\n' ' * Allen, T. I., Wald, D. J., Earle, P. S., Marano, K. D., ' ' Hotovec, A. J., Lin, K., and Hearne, M., 2009. An Atlas ' ' of ShakeMaps and population exposure catalog for ' ' earthquake loss modeling, Bull. Earthq. Eng. 7, 701-718.\n' ' * Jaiswal, K., and Wald, D., 2010. An empirical model for ' ' global earthquake fatality estimation, Earthq. Spectra ' ' 26, 1017-1037.\n') limitation = tr( ' - The model is based on limited number of observed fatality ' ' rates during 4 past fatal events. \n' ' - The model clearly over-predicts the fatality rates at ' ' intensities higher than VIII.\n' ' - The model only estimates the expected fatality rate ' ' for a given intensity level; however the associated ' ' uncertainty for the proposed model is not addressed.\n' ' - There are few known mistakes in developing the current ' ' model:\n\n' ' * rounding MMI values to the nearest 0.5,\n' ' * Implementing Finite-Fault models of candidate events, and\n' ' * consistency between selected GMPEs with those in use by ' ' BMKG.\n') actions = tr( 'Provide details about the population will be die or displaced') detailed_description = tr( 'This model was developed by Institut Teknologi Bandung (ITB) ' 'and implemented by Dr. Hadi Ghasemi, Geoscience Australia\n' 'Algorithm:\n' 'In this study, the same functional form as Allen (2009) is ' 'adopted o express fatality rate as a function of intensity ' '(see Eq. 10 in the report). The Matlab built-in function ' '(fminsearch) for Nelder-Mead algorithm was used to estimate ' 'the model parameters. The objective function (L2G norm) that ' 'is minimized during the optimisation is the same as the one ' 'used by Jaiswal et al. (2010).\n' 'The coefficients used in the indonesian model are x=0.62275231, ' 'y=8.03314466, zeta=2.15') defaults = get_defaults() parameters = OrderedDict([ ('x', 0.62275231), ('y', 8.03314466), # Model coefficients # Rates of people displaced for each MMI level ('displacement_rate', { 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 1.0, 7: 1.0, 8: 1.0, 9: 1.0, 10: 1.0 }), ('mmi_range', range(2, 10)), ('step', 0.5), # Threshold below which layer should be transparent ('tolerance', 0.01), ('calculate_displaced_people', True), ('postprocessors', OrderedDict([ ('Gender', { 'on': True }), ('Age', { 'on': True, 'params': OrderedDict([('youth_ratio', defaults['YOUTH_RATIO']), ('adult_ratio', defaults['ADULT_RATIO']), ('elder_ratio', defaults['ELDER_RATIO'])]) }), ('MinimumNeeds', { 'on': True }) ])), ('minimum needs', default_minimum_needs()) ]) def fatality_rate(self, mmi): """ ITB method to compute fatality rate :param mmi: """ # As per email discussion with Ole, Trevor, Hadi, mmi < 4 will have # a fatality rate of 0 - Tim if mmi < 4: return 0 x = self.parameters['x'] y = self.parameters['y'] return numpy.power(10.0, x * mmi - y) def run(self, layers): """Indonesian Earthquake Fatality Model Input: :param layers: List of layers expected to contain, my_hazard: Raster layer of MMI ground shaking my_exposure: Raster layer of population density """ displacement_rate = self.parameters['displacement_rate'] # Tolerance for transparency tolerance = self.parameters['tolerance'] # Extract input layers intensity = get_hazard_layer(layers) population = get_exposure_layer(layers) question = get_question(intensity.get_name(), population.get_name(), self) # Extract data grids my_hazard = intensity.get_data() # Ground Shaking my_exposure = population.get_data(scaling=True) # Population Density # Calculate population affected by each MMI level # FIXME (Ole): this range is 2-9. Should 10 be included? mmi_range = self.parameters['mmi_range'] number_of_exposed = {} number_of_displaced = {} number_of_fatalities = {} # Calculate fatality rates for observed Intensity values (my_hazard # based on ITB power model R = numpy.zeros(my_hazard.shape) for mmi in mmi_range: # Identify cells where MMI is in class i and # count population affected by this shake level I = numpy.where((my_hazard > mmi - self.parameters['step']) * (my_hazard <= mmi + self.parameters['step']), my_exposure, 0) # Calculate expected number of fatalities per level fatality_rate = self.fatality_rate(mmi) F = fatality_rate * I # Calculate expected number of displaced people per level try: D = displacement_rate[mmi] * I except KeyError, e: msg = 'mmi = %i, I = %s, Error msg: %s' % (mmi, str(I), str(e)) # noinspection PyExceptionInherit raise InaSAFEError(msg) # Adjust displaced people to disregard fatalities. # Set to zero if there are more fatalities than displaced. D = numpy.where(D > F, D - F, 0) # Sum up numbers for map R += D # Displaced # Generate text with result for this study # This is what is used in the real time system exposure table number_of_exposed[mmi] = numpy.nansum(I.flat) number_of_displaced[mmi] = numpy.nansum(D.flat) # noinspection PyUnresolvedReferences number_of_fatalities[mmi] = numpy.nansum(F.flat) # Set resulting layer to NaN when less than a threshold. This is to # achieve transparency (see issue #126). R[R < tolerance] = numpy.nan # Total statistics total = int(round(numpy.nansum(my_exposure.flat) / 1000) * 1000) # Compute number of fatalities fatalities = int( round(numpy.nansum(number_of_fatalities.values()) / 1000)) * 1000 # As per email discussion with Ole, Trevor, Hadi, total fatalities < 50 # will be rounded down to 0 - Tim if fatalities < 50: fatalities = 0 # Compute number of people displaced due to building collapse displaced = int( round(numpy.nansum(number_of_displaced.values()) / 1000)) * 1000 # Generate impact report table_body = [question] # Add total fatality estimate s = format_int(fatalities) table_body.append( TableRow([tr('Number of fatalities'), s], header=True)) if self.parameters['calculate_displaced_people']: # Add total estimate of people displaced s = format_int(displaced) table_body.append( TableRow([tr('Number of people displaced'), s], header=True)) else: displaced = 0 # Add estimate of total population in area s = format_int(int(total)) table_body.append( TableRow([tr('Total number of people'), s], header=True)) # Calculate estimated needs based on BNPB Perka 7/2008 minimum bantuan # FIXME: Refactor and share minimum_needs = self.parameters['minimum needs'] needs = evacuated_population_weekly_needs(displaced, minimum_needs) # Generate impact report for the pdf map table_body = [ question, TableRow([tr('Fatalities'), '%s' % format_int(fatalities)], header=True), TableRow([tr('People displaced'), '%s' % format_int(displaced)], header=True), TableRow( tr('Map shows density estimate of ' 'displaced population')), TableRow([tr('Needs per week'), tr('Total')], header=True), [tr('Rice [kg]'), format_int(needs['rice'])], [tr('Drinking Water [l]'), format_int(needs['drinking_water'])], [tr('Clean Water [l]'), format_int(needs['water'])], [tr('Family Kits'), format_int(needs['family_kits'])], TableRow(tr('Action Checklist:'), header=True) ] if fatalities > 0: table_body.append( tr('Are there enough victim identification ' 'units available for %s people?') % format_int(fatalities)) if displaced > 0: table_body.append( tr('Are there enough shelters and relief items ' 'available for %s people?') % format_int(displaced)) table_body.append( TableRow( tr('If yes, where are they located and ' 'how will we distribute them?'))) table_body.append( TableRow( tr('If no, where can we obtain ' 'additional relief items from and ' 'how will we transport them?'))) # Extend impact report for on-screen display table_body.extend([ TableRow(tr('Notes'), header=True), tr('Total population: %s') % format_int(total), tr('People are considered to be displaced if ' 'they experience and survive a shake level' 'of more than 5 on the MMI scale '), tr('Minimum needs are defined in BNPB ' 'regulation 7/2008'), tr('The fatality calculation assumes that ' 'no fatalities occur for shake levels below 4 ' 'and fatality counts of less than 50 are ' 'disregarded.'), tr('All values are rounded up to the nearest ' 'integer in order to avoid representing human ' 'lives as fractionals.') ]) table_body.append(TableRow(tr('Notes'), header=True)) table_body.append( tr('Fatality model is from ' 'Institute of Teknologi Bandung 2012.')) table_body.append(tr('Population numbers rounded to nearest 1000.')) # Result impact_summary = Table(table_body).toNewlineFreeString() impact_table = impact_summary # check for zero impact if numpy.nanmax(R) == 0 == numpy.nanmin(R): table_body = [ question, TableRow([tr('Fatalities'), '%s' % format_int(fatalities)], header=True) ] my_message = Table(table_body).toNewlineFreeString() raise ZeroImpactException(my_message) # Create style colours = ['#EEFFEE', '#FFFF7F', '#E15500', '#E4001B', '#730000'] classes = create_classes(R.flat[:], len(colours)) interval_classes = humanize_class(classes) style_classes = [] for i in xrange(len(colours)): style_class = dict() style_class['label'] = create_label(interval_classes[i]) style_class['quantity'] = classes[i] if i == 0: transparency = 100 else: transparency = 30 style_class['transparency'] = transparency style_class['colour'] = colours[i] style_classes.append(style_class) style_info = dict(target_field=None, style_classes=style_classes, style_type='rasterStyle') # For printing map purpose map_title = tr('Earthquake impact to population') legend_notes = tr('Thousand separator is represented by %s' % get_thousand_separator()) legend_units = tr('(people per cell)') legend_title = tr('Population density') # Create raster object and return L = Raster(R, projection=population.get_projection(), geotransform=population.get_geotransform(), keywords={ 'impact_summary': impact_summary, 'total_population': total, 'total_fatalities': fatalities, 'fatalites_per_mmi': number_of_fatalities, 'exposed_per_mmi': number_of_exposed, 'displaced_per_mmi': number_of_displaced, 'impact_table': impact_table, 'map_title': map_title, 'legend_notes': legend_notes, 'legend_units': legend_units, 'legend_title': legend_title }, name=tr('Estimated displaced population per cell'), style_info=style_info) return L
def run(self, layers): """Experimental impact function. Input layers: List of layers expected to contain H: Polygon layer of inundation areas E: Vector layer of roads """ target_field = self.parameters['target_field'] road_type_field = self.parameters['road_type_field'] threshold_min = self.parameters['min threshold [m]'] threshold_max = self.parameters['max threshold [m]'] if threshold_min > threshold_max: message = tr('''The minimal threshold is greater then the maximal specified threshold. Please check the values.''') raise GetDataError(message) # Extract data H = get_hazard_layer(layers) # Flood E = get_exposure_layer(layers) # Roads question = get_question(H.get_name(), E.get_name(), self) H = H.get_layer() E = E.get_layer() # Get necessary width and height of raster height = (self.extent[3] - self.extent[1]) / H.rasterUnitsPerPixelY() height = int(height) width = (self.extent[2] - self.extent[0]) / H.rasterUnitsPerPixelX() width = int(width) # Align raster extent and self.extent raster_extent = H.dataProvider().extent() xmin = raster_extent.xMinimum() xmax = raster_extent.xMaximum() ymin = raster_extent.yMinimum() ymax = raster_extent.yMaximum() x_delta = (xmax - xmin) / H.width() x = xmin for i in range(H.width()): if abs(x - self.extent[0]) < x_delta: # We have found the aligned raster boundary break x += x_delta _ = i y_delta = (ymax - ymin) / H.height() y = ymin for i in range(H.width()): if abs(y - self.extent[1]) < y_delta: # We have found the aligned raster boundary break y += y_delta clip_extent = [x, y, x + width * x_delta, y + height * y_delta] # Clip and polygonize small_raster = clip_raster(H, width, height, QgsRectangle(*clip_extent)) flooded_polygon = polygonize(small_raster, threshold_min, threshold_max) # Filter geometry and data using the extent extent = QgsRectangle(*self.extent) request = QgsFeatureRequest() request.setFilterRect(extent) if flooded_polygon is None: message = tr('''There are no objects in the hazard layer with "value">'%s'. Please check the value or use other extent.''' % (threshold_min, )) raise GetDataError(message) # Clip exposure by the extent extent_as_polygon = QgsGeometry().fromRect(extent) line_layer = clip_by_polygon(E, extent_as_polygon) # Find inundated roads, mark them line_layer = split_by_polygon(line_layer, flooded_polygon, request, mark_value=(target_field, 1)) # Find inundated roads, mark them # line_layer = split_by_polygon( # E, # flooded_polygon, # request, # mark_value=(target_field, 1)) target_field_index = line_layer.dataProvider().\ fieldNameIndex(target_field) # Generate simple impact report epsg = get_utm_epsg(self.extent[0], self.extent[1]) output_crs = QgsCoordinateReferenceSystem(epsg) transform = QgsCoordinateTransform(E.crs(), output_crs) road_len = flooded_len = 0 # Length of roads roads_by_type = dict() # Length of flooded roads by types roads_data = line_layer.getFeatures() road_type_field_index = line_layer.fieldNameIndex(road_type_field) for road in roads_data: attributes = road.attributes() road_type = attributes[road_type_field_index] if road_type.__class__.__name__ == 'QPyNullVariant': road_type = tr('Other') geom = road.geometry() geom.transform(transform) length = geom.length() road_len += length if road_type not in roads_by_type: roads_by_type[road_type] = {'flooded': 0, 'total': 0} roads_by_type[road_type]['total'] += length if attributes[target_field_index] == 1: flooded_len += length roads_by_type[road_type]['flooded'] += length table_body = [ question, TableRow([ tr('Road Type'), tr('Flooded in the threshold (m)'), tr('Total (m)') ], header=True), TableRow([tr('All'), int(flooded_len), int(road_len)]) ] table_body.append(TableRow(tr('Breakdown by road type'), header=True)) for t, v in roads_by_type.iteritems(): table_body.append(TableRow([t, int(v['flooded']), int(v['total'])])) impact_summary = Table(table_body).toNewlineFreeString() map_title = tr('Roads inundated') style_classes = [ dict(label=tr('Not Inundated'), value=0, colour='#1EFC7C', transparency=0, size=0.5), dict(label=tr('Inundated'), value=1, colour='#F31A1C', transparency=0, size=0.5) ] style_info = dict(target_field=target_field, style_classes=style_classes, style_type='categorizedSymbol') # Convert QgsVectorLayer to inasafe layer and return it line_layer = Vector(data=line_layer, name=tr('Flooded roads'), keywords={ 'impact_summary': impact_summary, 'map_title': map_title, 'target_field': target_field }, style_info=style_info) return line_layer
def run(self, layers, x=0.62275231, y=8.03314466): # , zeta=2.15): """Gender specific earthquake impact model Input layers: List of layers expected to contain H: Raster layer of MMI ground shaking P: Raster layer of population density """ # Define percentages of people being displaced at each mmi level displacement_rate = { 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0.1, 8: 0.5, 9: 0.75, 10: 1.0 } # Extract input layers intensity = get_hazard_layer(layers) population = get_exposure_layer(layers) question = get_question(intensity.get_name(), population.get_name(), self) # Extract data grids H = intensity.get_data() # Ground Shaking P = population.get_data() # Population Density # Calculate population affected by each MMI level # FIXME (Ole): this range is 2-9. Should 10 be included? mmi_range = range(2, 10) number_of_exposed = {} number_of_fatalities = {} # Calculate fatality rates for observed Intensity values (H # based on ITB power model R = numpy.zeros(H.shape) for mmi in mmi_range: # Identify cells where MMI is in class i mask = (H > mmi - 0.5) * (H <= mmi + 0.5) # Count population affected by this shake level I = numpy.where(mask, P, 0) # Calculate expected number of fatalities per level fatality_rate = numpy.power(10.0, x * mmi - y) F = fatality_rate * I # Sum up fatalities to create map R += F # Generate text with result for this study # This is what is used in the real time system exposure table number_of_exposed[mmi] = numpy.nansum(I.flat) number_of_fatalities[mmi] = numpy.nansum(F.flat) # Set resulting layer to zero when less than a threshold. This is to # achieve transparency (see issue #126). R[R < 1] = numpy.nan # Total statistics total = numpy.nansum(P.flat) # Compute number of fatalities fatalities = numpy.nansum(number_of_fatalities.values()) # Compute number of people displaced due to building collapse displaced = 0 for mmi in mmi_range: displaced += displacement_rate[mmi] * number_of_exposed[mmi] displaced_women = displaced * 0.52 # Could be made province dependent displaced_pregnant_women = displaced_women * 0.01387 # CHECK # Generate impact report table_body = [question] # Add total fatality estimate s = str(int(fatalities)).rjust(10) table_body.append( TableRow([tr('Number of fatalities'), s], header=True)) # Add total estimate of people displaced s = str(int(displaced)).rjust(10) table_body.append( TableRow([tr('Number of people displaced'), s], header=True)) s = str(int(displaced_women)).rjust(10) table_body.append( TableRow([tr('Number of women displaced'), s], header=True)) s = str(int(displaced_pregnant_women)).rjust(10) table_body.append( TableRow([tr('Number of pregnant women displaced'), s], header=True)) table_body.append(TableRow(tr('Action Checklist:'), header=True)) table_body.append( tr('Are enough shelters available for %i women?') % displaced_women) table_body.append( tr('Are enough facilities available to assist %i ' 'pregnant women?') % displaced_pregnant_women) table_body.append(TableRow(tr('Notes'), header=True)) table_body.append( tr('Fatality model is from ' 'Institute of Teknologi Bandung 2012.')) impact_summary = Table(table_body).toNewlineFreeString() impact_table = impact_summary map_title = tr('Earthquake impact to population') # Create new layer and return L = Raster(R, projection=population.get_projection(), geotransform=population.get_geotransform(), keywords={ 'impact_summary': impact_summary, 'total_population': total, 'total_fatalities': fatalities, 'impact_table': impact_table, 'map_title': map_title }, name=tr('Estimated fatalities'), style_info=earthquake_fatality_style) # Maybe return a shape file with contours instead return L
class ITBFatalityFunction(ImpactFunction): # noinspection PyUnresolvedReferences """Indonesian Earthquake Fatality Model. This model was developed by Institut Teknologi Bandung (ITB) and implemented by Dr. Hadi Ghasemi, Geoscience Australia. Reference: Indonesian Earthquake Building-Damage and Fatality Models and Post Disaster Survey Guidelines Development, Bali, 27-28 February 2012, 54pp. Algorithm: In this study, the same functional form as Allen (2009) is adopted to express fatality rate as a function of intensity (see Eq. 10 in the report). The Matlab built-in function (fminsearch) for Nelder-Mead algorithm was used to estimate the model parameters. The objective function (L2G norm) that is minimised during the optimisation is the same as the one used by Jaiswal et al. (2010). The coefficients used in the indonesian model are x=0.62275231, y=8.03314466, zeta=2.15 Allen, T. I., Wald, D. J., Earle, P. S., Marano, K. D., Hotovec, A. J., Lin, K., and Hearne, M., 2009. An Atlas of ShakeMaps and population exposure catalog for earthquake loss modeling, Bull. Earthq. Eng. 7, 701-718. Jaiswal, K., and Wald, D., 2010. An empirical model for global earthquake fatality estimation, Earthq. Spectra 26, 1017-1037. Caveats and limitations: The current model is the result of the above mentioned workshop and reflects the best available information. However, the current model has a number of issues listed below and is expected to evolve further over time. 1 - The model is based on limited number of observed fatality rates during 4 past fatal events. 2 - The model clearly over-predicts the fatality rates at intensities higher than VIII. 3 - The model only estimates the expected fatality rate for a given intensity level; however the associated uncertainty for the proposed model is not addressed. 4 - There are few known mistakes in developing the current model: - rounding MMI values to the nearest 0.5, - Implementing Finite-Fault models of candidate events, and - consistency between selected GMPEs with those in use by BMKG. These issues will be addressed by ITB team in the final report. Note: Because of these caveats, decisions should not be made solely on the information presented here and should always be verified by ground truthing and other reliable information sources. """ _metadata = ITBFatalityMetadata() def __init__(self): super(ITBFatalityFunction, self).__init__() # AG: Use the proper minimum needs, update the parameters self.parameters = add_needs_parameters(self.parameters) self.hardcoded_parameters = OrderedDict([ ('x', 0.62275231), ('y', 8.03314466), # Model coefficients # Rates of people displaced for each MMI level ('displacement_rate', { 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 1.0, 7: 1.0, 8: 1.0, 9: 1.0, 10: 1.0 }), ('mmi_range', range(2, 10)), ('step', 0.5), # Threshold below which layer should be transparent ('tolerance', 0.01), ('calculate_displaced_people', True) ]) def fatality_rate(self, mmi): """ITB method to compute fatality rate. :param mmi: """ # As per email discussion with Ole, Trevor, Hadi, mmi < 4 will have # a fatality rate of 0 - Tim if mmi < 4: return 0 x = self.hardcoded_parameters['x'] y = self.hardcoded_parameters['y'] # noinspection PyUnresolvedReferences return numpy.power(10.0, x * mmi - y) def run(self, layers=None): """Indonesian Earthquake Fatality Model. Input: :param layers: List of layers expected to contain, hazard: Raster layer of MMI ground shaking exposure: Raster layer of population count """ self.validate() self.prepare(layers) displacement_rate = self.hardcoded_parameters['displacement_rate'] # Tolerance for transparency tolerance = self.hardcoded_parameters['tolerance'] # Extract input layers intensity = self.hazard population = self.exposure # Extract data grids hazard = intensity.get_data() # Ground Shaking exposure = population.get_data(scaling=True) # Population Density # Calculate people affected by each MMI level # FIXME (Ole): this range is 2-9. Should 10 be included? mmi_range = self.hardcoded_parameters['mmi_range'] number_of_exposed = {} number_of_displaced = {} number_of_fatalities = {} # Calculate fatality rates for observed Intensity values (hazard # based on ITB power model mask = numpy.zeros(hazard.shape) for mmi in mmi_range: # Identify cells where MMI is in class i and # count people affected by this shake level mmi_matches = numpy.where( (hazard > mmi - self.hardcoded_parameters['step']) * ( hazard <= mmi + self.hardcoded_parameters['step']), exposure, 0) # Calculate expected number of fatalities per level fatality_rate = self.fatality_rate(mmi) fatalities = fatality_rate * mmi_matches # Calculate expected number of displaced people per level try: displacements = displacement_rate[mmi] * mmi_matches except KeyError, e: msg = 'mmi = %i, mmi_matches = %s, Error msg: %s' % ( mmi, str(mmi_matches), str(e)) # noinspection PyExceptionInherit raise InaSAFEError(msg) # Adjust displaced people to disregard fatalities. # Set to zero if there are more fatalities than displaced. displacements = numpy.where( displacements > fatalities, displacements - fatalities, 0) # Sum up numbers for map mask += displacements # Displaced # Generate text with result for this study # This is what is used in the real time system exposure table number_of_exposed[mmi] = numpy.nansum(mmi_matches.flat) number_of_displaced[mmi] = numpy.nansum(displacements.flat) # noinspection PyUnresolvedReferences number_of_fatalities[mmi] = numpy.nansum(fatalities.flat) # Set resulting layer to NaN when less than a threshold. This is to # achieve transparency (see issue #126). mask[mask < tolerance] = numpy.nan # Total statistics total, rounding = population_rounding_full(numpy.nansum(exposure.flat)) # Compute number of fatalities fatalities = population_rounding(numpy.nansum( number_of_fatalities.values())) # As per email discussion with Ole, Trevor, Hadi, total fatalities < 50 # will be rounded down to 0 - Tim if fatalities < 50: fatalities = 0 # Compute number of people displaced due to building collapse displaced = population_rounding(numpy.nansum( number_of_displaced.values())) # Generate impact report table_body = [self.question] # Add total fatality estimate s = format_int(fatalities) table_body.append(TableRow([tr('Number of fatalities'), s], header=True)) if self.hardcoded_parameters['calculate_displaced_people']: # Add total estimate of people displaced s = format_int(displaced) table_body.append(TableRow([tr('Number of people displaced'), s], header=True)) else: displaced = 0 # Add estimate of total population in area s = format_int(int(total)) table_body.append(TableRow([tr('Total number of people'), s], header=True)) minimum_needs = [ parameter.serialize() for parameter in self.parameters['minimum needs'] ] # Generate impact report for the pdf map table_body = [ self.question, TableRow( [tr('Fatalities'), '%s' % format_int(fatalities)], header=True), TableRow( [tr('People displaced'), '%s' % format_int(displaced)], header=True), TableRow(tr('Map shows the estimation of displaced population'))] total_needs = evacuated_population_needs( displaced, minimum_needs) for frequency, needs in total_needs.items(): table_body.append(TableRow( [ tr('Needs should be provided %s' % frequency), tr('Total') ], header=True)) for resource in needs: table_body.append(TableRow([ tr(resource['table name']), format_int(resource['amount'])])) table_body.append(TableRow(tr('Provenance'), header=True)) table_body.append(TableRow(self.parameters['provenance'])) table_body.append(TableRow(tr('Action Checklist:'), header=True)) if fatalities > 0: table_body.append(tr('Are there enough victim identification ' 'units available for %s people?') % format_int(fatalities)) if displaced > 0: table_body.append(tr('Are there enough shelters and relief items ' 'available for %s people?') % format_int(displaced)) table_body.append(TableRow(tr('If yes, where are they located and ' 'how will we distribute them?'))) table_body.append(TableRow(tr('If no, where can we obtain ' 'additional relief items from and ' 'how will we transport them?'))) # Extend impact report for on-screen display table_body.extend([TableRow(tr('Notes'), header=True), tr('Total population: %s') % format_int(total), tr('People are considered to be displaced if ' 'they experience and survive a shake level' 'of more than 5 on the MMI scale '), tr('Minimum needs are defined in BNPB ' 'regulation 7/2008'), tr('The fatality calculation assumes that ' 'no fatalities occur for shake levels below 4 ' 'and fatality counts of less than 50 are ' 'disregarded.'), tr('All values are rounded up to the nearest ' 'integer in order to avoid representing human ' 'lives as fractions.')]) table_body.append(TableRow(tr('Notes'), header=True)) table_body.append(tr('Fatality model is from ' 'Institute of Teknologi Bandung 2012.')) table_body.append( tr('Population numbers rounded up to the nearest %s.') % rounding) # Result impact_summary = Table(table_body).toNewlineFreeString() impact_table = impact_summary # check for zero impact if numpy.nanmax(mask) == 0 == numpy.nanmin(mask): table_body = [ self.question, TableRow([tr('Fatalities'), '%s' % format_int(fatalities)], header=True)] my_message = Table(table_body).toNewlineFreeString() raise ZeroImpactException(my_message) # Create style colours = ['#EEFFEE', '#FFFF7F', '#E15500', '#E4001B', '#730000'] classes = create_classes(mask.flat[:], len(colours)) interval_classes = humanize_class(classes) style_classes = [] for i in xrange(len(colours)): style_class = dict() style_class['label'] = create_label(interval_classes[i]) style_class['quantity'] = classes[i] if i == 0: transparency = 100 else: transparency = 30 style_class['transparency'] = transparency style_class['colour'] = colours[i] style_classes.append(style_class) style_info = dict(target_field=None, style_classes=style_classes, style_type='rasterStyle') # For printing map purpose map_title = tr('Earthquake impact to population') legend_notes = tr('Thousand separator is represented by %s' % get_thousand_separator()) legend_units = tr('(people per cell)') legend_title = tr('Population Count') # Create raster object and return raster = Raster( mask, projection=population.get_projection(), geotransform=population.get_geotransform(), keywords={ 'impact_summary': impact_summary, 'total_population': total, 'total_fatalities': fatalities, 'fatalities_per_mmi': number_of_fatalities, 'exposed_per_mmi': number_of_exposed, 'displaced_per_mmi': number_of_displaced, 'impact_table': impact_table, 'map_title': map_title, 'legend_notes': legend_notes, 'legend_units': legend_units, 'legend_title': legend_title, 'total_needs': total_needs}, name=tr('Estimated displaced population per cell'), style_info=style_info) self._impact = raster return raster
def run(self, layers): """Experimental impact function. Input layers: List of layers expected to contain H: Polygon layer of inundation areas E: Vector layer of roads """ target_field = self.parameters['target_field'] building_type_field = self.parameters['building_type_field'] affected_field = self.parameters['affected_field'] affected_value = self.parameters['affected_value'] # Extract data H = get_hazard_layer(layers) # Flood E = get_exposure_layer(layers) # Roads question = get_question(H.get_name(), E.get_name(), self) H = H.get_layer() h_provider = H.dataProvider() affected_field_index = h_provider.fieldNameIndex(affected_field) if affected_field_index == -1: message = tr('''Parameter "Affected Field"(='%s') is not present in the attribute table of the hazard layer.''' % (affected_field, )) raise GetDataError(message) E = E.get_layer() srs = E.crs().toWkt() e_provider = E.dataProvider() fields = e_provider.fields() # If target_field does not exist, add it: if fields.indexFromName(target_field) == -1: e_provider.addAttributes([QgsField(target_field, QVariant.Int)]) target_field_index = e_provider.fieldNameIndex(target_field) fields = e_provider.fields() # Create layer for store the lines from E and extent building_layer = QgsVectorLayer('Polygon?crs=' + srs, 'impact_buildings', 'memory') building_provider = building_layer.dataProvider() # Set attributes building_provider.addAttributes(fields.toList()) building_layer.startEditing() building_layer.commitChanges() # Filter geometry and data using the extent extent = QgsRectangle(*self.extent) request = QgsFeatureRequest() request.setFilterRect(extent) # Split building_layer by H and save as result: # 1) Filter from H inundated features # 2) Mark buildings as inundated (1) or not inundated (0) affected_field_type = h_provider.fields( )[affected_field_index].typeName() if affected_field_type in ['Real', 'Integer']: affected_value = float(affected_value) h_data = H.getFeatures(request) hazard_poly = None for mpolygon in h_data: attributes = mpolygon.attributes() if attributes[affected_field_index] != affected_value: continue if hazard_poly is None: hazard_poly = QgsGeometry(mpolygon.geometry()) else: # Make geometry union of inundated polygons # But some mpolygon.geometry() could be invalid, skip them tmp_geometry = hazard_poly.combine(mpolygon.geometry()) try: if tmp_geometry.isGeosValid(): hazard_poly = tmp_geometry except AttributeError: pass if hazard_poly is None: message = tr( '''There are no objects in the hazard layer with "Affected value"='%s'. Please check the value or use other extent.''' % (affected_value, )) raise GetDataError(message) e_data = E.getFeatures(request) for feat in e_data: building_geom = feat.geometry() attributes = feat.attributes() l_feat = QgsFeature() l_feat.setGeometry(building_geom) l_feat.setAttributes(attributes) if hazard_poly.intersects(building_geom): l_feat.setAttribute(target_field_index, 1) else: l_feat.setAttribute(target_field_index, 0) (_, __) = building_layer.dataProvider().addFeatures([l_feat]) building_layer.updateExtents() # Generate simple impact report building_count = flooded_count = 0 # Count of buildings buildings_by_type = dict() # Length of flooded roads by types buildings_data = building_layer.getFeatures() building_type_field_index = building_layer.fieldNameIndex( building_type_field) for building in buildings_data: building_count += 1 attributes = building.attributes() building_type = attributes[building_type_field_index] if building_type in [None, 'NULL', 'null', 'Null']: building_type = 'Unknown type' if building_type not in buildings_by_type: buildings_by_type[building_type] = {'flooded': 0, 'total': 0} buildings_by_type[building_type]['total'] += 1 if attributes[target_field_index] == 1: flooded_count += 1 buildings_by_type[building_type]['flooded'] += 1 table_body = [ question, TableRow([tr('Building Type'), tr('Flooded'), tr('Total')], header=True), TableRow([tr('All'), int(flooded_count), int(building_count)]), TableRow(tr('Breakdown by building type'), header=True) ] for t, v in buildings_by_type.iteritems(): table_body.append(TableRow([t, int(v['flooded']), int(v['total'])])) impact_summary = Table(table_body).toNewlineFreeString() map_title = tr('Buildings inundated') style_classes = [ dict(label=tr('Not Inundated'), value=0, colour='#1EFC7C', transparency=0, size=0.5), dict(label=tr('Inundated'), value=1, colour='#F31A1C', transparency=0, size=0.5) ] style_info = dict(target_field=target_field, style_classes=style_classes, style_type='categorizedSymbol') # Convert QgsVectorLayer to inasafe layer and return it. building_layer = Vector(data=building_layer, name=tr('Flooded buildings'), keywords={ 'impact_summary': impact_summary, 'map_title': map_title, 'target_field': target_field }, style_info=style_info) return building_layer
def run(self, layers): """Impact plugin for hazard impact """ # Extract data H = get_hazard_layer(layers) # Value E = get_exposure_layer(layers) # Building locations question = get_question(H.get_name(), E.get_name(), self) # Interpolate hazard level to building locations H = assign_hazard_values_to_exposure_data(H, E, attribute_name='hazard_lev', mode='constant') # Extract relevant numerical data coordinates = H.get_geometry() category = H.get_data() N = len(category) # List attributes to carry forward to result layer #attributes = E.get_attribute_names() # Calculate building impact according to guidelines count2 = 0 count1 = 0 count0 = 0 building_impact = [] for i in range(N): # Get category value val = float(category[i]['hazard_lev']) # Classify buildings according to value ## if val >= 2.0 / 3: ## affected = 2 ## count2 += 1 ## elif 1.0 / 3 <= val < 2.0 / 3: ## affected = 1 ## count1 += 1 ## else: ## affected = 0 ## count0 += 1 ## FIXME it would be good if the affected were words not numbers ## FIXME need to read hazard layer and see category or keyword if val == 3: affected = 3 count2 += 1 elif val == 2: affected = 2 count1 += 1 elif val == 1: affected = 1 count0 += 1 else: affected = 'None' # Collect depth and calculated damage result_dict = {self.target_field: affected, 'CATEGORY': val} # Record result for this feature building_impact.append(result_dict) # Create impact report # Generate impact summary table_body = [question, TableRow([tr('Category'), tr('Affected')], header=True), TableRow([tr('High'), format_int(count2)]), TableRow([tr('Medium'), format_int(count1)]), TableRow([tr('Low'), format_int(count0)]), TableRow([tr('All'), format_int(N)])] table_body.append(TableRow(tr('Notes'), header=True)) table_body.append(tr('Categorised hazard has only 3' ' classes, high, medium and low.')) impact_summary = Table(table_body).toNewlineFreeString() impact_table = impact_summary map_title = tr('Categorised hazard impact on buildings') #FIXME it would be great to do categorized rather than grduated # Create style style_classes = [dict(label=tr('Low'), min=1, max=1, colour='#1EFC7C', transparency=0, size=1), dict(label=tr('Medium'), min=2, max=2, colour='#FFA500', transparency=0, size=1), dict(label=tr('High'), min=3, max=3, colour='#F31A1C', transparency=0, size=1)] style_info = dict(target_field=self.target_field, style_classes=style_classes) # Create vector layer and return name = 'Buildings Affected' V = Vector(data=building_impact, projection=E.get_projection(), geometry=coordinates, geometry_type=E.geometry_type, keywords={'impact_summary': impact_summary, 'impact_table': impact_table, 'map_title': map_title, 'target_field': self.target_field, 'statistics_type': self.statistics_type, 'statistics_classes': self.statistics_classes}, name=name, style_info=style_info) return V
def run(self, layers): """Risk plugin for flood population evacuation. :param layers: List of layers expected to contain * hazard_layer : Vector polygon layer of flood depth * exposure_layer : Raster layer of population data on the same grid as hazard_layer Counts number of people exposed to areas identified as flood prone :returns: Map of population exposed to flooding Table with number of people evacuated and supplies required. :rtype: tuple """ # Identify hazard and exposure layers hazard_layer = get_hazard_layer(layers) # Flood inundation exposure_layer = get_exposure_layer(layers) question = get_question(hazard_layer.get_name(), exposure_layer.get_name(), self) # Check that hazard is polygon type if not hazard_layer.is_vector: message = ('Input hazard %s was not a vector layer as expected ' % hazard_layer.get_name()) raise Exception(message) message = ( 'Input hazard must be a polygon layer. I got %s with layer type ' '%s' % (hazard_layer.get_name(), hazard_layer.get_geometry_name())) if not hazard_layer.is_polygon_data: raise Exception(message) # Run interpolation function for polygon2raster P = assign_hazard_values_to_exposure_data(hazard_layer, exposure_layer, attribute_name='population') # Initialise attributes of output dataset with all attributes # from input polygon and a population count of zero new_attributes = hazard_layer.get_data() category_title = 'affected' # FIXME: Should come from keywords deprecated_category_title = 'FLOODPRONE' categories = {} for attr in new_attributes: attr[self.target_field] = 0 try: cat = attr[category_title] except KeyError: try: cat = attr['FLOODPRONE'] categories[cat] = 0 except KeyError: pass # Count affected population per polygon, per category and total affected_population = 0 for attr in P.get_data(): affected = False if 'affected' in attr: res = attr['affected'] if res is None: x = False else: x = bool(res) affected = x elif 'FLOODPRONE' in attr: # If there isn't an 'affected' attribute, res = attr['FLOODPRONE'] if res is not None: affected = res.lower() == 'yes' elif 'Affected' in attr: # Check the default attribute assigned for points # covered by a polygon res = attr['Affected'] if res is None: x = False else: x = res affected = x else: # assume that every polygon is affected (see #816) affected = True # there is no flood related attribute # message = ('No flood related attribute found in %s. ' # 'I was looking for either "Flooded", "FLOODPRONE" ' # 'or "Affected". The latter should have been ' # 'automatically set by call to ' # 'assign_hazard_values_to_exposure_data(). ' # 'Sorry I can\'t help more.') # raise Exception(message) if affected: # Get population at this location pop = float(attr['population']) # Update population count for associated polygon poly_id = attr['polygon_id'] new_attributes[poly_id][self.target_field] += pop # Update population count for each category if len(categories) > 0: try: cat = new_attributes[poly_id][category_title] except KeyError: cat = new_attributes[poly_id][ deprecated_category_title] categories[cat] += pop # Update total affected_population += pop # Estimate number of people in need of evacuation evacuated = (affected_population * self.parameters['evacuation_percentage'] / 100.0) affected_population, rounding = population_rounding_full( affected_population) total = int(numpy.sum(exposure_layer.get_data(nan=0, scaling=False))) # Don't show digits less than a 1000 total = population_rounding(total) evacuated, rounding_evacuated = population_rounding_full(evacuated) minimum_needs = [ parameter.serialize() for parameter in self.parameters['minimum needs'] ] # Generate impact report for the pdf map table_body = [ question, TableRow([ tr('People affected'), '%s*' % (format_int(int(affected_population))) ], header=True), TableRow([ TableCell(tr('* Number is rounded up to the nearest %s') % (rounding), col_span=2) ]), TableRow([ tr('People needing evacuation'), '%s*' % (format_int(int(evacuated))) ], header=True), TableRow([ TableCell(tr('* Number is rounded up to the nearest %s') % (rounding_evacuated), col_span=2) ]), TableRow([ tr('Evacuation threshold'), '%s%%' % format_int(self.parameters['evacuation_percentage']) ], header=True), TableRow( tr('Map shows the number of people affected in each flood prone ' 'area')), TableRow( tr('Table below shows the weekly minimum needs for all ' 'evacuated people')) ] total_needs = evacuated_population_needs(evacuated, minimum_needs) for frequency, needs in total_needs.items(): table_body.append( TableRow([ tr('Needs should be provided %s' % frequency), tr('Total') ], header=True)) for resource in needs: table_body.append( TableRow([ tr(resource['table name']), format_int(resource['amount']) ])) impact_table = Table(table_body).toNewlineFreeString() table_body.append(TableRow(tr('Action Checklist:'), header=True)) table_body.append(TableRow(tr('How will warnings be disseminated?'))) table_body.append(TableRow(tr('How will we reach stranded people?'))) table_body.append(TableRow(tr('Do we have enough relief items?'))) table_body.append( TableRow( tr('If yes, where are they located and how will we distribute ' 'them?'))) table_body.append( TableRow( tr('If no, where can we obtain additional relief items from and ' 'how will we transport them to here?'))) # Extend impact report for on-screen display table_body.extend([ TableRow(tr('Notes'), header=True), tr('Total population: %s') % format_int(total), tr('People need evacuation if in the area identified as ' '"Flood Prone"'), tr('Minimum needs are defined in BNPB regulation 7/2008') ]) impact_summary = Table(table_body).toNewlineFreeString() # Create style # Define classes for legend for flooded population counts colours = [ '#FFFFFF', '#38A800', '#79C900', '#CEED00', '#FFCC00', '#FF6600', '#FF0000', '#7A0000' ] population_counts = [x['population'] for x in new_attributes] classes = create_classes(population_counts, len(colours)) interval_classes = humanize_class(classes) # Define style info for output polygons showing population counts style_classes = [] for i in xrange(len(colours)): style_class = dict() style_class['label'] = create_label(interval_classes[i]) if i == 0: transparency = 0 style_class['min'] = 0 else: transparency = 0 style_class['min'] = classes[i - 1] style_class['transparency'] = transparency style_class['colour'] = colours[i] style_class['max'] = classes[i] style_classes.append(style_class) # Override style info with new classes and name style_info = dict(target_field=self.target_field, style_classes=style_classes, style_type='graduatedSymbol') # For printing map purpose map_title = tr('People affected by flood prone areas') legend_notes = tr('Thousand separator is represented by \'.\'') legend_units = tr('(people per polygon)') legend_title = tr('Population Count') # Create vector layer and return vector_layer = Vector(data=new_attributes, projection=hazard_layer.get_projection(), geometry=hazard_layer.get_geometry(), name=tr('People affected by flood prone areas'), keywords={ 'impact_summary': impact_summary, 'impact_table': impact_table, 'target_field': self.target_field, 'map_title': map_title, 'legend_notes': legend_notes, 'legend_units': legend_units, 'legend_title': legend_title, 'affected_population': affected_population, 'total_population': total, 'total_needs': total_needs }, style_info=style_info) return vector_layer
def run(self, layers): """Earthquake impact to buildings (e.g. from Open Street Map) """ # Thresholds for mmi breakdown t0 = 6 t1 = 7 t2 = 8 class_1 = 'Low' class_2 = 'Medium' class_3 = 'High' # Extract data H = get_hazard_layer(layers) # Depth E = get_exposure_layer(layers) # Building locations question = get_question(H.get_name(), E.get_name(), self) # Define attribute name for hazard levels hazard_attribute = 'mmi' # Interpolate hazard level to building locations I = assign_hazard_values_to_exposure_data( H, E, attribute_name=hazard_attribute) # Extract relevant exposure data #attribute_names = I.get_attribute_names() attributes = I.get_data() N = len(I) # Calculate building impact lo = 0 me = 0 hi = 0 building_values = {} contents_values = {} for key in range(4): building_values[key] = 0 contents_values[key] = 0 for i in range(N): # Classify building according to shake level x = float(attributes[i][hazard_attribute]) # Interpolated MMI val if t0 <= x < t1: lo += 1 cls = 1 elif t1 <= x < t2: me += 1 cls = 2 elif t2 <= x: hi += 1 cls = 3 else: # Buildings not reported for MMI levels < t0 cls = 0 attributes[i][self.target_field] = cls # Generate simple impact report for unspecific buildings table_body = [ question, TableRow(['Hazard Level', 'Buildings Affected'], header=True), TableRow([class_1, lo]), TableRow([class_2, me]), TableRow([class_3, hi]) ] table_body.append(TableRow('Notes', header=True)) table_body.append('High hazard is defined as shake levels greater ' 'than %i on the MMI scale.' % t2) table_body.append('Medium hazard is defined as shake levels ' 'between %i and %i on the MMI scale.' % (t1, t2)) table_body.append('Low hazard is defined as shake levels ' 'between %i and %i on the MMI scale.' % (t0, t1)) impact_summary = Table(table_body).toNewlineFreeString() impact_table = impact_summary map_title = 'Buildings affected' # Create style style_classes = [ dict(label=class_1, value=1, colour='#ffff00', transparency=1), dict(label=class_2, value=2, colour='#ffaa00', transparency=1), dict(label=class_3, value=3, colour='#ff0000', transparency=1) ] style_info = dict(target_field=self.target_field, style_classes=style_classes, style_type='categorizedSymbol') # Create vector layer and return V = Vector(data=attributes, projection=I.get_projection(), geometry=I.get_geometry(), name='Estimated buildings affected', keywords={ 'impact_summary': impact_summary, 'impact_table': impact_table, 'map_title': map_title, 'target_field': self.target_field, 'statistics_type': self.statistics_type, 'statistics_classes': self.statistics_classes }, style_info=style_info) return V
def run(self, layers): """Plugin for impact of population as derived by categorised hazard Input layers: List of layers expected to contain H: Raster layer of categorised hazard P: Raster layer of population data Counts number of people exposed to each category of the hazard Return Map of population exposed to high category Table with number of people in each category """ # The 3 category high_t = 1 medium_t = 0.66 low_t = 0.34 # Identify hazard and exposure layers inundation = get_hazard_layer(layers) # Categorised Hazard population = get_exposure_layer(layers) # Population Raster question = get_question(inundation.get_name(), population.get_name(), self) # Extract data as numeric arrays C = inundation.get_data(nan=0.0) # Category # Calculate impact as population exposed to each category P = population.get_data(nan=0.0, scaling=True) H = numpy.where(C == high_t, P, 0) M = numpy.where(C > medium_t, P, 0) L = numpy.where(C < low_t, P, 0) # Count totals total = int(numpy.sum(P)) high = int(numpy.sum(H)) medium = int(numpy.sum(M)) - int(numpy.sum(H)) low = int(numpy.sum(L)) - int(numpy.sum(M)) total_impact = high + medium + low # Don't show digits less than a 1000 if total > 1000: total = total // 1000 * 1000 if total_impact > 1000: total_impact = total_impact // 1000 * 1000 if high > 1000: high = high // 1000 * 1000 if medium > 1000: medium = medium // 1000 * 1000 if low > 1000: low = low // 1000 * 1000 # Calculate estimated needs based on BNPB Perka 7/2008 minimum bantuan ## rice = evacuated * 2.8 ## drinking_water = evacuated * 17.5 ## water = evacuated * 67 ## family_kits = evacuated / 5 ## toilets = evacuated / 20 # Generate impact report for the pdf map table_body = [question, TableRow([tr('People impacted '), '%i' % total_impact], header=True), TableRow([tr('People in high hazard area '), '%i' % high], header=True), TableRow([tr('People in medium hazard area '), '%i' % medium], header=True), TableRow([tr('People in low hazard area'), '%i' % low], header=True)] ## TableRow([tr('Needs per week'), tr('Total')], ## header=True), ## [tr('Rice [kg]'), int(rice)], ## [tr('Drinking Water [l]'), int(drinking_water)], ## [tr('Clean Water [l]'), int(water)], ## [tr('Family Kits'), int(family_kits)], ## [tr('Toilets'), int(toilets)]] impact_table = Table(table_body).toNewlineFreeString() # Extend impact report for on-screen display table_body.extend([TableRow(tr('Notes'), header=True), tr('Map shows population density in high or medium ' 'hazard area'), tr('Total population: %i') % total]) ## tr('Minimum needs are defined in BNPB ' ## 'regulation 7/2008')]) impact_summary = Table(table_body).toNewlineFreeString() map_title = tr('People in high hazard areas') # Generare 8 equidistant classes across the range of flooded population # 8 is the number of classes in the predefined flood population style # as imported classes = numpy.linspace(numpy.nanmin(M.flat[:]), numpy.nanmax(M.flat[:]), 8) # Modify labels in existing flood style to show quantities style_classes = style_info['style_classes'] style_classes[1]['label'] = tr('Low [%i people/cell]') % classes[1] style_classes[4]['label'] = tr('Medium [%i people/cell]') % classes[4] style_classes[7]['label'] = tr('High [%i people/cell]') % classes[7] style_info['legend_title'] = tr('Population Density') # Create raster object and return R = Raster(M, projection=inundation.get_projection(), geotransform=inundation.get_geotransform(), name=tr('Population which %s') % get_function_title(self), keywords={'impact_summary': impact_summary, 'impact_table': impact_table, 'map_title': map_title}, style_info=style_info) return R
def get_plugins_as_table(dict_filter=None): """Retrieve a table listing all plugins and their requirements. Or just a single plugin if name is passed. Args: * dict_filter = dictionary that contains filters - id = list_id - title = list_title - category : list_category - subcategory : list_subcategory - layertype : list_layertype - datatype : list_datatype - unit: list_unit - disabled : list_disabled # not included Returns: * table contains plugins match with dict_filter Raises: None """ if dict_filter is None: dict_filter = { 'id': [], 'title': [], 'category': [], 'subcategory': [], 'layertype': [], 'datatype': [], 'unit': [] } table_body = [] # use this list for avoiding wrong order in dict atts = ['category', 'subcategory', 'layertype', 'datatype', 'unit'] header = TableRow([ tr('Title'), tr('ID'), tr('Category'), tr('Sub Category'), tr('Layer type'), tr('Data type'), tr('Unit') ], header=True) table_body.append(header) plugins_dict = dict([(pretty_function_name(p), p) for p in FunctionProvider.plugins]) not_found_value = 'N/A' for key, func in plugins_dict.iteritems(): for requirement in requirements_collect(func): dict_found = { 'title': False, 'id': False, 'category': False, 'subcategory': False, 'layertype': False, 'datatype': False, 'unit': False } dict_req = parse_single_requirement(str(requirement)) # If the impact function is disabled, do not show it if dict_req.get('disabled', False): continue for myKey in dict_found.iterkeys(): myFilter = dict_filter.get(myKey, []) if myKey == 'title': myValue = str(get_function_title(func)) elif myKey == 'id': myValue = str(key) else: myValue = dict_req.get(myKey, not_found_value) if myFilter != []: for myKeyword in myFilter: if type(myValue) == type(str()): if myValue == myKeyword: dict_found[myKey] = True break elif type(myValue) == type(list()): if myKeyword in myValue: dict_found[myKey] = True break else: if myValue.find(str(myKeyword)) != -1: dict_found[myKey] = True break else: dict_found[myKey] = True add_row = True for found_value in dict_found.itervalues(): if not found_value: add_row = False break if add_row: row = [] row.append(TableCell(get_function_title(func), header=True)) row.append(key) for myKey in atts: myValue = pretty_string( dict_req.get(myKey, not_found_value)) row.append(myValue) table_body.append(TableRow(row)) cw = 100 / 7 table_col_width = [ str(cw) + '%', str(cw) + '%', str(cw) + '%', str(cw) + '%', str(cw) + '%', str(cw) + '%', str(cw) + '%' ] table = Table(table_body, col_width=table_col_width) table.caption = tr('Available Impact Functions') return table
def run(self, layers): """Experimental impact function. :param layers: List of layers expected to contain at least: H: Polygon layer of inundation areas E: Vector layer of roads :type layers: list :returns: A new line layer with inundated roads marked. :type: safe_layer """ target_field = self.parameters['target_field'] road_type_field = self.parameters['road_type_field'] threshold_min = self.parameters['min threshold [m]'] threshold_max = self.parameters['max threshold [m]'] if threshold_min > threshold_max: message = tr( 'The minimal threshold is greater then the maximal specified ' 'threshold. Please check the values.') raise GetDataError(message) # Extract data H = get_hazard_layer(layers) # Flood E = get_exposure_layer(layers) # Roads question = get_question(H.get_name(), E.get_name(), self) H = H.get_layer() E = E.get_layer() # reproject self.extent to the hazard projection hazard_crs = H.crs() hazard_authid = hazard_crs.authid() if hazard_authid == 'EPSG:4326': viewport_extent = self.extent else: geo_crs = QgsCoordinateReferenceSystem() geo_crs.createFromSrid(4326) viewport_extent = extent_to_geo_array(QgsRectangle(*self.extent), geo_crs, hazard_crs) # Align raster extent and viewport # assuming they are both in the same projection raster_extent = H.dataProvider().extent() clip_xmin = raster_extent.xMinimum() # clip_xmax = raster_extent.xMaximum() clip_ymin = raster_extent.yMinimum() # clip_ymax = raster_extent.yMaximum() if viewport_extent[0] > clip_xmin: clip_xmin = viewport_extent[0] if viewport_extent[1] > clip_ymin: clip_ymin = viewport_extent[1] # TODO: Why have these two clauses when they are not used? # Commenting out for now. # if viewport_extent[2] < clip_xmax: # clip_xmax = viewport_extent[2] # if viewport_extent[3] < clip_ymax: # clip_ymax = viewport_extent[3] height = ((viewport_extent[3] - viewport_extent[1]) / H.rasterUnitsPerPixelY()) height = int(height) width = ((viewport_extent[2] - viewport_extent[0]) / H.rasterUnitsPerPixelX()) width = int(width) raster_extent = H.dataProvider().extent() xmin = raster_extent.xMinimum() xmax = raster_extent.xMaximum() ymin = raster_extent.yMinimum() ymax = raster_extent.yMaximum() x_delta = (xmax - xmin) / H.width() x = xmin for i in range(H.width()): if abs(x - clip_xmin) < x_delta: # We have found the aligned raster boundary break x += x_delta _ = i y_delta = (ymax - ymin) / H.height() y = ymin for i in range(H.width()): if abs(y - clip_ymin) < y_delta: # We have found the aligned raster boundary break y += y_delta clip_extent = [x, y, x + width * x_delta, y + height * y_delta] # Clip and polygonize small_raster = clip_raster(H, width, height, QgsRectangle(*clip_extent)) (flooded_polygon_inside, flooded_polygon_outside) = polygonize_gdal(small_raster, threshold_min, threshold_max) # Filter geometry and data using the extent extent = QgsRectangle(*self.extent) request = QgsFeatureRequest() request.setFilterRect(extent) if flooded_polygon_inside is None: message = tr( 'There are no objects in the hazard layer with "value">%s.' 'Please check the value or use other extent.' % (threshold_min, )) raise GetDataError(message) # reproject the flood polygons to exposure projection exposure_crs = E.crs() exposure_authid = exposure_crs.authid() if hazard_authid != exposure_authid: flooded_polygon_inside = reproject_vector_layer( flooded_polygon_inside, E.crs()) flooded_polygon_outside = reproject_vector_layer( flooded_polygon_outside, E.crs()) # Clip exposure by the extent # extent_as_polygon = QgsGeometry().fromRect(extent) # no need to clip since It is using a bbox request # line_layer = clip_by_polygon( # E, # extent_as_polygon # ) # Find inundated roads, mark them line_layer = split_by_polygon_in_out(E, flooded_polygon_inside, flooded_polygon_outside, target_field, 1, request) target_field_index = line_layer.dataProvider().\ fieldNameIndex(target_field) # Generate simple impact report epsg = get_utm_epsg(self.extent[0], self.extent[1]) output_crs = QgsCoordinateReferenceSystem(epsg) transform = QgsCoordinateTransform(E.crs(), output_crs) road_len = flooded_len = 0 # Length of roads roads_by_type = dict() # Length of flooded roads by types roads_data = line_layer.getFeatures() road_type_field_index = line_layer.fieldNameIndex(road_type_field) for road in roads_data: attributes = road.attributes() road_type = attributes[road_type_field_index] if road_type.__class__.__name__ == 'QPyNullVariant': road_type = tr('Other') geom = road.geometry() geom.transform(transform) length = geom.length() road_len += length if road_type not in roads_by_type: roads_by_type[road_type] = {'flooded': 0, 'total': 0} roads_by_type[road_type]['total'] += length if attributes[target_field_index] == 1: flooded_len += length roads_by_type[road_type]['flooded'] += length table_body = [ question, TableRow([ tr('Road Type'), tr('Flooded in the threshold (m)'), tr('Total (m)') ], header=True), TableRow([tr('All'), int(flooded_len), int(road_len)]) ] table_body.append(TableRow(tr('Breakdown by road type'), header=True)) for t, v in roads_by_type.iteritems(): table_body.append(TableRow([t, int(v['flooded']), int(v['total'])])) impact_summary = Table(table_body).toNewlineFreeString() map_title = tr('Roads inundated') style_classes = [ dict(label=tr('Not Inundated'), value=0, colour='#1EFC7C', transparency=0, size=0.5), dict(label=tr('Inundated'), value=1, colour='#F31A1C', transparency=0, size=0.5) ] style_info = dict(target_field=target_field, style_classes=style_classes, style_type='categorizedSymbol') # Convert QgsVectorLayer to inasafe layer and return it line_layer = Vector(data=line_layer, name=tr('Flooded roads'), keywords={ 'impact_summary': impact_summary, 'map_title': map_title, 'target_field': target_field }, style_info=style_info) return line_layer