Пример #1
0
    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)
Пример #2
0
    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])
Пример #3
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)
Пример #4
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)
Пример #5
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)
Пример #6
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)
Пример #7
0
 def test_stream_poi_01(self):
     files = ["small25", "morocco", "tunez", "jebja30"]
     for file in files:
         net_path = infolder + "/{0}_network.net".format(file)
         net = Network(net_path)
         for kind in ["heads", "confluences", "outlets"]:
             poi = net.get_stream_poi(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)
    def test_get_chi_shp(self):
        files = ["small25", "tunez", "jebja30"]
        for file in files:
            # Cargamos objeto network guardado previamente
            net_path = infolder + "/{0}_net.dat".format(file)
            net = Network(net_path)

            # Exportamos canales a shapefile
            out_shp = outfolder + "/{0}_chi.shp".format(file)
            net.get_chi_shapefile(out_shp, 250)

            computed = os.path.exists(out_shp)
            self.assertEqual(computed, True)
Пример #9
0
    def test_get_channel2(self):
        """
        Test crea los canales hasta outlet para todas las cabeceras (sin mouth)
        """
        files = ["small25", "jebja30", "tunez"]

        for file in files:
            net = Network("data/in/{}_net.dat".format(file))
            heads = net.get_stream_poi(kind="heads", coords="XY")
            for head in heads:
                canal = net.get_channel(head)
                # Verificamos que canal se ha creado bien
                self.assertEqual(isinstance(canal, Channel), True)
Пример #10
0
    def test_get_streams(self):
        files = ["small25", "morocco", "tunez", "jebja30"]
        for file in files:
            # Cargamos objeto network guardado previamente
            net_path = infolder + "/{0}_network.net".format(file)
            net = Network(net_path)

            # Probamos que no haya fallos en las funciones de get_streams
            streams = net.get_streams()
            streams.save(outfolder + "/{0}_streams.tif".format(file))
            segments = net.get_stream_segments()
            segments.save(outfolder + "/{0}_segments.tif".format(file))
            orders = net.get_stream_order()
            orders.save(outfolder + "/{0}_ord.tif".format(file))
Пример #11
0
    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)
Пример #12
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)
Пример #13
0
 def test_stream_poi_02(self):
     files = ["small25", "tunez", "jebja30"]
     for file in files:
         net_path = infolder + "/{0}_net.dat".format(file)
         net = Network(net_path)
         for kind in ["heads", "confluences", "outlets"]:
             poi = net.get_stream_poi(kind, "CELL")
             x, y = net.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)
Пример #14
0
    def test_get_channel(self, plot_canales=False):
        """
        Test Obtiene shapefile con canales, selecciona 5 grupos de 10 canales
        aleaorios y crea objeto Channel (con puntos inicial y final de línea)
        """
        files = ["small25", "jebja30", "tunez"]
        for file in files:

            net = net = Network("{}/{}_net.dat".format(infolder, file))
            shp_path = outfolder + "/canales_{}.shp".format(file)
            net.export_to_shp(shp_path, False)

            dataset = ogr.Open(shp_path)
            layer = dataset.GetLayer(0)
            canales = []
            for n in range(5):
                for n in np.random.randint(0, layer.GetFeatureCount(), 10):
                    feat = layer.GetFeature(n)
                    geom = feat.GetGeometryRef()
                    head = geom.GetPoint(0)
                    mouth = geom.GetPoint(geom.GetPointCount() - 1)
                    canal = net.get_channel(head, mouth)
                    canales.append(canal)
                    # Verificamos que canal se ha creado bien
                    self.assertEqual(isinstance(canal, Channel), True)
Пример #15
0
    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)
Пример #16
0
    def test_snap_poi_01(self):
        # Test 10 random basins
        files = ["small25", "morocco", "tunez", "jebja30"]
        for file in files:
            # Cargamos objeto network guardado previamente
            net_path = infolder + "/{0}_network.net".format(file)
            net = Network(net_path)

            # Obtenemos 20 puntos aleatorios
            x1, x2, y1, y2 = net.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 = net.get_stream_poi(kind, "XY")
                snap_pp = net.snap_points(puntos, kind)
                # Comprobamos que punto esta entre los POI
                for row in snap_pp:
                    res = row in poi
                    self.assertEqual(res, True)
Пример #17
0
    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])
