def test_get_value_01(self): dem = Grid(infolder + "/small25.tif") # Taking row, col in a nan position (88) ind = 88 row, col = self.rows[ind], self.cols[ind] computed = dem.get_value(row, col) self.assertEqual(computed, -9999)
def test_get_value_05(self): dem = Grid(infolder + "/small25.tif") # Taking row, col as lists expected = np.nansum(self.zi.tolist()) res = dem.get_value(self.rows.tolist(), self.cols.tolist()) computed = np.nansum(res) self.assertEqual(computed, expected)
def test_set_read_array_00(self): # Set data to an empty array and to an array with differnt dimensions arr = np.arange(25).reshape(5,5).astype(np.int8) dem = Grid() res01 = dem.set_array(arr) res02 = dem.set_array(np.arange(9).reshape(3,3).astype(np.int8)) computed = (res01, res02) self.assertEqual(computed, (None, 0))
def test_get_value_02(self): dem = Grid(infolder + "/small25.tif") # Taking row, col in other position (with value) ind = 25 row, col = self.rows[ind], self.cols[ind] expected = self.zi[ind] computed = dem.get_value(row, col) self.assertEqual(computed, expected)
def test_get_nodata_pos(self): dem = Grid(infolder + "/small25.tif") arr = dem._array row, col = dem.get_nodata_pos() crow, ccol = np.where(arr == -9999) self.assertEqual( (np.array_equal(row, crow), np.array_equal(col, ccol)), (True, True))
def test_values_2_nodata(self): dem = Grid() dem.set_array(np.arange(25).reshape((5, 5))) dem.set_nodata(-99.) dem.values_2_nodata([10, 11, 12, 13, 14]) row, col = dem.ind_2_cell([10, 11, 12, 13, 14]) computed = dem.get_value(row, col) expected = np.array([-99, -99, -99, -99, -99]) res = np.array_equal(computed, expected) self.assertEqual(res, True)
def test_save(self): dem = Grid(infolder + "/small25.tif") dem.save(outfolder + "/a_dummy_dem.tif") dem2 = Grid(outfolder + "/a_dummy_dem.tif") expected = dem.get_value([20, 30, 40, 50], [20, 30, 40, 50]) computed = dem2.get_value([20, 30, 40, 50], [20, 30, 40, 50]) self.assertEqual(np.array_equal(computed, expected), True)
def test_set_data_00(self): arr = np.arange(9).reshape((3, 3)) arr[[1, 2], [2, 1]] = 8 dem = Grid() dem.set_nodata(8) dem.set_array(arr) row, col = dem.get_nodata_pos() computed = dem.get_value(row, col) expected = np.array([8, 8, 8]) self.assertEqual(np.array_equal(computed, expected), True)
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)
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)
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)
def test_is_inside(self): # points 1, 2, 4, 8 --> -Fuera de ráster # points 3, 5 --> Dentro de ráster, pero en NoData # points 6, 7, 9 --> Dentro de ráster puntos = np.array([[476154., 4115084.], [472289., 4112838.], [471317., 4114050.], [472874., 4117717.], [472205., 4114091.], [470795., 4116411.], [472257., 4115565.], [469572., 4115376.], [473877., 4114844.]]) x = puntos[:,0] y = puntos[:,1] raster = Grid(infolder + "/small25.tif") computed = raster.is_inside(x, y) expected = np.array([False, False, False, False, False, True, True ,False, True]) self.assertEqual(np.array_equal(computed, expected), True)
def test_save_02(self): # Testing nodata with value of Zero dem = Grid() np.random.seed(1) arr = np.random.randint(0, 100, (25, 25)) arr[np.where(arr%5==0)] = 0 dem.set_array(arr) dem.set_nodata(0) dem.save(outfolder + "/a_dummy_dem2.tif") # Open with gdal raster = gdal.Open(outfolder + "/a_dummy_dem2.tif") banda = raster.GetRasterBand(1) nodata = banda.GetNoDataValue() self.assertEqual(nodata, 0)
def test_set_read_array_02(self): # Read array (Sharing memory) arr = np.arange(25).reshape(5,5).astype(np.int8) dem = Grid() dem.set_array(arr) out_arr = dem.read_array(False) out_arr[0, 0] = 99 computed = dem.get_value(0, 0) expected = 99 self.assertEqual(computed, expected)
def test_get_value_04(self): dem = Grid(infolder + "/small25.tif") # Taking row, col as numpy arrays expected = self.zi computed = dem.get_value(self.rows, self.cols) self.assertEqual(np.nansum(computed),np.nansum(expected))
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] outgrid = Grid() outgrid._size = (ncol, nrow) outgrid._dims = (nrow, ncol) geot = net._geot ULx = geot[0] + geot[1] * c1 ULy = geot[3] + geot[5] * r1 outgrid._geot = (ULx, geot[1], 0.0, ULy, 0.0, geot[5]) outgrid._cellsize = geot[1] outgrid._proj = net._proj outgrid._ncells = basin_cl.size outgrid._nodata = 0 outgrid._array = basin_cl outgrid._tipo = str(basin.dtype) fd.get_drainage_basins(net.snap_points(outlet)).save(outfolder +
def test_get_value_03(self): dem = Grid(infolder + "/small25.tif") # Taking row, col outside array row, col = 199, 133 self.assertRaises(IndexError, dem.get_value, row, col)