Ejemplo n.º 1
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)
Ejemplo n.º 2
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)
Ejemplo n.º 3
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)
Ejemplo n.º 4
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)
Ejemplo n.º 5
0
def test_RasterTemplate_from_raster():
    _raster = resource_filename('tidegates.testing._Template', 'dem.tif')
    raster = utils.load_data(_raster, 'raster')
    template = utils.RasterTemplate.from_raster(raster)
    nt.assert_equal(template.meanCellWidth, raster.meanCellWidth)
    nt.assert_equal(template.meanCellHeight, raster.meanCellHeight)
    nt.assert_equal(template.extent.lowerLeft.X, raster.extent.lowerLeft.X)
    nt.assert_equal(template.extent.lowerLeft.Y, raster.extent.lowerLeft.Y)
Ejemplo n.º 6
0
    def test_actual_arrays(self):
        known_raster_file = resource_filename("tidegates.testing.polygons_to_raster", "test_zones_raster.tif")
        known_raster = utils.load_data(known_raster_file, 'raster')
        raster = utils.polygons_to_raster(self.testfile, "GeoID", **self.kwargs)
        arrays = utils.rasters_to_arrays(raster, known_raster)
        arcpy.management.Delete(raster)

        nptest.assert_array_almost_equal(*arrays)
Ejemplo n.º 7
0
def test_RasterTemplate_from_raster():
    _raster = resource_filename('tidegates.testing._Template', 'dem.tif')
    raster = utils.load_data(_raster, 'raster')
    template = utils.RasterTemplate.from_raster(raster)
    nt.assert_equal(template.meanCellWidth, raster.meanCellWidth)
    nt.assert_equal(template.meanCellHeight, raster.meanCellHeight)
    nt.assert_equal(template.extent.lowerLeft.X, raster.extent.lowerLeft.X)
    nt.assert_equal(template.extent.lowerLeft.Y, raster.extent.lowerLeft.Y)
Ejemplo n.º 8
0
    def test_actual_arrays(self):
        known_raster_file = resource_filename(
            "tidegates.testing.polygons_to_raster", "test_zones_raster.tif")
        known_raster = utils.load_data(known_raster_file, 'raster')
        raster = utils.polygons_to_raster(self.testfile, "GeoID",
                                          **self.kwargs)
        arrays = utils.rasters_to_arrays(raster, known_raster)
        arcpy.management.Delete(raster)

        nptest.assert_array_almost_equal(*arrays)
Ejemplo n.º 9
0
    def finish_results(outputname, results, **kwargs):
        """ Merges and cleans up compiled output from `analyze`.

        Parameters
        ----------
        outputname : str
            Path to where the final file sould be saved.
        results : list of str
            Lists of all of the floods, flooded wetlands, and flooded
            buildings, respectively, that will be merged and deleted.
        sourcename : str, optional
            Path to the original source file of the results. If
            provided, its attbutes will be spatially joined to the
            concatenated results.

        Returns
        -------
        None

        """

        sourcename = kwargs.pop('sourcename', None)
        cleanup = kwargs.pop('cleanup', True)

        if outputname is not None:
            if sourcename is not None:
                tmp_fname = utils.create_temp_filename(outputname, filetype='shape')
                utils.concat_results(tmp_fname, *results)
                utils.join_results_to_baseline(
                    outputname,
                    utils.load_data(tmp_fname, 'layer'),
                    utils.load_data(sourcename, 'layer')
                )
                utils.cleanup_temp_results(tmp_fname)

            else:
                utils.concat_results(outputname, *results)

        if cleanup:
            utils.cleanup_temp_results(*results)
Ejemplo n.º 10
0
    def finish_results(outputname, results, **kwargs):
        """ Merges and cleans up compiled output from `analyze`.

        Parameters
        ----------
        outputname : str
            Path to where the final file sould be saved.
        results : list of str
            Lists of all of the floods, flooded wetlands, and flooded
            buildings, respectively, that will be merged and deleted.
        sourcename : str, optional
            Path to the original source file of the results. If
            provided, its attbutes will be spatially joined to the
            concatenated results.

        Returns
        -------
        None

        """

        sourcename = kwargs.pop('sourcename', None)
        cleanup = kwargs.pop('cleanup', True)

        if outputname is not None:
            if sourcename is not None:
                tmp_fname = utils.create_temp_filename(outputname, filetype='shape')
                utils.concat_results(tmp_fname, *results)
                utils.join_results_to_baseline(
                    outputname,
                    utils.load_data(tmp_fname, 'layer'),
                    utils.load_data(sourcename, 'layer')
                )
                utils.cleanup_temp_results(tmp_fname)

            else:
                utils.concat_results(outputname, *results)

        if cleanup:
            utils.cleanup_temp_results(*results)
Ejemplo n.º 11
0
def test_clip_dem_to_zones():
    demfile = resource_filename("tidegates.testing.clip_dem_to_zones", 'test_dem.tif')
    zonefile = resource_filename("tidegates.testing.clip_dem_to_zones", "test_zones_raster_small.tif")
    raster = utils.clip_dem_to_zones(demfile, zonefile)

    zone_r = utils.load_data(zonefile, 'raster')

    arrays = utils.rasters_to_arrays(raster, zone_r)

    dem_a, zone_a = arrays[0], arrays[1]
    arcpy.management.Delete(raster)

    nt.assert_true(isinstance(raster, arcpy.Raster))

    known_shape = (146, 172)
    nt.assert_tuple_equal(dem_a.shape, zone_a.shape)
Ejemplo n.º 12
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)
Ejemplo n.º 13
0
def test_clip_dem_to_zones():
    demfile = resource_filename("tidegates.testing.clip_dem_to_zones",
                                'test_dem.tif')
    zonefile = resource_filename("tidegates.testing.clip_dem_to_zones",
                                 "test_zones_raster_small.tif")
    raster = utils.clip_dem_to_zones(demfile, zonefile)

    zone_r = utils.load_data(zonefile, 'raster')

    arrays = utils.rasters_to_arrays(raster, zone_r)

    dem_a, zone_a = arrays[0], arrays[1]
    arcpy.management.Delete(raster)

    nt.assert_true(isinstance(raster, arcpy.Raster))

    known_shape = (146, 172)
    nt.assert_tuple_equal(dem_a.shape, zone_a.shape)
Ejemplo n.º 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)
Ejemplo n.º 15
0
 def test_raster_as_layer_not_greedy(self):
     x = utils.load_data(self.rasterpath, 'layer', greedyRasters=False)
     nt.assert_true(isinstance(x, arcpy.mapping.Layer))
Ejemplo n.º 16
0
 def test_vector_as_raster_should_fail(self):
     x = utils.load_data(self.vectorpath, 'raster')
Ejemplo n.º 17
0
 def test_raster_as_grid_with_caps(self):
     x = utils.load_data(self.rasterpath, 'gRId')
     nt.assert_true(isinstance(x, arcpy.Raster))
Ejemplo n.º 18
0
 def test_vector_as_layer_with_caps(self):
     x = utils.load_data(self.vectorpath, 'LAyeR')
     nt.assert_true(isinstance(x, arcpy.mapping.Layer))
Ejemplo n.º 19
0
 def test_vector_as_grid_should_fail(self):
     x = utils.load_data(self.vectorpath, 'grid')
Ejemplo n.º 20
0
 def test_raster_as_layer_not_greedy(self):
     x = utils.load_data(self.rasterpath, 'layer', greedyRasters=False)
     nt.assert_true(isinstance(x, arcpy.mapping.Layer))
Ejemplo n.º 21
0
 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()
Ejemplo n.º 22
0
    def test_already_a_raster(self):
        raster = arcpy.Raster(self.rasterpath)
        x = utils.load_data(raster, 'raster')
        nt.assert_true(isinstance(x, arcpy.Raster))

        nptest.assert_array_almost_equal(*utils.rasters_to_arrays(x, raster))
Ejemplo n.º 23
0
 def test_already_a_layer(self):
     lyr = arcpy.mapping.Layer(self.vectorpath)
     x = utils.load_data(lyr, 'layer')
     nt.assert_equal(x, lyr)
Ejemplo n.º 24
0
 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()
Ejemplo n.º 25
0
 def test_vector_as_layer_with_caps(self):
     x = utils.load_data(self.vectorpath, 'LAyeR')
     nt.assert_true(isinstance(x, arcpy.mapping.Layer))
Ejemplo n.º 26
0
 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()
Ejemplo n.º 27
0
    def test_already_a_raster(self):
        raster = arcpy.Raster(self.rasterpath)
        x = utils.load_data(raster, 'raster')
        nt.assert_true(isinstance(x, arcpy.Raster))

        nptest.assert_array_almost_equal(*utils.rasters_to_arrays(x, raster))
Ejemplo n.º 28
0
 def test_raster_as_grid_with_caps(self):
     x = utils.load_data(self.rasterpath, 'gRId')
     nt.assert_true(isinstance(x, arcpy.Raster))
Ejemplo n.º 29
0
 def test_raster_as_layer_greedy(self):
     x = utils.load_data(self.rasterpath, 'layer')
     nt.assert_true(isinstance(x, arcpy.Raster))
Ejemplo n.º 30
0
 def test_datapath_doesnt_exist(self):
     utils.load_data('junk.shp', 'grid')
Ejemplo n.º 31
0
 def test_vector_as_shape(self):
     x = utils.load_data(self.vectorpath, 'shape')
     nt.assert_true(isinstance(x, arcpy.mapping.Layer))
Ejemplo n.º 32
0
 def test_raster_as_layer_greedy(self):
     x = utils.load_data(self.rasterpath, 'layer')
     nt.assert_true(isinstance(x, arcpy.Raster))
Ejemplo n.º 33
0
 def test_already_a_layer(self):
     lyr = arcpy.mapping.Layer(self.vectorpath)
     x = utils.load_data(lyr, 'layer')
     nt.assert_equal(x, lyr)
Ejemplo n.º 34
0
 def test_bad_datatype(self):
     utils.load_data(self.rasterpath, 'JUNK')
Ejemplo n.º 35
0
 def test_vector_as_grid_should_fail(self):
     x = utils.load_data(self.vectorpath, 'grid')
Ejemplo n.º 36
0
 def test_vector_as_raster_should_fail(self):
     x = utils.load_data(self.vectorpath, 'raster')
Ejemplo n.º 37
0
 def test_datapath_bad_value(self):
     utils.load_data(12345, 'grid')
Ejemplo n.º 38
0
 def test_bad_datatype(self):
     utils.load_data(self.rasterpath, 'JUNK')
Ejemplo n.º 39
0
 def test_datapath_doesnt_exist(self):
     utils.load_data('junk.shp', 'grid')
Ejemplo n.º 40
0
 def test_datapath_bad_value(self):
     utils.load_data(12345, 'grid')
Ejemplo n.º 41
0
 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()
Ejemplo n.º 42
0
 def test_vector_as_shape(self):
     x = utils.load_data(self.vectorpath, 'shape')
     nt.assert_true(isinstance(x, arcpy.mapping.Layer))