Пример #1
0
    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')
Пример #2
0
    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'),
        )
Пример #3
0
    def test_false_true(self):
        arcpy.env.overwriteOutput = False

        nt.assert_false(arcpy.env.overwriteOutput)
        with utils.OverwriteState(True):
            nt.assert_true(arcpy.env.overwriteOutput)

        nt.assert_false(arcpy.env.overwriteOutput)
Пример #4
0
    def test_single_squeeze_true(self):
        with utils.OverwriteState(True):
            newlayer = utils.copy_data(self.destfolder,
                                       *self.srclayers[:1],
                                       squeeze=True)

        nt.assert_true(isinstance(newlayer, arcpy.mapping.Layer))

        nt.assert_true(isinstance(newlayer, arcpy.mapping.Layer))
        tgtest.assert_shapefiles_are_close(self.output[0], self.srclayers[0])
Пример #5
0
    def test_single_squeeze_false(self):
        with utils.OverwriteState(True):
            newlayers = utils.copy_data(self.destfolder, *self.srclayers[:1])

        nt.assert_true(isinstance(newlayers, list))

        for newlyr, newname, oldname in zip(newlayers[:1], self.output[:1],
                                            self.srclayers[:1]):
            nt.assert_true(isinstance(newlyr, arcpy.mapping.Layer))
            tgtest.assert_shapefiles_are_close(newname, oldname)
Пример #6
0
def test_concat_results():
    known = resource_filename('tidegates.testing.concat_results', 'known.shp')
    with utils.OverwriteState(True):
        test = utils.concat_results(
            resource_filename('tidegates.testing.concat_results',
                              'result.shp'),
            resource_filename('tidegates.testing.concat_results',
                              'input1.shp'),
            resource_filename('tidegates.testing.concat_results',
                              'input2.shp'))

    nt.assert_true(isinstance(test, arcpy.mapping.Layer))
    tgtest.assert_shapefiles_are_close(test.dataSource, known)

    utils.cleanup_temp_results(test)
Пример #7
0
def test_raster_to_polygons_with_new_field():
    zonefile = resource_filename("tidegates.testing.raster_to_polygons",
                                 "input_raster_to_polygon.tif")
    knownfile = resource_filename("tidegates.testing.raster_to_polygons",
                                  "known_polygons_from_raster_2.shp")
    testfile = resource_filename("tidegates.testing.raster_to_polygons",
                                 "test_polygons_from_raster_2.shp")

    with utils.OverwriteState(True):
        zones = utils.load_data(zonefile, 'raster')
        known = utils.load_data(knownfile, 'layer')
        test = utils.raster_to_polygons(zones, testfile, newfield="GeoID")

    tgtest.assert_shapefiles_are_close(test.dataSource, known.dataSource)
    utils.cleanup_temp_results(testfile)
Пример #8
0
def test_join_results_to_baseline():
    known = resource_filename('tidegates.testing.join_results',
                              'merge_result.shp')
    with utils.OverwriteState(True):
        test = utils.join_results_to_baseline(
            resource_filename('tidegates.testing.join_results',
                              'merge_result.shp'),
            resource_filename('tidegates.testing.join_results',
                              'merge_join.shp'),
            resource_filename('tidegates.testing.join_results',
                              'merge_baseline.shp'))
    nt.assert_true(isinstance(test, arcpy.mapping.Layer))
    tgtest.assert_shapefiles_are_close(test.dataSource, known)

    utils.cleanup_temp_results(test)
Пример #9
0
def test_aggregate_polygons():
    inputfile = resource_filename("tidegates.testing.aggregate_polygons",
                                  "input_polygons_from_raster.shp")
    knownfile = resource_filename("tidegates.testing.aggregate_polygons",
                                  "known_dissolved_polygons.shp")
    testfile = resource_filename("tidegates.testing.aggregate_polygons",
                                 "test_dissolved_polygons.shp")

    with utils.OverwriteState(True):
        raw = utils.load_data(inputfile, 'layer')
        known = utils.load_data(knownfile, 'layer')
        test = utils.aggregate_polygons(raw, "gridcode", testfile)

    tgtest.assert_shapefiles_are_close(test.dataSource, known.dataSource)

    utils.cleanup_temp_results(testfile)
Пример #10
0
    def test_finish_results_no_source(self):
        results = [
            resource_filename('tidegates.testing.finish_result', 'res1.shp'),
            resource_filename('tidegates.testing.finish_result', 'res2.shp'),
            resource_filename('tidegates.testing.finish_result', 'res3.shp')
        ]
        with utils.OverwriteState(True):
            self.tbx.finish_results(resource_filename(
                'tidegates.testing.finish_result', 'finished_no_src.shp'),
                                    results,
                                    cleanup=False)

        tgtest.assert_shapefiles_are_close(
            resource_filename('tidegates.testing.finish_result',
                              'finished_no_src.shp'),
            resource_filename('tidegates.testing.finish_result',
                              'known_finished_no_src.shp'),
        )
Пример #11
0
    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))
Пример #12
0
    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')
Пример #13
0
def test_intersect_polygon_layers():
    input1_file = resource_filename("tidegates.testing.intersect_polygons",
                                    "intersect_input1.shp")
    input2_file = resource_filename("tidegates.testing.intersect_polygons",
                                    "intersect_input2.shp")
    known_file = resource_filename("tidegates.testing.intersect_polygons",
                                   "intersect_known.shp")
    output_file = resource_filename("tidegates.testing.intersect_polygons",
                                    "intersect_output.shp")

    with utils.OverwriteState(True):
        output = utils.intersect_polygon_layers(
            output_file,
            input1_file,
            input2_file,
        )

    nt.assert_true(isinstance(output, arcpy.mapping.Layer))
    tgtest.assert_shapefiles_are_close(output_file, known_file)

    utils.cleanup_temp_results(output)
Пример #14
0
    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)
Пример #15
0
    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')
Пример #16
0
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)
Пример #17
0
    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,
                )
Пример #18
0
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)