-
Notifications
You must be signed in to change notification settings - Fork 1
/
tree_cover.py
49 lines (39 loc) · 1.59 KB
/
tree_cover.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from .layer import Layer, get_utm_zone_epsg, get_image_collection
from dask.diagnostics import ProgressBar
import xarray as xr
import xee
import ee
class TreeCover(Layer):
"""
Merged tropical and nontropical tree cover from WRI
Attributes:
min_tree_cover: minimum tree-cover values used for filtering results
max_tree_cover: maximum tree-cover values used for filtering results
spatial_resolution: raster resolution in meters (see https://github.com/stac-extensions/raster)
"""
NO_DATA_VALUE = 255
def __init__(self, min_tree_cover=None, max_tree_cover=None, spatial_resolution=10, **kwargs):
super().__init__(**kwargs)
self.min_tree_cover = min_tree_cover
self.max_tree_cover = max_tree_cover
self.spatial_resolution = spatial_resolution
def get_data(self, bbox):
tropics = ee.ImageCollection('projects/wri-datalab/TropicalTreeCover')
non_tropics = ee.ImageCollection('projects/wri-datalab/TTC-nontropics')
merged_ttc = tropics.merge(non_tropics)
ttc_image = (merged_ttc
.reduce(ee.Reducer.mean())
.rename('ttc')
)
ttc_ic = ee.ImageCollection(ttc_image)
data = get_image_collection(
ttc_ic,
bbox,
self.spatial_resolution,
"tree cover"
).ttc
if self.min_tree_cover is not None:
data = data.where(data >= self.min_tree_cover)
if self.max_tree_cover is not None:
data = data.where(data <= self.max_tree_cover)
return data