Skip to content

Commit

Permalink
Add metadata for Builder Rasters (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
milos-colic authored Sep 10, 2024
1 parent b52ef4e commit 55c4c84
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
10 changes: 9 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,12 @@ cython_debug/
carto_credentials.json

# written by setuptools_scm
*/_version.py
*/_version.py
.idea/encodings.xml
.idea/misc.xml
.idea/modules.xml
.idea/raster-loader.iml
.idea/vcs.xml
.idea/codeStyles/codeStyleConfig.xml
.idea/codeStyles/Project.xml
.idea/.gitignore
15 changes: 15 additions & 0 deletions raster_loader/io/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import shapely
import numpy as np

from raster_loader._version import __version__
from collections import Counter
from typing import Iterable
from typing import Callable
from typing import List
Expand Down Expand Up @@ -347,13 +349,26 @@ def raster_band_stats(raster_dataset: rasterio.io.DatasetReader, band: int) -> d
else:
(raw_data, mask) = band_with_nodata_mask(raster_dataset, band)
stats = np.ma.masked_array(data=raw_data, mask=mask)
qdata = stats.compressed()
ranges = [[j / i for j in range(1, i)] for i in range(3, 20)]
quantiles = [
[int(np.quantile(qdata, q, method="lower")) for q in r] for r in ranges
]
quantiles = dict(zip(range(3, 20), quantiles))
most_common = Counter(qdata).most_common(100)
most_common.sort(key=lambda x: x[1], reverse=True)
most_common = dict([(int(x[0]), x[1]) for x in most_common])
version = ".".join(__version__.split(".")[:3])
return {
"min": float(stats.min()),
"max": float(stats.max()),
"mean": float(stats.mean()),
"stddev": float(stats.std()),
"sum": float(stats.sum()),
"sum_squares": float((stats**2).sum()),
"quantiles": quantiles,
"top_values": most_common,
"version": version,
"count": np.count_nonzero(stats.mask is False)
if masked
else math.prod(stats.shape), # noqa: E712
Expand Down

0 comments on commit 55c4c84

Please sign in to comment.