def test_stream_poi_02(self): # Test 10 random basins files = ["small25", "tunez", "jebja30"] for file in files: flw_path = infolder + "/{0}_fd.tif".format(file) fd = Flow(flw_path) thr = int(fd.get_ncells() * 0.0025) for kind in ["heads", "confluences", "outlets"]: poi = fd.get_stream_poi(thr, kind, "CELL") x, y = fd.cell_2_xy(poi[:, 0], poi[:, 1]) poi = np.array((x, y)).T spoi = np.loadtxt(infolder + "/{0}_{1}.txt".format(file, kind), delimiter=";", skiprows=1) compare = np.array_equal(poi, spoi) self.assertEqual(compare, True)
def test_drainage_basins_03(self): # Test extracting the biggest basin and input as a list [x, y] files = ["small25", "morocco", "tunez", "jebja30"] for file in files: flw_path = infolder + "/{0}_fd.tif".format(file) fd = Flow(flw_path) # Get coords of the highest flow accumulation fac = fd.get_flow_accumulation() maxval = fac.max() rowpos, colpos = np.where(fac.read_array() == maxval) xi, yi = fd.cell_2_xy(rowpos, colpos) # Extract basin outlets = np.array((xi, yi)).T basins = fd.get_drainage_basins(outlets) basins.save(outfolder + "/max_basin2_{0}".format(file))
def test_drainage_basins_01(self): # Test 10 random basins files = ["small25", "morocco", "tunez", "jebja30"] for file in files: flw_path = infolder + "/{0}_fd.tif".format(file) fd = Flow(flw_path) # Creamos 10 cuencas aleatorias ri = np.random.randint(0, fd._dims[0], 10) ci = np.random.randint(0, fd._dims[1], 10) xi, yi = fd.cell_2_xy(ri, ci) # Extract basins outlets = np.array((xi, yi)).T basins = fd.get_drainage_basins(outlets) basins.save(outfolder + "/rnd_basins_{0}".format(file))
def test_drainage_basins_00(self): # Test 10 random basins files = ["small25", "tunez", "jebja30"] for file in files: flw_path = infolder + "/{0}_fd.tif".format(file) fd = Flow(flw_path) # Creamos 10 cuencas aleatorias ri = np.random.randint(0, fd.get_dims()[0], 10) ci = np.random.randint(0, fd.get_dims()[1], 10) rdn_ids = np.random.randint(100, 700, 10) xi, yi = fd.cell_2_xy(ri, ci) # Extract basins outlets = np.array((xi, yi, rdn_ids)).T threshold = int(fd.get_ncells() * 0.05) snap_outlets = fd.snap_points(outlets, threshold, kind="channel") snap = np.append(snap_outlets, rdn_ids.reshape(rdn_ids.size, 1), 1) basins = fd.get_drainage_basins(snap) basins.save(outfolder + "/rnd_snap_basins_{0}.tif".format(file))
from topopy import DEM, Flow import numpy as np import ogr, gdal import matplotlib.pyplot as plt # Get DEM, Flow Direction and Flow Accumulation dem = DEM("../data/in/jebja30.tif") fd = Flow(dem) thr = 1000 fac = fd.get_flow_accumulation() row, col = np.where( np.logical_and(fac.read_array() > thr, fac.read_array() != fac.get_nodata())) x, y = fd.cell_2_xy(row, col) coords = np.array((x, y)).T geot = fac.get_geotransform() xmin = geot[0] xmax = geot[0] + fac.get_size()[0] * geot[1] ymax = geot[3] ymin = geot[3] + fac.get_size()[1] * geot[5] extent = (xmin, ymin, xmax, ymax) fig, ax = plt.subplots() ax.plot(x, y, "ro") class MyApp(): def __init__(self, extent, coords, ax):
class FlowValueTest(unittest.TestCase): def setUp(self): # Load test data self.ids = np.load(infolder + "/np_files/small25_100rnd_id.npy") self.rows = np.load(infolder + "/np_files/small25_100rnd_row.npy") self.cols = np.load(infolder + "/np_files/small25_100rnd_col.npy") self.xi = np.load(infolder + "/np_files/small25_100rnd_X.npy") self.yi = np.load(infolder + "/np_files/small25_100rnd_Y.npy") self.zi = np.load(infolder + "/np_files/small25_100rnd_Z.npy") # Load Flow object self.fd = Flow(infolder + "/small25_fd.tif") def test_xy_2_cell_01(self): xi = self.xi yi = self.yi rows = self.rows cols = self.cols expected = (rows, cols) computed = self.fd.xy_2_cell(xi, yi) comparison = (computed[0] == expected[0]).all() and (computed[1] == expected[1]).all() self.assertEqual(comparison, True) def test_xy_2_cell_02(self): ind = np.random.randint(0, 100) x = self.xi[ind] y = self.yi[ind] row = self.rows[ind] col = self.cols[ind] expected = (row, col) computed = self.fd.xy_2_cell(x, y) self.assertEqual(computed, expected) def test_xy_2_cell_03(self): xi = self.xi.tolist() yi = self.yi.tolist() rows = self.rows cols = self.cols crows, ccols = self.fd.xy_2_cell(xi, yi) computed = (np.array_equal(rows, crows), np.array_equal(cols, ccols)) self.assertEqual(computed, (True, True)) def test_ind_2_cell_01(self): idx = np.random.randint(0, 100) ind = self.ids[idx] row = self.rows[idx] col = self.cols[idx] expected = (row, col) computed = self.fd.ind_2_cell(ind) self.assertEqual(computed, expected) def test_ind_2_cell_02(self): ind = self.ids row = self.rows col = self.cols crow, ccol = self.fd.ind_2_cell(ind) computed = (np.array_equal(row, crow), np.array_equal(col, ccol)) self.assertEqual(computed, (True, True)) def test_ind_2_cell_03(self): ind = self.ids row = self.rows col = self.cols expected = (row, col) computed = self.fd.ind_2_cell(ind) comparison = (computed[0] == expected[0]).all() and (computed[1] == expected[1]).all() self.assertEqual(comparison, True) def test_cell_2_ind_01(self): idx = np.random.randint(0, 100) ind = self.ids[idx] row = self.rows[idx] col = self.cols[idx] expected = ind computed = self.fd.cell_2_ind(row, col) self.assertEqual(computed, expected) def test_cell_2_ind_02(self): ind = self.ids row = self.rows col = self.cols expected = ind computed = self.fd.cell_2_ind(row, col) res = np.array_equal(expected, computed) self.assertEqual(res, True) def test_cell_2_ind_03(self): ind = self.ids row = self.rows col = self.cols expected = ind computed = self.fd.cell_2_ind(row, col) comparison = (computed == expected).all() self.assertEqual(comparison, True) def test_cell_2_xy_01(self): xi = self.xi yi = self.yi rows = self.rows cols = self.cols expected = (xi, yi) computed = self.fd.cell_2_xy(rows, cols) res = (np.array_equal(expected[0], computed[0]), np.array_equal(expected[1], computed[1])) self.assertEqual(res, (True, True)) def test_cell_2_xy_02(self): ind = np.random.randint(0, 100) xi = self.xi[ind] yi = self.yi[ind] rows = self.rows[ind] cols = self.cols[ind] expected = (xi, yi) computed = self.fd.cell_2_xy(rows, cols) res = (np.array_equal(expected[0], computed[0]), np.array_equal(expected[1], computed[1])) self.assertEqual(res, (True, True)) def test_cell_2_xy_03(self): xi = self.xi.tolist() yi = self.yi.tolist() rows = self.rows cols = self.cols expected = (xi, yi) computed = self.fd.cell_2_xy(rows, cols) res = (np.array_equal(expected[0], computed[0]), np.array_equal(expected[1], computed[1])) self.assertEqual(res, (True, True))