def test_snap_poi_02(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) thr = int(fd.get_ncells() * 0.01) # Obtenemos 20 puntos aleatorios x1, x2, y1, y2 = fd.get_extent() xi = (x2 - x1) * np.random.random(25) + x1 yi = (y2 - y1) * np.random.random(25) + y1 puntos = np.array((xi, yi)).T # Obtenemos lista con Flow Accumulations cells fac = fd.get_flow_accumulation(nodata=False, asgrid=False) ch_cells = np.where(fac.ravel() >= thr)[0] # Hacemos snap a celdas de canal snap_pp = fd.snap_points(puntos, thr, kind="channel") row, col = fd.xy_2_cell(snap_pp[:, 0], snap_pp[:, 1]) inds = fd.cell_2_ind(row, col) # Comprobamos que punto esta entre los POI for ind in inds: res = ind in ch_cells self.assertEqual(res, True)
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))
""" from topopy import Flow, Network, DEM import numpy as np import matplotlib.pyplot as plt # Get DEM, Flow and Network dem = DEM("../data/in/jebja30.tif") fd = Flow(dem) net = Network(dem, fd, 1000) # Head x = 577413 y = 496519 row, col = fd.xy_2_cell(x, y) ix = fd.cell_2_ind(row, col) def get_channel(fd, ix): ixcix = np.zeros(fd._ncells, np.int) ixcix[fd._ix] = np.arange(len(fd._ix)) channel_points = [ix] new_ind = ix while ixcix[new_ind] != 0: new_ind = fd._ixc[ixcix[new_ind]] channel_points.append(new_ind) return channel_points def get_channel2(net, ix): channel = [ix] while ix.size > 0: idx = np.where(net._ix == ix)
w = fac > threshold w = w.ravel() I = w[fd._ix] ix = fd._ix[I] ixc = fd._ixc[I] ixcix = np.zeros(fd._ncells, np.int) ixcix[fd._ix] = np.arange(len(fd._ix)) ixcix[ix] = np.arange(len(ix)) x = 507194.338 y = 4060151.087 row, col = fd.xy_2_cell(x, y) channel_ix = fd.cell_2_ind(row, col) #channel_points = [channel_ix] # #new_ind = channel_ix #while ixcix[new_ind] != 0: # new_ind = fd._ixc[ixcix[new_ind]] # channel_points.append(new_ind) # ##while ixcix[channel_points[-1]] != 0: ## channel_points.append(fd._ixc[ixcix[channel_points[-1]]]) # #marr = np.zeros(fd._ncells, np.int) #marr[channel_points] = 1 #marr = marr.reshape(fd._dims) #plt.imshow(marr)