def test_BNetwork_class04(self):
        """
        Test que prueba cabeceras en cuenca 1 con small25 (sin utilizar id field)
        474260.9;4114339.6;3
        474856.9;4114711.1;2           
        """
        # Cargamos DEM, Flow, Network
        fd = Flow("{}/{}_fd.tif".format(infolder, "small25"))
        net = Network("{}/{}_net.dat".format(infolder, "small25"))

        # Cargamos outlets, heads y generamos cuencas
        outlets = np.loadtxt("{}/{}_bnet_outlets.txt".format(
            infolder, "small25"),
                             delimiter=";")
        heads = np.loadtxt("{}/{}_bnet_heads.txt".format(infolder, "small25"),
                           delimiter=";")
        # Remove the id column
        heads = heads[:, :-1]
        outlets = net.snap_points(outlets)
        cuencas = fd.get_drainage_basins(outlets)

        bid = 1
        bnet = BNetwork(net, cuencas, heads, bid)
        self.assertEqual(np.array_equal(bnet._heads, np.array([16171, 13494])),
                         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])
Exemple #3
0
    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_drainage_basins_04(self):
     # Test extracting all basins (min_area = 10%)
     files = ["small25", "tunez", "jebja30"]
     for file in files:
         flw_path = infolder + "/{0}_fd.tif".format(file)
         fd = Flow(flw_path)
         # Extract all basin with min_area
         basins = fd.get_drainage_basins()
         basins.save(outfolder + "/all_basins-minarea_{0}.tif".format(file))
Exemple #5
0
 def test_drainage_basins_05(self):
     # Test extracting all basins
     files = ["small25", "morocco", "tunez", "jebja30"]
     for file in files:
         flw_path = infolder + "/{0}_fd.tif".format(file)
         fd = Flow(flw_path)
         # Extract all basin without min_area
         basins = fd.get_drainage_basins(min_area=0)
         basins.save(outfolder + "/all_basins{0}".format(file))
Exemple #6
0
 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")
Exemple #7
0
 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)
Exemple #8
0
 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_stream_poi_01(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, "XY")
             spoi = np.loadtxt(infolder +  "/{0}_{1}.txt".format(file, kind), delimiter=";", skiprows=1)
             compare = np.array_equal(poi, spoi)
             self.assertEqual(compare, True)
Exemple #10
0
    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_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)
Exemple #12
0
    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))
Exemple #13
0
 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))
Exemple #14
0
    def test_create_network(self):
        files = ["small25", "tunez", "jebja30"]

        for file in files:
            flw_path = infolder + "/{0}_fd.tif".format(file)
            net_path = outfolder + "/{0}_net.dat".format(file)
            fd = Flow(flw_path)

            # Creamos objeto network
            net = Network(fd, gradients=True)
            # Guardamos objeto network y cargamos
            net.save(net_path)
            net2 = Network(net_path)

            # Comparamos propiedades,
            prop_net = [net._size, net._geot, net._proj]
            prop_net2 = [net2._size, net2._geot, 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._dd))
            arr2 = np.array((net2._ix, net2._ixc, net2._ax, net2._dx, net2._zx,
                             net2._chi, net2._slp, net2._ksn, net2._dd))
            res = np.array_equal(arr1, arr2)
            self.assertEqual(res, True)
    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)
Exemple #16
0
 def test_stream_segments(self):
     dem_files = ['tunez', 'tunez2', 'small25']
     for filename in dem_files:
         fd = Flow("data/fd_{0}.tif".format(filename))
         st = Network(fd, 1000)
         ssegments = st.get_stream_segments(False)
         esegments = Grid("data/mlab_files/{0}_segments.tif".format(
             filename)).read_array()
         self.assertTrue(np.array_equal(ssegments, esegments), True)
Exemple #17
0
 def test_streams(self):
     dem_files = ['tunez', 'tunez2', 'small25']
     for filename in dem_files:
         fd = Flow("data/fd_{0}.tif".format(filename))
         st = Network(fd, 1000)
         streams = st.get_streams()
         st01 = streams.read_array()
         st02 = Grid("data/str_{0}.tif".format(filename)).read_array()
         self.assertTrue(np.array_equal(st01, st02), True)
Exemple #18
0
    def test_stream_order(self):
        dem_files = ['tunez', 'tunez2', 'small25']
        for filename in dem_files:
            fd = Flow("data/fd_{0}.tif".format(filename))
            st = Network(fd, 1000)

            for kind in ['strahler', 'shreeve']:
                exp_order = st.get_stream_order(kind=kind, asgrid=False)
                cmp_order = Grid("data/mlab_files/{0}_{1}.tif".format(
                    filename, kind)).read_array()
                self.assertTrue(np.array_equal(exp_order, cmp_order), True)
Exemple #19
0
 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)    
Exemple #20
0
 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)
Exemple #21
0
 def test_streampoi(self):
     dem_files = ['tunez', 'tunez2', 'small25']
     for filename in dem_files:
         fd = Flow("data/fd_{0}.tif".format(filename))
         st = Network(fd, 1000)
         kinds = ['heads', 'confluences', 'outlets']
         for kind in kinds:
             poi = st.get_stream_poi(kind)
             rows = poi[0].reshape((poi[0].size, 1))
             cols = poi[1].reshape((poi[1].size, 1))
             comp_poi = np.append(rows, cols, axis=1)
             exp_poi = np.load("data/mlab_files/{0}_{1}.npy".format(
                 filename, kind))
             self.assertEqual(np.array_equal(comp_poi, exp_poi), True)
    def test_BNetwork_class00(self):
        """
        Test00 Crea BNetwork para cuencas de prueba a partir de un Grid de cuencas
        Sin utilizar cabeceras 
        """
        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))

            # 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
                bnet = BNetwork(net, cuencas, None, bid)
                self.assertEqual(int(bnet._heads[0]), self.results[file][bid])
Exemple #23
0
    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)
Exemple #24
0
    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
Exemple #25
0
 def test_create_load(self):
     dem_files = ['tunez', 'tunez2', 'small25']
     for filename in dem_files:
         fd = Flow("data/fd_{0}.tif".format(filename))
         st = Network(fd, 1000)
         computed = [
             st.get_dims(),
             st.get_size(),
             st.get_ncells(),
             st.get_cellsize(),
             st.get_geotransform(),
             st.get_projection()
         ]
         expected = [
             fd.get_dims(),
             fd.get_size(),
             fd.get_ncells(),
             fd.get_cellsize(),
             fd.get_geotransform(),
             fd.get_projection()
         ]
         self.assertTrue(computed, expected)
    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))
Exemple #27
0
    def test_snap_poi_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)
            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

            # Hacemos snap a los stream poi
            for kind in ["heads", "confluences", "outlets"]:
                poi = fd.get_stream_poi(thr, kind, "XY")
                snap_pp = fd.snap_points(puntos, thr, kind)
                # Comprobamos que punto esta entre los POI
                for row in snap_pp:
                    res = row in poi
                    self.assertEqual(res, True)
Exemple #28
0
# -*- 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))
Exemple #29
0
        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")
# -*- 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))