def test_hazard_additional_keywords(self): """Test for hazard_additional_keywords.""" impact_function = FloodPolygonBuildingFunction() result = impact_function.metadata().hazard_additional_keywords( layer_mode_key='classified', layer_geometry_key='polygon', hazard_category_key='single_event', hazard_key='flood' ) expected = [] self.assertItemsEqual(result, expected) result = impact_function.metadata().hazard_additional_keywords( layer_mode_key='classified', layer_geometry_key='polygon', hazard_category_key='single_event', ) expected = [] self.assertItemsEqual(result, expected) impact_function = VolcanoPointBuildingFunction() result = impact_function.metadata().hazard_additional_keywords( layer_mode_key='classified', layer_geometry_key='point', hazard_category_key='multiple_event', hazard_key='volcano' ) expected = [volcano_name_field] self.assertItemsEqual(result, expected)
def test_exposure_class_fields(self): """Test for exposure_class_fields.""" impact_function = FloodPolygonBuildingFunction() result = impact_function.metadata().exposure_class_fields( layer_mode_key='classified', layer_geometry_key='polygon', exposure_key='structure') expected = [structure_class_field] self.assertItemsEqual(result, expected) result = impact_function.metadata().exposure_class_fields( layer_geometry_key='polygon', exposure_key='structure') expected = [structure_class_field] self.assertItemsEqual(result, expected)
def test_run(self): function = FloodPolygonBuildingFunction.instance() hazard_path = test_data_path('hazard', 'flood_multipart_polygons.shp') # exposure_path = test_data_path('exposure', 'buildings.shp') # noinspection PyCallingNonCallable hazard_layer = QgsVectorLayer(hazard_path, 'Flood', 'ogr') # noinspection PyCallingNonCallable # exposure_layer = QgsVectorLayer(exposure_path, 'Buildings', 'ogr') exposure_layer = clone_shp_layer( name='buildings', include_keywords=True, source_directory=test_data_path('exposure')) # Let's set the extent to the hazard extent extent = hazard_layer.extent() rect_extent = [ extent.xMinimum(), extent.yMaximum(), extent.xMaximum(), extent.yMinimum()] function.hazard = SafeLayer(hazard_layer) function.exposure = SafeLayer(exposure_layer) function.requested_extent = rect_extent function.run() impact = function.impact # Count of flooded objects is calculated "by the hands" # total flooded = 27, total buildings = 129 count = sum(impact.get_data(attribute=function.target_field)) self.assertEquals(count, 33) count = len(impact.get_data()) self.assertEquals(count, 176)
def test_is_valid(self): """Test is_valid.""" impact_functions = [ # Earthquake EarthquakeBuildingFunction(), ITBFatalityFunction(), PAGFatalityFunction(), # Generic ClassifiedPolygonHazardBuildingFunction(), ClassifiedPolygonHazardPopulationFunction(), ClassifiedRasterHazardBuildingFunction(), ClassifiedRasterHazardPopulationFunction(), ContinuousHazardPopulationFunction(), # Inundation FloodEvacuationVectorHazardFunction(), FloodPolygonRoadsFunction(), FloodRasterBuildingFunction(), FloodEvacuationRasterHazardFunction(), FloodRasterRoadsFunction(), FloodPolygonBuildingFunction(), TsunamiEvacuationFunction(), # Volcanic VolcanoPointBuildingFunction(), VolcanoPointPopulationFunction(), VolcanoPolygonBuildingFunction(), VolcanoPolygonPopulationFunction() ] for impact_function in impact_functions: valid = impact_function.metadata().is_valid() impact_function_name = impact_function.__class__.__name__ message = '%s is invalid because %s' % (impact_function_name, valid[1]) self.assertTrue(valid[0], message) if valid[0]: print '%s has a valid metadata.' % impact_function_name
def test_run(self): function = FloodPolygonBuildingFunction.instance() hazard_path = test_data_path('hazard', 'flood_multipart_polygons.shp') # exposure_path = test_data_path('exposure', 'buildings.shp') # noinspection PyCallingNonCallable hazard_layer = QgsVectorLayer(hazard_path, 'Flood', 'ogr') # noinspection PyCallingNonCallable # exposure_layer = QgsVectorLayer(exposure_path, 'Buildings', 'ogr') exposure_layer = clone_shp_layer( name='buildings', include_keywords=True, source_directory=test_data_path('exposure')) # Let's set the extent to the hazard extent extent = hazard_layer.extent() rect_extent = [ extent.xMinimum(), extent.yMaximum(), extent.xMaximum(), extent.yMinimum() ] function.hazard = SafeLayer(hazard_layer) function.exposure = SafeLayer(exposure_layer) function.requested_extent = rect_extent function.run() impact = function.impact # Count of flooded objects is calculated "by the hands" # total flooded = 27, total buildings = 129 count = sum(impact.get_data(attribute=function.target_field)) self.assertEquals(count, 33) count = len(impact.get_data()) self.assertEquals(count, 176)
def test_run(self): function = FloodPolygonBuildingFunction.instance() hazard_path = test_data_path('hazard', 'flood_multipart_polygons.shp') exposure_path = test_data_path('exposure', 'buildings.shp') # noinspection PyCallingNonCallable hazard_layer = QgsVectorLayer(hazard_path, 'Flood', 'ogr') # noinspection PyCallingNonCallable exposure_layer = QgsVectorLayer(exposure_path, 'Buildings', 'ogr') # Let's set the extent to the hazard extent extent = hazard_layer.extent() rect_extent = [ extent.xMinimum(), extent.yMaximum(), extent.xMaximum(), extent.yMinimum()] function.hazard = QgisWrapper(hazard_layer) function.exposure = QgisWrapper(exposure_layer) function.requested_extent = rect_extent function.parameters['building_type_field'] = 'TYPE' function.parameters['affected_field'] = 'FLOODPRONE' function.parameters['affected_value'] = 'YES' function.run() impact = function.impact # Count of flooded objects is calculated "by the hands" # total flooded = 27, total buildings = 129 count = sum(impact.get_data(attribute='INUNDATED')) self.assertEquals(count, 33) count = len(impact.get_data()) self.assertEquals(count, 176)
def test_exposure_class_fields(self): """Test for exposure_class_fields.""" impact_function = FloodPolygonBuildingFunction() result = impact_function.metadata().exposure_class_fields( layer_mode_key='classified', layer_geometry_key='polygon', exposure_key='structure' ) expected = [structure_class_field] self.assertItemsEqual(result, expected) result = impact_function.metadata().exposure_class_fields( layer_geometry_key='polygon', exposure_key='structure' ) expected = [structure_class_field] self.assertItemsEqual(result, expected)
def test_hazard_additional_keywords(self): """Test for hazard_additional_keywords.""" impact_function = FloodPolygonBuildingFunction() result = impact_function.metadata().hazard_additional_keywords( layer_mode_key='classified', layer_geometry_key='polygon', hazard_category_key='single_event', hazard_key='flood' ) expected = [] self.assertItemsEqual(result, expected) result = impact_function.metadata().hazard_additional_keywords( layer_mode_key='classified', layer_geometry_key='polygon', hazard_category_key='single_event', ) expected = [] print [x['key'] for x in result]
def test_hazard_additional_keywords(self): """Test for hazard_additional_keywords.""" impact_function = FloodPolygonBuildingFunction() result = impact_function.metadata().hazard_additional_keywords( layer_mode_key='classified', layer_geometry_key='polygon', hazard_category_key='single_event', hazard_key='flood') expected = [] self.assertItemsEqual(result, expected) result = impact_function.metadata().hazard_additional_keywords( layer_mode_key='classified', layer_geometry_key='polygon', hazard_category_key='single_event', ) expected = [] print[x['key'] for x in result] self.assertItemsEqual(result, expected)
def test_is_valid(self): """Test is_valid.""" impact_functions = [ # Earthquake EarthquakeBuildingFunction(), ITBFatalityFunction(), PAGFatalityFunction(), ITBBayesianFatalityFunction(), # Generic ClassifiedPolygonHazardBuildingFunction(), ClassifiedPolygonHazardLandCoverFunction(), ClassifiedPolygonHazardPopulationFunction(), ClassifiedPolygonHazardPolygonPeopleFunction(), ClassifiedRasterHazardBuildingFunction(), ClassifiedRasterHazardPopulationFunction(), ContinuousHazardPopulationFunction(), # Inundation FloodEvacuationVectorHazardFunction(), FloodPolygonRoadsFunction(), FloodRasterBuildingFunction(), FloodEvacuationRasterHazardFunction(), FloodRasterRoadsFunction(), FloodPolygonBuildingFunction(), # Tsunami TsunamiEvacuationFunction(), TsunamiRasterRoadsFunction(), TsunamiRasterLandcoverFunction(), TsunamiRasterBuildingFunction(), # Volcanic VolcanoPointBuildingFunction(), VolcanoPointPopulationFunction(), VolcanoPolygonBuildingFunction(), VolcanoPolygonPopulationFunction(), # Volcanic Ash AshRasterLandCoverFunction(), AshRasterPlacesFunction(), AshRasterPopulationFunction() ] self.assertEqual(len(impact_functions), len(EXPECTED_IF)) for impact_function in impact_functions: valid = impact_function.metadata().is_valid() impact_function_name = impact_function.__class__.__name__ message = '%s is invalid because %s' % (impact_function_name, valid[1]) self.assertTrue(valid[0], message) if valid[0]: # print '%s has a valid metadata.' % impact_function_name continue
def test_hazard_additional_keywords(self): """Test for hazard_additional_keywords.""" impact_function = FloodPolygonBuildingFunction() result = impact_function.metadata().hazard_additional_keywords( layer_mode_key='classified', layer_geometry_key='polygon', hazard_category_key='single_event', hazard_key='flood') expected = [] self.assertItemsEqual(result, expected) result = impact_function.metadata().hazard_additional_keywords( layer_mode_key='classified', layer_geometry_key='polygon', hazard_category_key='single_event', ) expected = [] self.assertItemsEqual(result, expected) impact_function = VolcanoPointBuildingFunction() result = impact_function.metadata().hazard_additional_keywords( layer_mode_key='classified', layer_geometry_key='point', hazard_category_key='multiple_event', hazard_key='volcano') expected = [volcano_name_field] self.assertItemsEqual(result, expected)