def test_analysis_summary(self): """Test we can aggregate the aggregate hazard to the analysis.""" aggregate_hazard = load_test_vector_layer( 'gisv4', 'intermediate', 'aggregate_classified_hazard_summary.geojson') aggregate_hazard.keywords['hazard_keywords'] = { 'classification': 'generic_hazard_classes' } analysis = load_test_vector_layer('gisv4', 'intermediate', 'analysis.geojson', clone=True) number_of_fields = analysis.fields().count() layer = analysis_summary(aggregate_hazard, analysis) check_inasafe_fields(layer) fields = aggregate_hazard.keywords['inasafe_fields'] hazard_class = fields[hazard_class_field['key']] hazard_class_index = aggregate_hazard.fieldNameIndex(hazard_class) unique_hazard = aggregate_hazard.uniqueValues(hazard_class_index) # expected number of fields: # - one field for each hazard class # - 2 fields for analysis id and analysis name # - 4 fields for total affected, not_affected, not exposed and total self.assertEqual(layer.fields().count(), len(unique_hazard) + number_of_fields + 4)
def test_profiling(self): """Test running impact function on test data.""" hazard_layer = load_test_vector_layer('gisv4', 'hazard', 'classified_vector.geojson') exposure_layer = load_test_vector_layer('gisv4', 'exposure', 'building-points.geojson') aggregation_layer = load_test_vector_layer('gisv4', 'aggregation', 'small_grid.geojson') # Set up impact function impact_function = ImpactFunction() impact_function.aggregation = aggregation_layer impact_function.exposure = exposure_layer impact_function.hazard = hazard_layer status, message = impact_function.run() self.assertEqual(ANALYSIS_FAILED_BAD_INPUT, status, message) impact_function.prepare() status, message = impact_function.run() self.assertEqual(ANALYSIS_SUCCESS, status, message) message = impact_function.performance_log_message().to_text() expected_result = get_control_text('test-profiling-logs.txt') for line in expected_result: line = line.replace('\n', '') if line == '' or line == '-': continue self.assertIn(line, message) # Notes(IS): For some unknown reason I need to do this to make # test_provenance pass del hazard_layer
def test_is_polygonal_layer(self): """Test we can get the correct attributes back""" # Polygon layer layer = load_test_vector_layer( 'aggregation', 'district_osm_jakarta.geojson', clone=True ) message = 'isPolygonLayer, %s layer should be polygonal' % layer self.assertTrue(is_polygon_layer(layer), message) # Point layer layer = load_test_vector_layer('hazard', 'volcano_point.geojson') message = '%s layer should be polygonal' % layer self.assertFalse(is_polygon_layer(layer), message) # Raster layer layer = clone_raster_layer( name='earthquake', extension='.tif', include_keywords=True, source_directory=standard_data_path('hazard') ) message = ('%s raster layer should not be polygonal' % layer) self.assertFalse(is_polygon_layer(layer), message)
def test_aggregation_summary(self): """Test we can aggregate the aggregate hazard to the aggregation.""" aggregate_hazard = load_test_vector_layer( 'gisv4', 'intermediate', 'aggregate_classified_hazard_summary.geojson') aggregation = load_test_vector_layer( 'gisv4', 'aggregation', 'aggregation_cleaned.geojson', clone=True) number_of_fields = aggregation.fields().count() layer = aggregation_summary(aggregate_hazard, aggregation) check_inasafe_fields(layer) # I need the number of unique exposure pattern = exposure_count_field['key'] pattern = pattern.replace('%s', '') unique_exposure = [] inasafe_fields = aggregate_hazard.keywords['inasafe_fields'] for key, name_field in inasafe_fields.iteritems(): if key.endswith(pattern): unique_exposure.append(key.replace(pattern, '')) self.assertEqual( layer.fields().count(), len(unique_exposure) + number_of_fields + 1 )
def test_old_fields_keywords(self): """The IF is not ready with we have some wrong inasafe_fields.""" hazard_layer = load_test_vector_layer( 'gisv4', 'hazard', 'classified_vector.geojson') exposure_layer = load_test_vector_layer( 'gisv4', 'exposure', 'building-points.geojson', clone=True) aggregation_layer = load_test_vector_layer( 'gisv4', 'aggregation', 'small_grid.geojson') impact_function = ImpactFunction() impact_function.aggregation = aggregation_layer impact_function.exposure = exposure_layer impact_function.hazard = hazard_layer status, message = impact_function.prepare() # The layer should be fine. self.assertEqual(PREPARE_SUCCESS, status, message) # Now, we remove one field exposure_layer.startEditing() field = exposure_layer.keywords['inasafe_fields'].values()[0] index = exposure_layer.fieldNameIndex(field) exposure_layer.deleteAttribute(index) exposure_layer.commitChanges() # It shouldn't be fine as we removed one field which # was in inasafe_fields status, message = impact_function.prepare() self.assertNotEqual(PREPARE_SUCCESS, status, message)
def test_profiling(self): """Test running impact function on test data.""" hazard_layer = load_test_vector_layer( 'gisv4', 'hazard', 'classified_vector.geojson') exposure_layer = load_test_vector_layer( 'gisv4', 'exposure', 'building-points.geojson') aggregation_layer = load_test_vector_layer( 'gisv4', 'aggregation', 'small_grid.geojson') # Set up impact function impact_function = ImpactFunction() impact_function.aggregation = aggregation_layer impact_function.exposure = exposure_layer impact_function.hazard = hazard_layer status, message = impact_function.run() self.assertEqual(ANALYSIS_FAILED_BAD_INPUT, status, message) impact_function.prepare() status, message = impact_function.run() self.assertEqual(ANALYSIS_SUCCESS, status, message) message = impact_function.performance_log_message().to_text() expected_result = get_control_text( 'test-profiling-logs.txt') for line in expected_result: line = line.replace('\n', '') if line == '' or line == '-': continue self.assertIn(line, message) # Notes(IS): For some unknown reason I need to do this to make # test_provenance pass del hazard_layer
def test_pre_processors_earthquake_contour(self): """Test the pre_processors_earthquake_contour""" hazard_layer = load_test_raster_layer('gisv4', 'hazard', 'earthquake.asc') exposure_layer = load_test_vector_layer('gisv4', 'exposure', 'building-points.geojson') impact_function = ImpactFunction() impact_function.exposure = exposure_layer impact_function.hazard = hazard_layer impact_function.crs = QgsCoordinateReferenceSystem(4326) status, message = impact_function.prepare() self.assertEqual(PREPARE_SUCCESS, status, message) self.assertTrue( pre_processor_earthquake_contour['condition'](impact_function)) hazard_layer = load_test_raster_layer('hazard', 'classified_flood_20_20.asc') exposure_layer = load_test_vector_layer('gisv4', 'exposure', 'places.geojson') impact_function = ImpactFunction() impact_function.exposure = exposure_layer impact_function.hazard = hazard_layer impact_function.crs = QgsCoordinateReferenceSystem(4326) status, message = impact_function.prepare() self.assertEqual(PREPARE_SUCCESS, status, message) # not ok, since the hazard is flood, not earthquake self.assertFalse( pre_processor_earthquake_contour['condition'](impact_function))
def test_size_needed(self): """Test we can add the size when it is needed.""" # A building layer should be always false. layer = load_test_vector_layer( 'gisv4', 'exposure', 'buildings.geojson') layer.keywords['inasafe_fields'] = { population_count_field['key']: population_count_field['field_name'] } self.assertFalse(_size_is_needed(layer)) layer.keywords['inasafe_fields'] = { female_ratio_field['key']: female_ratio_field['field_name'] } self.assertFalse(_size_is_needed(layer)) # But a road layer should be true only if it has a absolute value. layer = load_test_vector_layer( 'gisv4', 'exposure', 'roads.geojson') layer.keywords['inasafe_fields'] = { population_count_field['key']: population_count_field['field_name'] } self.assertTrue(_size_is_needed(layer)) layer.keywords['inasafe_fields'] = { female_ratio_field['key']: female_ratio_field['field_name'] } self.assertFalse(_size_is_needed(layer))
def test_size_needed(self): """Test we can add the size when it is needed.""" # A building layer should be always false. layer = load_test_vector_layer('gisv4', 'exposure', 'buildings.geojson') layer.keywords['inasafe_fields'] = { population_count_field['key']: population_count_field['field_name'] } self.assertFalse(_size_is_needed(layer)) layer.keywords['inasafe_fields'] = { female_ratio_field['key']: female_ratio_field['field_name'] } self.assertFalse(_size_is_needed(layer)) # But a road layer should be true only if it has a absolute value. layer = load_test_vector_layer('gisv4', 'exposure', 'roads.geojson') layer.keywords['inasafe_fields'] = { population_count_field['key']: population_count_field['field_name'] } self.assertTrue(_size_is_needed(layer)) layer.keywords['inasafe_fields'] = { female_ratio_field['key']: female_ratio_field['field_name'] } self.assertFalse(_size_is_needed(layer))
def test_pre_processors_nearby_places(self): """Test the pre_processors_nearby_places""" hazard_layer = load_test_raster_layer('gisv4', 'hazard', 'earthquake.asc') exposure_layer = load_test_vector_layer('gisv4', 'exposure', 'building-points.geojson') impact_function = ImpactFunction() impact_function.exposure = exposure_layer impact_function.hazard = hazard_layer impact_function.crs = QgsCoordinateReferenceSystem(4326) status, message = impact_function.prepare() self.assertEqual(PREPARE_SUCCESS, status, message) # The exposure is not place but buildings self.assertFalse( pre_processors_nearby_places['condition'](impact_function)) hazard_layer = load_test_raster_layer('gisv4', 'hazard', 'earthquake.asc') exposure_layer = load_test_vector_layer('gisv4', 'exposure', 'places.geojson') impact_function = ImpactFunction() impact_function.exposure = exposure_layer impact_function.hazard = hazard_layer impact_function.crs = QgsCoordinateReferenceSystem(4326) status, message = impact_function.prepare() self.assertEqual(PREPARE_SUCCESS, status, message) # EQ on places, it must be OK. self.assertTrue( pre_processors_nearby_places['condition'](impact_function))
def test_pre_processors_earthquake_contour(self): """Test the pre_processors_earthquake_contour""" hazard_layer = load_test_raster_layer( 'gisv4', 'hazard', 'earthquake.asc') exposure_layer = load_test_vector_layer( 'gisv4', 'exposure', 'building-points.geojson') impact_function = ImpactFunction() impact_function.exposure = exposure_layer impact_function.hazard = hazard_layer impact_function.crs = QgsCoordinateReferenceSystem(4326) status, message = impact_function.prepare() self.assertEqual(PREPARE_SUCCESS, status, message) self.assertTrue( pre_processor_earthquake_contour['condition'](impact_function)) hazard_layer = load_test_raster_layer( 'hazard', 'classified_flood_20_20.asc') exposure_layer = load_test_vector_layer( 'gisv4', 'exposure', 'places.geojson') impact_function = ImpactFunction() impact_function.exposure = exposure_layer impact_function.hazard = hazard_layer impact_function.crs = QgsCoordinateReferenceSystem(4326) status, message = impact_function.prepare() self.assertEqual(PREPARE_SUCCESS, status, message) # not ok, since the hazard is flood, not earthquake self.assertFalse( pre_processor_earthquake_contour['condition'](impact_function))
def test_pre_processors_nearby_places(self): """Test the pre_processors_nearby_places""" hazard_layer = load_test_raster_layer( 'gisv4', 'hazard', 'earthquake.asc') exposure_layer = load_test_vector_layer( 'gisv4', 'exposure', 'building-points.geojson') impact_function = ImpactFunction() impact_function.exposure = exposure_layer impact_function.hazard = hazard_layer impact_function.crs = QgsCoordinateReferenceSystem(4326) status, message = impact_function.prepare() self.assertEqual(PREPARE_SUCCESS, status, message) # The exposure is not place but buildings self.assertFalse( pre_processors_nearby_places['condition'](impact_function)) hazard_layer = load_test_raster_layer( 'gisv4', 'hazard', 'earthquake.asc') exposure_layer = load_test_vector_layer( 'gisv4', 'exposure', 'places.geojson') impact_function = ImpactFunction() impact_function.exposure = exposure_layer impact_function.hazard = hazard_layer impact_function.crs = QgsCoordinateReferenceSystem(4326) status, message = impact_function.prepare() self.assertEqual(PREPARE_SUCCESS, status, message) # EQ on places, it must be OK. self.assertTrue( pre_processors_nearby_places['condition'](impact_function))
def test_old_fields_keywords(self): """The IF is not ready with we have some wrong inasafe_fields.""" hazard_layer = load_test_vector_layer('gisv4', 'hazard', 'classified_vector.geojson') exposure_layer = load_test_vector_layer('gisv4', 'exposure', 'building-points.geojson', clone=True) aggregation_layer = load_test_vector_layer('gisv4', 'aggregation', 'small_grid.geojson') impact_function = ImpactFunction() impact_function.aggregation = aggregation_layer impact_function.exposure = exposure_layer impact_function.hazard = hazard_layer status, message = impact_function.prepare() # The layer should be fine. self.assertEqual(PREPARE_SUCCESS, status, message) # Now, we remove one field exposure_layer.startEditing() field = exposure_layer.keywords['inasafe_fields'].values()[0] index = exposure_layer.fieldNameIndex(field) exposure_layer.deleteAttribute(index) exposure_layer.commitChanges() # It shouldn't be fine as we removed one field which # was in inasafe_fields status, message = impact_function.prepare() self.assertNotEqual(PREPARE_SUCCESS, status, message)
def test_aggregation_summary(self): """Test we can aggregate the aggregate hazard to the aggregation.""" aggregate_hazard = load_test_vector_layer( 'gisv4', 'intermediate', 'aggregate_classified_hazard_summary.geojson') aggregation = load_test_vector_layer( 'gisv4', 'aggregation', 'aggregation_cleaned.geojson', clone=True) number_of_fields = aggregation.fields().count() layer = aggregation_summary(aggregate_hazard, aggregation) check_inasafe_fields(layer) # I need the number of unique exposure pattern = exposure_count_field['key'] pattern = pattern.replace('%s', '') unique_exposure = [] inasafe_fields = aggregate_hazard.keywords['inasafe_fields'] for key, name_field in inasafe_fields.iteritems(): if key.endswith(pattern): unique_exposure.append(key.replace(pattern, '')) self.assertEqual( layer.fields().count(), len(unique_exposure) + number_of_fields + 1 )
def test_provenance_without_aggregation(self): """Test provenance of impact function without aggregation.""" hazard_layer = load_test_vector_layer( 'gisv4', 'hazard', 'classified_vector.geojson') exposure_layer = load_test_vector_layer( 'gisv4', 'exposure', 'building-points.geojson') hazard = definition(hazard_layer.keywords['hazard']) exposure = definition(exposure_layer.keywords['exposure']) hazard_category = definition(hazard_layer.keywords['hazard_category']) expected_provenance = { 'gdal_version': gdal.__version__, 'host_name': gethostname(), 'map_title': get_map_title(hazard, exposure, hazard_category), 'map_legend_title': exposure['layer_legend_title'], 'inasafe_version': get_version(), 'pyqt_version': PYQT_VERSION_STR, 'qgis_version': QGis.QGIS_VERSION, 'qt_version': QT_VERSION_STR, 'user': getpass.getuser(), 'os': readable_os_version(), 'aggregation_layer': None, 'aggregation_layer_id': None, 'exposure_layer': exposure_layer.source(), 'exposure_layer_id': exposure_layer.id(), 'hazard_layer': hazard_layer.source(), 'hazard_layer_id': hazard_layer.id(), 'analysis_question': get_analysis_question(hazard, exposure), 'aggregation_keywords': None, 'exposure_keywords': deepcopy(exposure_layer.keywords), 'hazard_keywords': deepcopy(hazard_layer.keywords), } # Set up impact function impact_function = ImpactFunction() impact_function.exposure = exposure_layer impact_function.hazard = hazard_layer status, message = impact_function.prepare() self.assertEqual(PREPARE_SUCCESS, status, message) status, message = impact_function.run() self.assertEqual(ANALYSIS_SUCCESS, status, message) self.maxDiff = None expected_provenance.update({ 'action_checklist': impact_function.action_checklist(), 'analysis_extent': impact_function.analysis_extent.exportToWkt(), 'impact_function_name': impact_function.name, 'impact_function_title': impact_function.title, 'notes': impact_function.notes(), 'requested_extent': impact_function.requested_extent, 'data_store_uri': impact_function.datastore.uri_path, 'start_datetime': impact_function.start_datetime, 'end_datetime': impact_function.end_datetime, 'duration': impact_function.duration }) self.assertDictEqual(expected_provenance, impact_function.provenance)
def test_clip_vector(self): """Test we can smart clip two layers, like indivisible polygons.""" analysis = load_test_vector_layer('gisv4', 'analysis', 'analysis.geojson') exposure = load_test_vector_layer('gisv4', 'exposure', 'buildings.geojson') layer = smart_clip(exposure, analysis) self.assertEqual(layer.featureCount(), 9)
def test_clip_vector(self): """Test we can smart clip two layers, like indivisible polygons.""" analysis = load_test_vector_layer( 'gisv4', 'analysis', 'analysis.geojson') exposure = load_test_vector_layer( 'gisv4', 'exposure', 'buildings.geojson') layer = smart_clip(exposure, analysis) self.assertEqual(layer.featureCount(), 9)
def test_clip_vector(self): """Test we can clip two layers, like buildings and aggregation.""" aggregation = load_test_vector_layer( 'gisv4', 'aggregation', 'small_grid.geojson') exposure = load_test_vector_layer( 'gisv4', 'exposure', 'building-points.geojson') layer = clip(exposure, aggregation) self.assertEqual(layer.featureCount(), 9)
def test_clip_vector(self): """Test we can clip two layers, like buildings and aggregation.""" aggregation = load_test_vector_layer( 'gisv4', 'aggregation', 'small_grid.geojson') exposure = load_test_vector_layer( 'gisv4', 'exposure', 'building-points.geojson') layer = clip(exposure, aggregation) self.assertEqual(layer.featureCount(), 9)
def test_impact_function_behaviour(self): """Test behaviour of impact function.""" hazard_layer = load_test_vector_layer('hazard', 'flood_multipart_polygons.shp') exposure_layer = load_test_vector_layer('exposure', 'roads.shp') impact_function = ImpactFunction() impact_function.exposure = exposure_layer impact_function.hazard = hazard_layer impact_function.prepare() self.assertEqual(impact_function.name, 'Flood Polygon On Roads Line') self.assertEqual(impact_function.title, 'be affected')
def test_exposure_summary_table_productivity(self): """Test we can produce the breakdown for the exposure type.""" aggregate_hazard = load_test_vector_layer( 'gisv4', 'intermediate', 'summaries', 'land_cover_aggregate_hazard_impacted.geojson') aggregate_hazard.keywords['hazard_keywords'] = { 'hazard': 'generic', 'classification': 'generic_hazard_classes' } aggregate_hazard.keywords['exposure_keywords'] = { 'exposure': 'land_cover' } exposure_summary = load_test_vector_layer( 'gisv4', 'intermediate', 'summaries', 'land_cover_exposure_summary.geojson' ) # I need the number of unique exposure unique_exposure = read_dynamic_inasafe_field( aggregate_hazard.keywords['inasafe_fields'], exposure_count_field) # I need the number of unique hazard fields = aggregate_hazard.keywords['inasafe_fields'] hazard_class = fields[hazard_class_field['key']] hazard_class_index = aggregate_hazard.fields().lookupField(hazard_class) unique_hazard = aggregate_hazard.uniqueValues(hazard_class_index) layer = exposure_summary_table(aggregate_hazard, exposure_summary) check_inasafe_fields(layer) self.assertEqual(len(unique_exposure), layer.featureCount()) # We should have # one column per hazard # 1. one for the exposure # 2. one for total affected # 3. one for total not affected # 4. one for total not exposed # 5. one for total # 6. one for affected productivity # 7. one for affected production cost # 8. one for affected production value self.assertEqual(layer.fields().count(), len(unique_hazard) + 8)
def test_impact_function_behaviour(self): """Test behaviour of impact function.""" hazard_layer = load_test_vector_layer( 'hazard', 'flood_multipart_polygons.shp') exposure_layer = load_test_vector_layer('exposure', 'roads.shp') impact_function = ImpactFunction() impact_function.exposure = exposure_layer impact_function.hazard = hazard_layer impact_function.prepare() self.assertEqual(impact_function.name, 'Flood Polygon On Road Line') self.assertEqual(impact_function.title, 'be affected')
def test_aggregation_multi_exposure(self): """Test we can merge two aggregation summary layer.""" aggregation_summary_buildings = load_test_vector_layer( 'gisv4', 'intermediate', 'summaries', 'multi_exposure_aggregation_buildings.geojson' ) aggregation_summary_roads = load_test_vector_layer( 'gisv4', 'intermediate', 'summaries', 'multi_exposure_aggregation_roads.geojson' ) aggregation = load_test_vector_layer( 'gisv4', 'aggregation', 'aggregation_cleaned.geojson', clone=True) aggregation = multi_exposure_aggregation_summary( aggregation, [ aggregation_summary_buildings, aggregation_summary_roads ] ) concatenation = [] # This test checks only the first row of each layer. Not the best test. iterator = aggregation_summary_buildings.getFeatures() feature = next(iterator) attributes = feature.attributes() self.assertEqual(len(attributes), 10) concatenation.extend(attributes[3:]) # We drop female, aggr id, name iterator = aggregation_summary_roads.getFeatures() feature = next(iterator) attributes = feature.attributes() self.assertEqual(len(attributes), 6) concatenation.extend(attributes[3:]) # We drop female, aggr id, name iterator = aggregation.getFeatures() feature = next(iterator) attributes = feature.attributes() self.assertEqual(len(attributes), 12) # Concatenation is a subset of attributes self.assertTrue(set(concatenation) < set(attributes))
def test_analysis_earthquake_summary(self): """Test we can compute summary after an EQ on population.""" hazard = load_test_raster_layer('gisv4', 'hazard', 'earthquake.asc') exposure = load_test_raster_layer('gisv4', 'exposure', 'raster', 'population.asc') aggregation = load_test_vector_layer('gisv4', 'aggregation', 'small_grid.geojson') impact_function = ImpactFunction() impact_function.hazard = hazard impact_function.exposure = exposure impact_function.aggregation = aggregation status, message = impact_function.prepare() self.assertEqual(PREPARE_SUCCESS, status, message) status, message = impact_function.run() self.assertEqual(ANALYSIS_SUCCESS, status, message) layer = impact_function.analysis_impacted classification = hazard.keywords['classification'] classes = definition(classification)['classes'] for hazard_class in classes: field_name = hazard_count_field['field_name'] % hazard_class['key'] message = '%s is not found in the EQ summary layer.' % field_name self.assertNotEqual(-1, layer.fieldNameIndex(field_name), message) check_inasafe_fields(impact_function.analysis_impacted) check_inasafe_fields(impact_function.aggregation_summary)
def test_init(self): """Test FieldMappingTab initialization.""" layer = load_test_vector_layer( 'gisv4', 'aggregation', 'small_grid_complex.geojson', with_keywords=False, clone_to_memory=True) layer.keywords = {} field_mapping = FieldMappingTab(age_ratio_group, PARENT, IFACE) field_mapping.set_layer(layer) # Empty keywords should give empty for all aspect parameter_values = field_mapping.get_parameter_value() self.assertEqual(parameter_values['fields'], {}) for v in parameter_values['values'].values(): self.assertIsNone(v) # Make sure all keys exist fields_keys = parameter_values['fields'].keys() values_keys = parameter_values['values'].keys() age_ratio_fields_keys = [field['key'] for field in age_ratio_group[ 'fields']] for key in fields_keys + values_keys: self.assertIn(key, age_ratio_fields_keys) # Check field list fields = [] for index in xrange(field_mapping.field_list.count()): fields.append(field_mapping.field_list.item(index)) labels = [i.text() for i in fields] for field in layer.dataProvider().fields(): if field.type() not in qvariant_numbers: continue self.assertIn(field.name(), labels)
def test_minimum_needs_calculator(self): """Test behaviour of the minimum needs function.""" dialog = NeedsCalculatorDialog(PARENT) layer = load_test_vector_layer('other', 'minimum_needs.shp') QgsProject.instance().addMapLayers([layer]) # Set selected layer and displaced field dialog.layer.setLayer(layer) dialog.displaced.setField('displaced') # run minimum needs function dialog.accept() # get output layer layer = dialog.result_layer assert layer is not None field_names = [field.name() for field in layer.fields()] for feature in layer.getFeatures(): value = [attribute for attribute in feature.attributes()] actual_attributes = dict(list(zip(field_names, value))) expected_attributes = { 'displaced': 1000, 'minimum_needs__rice': 2800, 'minimum_needs__drinking_water': 17500, 'minimum_needs__clean_water': 67000, 'minimum_needs__family_kits': 200, 'minimum_needs__toilets': 50 } self.assertDictEqual(byteify(expected_attributes), byteify(actual_attributes))
def test_ok_button(self): """Test behaviour of Ok button.""" # Test Ok button without any input in the combo box dialog = NeedsCalculatorDialog(PARENT) ok_button = dialog.button_box.button(QtWidgets.QDialogButtonBox.Ok) self.assertFalse(ok_button.isEnabled()) # Close because this is a modal dialog dialog.reject() input_layer = load_test_vector_layer('other', 'minimum_needs.shp') QgsProject.instance().addMapLayers([input_layer]) # Open the dialog again dialog = NeedsCalculatorDialog(PARENT) ok_button = dialog.button_box.button(QtWidgets.QDialogButtonBox.Ok) # Test Ok button with layer and displaced field # selected in the combo box dialog.layer.setLayer(input_layer) dialog.displaced.setField('displaced') self.assertTrue(ok_button.isEnabled())
def test_analysis_earthquake_summary(self): """Test we can compute summary after an EQ on population.""" hazard = load_test_raster_layer('gisv4', 'hazard', 'earthquake.asc') exposure = load_test_raster_layer( 'gisv4', 'exposure', 'raster', 'population.asc') aggregation = load_test_vector_layer( 'gisv4', 'aggregation', 'small_grid.geojson') impact_function = ImpactFunction() impact_function.hazard = hazard impact_function.exposure = exposure impact_function.aggregation = aggregation status, message = impact_function.prepare() self.assertEqual(PREPARE_SUCCESS, status, message) status, message = impact_function.run() self.assertEqual(ANALYSIS_SUCCESS, status, message) layer = impact_function.analysis_impacted classification = hazard.keywords['classification'] classes = definition(classification)['classes'] for hazard_class in classes: field_name = hazard_count_field['field_name'] % hazard_class['key'] message = '%s is not found in the EQ summary layer.' % field_name self.assertNotEqual(-1, layer.fieldNameIndex(field_name), message) check_inasafe_fields(impact_function.analysis_impacted) check_inasafe_fields(impact_function.aggregation_summary)
def test_own_id_column(self): """Test if we can re-use the column ID from the user.""" layer = load_test_vector_layer( 'gisv4', 'exposure', 'buildings.geojson', clone=True) # This layer is set to use custom ID column. We should have the values # after preparing the vector layer. field = layer.fieldNameIndex(exposure_id_field['field_name']) self.assertNotEqual(-1, field) unique_values_before = layer.uniqueValues(field) self.assertEqual( unique_values_before, [10, 11, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110]) _add_id_column(layer) field = layer.fieldNameIndex(exposure_id_field['field_name']) self.assertNotEqual(-1, field) unique_values_after = layer.uniqueValues(field) self.assertEqual(unique_values_after, unique_values_before) # Let's remove the keyword now to use the auto-increment ID del layer.keywords['inasafe_fields'][exposure_id_field['key']] _add_id_column(layer) field = layer.fieldNameIndex(exposure_id_field['field_name']) self.assertNotEqual(-1, field) unique_values_automatic = layer.uniqueValues(field) self.assertNotEqual(unique_values_automatic, unique_values_before) self.assertEqual(unique_values_automatic, range(layer.featureCount()))
def test_sum_fields(self): """Test sum_fields method.""" layer = load_test_vector_layer( 'gisv4', 'exposure', 'population_multi_fields.geojson', clone=True) sum_fields( layer, exposure_id_field['key'], ['F_0_4', 'F_5_9', 'F_9_15']) exposure_id__idx = layer.fieldNameIndex( exposure_id_field['field_name']) F_0_4__idx = layer.fieldNameIndex('F_0_4') F_5_9__idx = layer.fieldNameIndex('F_5_9') F_9_15__idx = layer.fieldNameIndex('F_9_15') for feature in layer.getFeatures(): sum_value = ( feature[F_0_4__idx] + feature[F_5_9__idx] + feature[ F_9_15__idx]) self.assertEqual(feature[exposure_id__idx], sum_value) new_field__idx = layer.fieldNameIndex(female_count_field['field_name']) # Check if the new field doesn't exist self.assertEqual(new_field__idx, -1) sum_fields(layer, female_count_field['key'], ['F_0_4', 'F_5_9']) new_field__idx = layer.fieldNameIndex(female_count_field['field_name']) for feature in layer.getFeatures(): sum_value = (feature[F_0_4__idx] + feature[F_5_9__idx]) self.assertEqual(feature[new_field__idx], sum_value)
def test_copy_vector_layer(self): """Test we can copy a vector layer.""" layer = load_test_vector_layer('exposure', 'buildings.shp') new_layer = create_memory_layer( 'New layer', layer.geometryType(), layer.crs(), layer.fields()) new_layer.keywords = layer.keywords copy_layer(layer, new_layer) self.assertEqual(layer.featureCount(), new_layer.featureCount()) self.assertEqual( len(layer.fields().toList()), len(new_layer.fields().toList())) expected = len(new_layer.fields().toList()) + 1 new_fields = {'STRUCTURE': 'my_new_field'} copy_fields(new_layer, new_fields) self.assertEqual( len(new_layer.fields().toList()), expected) self.assertGreater(new_layer.fieldNameIndex('my_new_field'), -1) remove_fields(new_layer, ['STRUCTURE', 'OSM_TYPE']) self.assertEqual( len(new_layer.fields().toList()), expected - 2) self.assertEqual(new_layer.fieldNameIndex('STRUCTURE'), -1) self.assertEqual(new_layer.fieldNameIndex('OSM_TYPE'), -1) _add_id_column(new_layer) field_name = exposure_id_field['field_name'] self.assertGreater(new_layer.fieldNameIndex(field_name), -1)
def test_recompute_counts(self): """Test we can recompute counts in a layer.""" layer = load_test_vector_layer( 'gisv4', 'exposure', 'population.geojson', clone=True) self.assertIn( female_count_field['key'], layer.keywords['inasafe_fields']) layer = prepare_vector_layer(layer) layer = from_counts_to_ratios(layer) self.assertIn( female_count_field['key'], layer.keywords['inasafe_fields']) self.assertIn( female_ratio_field['key'], layer.keywords['inasafe_fields']) # Check that each feature has correct ratio for feature in layer.getFeatures(): female_count = feature[female_count_field['field_name']] population_count = feature[population_count_field['field_name']] manual_ratio = female_count / float(population_count) computing_ratio = feature[female_ratio_field['field_name']] diff = abs(manual_ratio - computing_ratio) message = 'The ratio difference is too big, diff = %s' % diff self.assertTrue(diff < 10 ** -2, message)
def test_size_post_processor(self): """Test size, size rate, productivity post processor.""" impact_layer = load_test_vector_layer( 'impact', 'indivisible_polygon_impact.geojson', clone_to_memory=True) impact_layer.keywords['exposure_keywords'] = {'exposure': 'population'} self.assertIsNotNone(impact_layer) # Test the size post processor. result, message = run_single_post_processor(impact_layer, post_processor_size) self.assertTrue(result, message) # Check if new field is added impact_fields = impact_layer.dataProvider().fieldNameMap().keys() self.assertIn(size_field['field_name'], impact_fields) # Test the size rate post processor. result, message = run_single_post_processor(impact_layer, post_processor_size_rate) self.assertTrue(result, message) # Check if new field is added impact_fields = impact_layer.dataProvider().fieldNameMap().keys() self.assertIn(feature_value_field['field_name'], impact_fields)
def test_exposure_summary_table(self): """Test we can produce the breakdown for the exposure type.""" aggregate_hazard = load_test_vector_layer( 'gisv4', 'intermediate', 'aggregate_classified_hazard_summary.geojson') aggregate_hazard.keywords['hazard_keywords'] = { 'classification': 'generic_hazard_classes' } # I need the number of unique exposure unique_exposure = read_dynamic_inasafe_field( aggregate_hazard.keywords['inasafe_fields'], exposure_count_field) # I need the number of unique hazard fields = aggregate_hazard.keywords['inasafe_fields'] hazard_class = fields[hazard_class_field['key']] hazard_class_index = aggregate_hazard.fieldNameIndex(hazard_class) unique_hazard = aggregate_hazard.uniqueValues(hazard_class_index) layer = exposure_summary_table(aggregate_hazard, None) check_inasafe_fields(layer) self.assertEqual(len(unique_exposure), layer.featureCount()) # We should have # one column per hazard # one for the exposure # one for total affected # one for total not affected # one for total not exposed # one for total self.assertEqual(layer.fields().count(), len(unique_hazard) + 5)
def test_minimum_needs_calculator(self): """Test behaviour of the minimum needs function.""" dialog = NeedsCalculatorDialog(PARENT) layer = load_test_vector_layer('other', 'minimum_needs.shp') QgsMapLayerRegistry.instance().addMapLayers([layer]) # Set selected layer and displaced field dialog.layer.setLayer(layer) dialog.displaced.setField(u'displaced') # run minimum needs function dialog.accept() # get output layer layer = dialog.result_layer assert layer is not None field_names = [field.name() for field in layer.pendingFields()] for feature in layer.getFeatures(): value = [attribute for attribute in feature.attributes()] actual_attributes = dict(zip(field_names, value)) expected_attributes = { 'displaced': 1000, 'minimum_needs__rice': 2800, 'minimum_needs__drinking_water': 17500, 'minimum_needs__clean_water': 67000, 'minimum_needs__family_kits': 200, 'minimum_needs__toilets': 50} self.assertDictEqual( byteify(expected_attributes), byteify(actual_attributes))
def test_reclassify_vector(self): """Test we can reclassify a continuous vector layer.""" classes = { 1: [None, 0.0], # value <= 0.0 2: [0.0, 1], # 0.0 < value <= 1 10: [1, 1.5], # 1 < value <= 1.5 and gap in output classes 11: [1.3, None] # value > 1.5 } ranges = { exposure_structure['key']: { 'flood_hazard_classes': { 'active': True, 'classes': classes } } } # Let's add a vector layer. layer = load_test_vector_layer( 'hazard', 'continuous_vector.geojson', clone=True) layer.keywords['thresholds'] = ranges self.assertEqual(layer.featureCount(), 400) classified = reclassify(layer, exposure_structure['key']) self.assertEqual(layer.featureCount(), 375) expected_field = hazard_class_field['field_name'] self.assertEqual(classified.fieldNameIndex(expected_field), 1) self.assertEqual( layer.keywords['inasafe_fields'][hazard_class_field['key']], hazard_class_field['field_name']) """
def test_size_post_processor(self): """Test size, size rate, productivity post processor.""" impact_layer = load_test_vector_layer( 'impact', 'indivisible_polygon_impact.geojson', clone_to_memory=True) impact_layer.keywords['exposure_keywords'] = { 'exposure': 'population' } self.assertIsNotNone(impact_layer) # Test the size post processor. result, message = run_single_post_processor( impact_layer, post_processor_size) self.assertTrue(result, message) # Check if new field is added impact_fields = impact_layer.dataProvider().fieldNameMap().keys() self.assertIn(size_field['field_name'], impact_fields) # Test the size rate post processor. result, message = run_single_post_processor( impact_layer, post_processor_size_rate) self.assertTrue(result, message) # Check if new field is added impact_fields = impact_layer.dataProvider().fieldNameMap().keys() self.assertIn(feature_value_field['field_name'], impact_fields)
def test_copy_vector_layer(self): """Test we can copy a vector layer.""" layer = load_test_vector_layer('exposure', 'buildings.shp') new_layer = create_memory_layer('New layer', layer.geometryType(), layer.crs(), layer.fields()) new_layer.keywords = layer.keywords copy_layer(layer, new_layer) self.assertEqual(layer.featureCount(), new_layer.featureCount()) self.assertEqual(len(layer.fields().toList()), len(new_layer.fields().toList())) expected = len(new_layer.fields().toList()) + 1 new_fields = {'STRUCTURE': 'my_new_field'} copy_fields(new_layer, new_fields) self.assertEqual(len(new_layer.fields().toList()), expected) self.assertGreater(new_layer.fieldNameIndex('my_new_field'), -1) remove_fields(new_layer, ['STRUCTURE', 'OSM_TYPE']) self.assertEqual(len(new_layer.fields().toList()), expected - 2) self.assertEqual(new_layer.fieldNameIndex('STRUCTURE'), -1) self.assertEqual(new_layer.fieldNameIndex('OSM_TYPE'), -1) _add_id_column(new_layer) field_name = exposure_id_field['field_name'] self.assertGreater(new_layer.fieldNameIndex(field_name), -1)
def test_init(self): """Test FieldMappingTab initialization.""" layer = load_test_vector_layer( 'gisv4', 'aggregation', 'small_grid_complex.geojson', with_keywords=False, clone_to_memory=True) layer.keywords = {} field_mapping = FieldMappingTab(age_ratio_group, PARENT, IFACE) field_mapping.set_layer(layer) # Empty keywords should give empty for all aspect parameter_values = field_mapping.get_parameter_value() self.assertEqual(parameter_values['fields'], {}) for v in parameter_values['values'].values(): self.assertIsNone(v) # Make sure all keys exist fields_keys = parameter_values['fields'].keys() values_keys = parameter_values['values'].keys() age_ratio_fields_keys = [field['key'] for field in age_ratio_group[ 'fields']] for key in fields_keys + values_keys: self.assertIn(key, age_ratio_fields_keys) # Check field list fields = [] for index in xrange(field_mapping.field_list.count()): fields.append(field_mapping.field_list.item(index)) labels = [i.text() for i in fields] for field in layer.dataProvider().fields(): if field.type() not in qvariant_numbers: continue self.assertIn(field.name(), labels)
def test_recompute_counts(self): """Test we can recompute counts in a layer.""" layer = load_test_vector_layer('gisv4', 'exposure', 'population.geojson', clone=True) self.assertIn(female_count_field['key'], layer.keywords['inasafe_fields']) layer = prepare_vector_layer(layer) layer = from_counts_to_ratios(layer) self.assertIn(female_count_field['key'], layer.keywords['inasafe_fields']) self.assertIn(female_ratio_field['key'], layer.keywords['inasafe_fields']) # Check that each feature has correct ratio for feature in layer.getFeatures(): manual_ratio = feature[female_count_field['field_name']] / feature[ population_count_field['field_name']] diff = abs(manual_ratio - feature[female_ratio_field['field_name']]) self.assertTrue(diff < 10**-2, diff)
def test_recompute_counts(self): """Test we can recompute counts in a layer.""" layer = load_test_vector_layer('gisv4', 'exposure', 'population.geojson', clone=True) self.assertIn(female_count_field['key'], layer.keywords['inasafe_fields']) layer = prepare_vector_layer(layer) layer = from_counts_to_ratios(layer) self.assertIn(female_count_field['key'], layer.keywords['inasafe_fields']) self.assertIn(female_ratio_field['key'], layer.keywords['inasafe_fields']) # Check that each feature has correct ratio for feature in layer.getFeatures(): female_count = feature[female_count_field['field_name']] population_count = feature[population_count_field['field_name']] manual_ratio = female_count / float(population_count) computing_ratio = feature[female_ratio_field['field_name']] diff = abs(manual_ratio - computing_ratio) message = 'The ratio difference is too big, diff = %s' % diff self.assertTrue(diff < 10**-2, message)
def test_recompute_counts(self): """Test we can recompute counts in a layer.""" layer = load_test_vector_layer( 'gisv4', 'exposure', 'population.geojson', clone=True) self.assertIn( female_count_field['key'], layer.keywords['inasafe_fields']) layer = prepare_vector_layer(layer) layer = from_counts_to_ratios(layer) self.assertIn( female_count_field['key'], layer.keywords['inasafe_fields']) self.assertIn( female_ratio_field['key'], layer.keywords['inasafe_fields']) # Check that each feature has correct ratio for feature in layer.getFeatures(): manual_ratio = feature[female_count_field['field_name']] / feature[ population_count_field['field_name']] diff = abs( manual_ratio - feature[female_ratio_field['field_name']]) self.assertTrue(diff < 10 ** -2, diff)
def test_button_behaviour(self): """Test behaviour of each button on multi buffer dialog.""" dialog = MultiBufferDialog(PARENT) directory_button = dialog.directory_button add_class_button = dialog.add_class_button ok_button = dialog.button_box.button(QtWidgets.QDialogButtonBox.Ok) # Test every button without any input in the combo box. self.assertFalse(directory_button.isEnabled()) self.assertFalse(add_class_button.isEnabled()) self.assertFalse(ok_button.isEnabled()) layer = load_test_vector_layer('hazard', 'volcano_point.geojson') QgsProject.instance().addMapLayers([layer]) # Test directory button after a layer is selected dialog.layer.setLayer(layer) self.assertTrue(directory_button.isEnabled()) # Test add class button after radius and class form is filled dialog.radius_form.setValue(500) dialog.class_form.setText('high') self.assertTrue(add_class_button.isEnabled()) # Test ok button after hazard class is populated dialog.populate_hazard_classification() self.assertTrue(ok_button.isEnabled())
def test_sum_fields(self): """Test sum_fields method.""" layer = load_test_vector_layer('gisv4', 'exposure', 'population_multi_fields.geojson', clone=True) sum_fields(layer, exposure_id_field['key'], ['F_0_4', 'F_5_9', 'F_9_15']) exposure_id__idx = layer.fieldNameIndex( exposure_id_field['field_name']) F_0_4__idx = layer.fieldNameIndex('F_0_4') F_5_9__idx = layer.fieldNameIndex('F_5_9') F_9_15__idx = layer.fieldNameIndex('F_9_15') for feature in layer.getFeatures(): sum_value = (feature[F_0_4__idx] + feature[F_5_9__idx] + feature[F_9_15__idx]) self.assertEqual(feature[exposure_id__idx], sum_value) new_field__idx = layer.fieldNameIndex(female_count_field['field_name']) # Check if the new field doesn't exist self.assertEqual(new_field__idx, -1) sum_fields(layer, female_count_field['key'], ['F_0_4', 'F_5_9']) new_field__idx = layer.fieldNameIndex(female_count_field['field_name']) for feature in layer.getFeatures(): sum_value = (feature[F_0_4__idx] + feature[F_5_9__idx]) self.assertEqual(feature[new_field__idx], sum_value)
def test_union(self): """Test we can union two layers like hazard and aggregation (1).""" union_a = load_test_vector_layer('gisv4', 'hazard', 'classified_vector.geojson') union_a.keywords['inasafe_fields'][hazard_class_field['key']] = ( union_a.keywords['inasafe_fields'][hazard_value_field['key']]) union_b = load_test_vector_layer('gisv4', 'aggregation', 'small_grid.geojson') layer = union(union_a, union_b) self.assertEqual(layer.featureCount(), 11) self.assertEqual(union_a.fields().count() + union_b.fields().count(), layer.fields().count())
def test_own_id_column(self): """Test if we can re-use the column ID from the user.""" layer = load_test_vector_layer('gisv4', 'exposure', 'buildings.geojson', clone=True) # This layer is set to use custom ID column. We should have the values # after preparing the vector layer. field = layer.fieldNameIndex(exposure_id_field['field_name']) self.assertNotEqual(-1, field) unique_values_before = layer.uniqueValues(field) self.assertEqual(unique_values_before, [10, 11, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110]) _add_id_column(layer) field = layer.fieldNameIndex(exposure_id_field['field_name']) self.assertNotEqual(-1, field) unique_values_after = layer.uniqueValues(field) self.assertEqual(unique_values_after, unique_values_before) # Let's remove the keyword now to use the auto-increment ID del layer.keywords['inasafe_fields'][exposure_id_field['key']] _add_id_column(layer) field = layer.fieldNameIndex(exposure_id_field['field_name']) self.assertNotEqual(-1, field) unique_values_automatic = layer.uniqueValues(field) self.assertNotEqual(unique_values_automatic, unique_values_before) self.assertEqual(unique_values_automatic, range(layer.featureCount()))
def test_exposure_summary_table_productivity(self): """Test we can produce the breakdown for the exposure type.""" aggregate_hazard = load_test_vector_layer( 'gisv4', 'intermediate', 'summaries', 'land_cover_aggregate_hazard_impacted.geojson') aggregate_hazard.keywords['hazard_keywords'] = { 'classification': 'generic_hazard_classes' } exposure_summary = load_test_vector_layer( 'gisv4', 'intermediate', 'summaries', 'land_cover_exposure_summary.geojson' ) # I need the number of unique exposure unique_exposure = read_dynamic_inasafe_field( aggregate_hazard.keywords['inasafe_fields'], exposure_count_field) # I need the number of unique hazard fields = aggregate_hazard.keywords['inasafe_fields'] hazard_class = fields[hazard_class_field['key']] hazard_class_index = aggregate_hazard.fieldNameIndex(hazard_class) unique_hazard = aggregate_hazard.uniqueValues(hazard_class_index) layer = exposure_summary_table(aggregate_hazard, exposure_summary) check_inasafe_fields(layer) self.assertEqual(len(unique_exposure), layer.featureCount()) # We should have # one column per hazard # one for the exposure # one for total affected # one for total not affected # one for total not exposed # one for total # one for affected productivity # one for affected production cost # one for affected production value self.assertEqual(layer.fields().count(), len(unique_hazard) + 8)
def test_analysis_multi_exposure(self): """Test we can merge two analysis layers.""" analysis_summary_buildings = load_test_vector_layer( 'gisv4', 'intermediate', 'summaries', 'multi_exposure_analysis_buildings.geojson' ) analysis_summary_roads = load_test_vector_layer( 'gisv4', 'intermediate', 'summaries', 'multi_exposure_analysis_roads.geojson' ) analysis = load_test_vector_layer( 'gisv4', 'impacts', 'multi_exposure_analysis.geojson', clone=True) analysis = multi_exposure_analysis_summary( analysis, [ analysis_summary_buildings, analysis_summary_roads ] ) concatenation = [] iterator = analysis_summary_buildings.getFeatures() feature = next(iterator) attributes = feature.attributes() self.assertEqual(len(attributes), 9) concatenation.extend(attributes[1:]) # We drop analysis_name iterator = analysis_summary_roads.getFeatures() feature = next(iterator) attributes = feature.attributes() self.assertEqual(len(attributes), 9) concatenation.extend(attributes[1:]) # We drop analysis_name iterator = analysis.getFeatures() feature = next(iterator) attributes = feature.attributes() self.assertEqual(len(attributes), 17) # Concatenation is a subset of attributes self.assertTrue(set(concatenation) < set(attributes))
def test_remove_rows(self): """Test we can remove rows.""" layer = load_test_vector_layer( 'gisv4', 'hazard', 'classified_vector.geojson', clone=True) feature_count = layer.featureCount() _remove_features(layer) self.assertEqual(layer.featureCount(), feature_count - 1)
def test_remove_rows(self): """Test we can remove rows.""" layer = load_test_vector_layer( 'gisv4', 'hazard', 'classified_vector.geojson', clone=True) feature_count = layer.featureCount() _remove_features(layer) self.assertEqual(layer.featureCount(), feature_count - 1)
def test_vector_post_minimum_needs_value_generation(self): """Test minimum needs postprocessors on vector exposure. Test with vector exposure data with population_count_field exists. Minimum needs postprocessors is defined to only generate values when exposure contains population data. """ hazard_layer = load_test_vector_layer('gisv4', 'hazard', 'tsunami_vector.geojson') exposure_layer = load_test_vector_layer('gisv4', 'exposure', 'population.geojson') aggregation_layer = load_test_vector_layer('gisv4', 'aggregation', 'small_grid.geojson') impact_function = ImpactFunction() impact_function.aggregation = aggregation_layer impact_function.exposure = exposure_layer impact_function.hazard = hazard_layer status, message = impact_function.prepare() self.assertEqual(PREPARE_SUCCESS, status, message) return_code, message = impact_function.run() self.assertEqual(return_code, ANALYSIS_SUCCESS, message) # minimum needs fields should exists in the results self._check_minimum_fields_exists(impact_function) expected_value = { u'population': 69, u'total': 9.0, u'minimum_needs__rice': 491, u'minimum_needs__clean_water': 11763, u'minimum_needs__toilets': 8, u'minimum_needs__drinking_water': 3072, u'minimum_needs__family_kits': 35, u'male': 34, u'female': 34, u'youth': 17, u'adult': 45, u'elderly': 6, u'total_affected': 6.0, } self._check_minimum_fields_value(expected_value, impact_function)
def test_vector_post_minimum_needs_value_generation(self): """Test minimum needs postprocessors on vector exposure. Test with vector exposure data with population_count_field exists. Minimum needs postprocessors is defined to only generate values when exposure contains population data. """ hazard_layer = load_test_vector_layer( 'gisv4', 'hazard', 'tsunami_vector.geojson') exposure_layer = load_test_vector_layer( 'gisv4', 'exposure', 'population.geojson') aggregation_layer = load_test_vector_layer( 'gisv4', 'aggregation', 'small_grid.geojson') impact_function = ImpactFunction() impact_function.aggregation = aggregation_layer impact_function.exposure = exposure_layer impact_function.hazard = hazard_layer status, message = impact_function.prepare() self.assertEqual(PREPARE_SUCCESS, status, message) return_code, message = impact_function.run() self.assertEqual(return_code, ANALYSIS_SUCCESS, message) # minimum needs fields should exists in the results self._check_minimum_fields_exists(impact_function) expected_value = { u'population': 69, u'total': 9.0, u'minimum_needs__rice': 491, u'minimum_needs__clean_water': 11763, u'minimum_needs__toilets': 8, u'minimum_needs__drinking_water': 3072, u'minimum_needs__family_kits': 35, u'male': 34, u'female': 34, u'youth': 17, u'adult': 45, u'elderly': 6, u'total_affected': 6.0, } self._check_minimum_fields_value(expected_value, impact_function)
def test_union(self): """Test we can union two layers like hazard and aggregation (1).""" union_a = load_test_vector_layer( 'gisv4', 'hazard', 'classified_vector.geojson') union_a.keywords['inasafe_fields'][hazard_class_field['key']] = ( union_a.keywords['inasafe_fields'][hazard_value_field['key']]) union_b = load_test_vector_layer( 'gisv4', 'aggregation', 'small_grid.geojson') layer = union(union_a, union_b) self.assertEqual(layer.featureCount(), 11) self.assertEqual( union_a.fields().count() + union_b.fields().count(), layer.fields().count() )
def test_union_error(self): """Test we can union two layers like hazard and aggregation (2).""" union_a = clean_layer( load_test_vector_layer('gisv4', 'hazard', 'union_check_hazard.geojson')) union_a.keywords['inasafe_fields'][hazard_class_field['key']] = ( union_a.keywords['inasafe_fields'][hazard_value_field['key']]) union_b = clean_layer( load_test_vector_layer('gisv4', 'aggregation', 'union_check_aggregation.geojson')) layer = union(union_a, union_b) self.assertEqual(layer.featureCount(), 11) self.assertEqual(union_a.fields().count() + union_b.fields().count(), layer.fields().count())
def test_union_error(self): """Test we can union two layers like hazard and aggregation (2).""" union_a = clean_layer(load_test_vector_layer( 'gisv4', 'hazard', 'union_check_hazard.geojson')) union_a.keywords['inasafe_fields'][hazard_class_field['key']] = ( union_a.keywords['inasafe_fields'][hazard_value_field['key']]) union_b = clean_layer(load_test_vector_layer( 'gisv4', 'aggregation', 'union_check_aggregation.geojson')) layer = union(union_a, union_b) self.assertEqual(layer.featureCount(), 11) self.assertEqual( union_a.fields().count() + union_b.fields().count(), layer.fields().count() )
def test_impact_summary(self): """Test we can aggregate the impact to the aggregate hazard.""" impact = load_test_vector_layer( 'gisv4', 'impacts', 'building-points-classified-vector.geojson') aggregate_hazard = load_test_vector_layer( 'gisv4', 'intermediate', 'aggregate_classified_hazard.geojson', clone=True) aggregate_hazard.keywords['hazard_keywords'] = { 'classification': 'generic_hazard_classes' } impact.keywords['classification'] = { 'classification': 'generic_structure_classes' } number_of_fields = aggregate_hazard.fields().count() layer = aggregate_hazard_summary(impact, aggregate_hazard) self.assertIn(total_field['key'], layer.keywords['inasafe_fields']) check_inasafe_fields(layer) fields = impact.keywords['inasafe_fields'] exposure_class = fields[exposure_class_field['key']] exposure_class_index = impact.fieldNameIndex(exposure_class) unique_exposure = impact.uniqueValues(exposure_class_index) # One field per exposure type # Number of previous fields in the layer # 3 : 1 fields for absolute values, 2 fields for affected and total. self.assertEqual( layer.fields().count(), len(unique_exposure) + number_of_fields + 3 )
def test_assign_highest_value_vector(self): """Test we can assign the highest value to a feature.""" exposure = load_test_vector_layer( 'gisv4', 'exposure', 'buildings.geojson', clone_to_memory=True) aggregate_hazard = load_test_vector_layer( 'gisv4', 'intermediate', 'aggregate_classified_hazard.geojson') # Monkey patching classification. We should remove this line # when we will have aggregate_hazard definitions. aggregate_hazard.keywords['classification'] = 'generic_hazard_classes' aggregate_hazard.keywords['aggregation_keywords'] = {} aggregate_hazard.keywords['hazard_keywords'] = {} count = exposure.fields().count() layer = assign_highest_value(exposure, aggregate_hazard) self.assertEqual(layer.featureCount(), 12) self.assertEqual( count + aggregate_hazard.fields().count(), layer.fields().count()) expected_count = { 'high': 4, 'medium': 2, '': 6 } inasafe_fields = layer.keywords['inasafe_fields'] expected_field = inasafe_fields[hazard_class_field['key']] for value, count in expected_count.iteritems(): if value: expression = '"%s" = \'%s\'' % (expected_field, value) else: expression = '"%s" is NULL' % expected_field request = QgsFeatureRequest().setFilterExpression(expression) self.assertEqual( sum(1 for _ in layer.getFeatures(request)), count)
def test_intersection_mask_vector(self): """Test we can intersect two layers, like exposure and aggregation.""" exposure = load_test_vector_layer( 'gisv4', 'exposure', 'roads.geojson') # This intersection algorithm needs a aggregate hazard layer so add # hazard and aggregation keywords. aggregation = load_test_vector_layer( 'gisv4', 'hazard', 'classified_vector.geojson') aggregation.keywords = { 'aggregation_keywords': {}, 'hazard_keywords': {}, 'inasafe_fields': {} } layer = intersection(exposure, aggregation) self.assertEqual(layer.featureCount(), 6) self.assertEqual( aggregation.fields().count() + exposure.fields().count(), layer.fields().count() )