-
Notifications
You must be signed in to change notification settings - Fork 2
/
aohcalc.py
270 lines (237 loc) · 9.52 KB
/
aohcalc.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import argparse
import json
import math
import os
import logging
import sys
from typing import Dict, List, Optional, Set
# import pyshark # pylint: disable=W0611
import numpy as np
import pandas as pd
from geopandas import gpd
from yirgacheffe.layers import RasterLayer, VectorLayer, ConstantLayer, UniformAreaLayer
from alive_progress import alive_bar
from osgeo import gdal
gdal.UseExceptions()
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)-8s %(message)s')
def load_crosswalk_table(table_file_name: str) -> Dict[str,List[int]]:
rawdata = pd.read_csv(table_file_name)
result = {}
for _, row in rawdata.iterrows():
code = str(row.code)
try:
result[code].append(int(row.value))
except KeyError:
result[code] = [int(row.value)]
return result
def crosswalk_habitats(crosswalk_table: Dict[str, List[int]], raw_habitats: Set[str]) -> Set[int]:
result = set()
for habitat in raw_habitats:
try:
crosswalked_habatit = crosswalk_table[habitat]
except KeyError:
continue
result |= set(crosswalked_habatit)
return result
def aohcalc(
habitat_path: str,
min_elevation_path: str,
max_elevation_path: str,
area_path: Optional[str],
crosswalk_path: str,
species_data_path: str,
force_habitat: bool,
output_directory_path: str,
) -> None:
os.makedirs(output_directory_path, exist_ok=True)
crosswalk_table = load_crosswalk_table(crosswalk_path)
os.environ["OGR_GEOJSON_MAX_OBJ_SIZE"] = "0"
try:
filtered_species_info = gpd.read_file(species_data_path)
except: # pylint:disable=W0702
logger.error("Failed to read %s", species_data_path)
sys.exit(1)
assert filtered_species_info.shape[0] == 1
species_id = filtered_species_info.id_no.values[0]
try:
seasonality = filtered_species_info.season.values[0]
result_filename = os.path.join(output_directory_path, f"{species_id}_{seasonality}.tif")
manifest_filename = os.path.join(output_directory_path, f"{species_id}_{seasonality}.json")
except AttributeError:
seasonality = None
result_filename = os.path.join(output_directory_path, f"{species_id}.tif")
manifest_filename = os.path.join(output_directory_path, f"{species_id}.json")
try:
elevation_lower = math.floor(float(filtered_species_info.elevation_lower.values[0]))
elevation_upper = math.ceil(float(filtered_species_info.elevation_upper.values[0]))
raw_habitats = set(filtered_species_info.full_habitat_code.values[0].split('|'))
except (AttributeError, TypeError):
logger.error("Species data missing one or more needed attributes: %s", filtered_species_info)
sys.exit()
habitat_list = crosswalk_habitats(crosswalk_table, raw_habitats)
if force_habitat and len(habitat_list) == 0:
logger.error("No habitats found in crosswalk! %s_%s had %s", species_id, seasonality, raw_habitats)
sys.exit()
ideal_habitat_map_files = [os.path.join(habitat_path, f"lcc_{x}.tif") for x in habitat_list]
habitat_map_files = [x for x in ideal_habitat_map_files if os.path.exists(x)]
if force_habitat and len(habitat_map_files) == 0:
logger.error("No matching habitat layers found for %s_%s in %s: %s",
species_id, seasonality, habitat_path, habitat_list)
sys.exit()
habitat_maps = [RasterLayer.layer_from_file(x) for x in habitat_map_files]
min_elevation_map = RasterLayer.layer_from_file(min_elevation_path)
max_elevation_map = RasterLayer.layer_from_file(max_elevation_path)
range_map = VectorLayer.layer_from_file_like(
species_data_path,
None,
min_elevation_map
)
area_map = ConstantLayer(1.0)
if area_path:
try:
area_map = UniformAreaLayer.layer_from_file(area_path)
except ValueError:
area_map = RasterLayer.layer_from_file(area_path)
layers = habitat_maps + [min_elevation_map, max_elevation_map, range_map, area_map]
try:
intersection = RasterLayer.find_intersection(layers)
except ValueError:
logger.warning("Failed to find intersection for %s: %s", species_data_path, range_map.area)
for layer in layers:
print(f"\t{layer.name}: {layer.area}")
print("Just using range")
result = RasterLayer.empty_raster_layer_like(
area_map,
filename=result_filename,
compress=True,
)
with alive_bar(manual=True) as bar:
range_map.save(result, callback=bar)
return
for layer in layers:
layer.set_window_for_intersection(intersection)
range_total = (range_map * area_map).sum()
result = RasterLayer.empty_raster_layer_like(
min_elevation_map,
filename=result_filename,
compress=True,
datatype=gdal.GDT_Float32
)
# Habitat evaluation. In the IUCN Redlist Technical Working Group recommendations, if there are no defined
# habitats, then we revert to range. If the area of the habitat map filtered by species habitat is zero then we
# similarly revert to range as the assumption is that there is an error in the habitat coding.
#
# However, for methodologies, such as the LIFE biodiversity metric by Eyres et al, where you want to do
# land use change impact scenarios, this rule doesn't work, as it treats extinction due to land use change as
# then actually filling the range. This we have the force_habitat flag for this use case.
if habitat_maps or force_habitat:
combined_habitat = habitat_maps[0]
for map_layer in habitat_maps[1:]:
combined_habitat = combined_habitat + map_layer
combined_habitat = combined_habitat.numpy_apply(lambda c: np.where(c > 1, 1, c))
filtered_by_habtitat = range_map * combined_habitat
if filtered_by_habtitat.sum() == 0:
if force_habitat:
print("No matching habitats, not generating AoH")
return
else:
filtered_by_habtitat = range_map
else:
filtered_by_habtitat = range_map
# Elevation evaluation. As per the IUCN Redlist Technical Working Group recommendations, if the elevation
# filtering of the DEM returns zero, then we ignore this layer on the assumption that there is error in the
# elevation data. This aligns with the data hygine practices recommended by Busana et al, as implemented
# in cleaning.py, where any bad values for elevation cause us assume the entire range is valid.
hab_only_total = (filtered_by_habtitat * area_map).sum()
filtered_elevation = (min_elevation_map.numpy_apply(lambda chunk: chunk <= elevation_upper) *
max_elevation_map.numpy_apply(lambda chunk: chunk >= elevation_lower))
dem_only_total = (filtered_elevation * range_map * area_map).sum()
filtered_by_both = filtered_elevation * filtered_by_habtitat
if filtered_by_both.sum() == 0:
filtered_by_both = filtered_by_habtitat
calc = filtered_by_both * area_map
with alive_bar(manual=True) as bar:
aoh_total = calc.save(result, and_sum=True, callback=bar)
# We drop the geometry as that's a lot of data, more than the raster often
species_info = filtered_species_info.drop('geometry', axis=1)
manifest = {k: v[0] for (k, v) in species_info.items()}
manifest.update({
'range_total': range_total,
'hab_total': hab_only_total,
'dem_total': dem_only_total,
'aoh_total': aoh_total,
'prevalence': (aoh_total / range_total) if range_total else 0,
})
with open(manifest_filename, 'w', encoding="utf-8") as f:
json.dump(manifest, f)
def main() -> None:
parser = argparse.ArgumentParser(description="Area of habitat calculator.")
parser.add_argument(
'--habitats',
type=str,
help="Directory of habitat rasters, one per habitat class.",
required=True,
dest="habitat_path"
)
parser.add_argument(
'--elevation-min',
type=str,
help="Minimum elevation raster.",
required=True,
dest="min_elevation_path",
)
parser.add_argument(
'--elevation-max',
type=str,
help="Maximum elevation raster",
required=True,
dest="max_elevation_path",
)
parser.add_argument(
'--area',
type=str,
help="Optional area per pixel raster. Can be 1xheight.",
required=False,
dest="area_path",
)
parser.add_argument(
'--crosswalk',
type=str,
help="Path of habitat crosswalk table.",
required=True,
dest="crosswalk_path",
)
parser.add_argument(
'--speciesdata',
type=str,
help="Single species/seasonality geojson.",
required=True,
dest="species_data_path"
)
parser.add_argument(
'--force-habitat',
help="If set, don't treat an empty habitat layer layer as per IRTWG.",
dest="force_habitat",
action='store_true',
)
parser.add_argument(
'--output',
type=str,
help='Directory where area geotiffs should be stored.',
required=True,
dest='output_path',
)
args = parser.parse_args()
aohcalc(
args.habitat_path,
args.min_elevation_path,
args.max_elevation_path,
args.area_path,
args.crosswalk_path,
args.species_data_path,
args.force_habitat,
args.output_path
)
if __name__ == "__main__":
main()