Skip to content

Commit b487d04

Browse files
wip
1 parent e28f9ed commit b487d04

File tree

4 files changed

+100
-69
lines changed

4 files changed

+100
-69
lines changed

workers/ohsome_quality_analyst/api/request_models.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,16 @@
1212

1313
import pydantic
1414
from geojson import Feature, FeatureCollection
15-
from pydantic import BaseModel
16-
1715
from ohsome_quality_analyst.base.layer import LayerData
18-
from ohsome_quality_analyst.definitions import (
19-
INDICATOR_LAYER,
20-
get_dataset_names,
21-
get_fid_fields,
22-
get_indicator_names,
23-
get_layer_keys,
24-
get_report_names,
25-
)
26-
from ohsome_quality_analyst.utils.helper import loads_geojson, snake_to_lower_camel
16+
from ohsome_quality_analyst.definitions import (INDICATOR_LAYER_THRESHOLDS,
17+
get_dataset_names,
18+
get_fid_fields,
19+
get_indicator_names,
20+
get_layer_keys,
21+
get_report_names)
22+
from ohsome_quality_analyst.utils.helper import (loads_geojson,
23+
snake_to_lower_camel)
24+
from pydantic import BaseModel
2725

2826
IndicatorEnum = Enum("IndicatorEnum", {name: name for name in get_indicator_names()})
2927
ReportEnum = Enum("ReportEnum", {name: name for name in get_report_names()})

workers/ohsome_quality_analyst/base/indicator.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from dataclasses import asdict, dataclass
66
from datetime import datetime, timezone
77
from io import StringIO
8-
from typing import Dict, Literal, Optional
8+
from typing import Dict, Literal, Optional, Tuple
99

1010
import matplotlib.pyplot as plt
1111
from dacite import from_dict
@@ -63,15 +63,25 @@ def label(self) -> Literal["green", "yellow", "red", "undefined"]:
6363

6464

6565
class BaseIndicator(metaclass=ABCMeta):
66-
"""The base class of every indicator."""
66+
"""The base class of every indicator.
67+
68+
Attributes:
69+
thresholds (tuple): A tuple with four float values representing the thresholds
70+
between the result classes. The first element is the threshold between the
71+
result class 1 and 2, the second element is the threshold between the result
72+
class 2 and 3 and so on.
73+
"""
6774

6875
def __init__(
6976
self,
7077
layer: Layer,
7178
feature: Feature,
79+
thresholds: Tuple[float, float, float, float],
7280
) -> None:
7381
self.layer: Layer = layer
7482
self.feature: Feature = feature
83+
self.thresholds: Tuple[float, float, float, float] = thresholds
84+
7585
# setattr(object, key, value) could be used instead of relying on from_dict.
7686
metadata = get_metadata("indicators", type(self).__name__)
7787
self.metadata: Metadata = from_dict(data_class=Metadata, data=metadata)

workers/ohsome_quality_analyst/definitions.py

Lines changed: 74 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
import os
55
from dataclasses import dataclass
66
from types import MappingProxyType
7-
from typing import Dict, List, Optional
7+
from typing import Dict, List, Optional, Tuple
88

99
import yaml
10-
1110
from ohsome_quality_analyst.base.layer import LayerDefinition
1211
from ohsome_quality_analyst.config import get_config_value
1312
from ohsome_quality_analyst.utils.exceptions import RasterDatasetUndefinedError
14-
from ohsome_quality_analyst.utils.helper import flatten_sequence, get_module_dir
13+
from ohsome_quality_analyst.utils.helper import (flatten_sequence,
14+
get_module_dir)
1515

1616

1717
@dataclass(frozen=True)
@@ -30,6 +30,22 @@ class RasterDataset:
3030
nodata: Optional[int]
3131

3232

