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)
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)
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)
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)
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)
    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)
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)
    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)
    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)
Exemple #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)
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)
    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_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)
    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_raster_as_layer_not_greedy(self):
     x = utils.load_data(self.rasterpath, 'layer', greedyRasters=False)
     nt.assert_true(isinstance(x, arcpy.mapping.Layer))
 def test_vector_as_raster_should_fail(self):
     x = utils.load_data(self.vectorpath, 'raster')
 def test_raster_as_grid_with_caps(self):
     x = utils.load_data(self.rasterpath, 'gRId')
     nt.assert_true(isinstance(x, arcpy.Raster))
 def test_vector_as_layer_with_caps(self):
     x = utils.load_data(self.vectorpath, 'LAyeR')
     nt.assert_true(isinstance(x, arcpy.mapping.Layer))
 def test_vector_as_grid_should_fail(self):
     x = utils.load_data(self.vectorpath, 'grid')
 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))
 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_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))
 def test_already_a_layer(self):
     lyr = arcpy.mapping.Layer(self.vectorpath)
     x = utils.load_data(lyr, 'layer')
     nt.assert_equal(x, lyr)
 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 test_vector_as_layer_with_caps(self):
     x = utils.load_data(self.vectorpath, 'LAyeR')
     nt.assert_true(isinstance(x, arcpy.mapping.Layer))
 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_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))
 def test_raster_as_grid_with_caps(self):
     x = utils.load_data(self.rasterpath, 'gRId')
     nt.assert_true(isinstance(x, arcpy.Raster))
 def test_raster_as_layer_greedy(self):
     x = utils.load_data(self.rasterpath, 'layer')
     nt.assert_true(isinstance(x, arcpy.Raster))
 def test_datapath_doesnt_exist(self):
     utils.load_data('junk.shp', 'grid')
 def test_vector_as_shape(self):
     x = utils.load_data(self.vectorpath, 'shape')
     nt.assert_true(isinstance(x, arcpy.mapping.Layer))
 def test_raster_as_layer_greedy(self):
     x = utils.load_data(self.rasterpath, 'layer')
     nt.assert_true(isinstance(x, arcpy.Raster))
 def test_already_a_layer(self):
     lyr = arcpy.mapping.Layer(self.vectorpath)
     x = utils.load_data(lyr, 'layer')
     nt.assert_equal(x, lyr)
 def test_bad_datatype(self):
     utils.load_data(self.rasterpath, 'JUNK')
 def test_vector_as_grid_should_fail(self):
     x = utils.load_data(self.vectorpath, 'grid')
 def test_vector_as_raster_should_fail(self):
     x = utils.load_data(self.vectorpath, 'raster')
 def test_datapath_bad_value(self):
     utils.load_data(12345, 'grid')
 def test_bad_datatype(self):
     utils.load_data(self.rasterpath, 'JUNK')
 def test_datapath_doesnt_exist(self):
     utils.load_data('junk.shp', 'grid')
 def test_datapath_bad_value(self):
     utils.load_data(12345, 'grid')
 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 test_vector_as_shape(self):
     x = utils.load_data(self.vectorpath, 'shape')
     nt.assert_true(isinstance(x, arcpy.mapping.Layer))