Пример #18
0
    def test_get_streams(self):
        files = ["small25", "tunez", "jebja30"]
        for file in files:
            # Cargamos objeto network guardado previamente
            net_path = infolder + "/{0}_net.dat".format(file)
            net = Network(net_path)

            # Probamos que no haya fallos en las funciones de get_streams
            streams = net.get_streams()
            streams.save(outfolder + "/{0}_streams.tif".format(file))
            computed = [
                os.path.exists(outfolder + "/{0}_streams.tif".format(file))
            ]
            segments = net.get_stream_segments()
            segments.save(outfolder + "/{0}_segments.tif".format(file))
            computed.append(
                os.path.exists(outfolder + "/{0}_segments.tif".format(file)))
            orders = net.get_stream_orders()
            orders.save(outfolder + "/{0}_ord.tif".format(file))
            computed.append(
                os.path.exists(outfolder + "/{0}_ord.tif".format(file)))

            self.assertEqual(computed, [True, True, True])
Пример #19
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)
Пример #20
0
    def test_get_streams(self):
        files = ["small25", "morocco", "tunez", "jebja30"]
        for file in files:
            # Cargamos objeto network guardado previamente
            net_path = infolder + "/{0}_network.net".format(file)
            net = Network(net_path)

            # Exportamos canales a shapefile
            out_shp = outfolder + "/{0}_str.shp".format(file)
            out_con_shp = outfolder + "/{0}_strc.shp".format(file)
            net.export_to_shp(out_shp)
            net.export_to_shp(out_con_shp, True)
    def test_get_streams(self):
        files = ["small25", "tunez", "jebja30"]
        for file in files:
            # Cargamos objeto network guardado previamente
            net_path = infolder + "/{0}_net.dat".format(file)
            net = Network(net_path)

            # Exportamos canales a shapefile
            out_shp = outfolder + "/{0}_str.shp".format(file)
            out_con_shp = outfolder + "/{0}_strcon.shp".format(file)
            net.export_to_shp(out_shp)
            net.export_to_shp(out_con_shp, True)
            computed = [os.path.exists(out_shp), os.path.exists(out_con_shp)]
            self.assertEqual(computed, [True, True])
Пример #22
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)
Пример #23
0
    def test_snap_poi_02(self):
        # Test 10 random basins
        files = ["small25", "morocco", "tunez", "jebja30"]
        for file in files:
            # Cargamos objeto network guardado previamente
            net_path = infolder + "/{0}_network.net".format(file)
            net = Network(net_path)

            # Obtenemos 20 puntos aleatorios
            x1, x2, y1, y2 = net.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 celdas de canal
            snap_pp = net.snap_points(puntos, kind="channel")
            row, col = net.xy_2_cell(snap_pp[:, 0], snap_pp[:, 1])
            inds = net.cell_2_ind(row, col)
            # Comprobamos que punto esta entre los POI
            for ind in inds:
                res = ind in net._ix
                self.assertEqual(res, True)
Пример #24
0
# -*- 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"

net.export_to_shapefile(path)
Пример #25
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))
Пример #26
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 26 17:53:42 2018

@author: vicen
"""

from topopy import Network
import numpy as np
import matplotlib.pyplot as plt
import ogr, osr
import os

net = Network("../data/in/jebja30_network.net")

self = net
#fig, ax = plt.subplots()


def export_to_shp(self, path, con=False):
    """
    Export Network channels to shapefile format.
    
    path : str
      Path to save the shapefile
    con : bool
      If False, channels will split in each confluence (segmented channels). If True, 
      they will split only when order changes (continuous channels).
    """
    if con:
Пример #27
0
# -*- 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))
Пример #28
0
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]

nrow = basin_cl.shape[0]
ncol = basin_cl.shape[1]
Пример #29
0
#!/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]
Пример #30
0
# -*- coding: utf-8 -*-
"""
Editor de Spyder

Este es un archivo temporal
"""

from topopy import Network, Channel, Flow, BNetwork
from osgeo import ogr
import numpy as np

#def rivers_to_channels(path, net, idfield=""):

path = "../data/in/jebja_channels.shp"
idfield = ""
net = Network("../data/in/jebja30_net.dat")

# Open river shapefile
driver = ogr.GetDriverByName("ESRI Shapefile")
dataset = driver.Open(path)
layer = dataset.GetLayer()
geom_type = layer.GetGeomType()
lydef = layer.GetLayerDefn()
id_fld = lydef.GetFieldIndex(idfield)

points = []

for feat in layer:
    geom = feat.GetGeometryRef()
    if geom.GetGeometryCount() > 1:
        continue