Skip to content

Commit

Permalink
Make shape, rows and columns properties of GridObject (TopoToolbox#93)
Browse files Browse the repository at this point in the history
See the discussion in TopoToolbox#90.

This ensures that these attributes remain synchronized with the
underlying Numpy array that stores the data, but should not change the
API of the object. It will break code that directly sets these
attributes. I believe I have found and corrected all of those in
the package.
  • Loading branch information
wkearn authored Nov 29, 2024
1 parent 470cfe2 commit 5d6a79b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
1 change: 0 additions & 1 deletion src/topotoolbox/flow_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ def flow_accumulation(self, weights: np.ndarray | float = 1.0):
result.name = self.name

result.z = acc
result.shape = self.shape
result.cellsize = self.cellsize

result.bounds = self.bounds
Expand Down
25 changes: 18 additions & 7 deletions src/topotoolbox/grid_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ def __init__(self) -> None:

# raster metadata
self.z = np.empty((), order='F', dtype=np.float32)
self.rows = 0
self.columns = 0
self.shape = self.z.shape

self.cellsize = 0.0 # in meters if crs.is_projected == True

Expand All @@ -41,6 +38,24 @@ def __init__(self) -> None:
self.transform = None
self.crs = None

@property
def shape(self):
"""Tuple of grid dimensions
"""
return self.z.shape

@property
def rows(self):
"""The size of the first dimension of the grid
"""
return self.z.shape[0]

@property
def columns(self):
"""The size of the second dimension of the grid
"""
return self.z.shape[1]

def reproject(self,
crs: 'CRS',
resolution: 'float | None' = None,
Expand Down Expand Up @@ -87,10 +102,6 @@ def reproject(self,
if dst.transform is not None:
dst.cellsize = abs(dst.transform[0])

dst.shape = dst.z.shape
dst.rows = dst.shape[0]
dst.columns = dst.shape[1]

return dst

def fillsinks(self,
Expand Down
9 changes: 0 additions & 9 deletions src/topotoolbox/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,6 @@ def read_tif(path: str) -> GridObject:
grid.name = os.path.splitext(os.path.basename(grid.path))[0]

grid.z = dataset.read(1).astype(np.float32, order='F')
grid.rows = dataset.height
grid.columns = dataset.width
grid.shape = grid.z.shape

grid.cellsize = dataset.res[0]
grid.bounds = dataset.bounds
Expand Down Expand Up @@ -192,9 +189,6 @@ def gen_random(hillsize: int = 24, rows: int = 128, columns: int = 128,
grid = GridObject()

grid.z = noise_array
grid.rows = rows
grid.columns = columns
grid.shape = grid.z.shape
grid.cellsize = cellsize
grid.name = name
return grid
Expand Down Expand Up @@ -230,9 +224,6 @@ def gen_random_bool(

grid.path = ''
grid.z = bool_array
grid.rows = rows
grid.columns = columns
grid.shape = grid.z.shape
grid.cellsize = cellsize
grid.name = name

Expand Down
3 changes: 3 additions & 0 deletions tests/test_flow_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ def test_flowobject(wide_dem):
original_dem = dem.z.copy()
fd = topo.FlowObject(dem);

# Run flow_accumulation at least once during the tests
acc = fd.flow_accumulation();

# Ensure that FlowObject does not modify the original DEM
assert np.all(dem.z == original_dem)

0 comments on commit 5d6a79b

Please sign in to comment.