Skip to content

Commit

Permalink
Merge pull request #69 from earth-chris/sample-raster-nodata
Browse files Browse the repository at this point in the history
fix `sample_raster` nodata for certain rasters
  • Loading branch information
Christopher Anderson committed Oct 21, 2022
2 parents f3fc980 + 3be990c commit db72c1a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion elapid/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"0.3.16"
"0.3.17"
13 changes: 11 additions & 2 deletions elapid/geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,17 @@ def sample_raster(raster_path: str, count: int, nodata: float = None, ignore_mas
with rio.open(raster_path) as src:

if src.nodata is None or ignore_mask:
xmin, ymin, xmax, ymax = src.bounds
xy = np.random.uniform((xmin, ymin), (xmax, ymax), (count, 2))
if nodata is None:
xmin, ymin, xmax, ymax = src.bounds
xy = np.random.uniform((xmin, ymin), (xmax, ymax), (count, 2))
else:
data = src.read(1)
mask = data != nodata
rows, cols = np.where(mask)
samples = np.random.randint(0, len(rows), count)
xy = np.zeros((count, 2))
for i, sample in enumerate(samples):
xy[i] = src.xy(rows[sample], cols[sample])

else:
if nodata is None:
Expand Down

0 comments on commit db72c1a

Please sign in to comment.