-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removed post-processing function form Layers class and moved into tes…
…ting code
- Loading branch information
1 parent
3f22593
commit 5e61380
Showing
7 changed files
with
58 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
-27 Bytes
(100%)
tests/resources/layer_dumps_for_br_lauro_de_freitas/layers_for_br_lauro_de_freitas.qgz
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from city_metrix.layers import NdviSentinel2 | ||
from tests.resources.bbox_constants import BBOX_BRA_LAURO_DE_FREITAS_1 | ||
from tests.tools import post_process_layer | ||
|
||
COUNTRY_CODE_FOR_BBOX = 'BRA' | ||
BBOX = BBOX_BRA_LAURO_DE_FREITAS_1 | ||
|
||
def test_ndvi_dimensions(): | ||
data = NdviSentinel2(year=2023).get_data(BBOX) | ||
data_for_map = post_process_layer(data, value_threshold=0.4, convert_to_percentage=True) | ||
|
||
expected_size = 37213 | ||
actual_size = data_for_map.size | ||
expected_min = 0 | ||
actual_min = data_for_map.values.min() | ||
expected_max = 85 | ||
actual_max = data_for_map.values.max() | ||
|
||
assert actual_size == expected_size | ||
assert actual_min == expected_min | ||
assert actual_max == expected_max |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import numpy as np | ||
|
||
def post_process_layer(data, value_threshold=0.4, convert_to_percentage=True): | ||
""" | ||
Applies the standard post-processing adjustment used for rendering of NDVI including masking | ||
to a threshold and conversion to percentage values. | ||
:param value_threshold: (float) minimum threshold for keeping values | ||
:param convert_to_percentage: (bool) controls whether NDVI values are converted to a percentage | ||
:return: A rioxarray-format DataArray | ||
""" | ||
# Remove values less than the specified threshold | ||
if value_threshold is not None: | ||
data = data.where(data >= value_threshold) | ||
|
||
# Convert to percentage in byte data_type | ||
if convert_to_percentage is True: | ||
data = convert_ratio_to_percentage(data) | ||
|
||
return data | ||
|
||
def convert_ratio_to_percentage(data): | ||
""" | ||
Converts xarray variable from a ratio to a percentage | ||
:param data: (xarray) xarray to be converted | ||
:return: A rioxarray-format DataArray | ||
""" | ||
|
||
# convert to percentage and to bytes for efficient storage | ||
values_as_percent = np.round(data * 100).astype(np.uint8) | ||
|
||
# reset CRS | ||
source_crs = data.rio.crs | ||
values_as_percent.rio.write_crs(source_crs, inplace=True) | ||
|
||
return values_as_percent |
This file was deleted.
Oops, something went wrong.