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)
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 processAlgorithm(self, parameters, context, feedback): dem_raster = self.parameterAsRasterLayer(parameters, self.INPUT_DEM, context) fac_raster = self.parameterAsOutputLayer(parameters, self.OUTPUT_FAC, context) fd_raster = self.parameterAsOutputLayer(parameters, self.OUTPUT_FD, context) verbose = self.parameterAsBool(parameters, self.VERBOSE, context) filled = self.parameterAsBool(parameters, self.FILLED, context) auxtopo = self.parameterAsBool(parameters, self.AUXTOPO, context) dem = DEM(dem_raster.source()) fd = Flow(dem, auxtopo=auxtopo, filled=filled, verbose=verbose, verb_func=feedback.setProgressText) fac = fd.get_flow_accumulation() fd.save_gtiff(fd_raster) fac.save(fac_raster) results = {} results[self.OUTPUT_FD] = fd_raster results[self.OUTPUT_FAC] = fac_raster return results
# -*- coding: utf-8 -*- 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():
@author: vicen """ import sys sys.path.append("../../") from topopy import Flow, Grid import numpy as np from scipy.sparse import csc_matrix import matplotlib.pyplot as plt fd = Flow() fd.load_gtiff("../data/in/fd_tunez.tif") threshold = 1000 fac = fd.get_flow_accumulation(nodata=False, asgrid=False) 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)