def test_fxn(self): with utils.WorkSpace(self.workspace), utils.OverwriteState(True): floodslyr, wetlandslyr, buildingslyr = tidegates.assess_impact( floods_path=self.floods, flood_idcol='GeoID', wetlands_path=self.wetlands, buildings_path=self.buildings, buildings_output=os.path.join(self.workspace, 'flooded_buildings.shp'), wetlands_output=os.path.join(self.workspace, 'flooded_wetlands.shp'), cleanup=False, verbose=False, ) nt.assert_true(isinstance(floodslyr, arcpy.mapping.Layer)) tgtest.assert_shapefiles_are_close(floodslyr.dataSource, self.known_floods) nt.assert_true(isinstance(wetlandslyr, arcpy.mapping.Layer)) tgtest.assert_shapefiles_are_close(wetlandslyr.dataSource, self.known_wetlands) nt.assert_true(isinstance(buildingslyr, arcpy.mapping.Layer)) tgtest.assert_shapefiles_are_close(buildingslyr.dataSource, self.known_buildings) with utils.WorkSpace(self.workspace): utils.cleanup_temp_results(self.floods, 'flooded_buildings.shp', 'flooded_wetlands.shp', '_temp_flooded_wetlands.shp')
def test_main_execute(self): with utils.OverwriteState(True), utils.WorkSpace(self.main_execute_ws): self.tbx.main_execute(zones='zones.shp', workspace=self.main_execute_ws, flood_output='test_floods.shp', wetland_output='test_wetlands.shp', building_output='test_buildings.shp', wetlands='wetlands.shp', buildings='buildings.shp', ID_column='GeoID', dem='dem.tif', elevation=self.elev_list) utils.cleanup_temp_results("tempraster") tgtest.assert_shapefiles_are_close( resource_filename(self.main_execute_dir, 'test_wetlands.shp'), resource_filename(self.main_execute_dir, 'known_wetlands.shp'), ) tgtest.assert_shapefiles_are_close( resource_filename(self.main_execute_dir, 'test_floods.shp'), resource_filename(self.main_execute_dir, 'known_floods.shp'), ) tgtest.assert_shapefiles_are_close( resource_filename(self.main_execute_dir, 'test_buildings.shp'), resource_filename(self.main_execute_dir, 'known_buildings.shp'), )
def test_with_results(self): with utils.WorkSpace(self.workspace): res1 = arcpy.Result(toolname='Clip_management') res2 = arcpy.Result(toolname='Clip_management') with mock.patch.object(res1, 'getOutput', return_value='temp_1.tif'), \ mock.patch.object(res2, 'getOutput', return_value='temp_2.tif'): utils.cleanup_temp_results(res1, res2) self.check_outcome()
def test_geodb_barefile(self): with utils.WorkSpace(self.geodbworkspace): known_raster = os.path.join(self.geodbworkspace, '_temp_test') temp_raster = utils.create_temp_filename('test', filetype='raster') nt.assert_equal(temp_raster, known_raster) known_shape = os.path.join(self.geodbworkspace, '_temp_test') temp_shape = utils.create_temp_filename('test', filetype='shape') nt.assert_equal(temp_shape, known_shape)
def test_with_extension_folder(self): with utils.WorkSpace(self.folderworkspace): filename = 'test' known_raster = os.path.join(self.folderworkspace, '_temp_test.tif') temp_raster = utils.create_temp_filename(filename + '.tif', filetype='raster') nt.assert_equal(temp_raster, known_raster) known_shape = os.path.join(self.folderworkspace, '_temp_test.shp') temp_shape = utils.create_temp_filename(filename + '.shp', filetype='shape') nt.assert_equal(temp_shape, known_shape)
def test_buildings(self): with utils.WorkSpace(self.workspace), utils.OverwriteState(True): outputlayer = tidegates.area_of_impacts( floods_path=self.floods, flood_idcol='GeoID', assets_input=self.buildings, fieldname='bldg_area', assets_output=os.path.join(self.workspace, 'flooded_buildings.shp'), cleanup=False, verbose=False, ) nt.assert_true(isinstance(outputlayer, arcpy.mapping.Layer)) tgtest.assert_shapefiles_are_close(outputlayer.dataSource, self.known_buildings) with utils.WorkSpace(self.workspace): utils.cleanup_temp_results(self.floods, 'flooded_buildings.shp', '_temp_flooded_buildings.shp')
def test_geodb_as_subfolder(self): with utils.WorkSpace(self.folderworkspace): filename = os.path.join(self.geodbworkspace, 'test') known_raster = os.path.join(self.folderworkspace, self.geodbworkspace, '_temp_test') temp_raster = utils.create_temp_filename(filename, filetype='raster') nt.assert_equal(temp_raster, known_raster) known_shape = os.path.join(self.folderworkspace, self.geodbworkspace, '_temp_test') temp_shape = utils.create_temp_filename(filename, filetype='shape') nt.assert_equal(temp_shape, known_shape)
def test_wetlands(self): with utils.WorkSpace(self.workspace), utils.OverwriteState(True): outputlayer = tidegates.count_of_impacts( floods_path=self.floods, flood_idcol='GeoID', fieldname='wetlands', assets_input=self.wetlands, asset_idcol='WETCODE', assets_output=os.path.join(self.workspace, 'flooded_wetlands.shp'), verbose=False, ) nt.assert_true(isinstance(outputlayer, arcpy.mapping.Layer)) tgtest.assert_shapefiles_are_close(outputlayer.dataSource, self.known_wetlands) tgtest.assert_shapefiles_are_close(self.floods, self.known_floods_with_wtld) with utils.WorkSpace(self.workspace): utils.cleanup_temp_results(self.floods, 'flooded_wetlands.shp', '_temp_flooded_wetlands.shp')
def test_folderworkspace_withsubfolder(self): with utils.WorkSpace(self.folderworkspace): known_raster = os.path.join(self.folderworkspace, 'subfolder', '_temp_test.tif') temp_raster = utils.create_temp_filename(os.path.join( 'subfolder', 'test'), filetype='raster') nt.assert_equal(temp_raster, known_raster) known_shape = os.path.join(self.folderworkspace, 'subfolder', '_temp_test.shp') temp_shape = utils.create_temp_filename(os.path.join( 'subfolder', 'test'), filetype='shape') nt.assert_equal(temp_shape, known_shape)
def test_folderworkspace_barefile_with_num(self): with utils.WorkSpace(self.folderworkspace): known_raster = os.path.join(self.folderworkspace, '_temp_test_14.tif') temp_raster = utils.create_temp_filename('test', filetype='raster', num=14) nt.assert_equal(temp_raster, known_raster) known_shape = os.path.join(self.folderworkspace, '_temp_test_3.shp') temp_shape = utils.create_temp_filename('test', filetype='shape', num=3) nt.assert_equal(temp_shape, known_shape)
def test_with_extension_geodb_with_num(self): with utils.WorkSpace(self.folderworkspace): filename = os.path.join(self.geodbworkspace, 'test') known_raster = os.path.join(self.folderworkspace, self.geodbworkspace, '_temp_test_2000') temp_raster = utils.create_temp_filename(filename + '.tif', filetype='raster', num=2000) nt.assert_equal(temp_raster, known_raster) known_shape = os.path.join(self.folderworkspace, self.geodbworkspace, '_temp_test_999') temp_shape = utils.create_temp_filename(filename + '.tif', filetype='shape', num=999) nt.assert_equal(temp_shape, known_shape)
def test_analyze(self): ws = resource_filename('tidegates.testing', 'analyze') testdir = 'tidegates.testing.analyze' test_dem = resource_filename(testdir, 'dem.tif') test_zones = resource_filename(testdir, 'zones.shp') test_wetlands = resource_filename(testdir, 'wetlands.shp') test_buidlings = resource_filename(testdir, 'buildings.shp') output = resource_filename(testdir, 'flooding.shp') topo_array, zones_array, template = tidegates.process_dem_and_zones( dem=test_dem, zones=test_zones, ID_column="GeoID", cleanup=True, verbose=False) with utils.WorkSpace(ws), utils.OverwriteState(True): flood, wetland, building = self.tbx.analyze( topo_array=topo_array, zones_array=zones_array, template=template, elev=self.elev, slr=self.slr, surge=self.surge, num=None, flood_output=output, dem=test_dem, zones=test_zones, ID_column='GeoID', wetlands=test_wetlands, buildings=test_buidlings, ) nt.assert_true(isinstance(flood, arcpy.mapping.Layer)) nt.assert_equal(flood.dataSource, resource_filename(testdir, self.flood_output)) nt.assert_true(isinstance(wetland, arcpy.mapping.Layer)) nt.assert_true(isinstance(building, arcpy.mapping.Layer)) tgtest.assert_shapefiles_are_close( resource_filename(testdir, self.flood_output), resource_filename(testdir, self.known_flood_output))
def setup(self): self.workspace = os.path.abspath( resource_filename('tidegates.testing', 'cleanup_temp_results')) self.template_file = resource_filename( 'tidegates.testing.cleanup_temp_results', 'test_dem.tif') self.template = utils.load_data(self.template_file, 'raster') raster1 = utils.array_to_raster(numpy.random.normal(size=(30, 30)), self.template) raster2 = utils.array_to_raster(numpy.random.normal(size=(60, 60)), self.template) self.name1 = 'temp_1.tif' self.name2 = 'temp_2.tif' self.path1 = os.path.join(self.workspace, self.name1) self.path2 = os.path.join(self.workspace, self.name2) with utils.OverwriteState(True), utils.WorkSpace(self.workspace): raster1.save(self.path1) raster2.save(self.path2)
def test_with_rasters(self): with utils.WorkSpace(self.workspace): raster1 = utils.load_data(self.path1, 'raster') raster2 = utils.load_data(self.path2, 'raster') utils.cleanup_temp_results(raster1, raster2) self.check_outcome()
def test_with_names_in_a_workspace(self): with utils.WorkSpace(self.workspace): utils.cleanup_temp_results(self.name1, self.name2) self.check_outcome()
def main_execute(self, **params): """ Performs the flood-impact analysis on multiple flood elevations. Parameters ---------- workspace : str The folder or geodatabase where the analysis will be executed. dem : str Filename of the digital elevation model (topography data) to be used in determinging the inundated areas. zones : str Name of zones of influence layer. ID_column : str Name of the field in ``zones`` that uniquely identifies each zone of influence. elevation : list, optional List of (custom) flood elevations to be analyzed. If this is not provided, *all* of the standard scenarios will be evaluated. flood_output : str Filename where the extent of flooding and damage will be saved. wetlands, buildings : str, optional Names of the wetland and building footprint layers. wetland_output, building_output : str, optional Filenames where the flooded wetlands and building footprints will be saved. Returns ------- None """ wetlands = params.get('wetlands', None) buildings = params.get('buildings', None) all_floods = [] all_wetlands = [] all_buildings = [] with utils.WorkSpace(params['workspace']), utils.OverwriteState(True): topo_array, zones_array, template = tidegates.process_dem_and_zones( dem=params['dem'], zones=params['zones'], ID_column=params['ID_column'] ) for scenario in self.make_scenarios(**params): fldlyr, wtlndlyr, blgdlyr = self.analyze( topo_array=topo_array, zones_array=zones_array, template=template, elev=scenario['elev'], surge=scenario['surge_name'], slr=scenario['slr'], **params ) all_floods.append(fldlyr.dataSource) if wetlands is not None: all_wetlands.append(wtlndlyr.dataSource) if buildings is not None: all_buildings.append(blgdlyr.dataSource) self.finish_results( params['flood_output'], all_floods, msg="Merging and cleaning up all flood results", verbose=True, asMessage=True, ) if wetlands is not None: wtld_output = params.get( 'wetland_output', utils.create_temp_filename(params['wetlands'], prefix='output_', filetype='shape') ) self.finish_results( wtld_output, all_wetlands, sourcename=params['wetlands'], msg="Merging and cleaning up all wetlands results", verbose=True, asMessage=True, ) if buildings is not None: bldg_output = params.get( 'building_output', utils.create_temp_filename(params['buildings'], prefix='output_', filetype='shape') ) self.finish_results( bldg_output, all_buildings, sourcename=params['buildings'], msg="Merging and cleaning up all buildings results", verbose=True, asMessage=True, )
def test_workspace(self): nt.assert_equal(arcpy.env.workspace, self.baseline) with utils.WorkSpace(self.new_ws): nt.assert_equal(arcpy.env.workspace, self.new_ws) nt.assert_equal(arcpy.env.workspace, self.baseline)
def test_process_dem_and_zones(): known_topo = numpy.array([ [ -999.0, 0.305, 0.610, 0.914, 1.219, 1.524, 1.829, 2.134, ], [ 0.305, 0.610, 0.914, 1.219, 1.524, 1.829, 2.134, 2.438, ], [ 0.610, 0.914, 1.219, 1.524, 1.829, 2.134, 2.438, 2.743, ], [ 0.914, 1.219, 1.524, 1.829, 2.134, 2.438, 2.743, 3.048, ], [ 1.219, 1.524, 1.829, 2.134, 2.438, 2.743, 3.048, 3.353, ], [ 1.524, 1.829, 2.134, 2.438, 2.743, 3.048, 3.353, 3.658, ], ]) known_zones = numpy.array([[-999, 2, 2, 2, 2, 2, 2, -999], [-999, 2, 2, 2, -999, 2, 2, -999], [1, 1, 1, 1, -999, 2, 2, -999], [1, 1, 1, 1, -999, 2, 2, -999], [1, -999, 1, 1, 2, 2, 2, 2], [-999, -999, 1, 1, 2, 2, 2, 2]]) known_cell_size = 8 known_X, known_Y = 4, 22 ws = resource_filename('tidegates.testing', 'process_dem_and_zones') with utils.WorkSpace(ws), utils.OverwriteState(True): ta, za, template = tidegates.process_dem_and_zones(dem='topo.tif', zones='zones.shp', ID_column='GeoID', cleanup=True, verbose=False) nptest.assert_array_almost_equal(ta, known_topo, decimal=3) nptest.assert_array_almost_equal(za, known_zones, decimal=3) nt.assert_equal(template.meanCellWidth, known_cell_size) nt.assert_equal(template.meanCellHeight, known_cell_size) nt.assert_equal(template.extent.lowerLeft.X, known_X) nt.assert_equal(template.extent.lowerLeft.Y, known_Y)
def test_with_layers(self): with utils.WorkSpace(self.workspace): lyr1 = utils.load_data('temp_1.tif', 'layer', greedyRasters=False) lyr2 = utils.load_data('temp_2.tif', 'layer', greedyRasters=False) utils.cleanup_temp_results(lyr1, lyr2) self.check_outcome()
def teardown(self): with utils.WorkSpace(self.workspace): utils.cleanup_temp_results('temp_1.tif', 'temp_2.tif')
def test_flood_area(): topo = numpy.mgrid[:8, :8].sum(axis=0) * tidegates.METERS_PER_FOOT zones = numpy.array([[ -1, 2, 2, 2, 2, 2, 2, -1, ], [ -1, 2, 2, 2, -1, 2, 2, -1, ], [ 1, 1, 1, 1, -1, 2, 2, -1, ], [ 1, 1, 1, 1, -1, 2, 2, -1, ], [ 1, -1, 1, 1, 2, 2, 2, 2, ], [ -1, -1, 1, 1, 2, 2, 2, 2, ], [ -1, -1, -1, -1, -1, -1, -1, -1, ], [ -1, -1, -1, -1, -1, -1, -1, -1, ]]) template = utils.RasterTemplate(8, 4, 6) ws = resource_filename('tidegates.testing', 'flood_area') filename = 'test_flood_area_output.shp' with utils.WorkSpace(ws), utils.OverwriteState(True): floods = tidegates.flood_area( topo_array=topo, zones_array=zones, template=template, ID_column='GeoID', elevation_feet=5, filename=filename, cleanup=True, verbose=False, ) nt.assert_true(isinstance(floods, arcpy.mapping.Layer)) tgtest.assert_shapefiles_are_close( resource_filename('tidegates.testing.flood_area', 'known_flood_area_output.shp'), resource_filename('tidegates.testing.flood_area', filename)) utils.cleanup_temp_results(floods)