Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed allocating a large amount of memory with pixel coordinates #45

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions pyflwdir/gis_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ def distance(idx0, idx1, ncol, latlon=False, transform=IDENTITY):


## VECTORIZE
def features(flowpaths, xs, ys, **kwargs):
def features(flowpaths, xs=None, ys=None, transform=None, shape=None, **kwargs):
"""Returns a LineString feature for each stream

Parameters
Expand All @@ -496,8 +496,10 @@ def features(flowpaths, xs, ys, **kwargs):
linear indices of flowpaths
xs, ys : 1D-array of float
x, y coordinates
idxs_ds: list of intp
linear index of first cell on next downstream flow path
transform : Affine
Coefficients mapping pixel coordinates to coordinate reference system.
shape : tuple of int
The height, width of the raster.
kwargs : extra sample maps key-word arguments
optional maps to sample from
e.g.: strord=flw.stream_order()
Expand All @@ -507,8 +509,17 @@ def features(flowpaths, xs, ys, **kwargs):
feats : list of dict
Geofeatures, to be parsed by e.g. geopandas.GeoDataFrame.from_features
"""
if xs is None or ys is None:
if transform is None or shape is None:
raise ValueError(
"transform and shape should be provided if xs and ys are None"
)
_size = shape[0] * shape[1]
else:
_size = xs.size

for key in kwargs:
if not isinstance(kwargs[key], np.ndarray) or kwargs[key].size != xs.size:
if not isinstance(kwargs[key], np.ndarray) or kwargs[key].size != _size:
raise ValueError(
f'Kwargs map "{key}" should be ndarrays of same size as coordinates'
)
Expand All @@ -520,12 +531,17 @@ def features(flowpaths, xs, ys, **kwargs):
idx0 = idxs[0]
pit = idxs[-1] == idxs[-2]
props = {key: kwargs[key].flat[idx0] for key in kwargs}
if xs is None or ys is None:
xi, yi = idxs_to_coords(idxs, transform, shape)
coordinates = list(zip(xi, yi))
else:
coordinates = [(xs[i], ys[i]) for i in idxs]
feats.append(
{
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [(xs[i], ys[i]) for i in idxs],
"coordinates": coordinates,
},
"properties": {"idx": idx0, "idx_ds": idxs[-1], "pit": pit, **props},
}
Expand Down
9 changes: 4 additions & 5 deletions pyflwdir/pyflwdir.py
Original file line number Diff line number Diff line change
Expand Up @@ -974,13 +974,12 @@ def geofeatures(self, flowpaths, xs=None, ys=None, **kwargs):
Geofeatures, to be parsed by e.g. geopandas.GeoDataFrame.from_features
"""
# get geoms and return features
if xs is None or ys is None:
idxs0 = np.arange(self.size, dtype=np.intp)
xs, ys = gis.idxs_to_coords(idxs0, self.transform, self.shape)
feats = gis.features(
flowpaths=flowpaths,
xs=self._check_data(xs, "xs"),
ys=self._check_data(ys, "ys"),
xs=self._check_data(xs, "xs", optional=True),
ys=self._check_data(ys, "ys", optional=True),
transform=self.transform,
shape=self.shape,
**kwargs,
)
return feats
Expand Down
Loading