def test_copyDEM(self): files = ["small25", "jebja30", "tunez"] for file in files: dem = DEM("{}/{}.tif".format(infolder, file)) dem2 = dem.copy() computed = np.array_equal(dem._array, dem2._array) self.assertEqual(computed, True)
def test_fill_01(self): arr = np.array([[49, 36, 29, 29], [32, 19, 17, 20], [19, 18, 31, 39], [19, 29, 42, 51]]) dem = DEM() dem.set_array(arr) fill = dem.fill_sinks() computed = fill.read_array().tolist() arr = np.array([[49, 36, 29, 29], [32, 19, 19, 20], [19, 19, 31, 39], [19, 29, 42, 51]]) expected = arr.tolist() self.assertEqual(computed, expected)
def test_fill_05(self): dem = DEM(infolder + "/tunez.tif") fill = dem.fill_sinks().read_array().astype("int16") mfill = sio.loadmat(infolder + '/mlab_files/fill_tunez.mat')['fill'] mfill = mfill.astype("int16") # Matlab files contain "nan" in the nodata positions nodatapos = dem.get_nodata_pos() mfill[nodatapos] = dem.get_nodata() computed = np.array_equal(fill, mfill) self.assertEqual(computed, True)
def test_BasinClass01(self): files = ["small25", "jebja30", "tunez"] for file in files: # Cargamos flow flw_path = "{}/{}_fd.tif".format(infolder, file) dem_path = "{}/{}.tif".format(infolder, file) dem = DEM(dem_path) fd = Flow(flw_path) # Obtenemos cuencas cuencas = fd.get_drainage_basins(min_area=0.0025) # Mayor, menor, y random bids, counts = np.unique(cuencas.read_array(), return_counts=True) if 0 in bids: bids = bids[1:] counts = counts[1:] idmax = bids[np.argmax(counts)] idmin = bids[np.argmin(counts)] idrdn = np.random.choice(bids) idxs = [idmax, idmin, idrdn] lbls = ["max", "min", "rdn"] for n in range(3): basin = Basin(dem, cuencas, idxs[n]) basin_path = "{}/{}_basin{}.tif".format( outfolder, file, lbls[n]) basin.save(basin_path) basin2 = Basin(basin_path) computed = np.array_equal(basin.read_array(), basin2.read_array()) self.assertEqual(computed, True)
def test_BNetwork_class01(self): """ Test00 Crea BNetwork para cuencas de prueba a partir de un objeto Basin Sin utilizar cabeceras """ files = ["small25", "jebja30", "tunez"] for file in files: # Cargamos DEM, Flow, Network fd = Flow("{}/{}_fd.tif".format(infolder, file)) dem = DEM("{}/{}.tif".format(infolder, file)) net = Network("{}/{}_net.dat".format(infolder, file)) # Cargamos outlets y generamos cuencas outlets = np.loadtxt("{}/{}_bnet_outlets.txt".format( infolder, file), delimiter=";") outlets = net.snap_points(outlets) cuencas = fd.get_drainage_basins(outlets) for bid in np.unique(cuencas.read_array()): if bid == 0: continue basin = Basin(dem, cuencas, bid) bnet = BNetwork(net, basin) # Este test solo verifica que se realice sin fallos y que # el objeto bnet tiene una única cabecera bnet = BNetwork(net, cuencas, None, bid) self.assertEqual(int(bnet._heads[0]), self.results[file][bid])
def test_create_Flow_03(self): # Create Flow object with previously filled DEM (auxtopo is ignored) files = ["small", "small25", "morocco", "tunez", "jebja30"] for file in files: dem_path = infolder + "/{0}_fil.tif".format(file) flw_path = infolder + "/{0}_fd.tif".format(file) dem = DEM(dem_path) fd = Flow(dem, auxtopo=False, filled=True, verbose=False) # Flow obj with filled dem and without auxiliar topography fd2 = Flow(flw_path) computed = np.array_equal(fd2._ix, fd._ix) self.assertEqual(computed, True)
def test_identify_flats_02(self): # Create a DEM object and make fill dem = DEM(infolder + "/small25.tif") fill = dem.fill_sinks() # Identify flats and sills and load arrays flats, sills = fill.identify_flats(nodata=False) flats = flats.read_array() sills = sills.read_array() # Load matlab flats and sills m_flats = sio.loadmat(infolder + "/mlab_files/flats_small25.mat")['flats'] m_sills = sio.loadmat(infolder + "/mlab_files/sills_small25.mat")['sills'] # Compare computed = (np.array_equal(m_flats, flats), np.array_equal(m_sills, sills)) self.assertEqual(computed, (True, True))
def test_create_Flow_02(self): # Create Flow object with auxiliar topography files = ["small", "small25", "tunez", "jebja30"] for file in files: dem_path = infolder + "/{0}.tif".format(file) flw_path = infolder + "/{0}_fd_auxtp.tif".format(file) dem = DEM(dem_path) fd = Flow(dem, auxtopo=True, filled=False, verbose=False) # Flow obj with auxiliar topography fd2 = Flow(flw_path) computed = np.array_equal(fd2._ix, fd._ix) self.assertEqual(computed, True)
def test_flow_properties_02(self): # Testing an empty Flow object dem = DEM() fd = Flow() computed = (fd.get_size(), fd.get_dims(), fd.get_ncells(), fd.get_projection(), fd.get_cellsize(), fd.get_geotransform()) expected = (dem.get_size(), dem.get_dims(), dem.get_ncells(), dem.get_projection(), dem.get_cellsize(), dem.get_geotransform()) self.assertEqual(computed, expected)
def test_flow_properties_01(self): files = ["small", "small25", "morocco", "tunez", "jebja30"] for file in files: dem = DEM(infolder + "/{0}.tif".format(file)) fd = Flow(infolder + "/{0}_fd.tif".format(file)) computed = (fd.get_size(), fd.get_dims(), fd.get_ncells(), fd.get_projection(), fd.get_cellsize(), fd.get_geotransform()) expected = (dem.get_size(), dem.get_dims(), dem.get_ncells(), dem.get_projection(), dem.get_cellsize(), dem.get_geotransform()) self.assertEqual(computed, expected)
def test_stream_poi_02(self): dem_files = ['tunez.tif', 'small25.tif', "jebja30.tif"] for file in dem_files: dem = DEM(infolder + "/" + file) fd = Flow(dem) thr = int(fd.get_ncells() * 0.01) net = Network(fd, dem, thr) out01 = fd.get_stream_poi(thr, "confluences", "CELL") out02 = net.get_stream_poi("confluences", "CELL") computed = np.array_equal(out01, out02) print(file) self.assertEqual(computed, True)
def test_fill_03(self): arr = np.array([[49, 36, 29, 29], [32, 17, 12, 20], [19, 17, 19, 39], [19, 29, 42, -99]]) dem = DEM() dem.set_nodata(-99) dem.set_array(arr) fill = dem.fill_sinks() computed = fill.read_array(False).tolist() arr = np.array([[49, 36, 29, 29], [32, 19, 19, 20], [19, 19, 19, 39], [19, 29, 42, -99]]) expected = arr.tolist() self.assertEqual(computed, expected)
def test_BNetwork_class05(self): """ Test de creado masivo de cuencas con cabeceras aleatorias """ files = ["small25", "jebja30", "tunez"] for file in files: # Cargamos DEM, Flow, Network fd = Flow("{}/{}_fd.tif".format(infolder, file)) net = Network("{}/{}_net.dat".format(infolder, file)) dem = DEM("{}/{}.tif".format(infolder, file)) # Generamos todas las cuencas cuencas = fd.get_drainage_basins(min_area=0.0025) # Generamos 50 puntos aleatorios dentro de la extensión del objeto Network # Estos 50 puntos se usaran como cabeceras xmin, xmax, ymin, ymax = net.get_extent() xi = np.random.randint(xmin, xmax, 50) yi = np.random.randint(ymin, ymax, 50) heads = np.array((xi, yi)).T # Cogemos 5 cuencas aleatorias bids = np.random.choice(np.unique(cuencas.read_array())[1:], 5) for bid in bids: try: if np.random.randint(100) < 70: bnet = BNetwork(net, cuencas, heads, bid) else: basin = Basin(dem, cuencas, bid) bnet = BNetwork(net, basin, heads) except NetworkError: print( "Network of {} file inside the basin {} has not enough pixels" .format(file, bid)) continue # Salvamos BNetwork y volvemos a cargar para comprobar que se cargan-guardan bien bnet_path = "{}/{}_{}_bnet.dat".format(outfolder, file, bid) bnet.save(bnet_path) bnet2 = BNetwork(bnet_path) computed = np.array_equal(bnet._ix, bnet2._ix) self.assertEqual(computed, True) # borramos archivo os.remove(bnet_path)
def test_stream_poi_01(self): files = ["small25", "morocco", "tunez", "jebja30"] for file in files: flw_path = infolder + "/{0}_fd.tif".format(file) dem_path = infolder + "/{0}.tif".format(file) net_path = infolder + "/{0}_network.net".format(file) dem = DEM(dem_path) fd = Flow(flw_path) thr = int(fd.get_ncells() * 0.01) # Creamos objeto network net = Network(dem, fd, thr) net.save(net_path) # Cargamos objeto network guardado net2 = Network(net_path) # Comparamos propiedades, prop_net = [ net._size, net._dims, net._geot, net._cellsize, net._ncells, net._proj ] prop_net2 = [ net2._size, net2._dims, net2._geot, net2._cellsize, net2._ncells, net2._proj ] self.assertEqual(prop_net, prop_net2) # Comparamos los datos arr1 = np.array( (net._ix, net._ixc, net._ax, net._dx, net._zx, net._chi, net._slp, net._ksn, net._r2slp, net._r2ksn, net._dd)) arr2 = np.array( (net2._ix, net2._ixc, net2._ax, net2._dx, net2._zx, net2._chi, net2._slp, net2._ksn, net2._r2slp, net2._r2ksn, net2._dd)) res = np.array_equal(arr1, arr2) self.assertEqual(res, True)
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 -*- """ Editor de Spyder Este es un archivo temporal """ import numpy as np from topopy import DEM, Flow, Network import matplotlib.pyplot as plt import gdal, ogr, osr dem = DEM("../data/in/jebja30.tif") fd = Flow("../data/in/jebja30_fd.tif") net = Network(dem, fd, 2000) path = "../data/out/jebja_streams.shp" # Prepare auxiliar arrays seg_array = net.get_stream_segments(False).ravel() seg_array = seg_array[net._ix] ord_array = net.get_stream_order('strahler', False).ravel() ord_array = ord_array[net._ix] ixcix = np.zeros(net._ncells, np.int) ixcix[net._ix] = np.arange(net._ix.size) # Create output shapefile driver = ogr.GetDriverByName("ESRI Shapefile") dataset = driver.CreateDataSource(path) sp = osr.SpatialReference() sp.ImportFromWkt(net._proj) layer = dataset.CreateLayer("rios", sp, ogr.wkbLineString) layer.CreateField(ogr.FieldDefn("sid", ogr.OFTInteger))
# -*- coding: utf-8 -*- """ Created on 08 October, 2018 Testing suite for Network export_to_shp function @author: J. Vicente Perez @email: [email protected] @date: 23 October, 2018 """ from topopy import Network, DEM, Flow, Grid import matplotlib.pyplot as plt import numpy as np infolder = "../data/in" outfolder = "../data/out" dem = DEM(infolder + "/morocco.tif") fd = Flow(dem) net = Network(dem, fd, 1500) streams = net.get_streams() streams.save(outfolder + "/canales_orig.tif") outlet = np.array([579213, 504282]).reshape(1, 2) basin = fd.get_drainage_basins(net.snap_points(outlet), asgrid=False) c1 = basin.max(axis=0).argmax() r1 = basin.max(axis=1).argmax() c2 = basin.shape[1] - np.fliplr(basin).max(axis=0).argmax() r2 = basin.shape[0] - np.flipud(basin).max(axis=1).argmax() basin_cl = basin[r1:r2, c1:c2]
@author: vicen """ import sys import numpy as np from scipy import ndimage import time # Add to the path code folder and data folder sys.path.append("../../") from topopy import DEM from skimage import graph start = time.time() # Get DEM and fill sinks dem = DEM("../data/tunez2.tif") dims = dem._array.shape ncells = dem._array.size time_lap = time.time() # 01 Fill sinks fill = dem.fill_sinks() topodiff = fill.read_array() - dem.read_array() topodiff = topodiff.astype(np.float32) dem = fill print("Sinks filled -- {0:.3f} seconds".format(time.time() - time_lap)) time_lap = time.time() # 02 Get flats and sills flats, sills = dem.identify_flats(False) print("Flats identified -- {0:.3f} seconds".format(time.time() - time_lap))
import sys sys.path.append("../../") from topopy import Flow, Grid, DEM import numpy as np from scipy.sparse import csc_matrix import matplotlib.pyplot as plt arr = np.array([[49, 50, 50, 50], [49, 47, 47, 46], [49, 48, 45, 45], [49, 47, 46, 43], [46, 47, 47, 47]], dtype=np.int16) dem = DEM() dem.set_array(arr) flow = Flow(dem) ixcix = np.zeros(flow._ncells, np.int) ixcix[flow._ix] = np.arange(len(flow._ix)) new_ind = 0 channel_points = [new_ind] while ixcix[new_ind] != 0: new_ind = flow._ixc[ixcix[new_ind]] channel_points.append(new_ind) print(flow._ix) print(flow._ixc) print(ixcix)
def test_empty_dem02(self): dem = DEM() computed = np.array([[0]], dtype=np.float) expected = dem._array self.assertEqual(np.array_equal(computed, expected), True)
sum_arr = np.asarray(np.sum(sp_arr, 0)).ravel() out_pos = (sum_arr == 0) & w elif kind == 'confluences': sum_arr = np.asarray(np.sum(sp_arr, 0)).ravel() out_pos = sum_arr > 1 elif kind == 'outlets': sum_arr = np.asarray(np.sum(sp_arr, 1)).ravel() out_pos = (sum_arr == 0) & w out_pos = out_pos.reshape(fd._dims) row, col = np.where(out_pos) return row, col dem = DEM("../data/tunez.tif") fd = Flow() fd.load_gtiff("../data/tunez_fd.tif") fig, ax = plt.subplots() dem.plot(ax) streams = fd.get_network(1000) streams.plot(ax) # Heads row, col = get_stream_poi(fd, 1000, 'heads') ax.plot(col, row, "ro") # # Confluences row, col = get_stream_poi(fd, 1000, 'confluences') ax.plot(col, row, "bs")
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Tue Jan 23 17:14:39 2018 Temp testing script for Network @author: vicen """ import sys sys.path.append("../../") from topopy import DEM, Network from scipy.sparse import csc_matrix import numpy as np dem = DEM("../data/small25.tif") fd = Network() fd.load_gtiff("sample_fd.tif") fac = fd.flow_accumulation() fac.save("small25_fac.tif") nodata = fac.get_nodata_pos() fac = fac.read_array() fac[nodata] = 0 # Get pixels larger than threshold w = fac > 1000 siz = fd._dims nrc = fd._ncells w = w.ravel() I = w[fd._ix] ix = fd._ix[I] ixc = fd._ixc[I]
@author: vicen """ import warnings warnings.filterwarnings('ignore') import sys import numpy as np from scipy import ndimage # Add to the path code folder and data folder sys.path.append("../") from topopy import DEM from topopy.ext.distance import cost from mcompare import compare_indexes, compare_row_cols, compare_arrays, load_array from skimage import graph import scipy.io as sio dem = DEM("../data/tunez.tif") # USED TO DEBUG dims = dem._array.shape ncells = dem._array.size def _get_aux_topography(dem): """ This function calculate the auxiliary topography by using the topography in depressions to derive the most realistic flow paths. It uses as weight sweights the differences between the filled and the raw DEM. References: ----------- This algoritm is adapted to Python from TopoToolbox matlab codes by Wolfgang Schwanghart
# -*- coding: utf-8 -*- """ Editor de Spyder Este es un archivo temporal """ from topopy import DEM, Flow, Network infolder = "data/in" outfolder = "data/out" files = ["small25", "morocco", "tunez", "jebja30"] for file in files: flw_path = infolder + "/{0}_fd.tif".format(file) dem_path = infolder + "/{0}.tif".format(file) dem = DEM(dem_path) fd = Flow(flw_path) thr = int(fd.get_ncells() * 0.001) # Simplemente probamos que no hay fallos al crear el objeto net net = Network(dem, fd, thr) net.save(infolder + "/{0}_network.net".format(file))
def test_empty_dem(self): # test an empty DEM dem = DEM() computed = (dem._size, dem._geot, dem._proj, dem._nodata) expected = ((1, 1), (0.0, 1.0, 0.0, 1.0, 0.0, -1.0), "", -9999.0) self.assertEqual(computed, expected)