-
Notifications
You must be signed in to change notification settings - Fork 1
/
natural_areas.py
44 lines (37 loc) · 1.64 KB
/
natural_areas.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
import xarray as xr
from xrspatial.classify import reclassify
from .layer import Layer
from .esa_world_cover import EsaWorldCover, EsaWorldCoverClass
class NaturalAreas(Layer):
"""
Attributes:
spatial_resolution: raster resolution in meters (see https://github.com/stac-extensions/raster)
"""
def __init__(self, spatial_resolution=10, **kwargs):
super().__init__(**kwargs)
self.spatial_resolution = spatial_resolution
def get_data(self, bbox):
esa_world_cover = EsaWorldCover(spatial_resolution=self.spatial_resolution).get_data(bbox)
reclass_map = {
EsaWorldCoverClass.TREE_COVER.value: 1,
EsaWorldCoverClass.SHRUBLAND.value: 1,
EsaWorldCoverClass.GRASSLAND.value: 1,
EsaWorldCoverClass.CROPLAND.value: 0,
EsaWorldCoverClass.BUILT_UP.value: 0,
EsaWorldCoverClass.BARE_OR_SPARSE_VEGETATION.value: 0,
EsaWorldCoverClass.SNOW_AND_ICE.value: 0,
EsaWorldCoverClass.PERMANENT_WATER_BODIES.value: 0,
EsaWorldCoverClass.HERBACEOUS_WET_LAND.value: 1,
EsaWorldCoverClass.MANGROVES.value: 1,
EsaWorldCoverClass.MOSS_AND_LICHEN.value: 1
# Add other mappings as needed
}
# Perform the reclassification
reclassified_data = reclassify(
esa_world_cover,
bins=list(reclass_map.keys()),
new_values=list(reclass_map.values())
)
# Apply the original CRS (Coordinate Reference System)
reclassified_data = reclassified_data.rio.write_crs(esa_world_cover.rio.crs, inplace=True)
return reclassified_data