Skip to content

Commit 182de0d

Browse files
Add Level-3 and Level-4 bands for Land Cover product. (#161)
* Added Level4 classes * Added unit tests for cultivated terrestrial vegetation * Added unit tests for natural terrestrial vegetation and cultivated terrestrial vegetation * Added unit tests for natural terrestrial vegetation and cultivated terrestrial vegetation * Added level-4 water classes * Cleaned up inconsistencies between the land cover modules * Improved class names and applied some other clean-ups. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Added more improvement on level3 and level4 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Applied formatting and code improvements * Further modularised the level4 * Added an integration test for level 4. Also changed data types in unit tests to dask arrays * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Applied linting and formatting * Applied fixes and improvements based on review comments * Removed commented code * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Applied formatting changes * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fixed a unit test --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent d799a99 commit 182de0d

25 files changed

+3489
-89
lines changed

odc/stats/plugins/_registry.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ def import_all():
3939

4040
# TODO: make that more automatic
4141
modules = [
42-
"odc.stats.plugins.lc_treelite_cultivated.py",
42+
"odc.stats.plugins.lc_treelite_cultivated",
4343
"odc.stats.plugins.lc_level3",
4444
"odc.stats.plugins.lc_treelite_woody",
4545
"odc.stats.plugins.lc_tf_urban",
46+
"odc.stats.plugins.lc_level34",
4647
"odc.stats.plugins.lc_veg_class_a1",
4748
"odc.stats.plugins.lc_fc_wo_a0",
4849
"odc.stats.plugins.mangroves",

odc/stats/plugins/l34_utils/ __init__.py

Whitespace-only changes.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import xarray as xr
2+
from odc.stats._algebra import expr_eval
3+
4+
5+
NODATA = 255
6+
7+
8+
def bare_gradation(xx: xr.Dataset, bare_threshold, veg_cover):
9+
10+
# Map any data > 100 ---> 100
11+
bs_pc_50 = expr_eval(
12+
"where((a>100)&(a!=nodata), 100, a)",
13+
{"a": xx.bs_pc_50.data},
14+
name="mark_veg",
15+
dtype="uint8",
16+
**{"nodata": NODATA},
17+
)
18+
19+
# 60% <= data --> 15
20+
bs_mask = expr_eval(
21+
"where((a>=m)&(a!=nodata), 15, a)",
22+
{"a": bs_pc_50},
23+
name="mark_veg",
24+
dtype="uint8",
25+
**{"m": bare_threshold[1], "nodata": NODATA},
26+
)
27+
28+
# 20% <= data < 60% --> 12
29+
bs_mask = expr_eval(
30+
"where((a>=m)&(a<n), 12, b)",
31+
{"a": bs_pc_50, "b": bs_mask},
32+
name="mark_veg",
33+
dtype="uint8",
34+
**{"m": bare_threshold[0], "n": bare_threshold[1]},
35+
)
36+
37+
# data < 20% --> 10
38+
bs_mask = expr_eval(
39+
"where(a<m, 10, b)",
40+
{"a": bs_pc_50, "b": bs_mask},
41+
name="mark_veg",
42+
dtype="uint8",
43+
**{"m": bare_threshold[0]},
44+
)
45+
46+
return bs_mask
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
from odc.stats._algebra import expr_eval
2+
3+
NODATA = 255
4+
5+
6+
def lc_l4_cultivated(l34, level3, lifeform, veg_cover):
7+
8+
l4 = expr_eval(
9+
"where((d==110)&(a==111)&(b==10)&(c==1), 9, d)",
10+
{"a": level3, "b": veg_cover, "c": lifeform, "d": l34},
11+
name="mark_cultivated",
12+
dtype="uint8",
13+
)
14+
15+
l4 = expr_eval(
16+
"where((d==110)&(a==111)&(b==12)&(c==1), 10, d)",
17+
{"a": level3, "b": veg_cover, "c": lifeform, "d": l4},
18+
name="mark_cultivated",
19+
dtype="uint8",
20+
)
21+
l4 = expr_eval(
22+
"where((d==110)&(a==111)&(b==13)&(c==1), 11, d)",
23+
{"a": level3, "b": veg_cover, "c": lifeform, "d": l4},
24+
name="mark_cultivated",
25+
dtype="uint8",
26+
)
27+
l4 = expr_eval(
28+
"where((d==110)&(a==111)&(b==15)&(c==1), 12, d)",
29+
{"a": level3, "b": veg_cover, "c": lifeform, "d": l4},
30+
name="mark_cultivated",
31+
dtype="uint8",
32+
)
33+
34+
l4 = expr_eval(
35+
"where((d==110)&(a==111)&(b==16)&(c==1), 13, d)",
36+
{"a": level3, "b": veg_cover, "c": lifeform, "d": l4},
37+
name="mark_cultivated",
38+
dtype="uint8",
39+
)
40+
41+
l4 = expr_eval(
42+
"where((d==110)&(a==111)&(b==10)&(c==2), 14, d)",
43+
{"a": level3, "b": veg_cover, "c": lifeform, "d": l4},
44+
name="mark_cultivated",
45+
dtype="uint8",
46+
)
47+
l4 = expr_eval(
48+
"where((d==110)&(a==111)&(b==12)&(c==2), 15, d)",
49+
{"a": level3, "b": veg_cover, "c": lifeform, "d": l4},
50+
name="mark_cultivated",
51+
dtype="uint8",
52+
)
53+
l4 = expr_eval(
54+
"where((d==110)&(a==111)&(b==13)&(c==2), 16, d)",
55+
{"a": level3, "b": veg_cover, "c": lifeform, "d": l4},
56+
name="mark_cultivated",
57+
dtype="uint8",
58+
)
59+
l4 = expr_eval(
60+
"where((d==110)&(a==111)&(b==15)&(c==2), 17, d)",
61+
{"a": level3, "b": veg_cover, "c": lifeform, "d": l4},
62+
name="mark_cultivated",
63+
dtype="uint8",
64+
)
65+
66+
l4 = expr_eval(
67+
"where((d==110)&(a==111)&(b==16)&(c==2), 18, d)",
68+
{"a": level3, "b": veg_cover, "c": lifeform, "d": l4},
69+
name="mark_cultivated",
70+
dtype="uint8",
71+
)
72+
73+
l4 = expr_eval(
74+
"where((d==110)&(a==111)&(b==1), 2, d)",
75+
{"a": level3, "b": lifeform, "d": l4},
76+
name="mark_cultivated",
77+
dtype="uint8",
78+
)
79+
80+
l4 = expr_eval(
81+
"where((d==110)&(a==111)&(b==2), 3, d)",
82+
{"a": level3, "b": lifeform, "d": l4},
83+
name="mark_cultivated",
84+
dtype="uint8",
85+
)
86+
87+
# the 4-8 classes can't happen in LC since cultivated class will not be classified if vegetation doesn't exist.
88+
# skip these classes in level4
89+
90+
l4 = expr_eval(
91+
"where((d==110)&(a==111), 1, d)",
92+
{"a": level3, "d": l4},
93+
name="mark_cultivated",
94+
dtype="uint8",
95+
)
96+
97+
return l4

0 commit comments

Comments
 (0)