diff --git a/src/topotoolbox/grid_object.py b/src/topotoolbox/grid_object.py index 1a240ad..c1f1ada 100755 --- a/src/topotoolbox/grid_object.py +++ b/src/topotoolbox/grid_object.py @@ -64,6 +64,18 @@ def columns(self): """ return self.z.shape[1] + @property + def dims(self): + """The dimensions of the grid in the correct order for libtopotoolbox + """ + if self.z.flags.c_contiguous: + return (self.columns, self.rows) + + if self.z.flags.f_contiguous: + return (self.rows, self.columns) + + raise TypeError("Grid is not stored as a contiguous row- or column-major array") + def reproject(self, crs: 'CRS', resolution: 'float | None' = None, @@ -137,7 +149,7 @@ def fillsinks(self, """ - dem = self.z.astype(np.float32, order='F') + dem = self.z.astype(np.float32) output = np.zeros_like(dem) restore_nans = False @@ -161,9 +173,9 @@ def fillsinks(self, if hybrid: queue = np.zeros_like(dem, dtype=np.int64) - _grid.fillsinks_hybrid(output, queue, dem, bc, self.shape) + _grid.fillsinks_hybrid(output, queue, dem, bc, self.dims) else: - _grid.fillsinks(output, dem, bc, self.shape) + _grid.fillsinks(output, dem, bc, self.dims) if restore_nans: dem[nans] = np.nan diff --git a/tests/test_grid_object.py b/tests/test_grid_object.py index 8da6ef6..73052e7 100755 --- a/tests/test_grid_object.py +++ b/tests/test_grid_object.py @@ -3,6 +3,8 @@ import topotoolbox as topo +import opensimplex + @pytest.fixture def square_dem(): @@ -67,6 +69,40 @@ def test_fillsinks(square_dem, wide_dem, tall_dem): assert sink < 8 +def test_fillsinks_order(): + opensimplex.seed(12) + + x = np.arange(0,128) + y = np.arange(0,256) + + dem_C = topo.GridObject() + dem_C.z = 64 * (opensimplex.noise2array(x,y) + 1) + + assert dem_C.shape[0] == 256 + assert dem_C.shape[1] == 128 + + assert dem_C.z.flags.c_contiguous + assert dem_C.dims[0] == 128 + assert dem_C.dims[1] == 256 + + dem_F = topo.GridObject() + dem_F.z = np.asfortranarray(dem_C.z) + + assert dem_F.shape[0] == 256 + assert dem_F.shape[1] == 128 + + assert dem_F.z.flags.f_contiguous + assert dem_F.dims[0] == 256 + assert dem_F.dims[1] == 128 + + filled_C = dem_C.fillsinks() + assert filled_C.z.flags.c_contiguous + + filled_F = dem_F.fillsinks() + assert filled_F.z.flags.f_contiguous + + assert np.array_equal(filled_F.z, filled_C.z) + def test_identifyflats(square_dem, wide_dem, tall_dem): # TODO: add more tests for dem in [square_dem, wide_dem, tall_dem]: