示例#1
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)
示例#2
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)
示例#3
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)
示例#4
0
    def test_empty_network(self):
        net = Network()

        # Test PRaster properties
        self.assertEqual(net.get_cellsize(), (1.0, -1.0))
        self.assertEqual(net.get_dims(), (1, 1))
        self.assertEqual(net.get_size(), (1, 1))
        self.assertEqual(net.get_extent(), (0.0, 1.0, 0.0, 1.0))
        self.assertEqual(net.get_geotransform(),
                         (0.0, 1.0, 0.0, 1.0, 0.0, -1.0))
        self.assertEqual(net.get_ncells(), 1)
        self.assertEqual(net.get_projection(), "")

        # Test PRaster functions
        self.assertEqual(net.cell_2_ind(0, 0), .0)
        self.assertEqual(net.cell_2_xy(0, 0), (0.5, 0.5))
        self.assertEqual(net.xy_2_cell(1, 1), (0, 1))
        self.assertEqual(net.ind_2_cell(0), (0, 0))

        # Test saving functions
        path = outfolder + "/net_delete.dat"
        net.save(path)
        self.assertEqual(os.path.exists(path), True)
        if os.path.exists(path):
            os.remove(path)

        path = outfolder + "/points_delete.txt"
        net.export_to_points(path)
        self.assertEqual(os.path.exists(path), True)
        if os.path.exists(path):
            os.remove(path)

        path = outfolder + "/shp_delete.shp"
        net.export_to_shp(path)
        self.assertEqual(os.path.exists(path), True)
        if os.path.exists(path):
            os.remove(path)

        path = outfolder + "/chi_delete.shp"
        net.get_chi_shapefile(path, 0)
        self.assertEqual(os.path.exists(path), True)
        if os.path.exists(path):
            os.remove(path)

        # Test other functions
        self.assertEqual(
            np.array_equal(net.get_streams(False),
                           np.array([1]).reshape(1, 1)), True)
        self.assertEqual(
            np.array_equal(net.get_stream_segments(False),
                           np.array([0]).reshape(1, 1)), True)
        self.assertEqual(
            np.array_equal(net.get_stream_orders("strahler", False),
                           np.array([1]).reshape(1, 1)), True)
        self.assertEqual(
            np.array_equal(net.get_stream_orders('shreeve', False),
                           np.array([1]).reshape(1, 1)), True)
        self.assertEqual(
            np.array_equal(net.get_stream_orders('dinosaur', False),
                           np.array([1]).reshape(1, 1)), True)
        self.assertEqual(
            np.array_equal(net.get_stream_poi("heads", "XY"),
                           np.array([]).reshape(0, 2)), True)
        self.assertEqual(
            np.array_equal(net.get_stream_poi("heads", "CELL"),
                           np.array([]).reshape(0, 2)), True)
        self.assertEqual(
            np.array_equal(net.get_stream_poi("heads", "IND"), np.array([])),
            True)
        self.assertEqual(
            np.array_equal(net.get_stream_poi("confluences", "XY"),
                           np.array([]).reshape(0, 2)), True)
        self.assertEqual(
            np.array_equal(net.get_stream_poi("confluences", "CELL"),
                           np.array([]).reshape(0, 2)), True)
        self.assertEqual(
            np.array_equal(net.get_stream_poi("confluences", "IND"),
                           np.array([])), True)
        self.assertEqual(
            np.array_equal(net.get_stream_poi("outlets", "XY"),
                           np.array([]).reshape(0, 2)), True)
        self.assertEqual(
            np.array_equal(net.get_stream_poi("outlets", "CELL"),
                           np.array([]).reshape(0, 2)), True)
        self.assertEqual(
            np.array_equal(net.get_stream_poi("outlets", "IND"), np.array([])),
            True)
        self.assertEqual(
            np.array_equal(net.get_stream_poi("dinosaur", "dinosaur"),
                           np.array([]).reshape(0, 2)), True)
        self.assertEqual(
            np.array_equal(net.snap_points(np.array((5, 5)).reshape(1, 2)),
                           np.array([[0.5, 0.5]])), True)
        self.assertEqual(net.is_inside(0, 0), False)
        net.calculate_gradients(0)