33+
@dataclass(frozen=True)
34+
class IndicatorLayerThreshold:
35+
"""Valid Indicator/Layer combinations with optional threshold values.
36+
37+
Args:
38+
indicator (str): Indicator name
39+
layer (str): Layer name
40+
thresholds (tuple): A tuple with thresholds values.
41+
First element of the tuple is the threshold between result class 1 and 2.
42+
"""
43+
44+
indicator: str
45+
layer: str
46+
threshold: Tuple[float, float, float, float] = None
47+
48+
3349
RASTER_DATASETS = (
3450
RasterDataset(
3551
"GHS_BUILT_R2018A",
@@ -58,57 +74,57 @@ class RasterDataset:
5874
)
5975

6076
# Possible indicator layer combinations
61-
INDICATOR_LAYER = (
62-
("BuildingCompleteness", "building_area"),
63-
("GhsPopComparisonBuildings", "building_count"),
64-
("GhsPopComparisonRoads", "jrc_road_length"),
65-
("GhsPopComparisonRoads", "major_roads_length"),
66-
("MappingSaturation", "building_count"),
67-
("MappingSaturation", "major_roads_length"),
68-
("MappingSaturation", "amenities"),
69-
("MappingSaturation", "jrc_health_count"),
70-
("MappingSaturation", "jrc_mass_gathering_sites_count"),
71-
("MappingSaturation", "jrc_railway_length"),
72-
("MappingSaturation", "jrc_road_length"),
73-
("MappingSaturation", "jrc_education_count"),
74-
("MappingSaturation", "mapaction_settlements_count"),
75-
("MappingSaturation", "mapaction_major_roads_length"),
76-
("MappingSaturation", "mapaction_rail_length"),
77-
("MappingSaturation", "mapaction_lakes_area"),
78-
("MappingSaturation", "mapaction_rivers_length"),
79-
("MappingSaturation", "ideal_vgi_infrastructure"),
80-
("MappingSaturation", "poi"),
81-
("MappingSaturation", "lulc"),
82-
("Currentness", "major_roads_count"),
83-
("Currentness", "building_count"),
84-
("Currentness", "amenities"),
85-
("Currentness", "jrc_health_count"),
86-
("Currentness", "jrc_education_count"),
87-
("Currentness", "jrc_road_count"),
88-
("Currentness", "jrc_railway_count"),
89-
("Currentness", "jrc_airport_count"),
90-
("Currentness", "jrc_water_treatment_plant_count"),
91-
("Currentness", "jrc_power_generation_plant_count"),
92-
("Currentness", "jrc_cultural_heritage_site_count"),
93-
("Currentness", "jrc_bridge_count"),
94-
("Currentness", "jrc_mass_gathering_sites_count"),
95-
("Currentness", "mapaction_settlements_count"),
96-
("Currentness", "mapaction_major_roads_length"),
97-
("Currentness", "mapaction_rail_length"),
98-
("Currentness", "mapaction_lakes_count"),
99-
("Currentness", "mapaction_rivers_length"),
100-
("PoiDensity", "poi"),
101-
("TagsRatio", "building_count"),
102-
("TagsRatio", "major_roads_length"),
103-
("TagsRatio", "jrc_health_count"),
104-
("TagsRatio", "jrc_education_count"),
105-
("TagsRatio", "jrc_road_length"),
106-
("TagsRatio", "jrc_airport_count"),
107-
("TagsRatio", "jrc_power_generation_plant_count"),
108-
("TagsRatio", "jrc_cultural_heritage_site_count"),
109-
("TagsRatio", "jrc_bridge_count"),
110-
("TagsRatio", "jrc_mass_gathering_sites_count"),
111-
("Minimal", "minimal"),
77+
INDICATOR_LAYER_THRESHOLDS = (
78+
IndicatorLayerThreshold("BuildingCompleteness", "building_area"),
79+
IndicatorLayerThreshold("GhsPopComparisonBuildings", "building_count"),
80+
IndicatorLayerThreshold("GhsPopComparisonRoads", "jrc_road_length"),
81+
IndicatorLayerThreshold("GhsPopComparisonRoads", "major_roads_length"),
82+
IndicatorLayerThreshold("MappingSaturation", "building_count"),
83+
IndicatorLayerThreshold("MappingSaturation", "major_roads_length"),
84+
IndicatorLayerThreshold("MappingSaturation", "amenities"),
85+
IndicatorLayerThreshold("MappingSaturation", "jrc_health_count"),
86+
IndicatorLayerThreshold("MappingSaturation", "jrc_mass_gathering_sites_count"),
87+
IndicatorLayerThreshold("MappingSaturation", "jrc_railway_length"),
88+
IndicatorLayerThreshold("MappingSaturation", "jrc_road_length"),
89+
IndicatorLayerThreshold("MappingSaturation", "jrc_education_count"),
90+
IndicatorLayerThreshold("MappingSaturation", "mapaction_settlements_count"),
91+
IndicatorLayerThreshold("MappingSaturation", "mapaction_major_roads_length"),
92+
IndicatorLayerThreshold("MappingSaturation", "mapaction_rail_length"),
93+
IndicatorLayerThreshold("MappingSaturation", "mapaction_lakes_area"),
94+
IndicatorLayerThreshold("MappingSaturation", "mapaction_rivers_length"),
95+
IndicatorLayerThreshold("MappingSaturation", "ideal_vgi_infrastructure"),
96+
IndicatorLayerThreshold("MappingSaturation", "poi"),
97+
IndicatorLayerThreshold("MappingSaturation", "lulc"),
98+
IndicatorLayerThreshold("Currentness", "major_roads_count"),
99+
IndicatorLayerThreshold("Currentness", "building_count"),
100+
IndicatorLayerThreshold("Currentness", "amenities"),
101+
IndicatorLayerThreshold("Currentness", "jrc_health_count"),
102+
IndicatorLayerThreshold("Currentness", "jrc_education_count"),
103+
IndicatorLayerThreshold("Currentness", "jrc_road_count"),
104+
IndicatorLayerThreshold("Currentness", "jrc_railway_count"),
105+
IndicatorLayerThreshold("Currentness", "jrc_airport_count"),
106+
IndicatorLayerThreshold("Currentness", "jrc_water_treatment_plant_count"),
107+
IndicatorLayerThreshold("Currentness", "jrc_power_generation_plant_count"),
108+
IndicatorLayerThreshold("Currentness", "jrc_cultural_heritage_site_count"),
109+
IndicatorLayerThreshold("Currentness", "jrc_bridge_count"),
110+
IndicatorLayerThreshold("Currentness", "jrc_mass_gathering_sites_count"),
111+
IndicatorLayerThreshold("Currentness", "mapaction_settlements_count"),
112+
IndicatorLayerThreshold("Currentness", "mapaction_major_roads_length"),
113+
IndicatorLayerThreshold("Currentness", "mapaction_rail_length"),
114+
IndicatorLayerThreshold("Currentness", "mapaction_lakes_count"),
115+
IndicatorLayerThreshold("Currentness", "mapaction_rivers_length"),
116+
IndicatorLayerThreshold("PoiDensity", "poi"),
117+
IndicatorLayerThreshold("TagsRatio", "building_count"),
118+
IndicatorLayerThreshold("TagsRatio", "major_roads_length"),
119+
IndicatorLayerThreshold("TagsRatio", "jrc_health_count"),
120+
IndicatorLayerThreshold("TagsRatio", "jrc_education_count"),
121+
IndicatorLayerThreshold("TagsRatio", "jrc_road_length"),
122+
IndicatorLayerThreshold("TagsRatio", "jrc_airport_count"),
123+
IndicatorLayerThreshold("TagsRatio", "jrc_power_generation_plant_count"),
124+
IndicatorLayerThreshold("TagsRatio", "jrc_cultural_heritage_site_count"),
125+
IndicatorLayerThreshold("TagsRatio", "jrc_bridge_count"),
126+
IndicatorLayerThreshold("TagsRatio", "jrc_mass_gathering_sites_count"),
127+
IndicatorLayerThreshold("Minimal", "minimal"),
112128
)
113129

114130
ATTRIBUTION_TEXTS = MappingProxyType(
@@ -274,9 +290,11 @@ def get_attribution(data_keys: list) -> str:
274290

275291
def get_valid_layers(indcator_name: str) -> tuple:
276292
"""Get valid Indicator/Layer combination of an Indicator."""
277-
return tuple([tup[1] for tup in INDICATOR_LAYER if tup[0] == indcator_name])
293+
return tuple(
294+
[tup[1] for tup in INDICATOR_LAYER_THRESHOLDS if tup[0] == indcator_name]
295+
)
278296

279297

280298
def get_valid_indicators(layer_key: str) -> tuple:
281299
"""Get valid Indicator/Layer combination of a Layer."""
282-
return tuple([tup[0] for tup in INDICATOR_LAYER if tup[1] == layer_key])
300+
return tuple([tup[0] for tup in INDICATOR_LAYER_THRESHOLDS if tup[1] == layer_key])

workers/ohsome_quality_analyst/indicators/building_completeness/indicator.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
from io import StringIO
44
from string import Template
5+
from typing import Optional, Tuple
56

67
import dateutil.parser
78
import geojson
@@ -58,10 +59,14 @@ def __init__(
5859
self,
5960
layer: Layer,
6061
feature: Feature,
62+
thresholds: Optional[Tuple[float, float, float, float]],
6163
) -> None:
64+
if thresholds is None:
65+
thresholds = (0.2, 0.5, 0.8, 0.9)
6266
super().__init__(
6367
layer=layer,
6468
feature=feature,
69+
thresholds=thresholds,
6570
)
6671
self.model_name: str = "Random Forest Regressor"
6772
# Lists of elements per hexagonal cell

0 commit comments

Comments
 (0)