-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
589 lines (495 loc) · 21.5 KB
/
models.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
from __future__ import annotations
import enum
import logging
import os
from dataclasses import dataclass, field
from pathlib import Path
from typing import TYPE_CHECKING
import yaml
from arho_feature_template.exceptions import ConfigSyntaxError
from arho_feature_template.project.layers.code_layers import AdditionalInformationTypeLayer, UndergroundTypeLayer
from arho_feature_template.qgis_plugin_tools.tools.resources import resources_path
from arho_feature_template.utils.misc_utils import deserialize_localized_text, get_layer_by_name, iface
if TYPE_CHECKING:
from datetime import datetime
from qgis.core import QgsFeature, QgsGeometry
from qgis.PyQt.QtCore import QDate
logger = logging.getLogger(__name__)
DEFAULT_PLAN_REGULATIONS_CONFIG_PATH = Path(os.path.join(resources_path(), "libraries", "kaavamaaraykset.yaml"))
ADDITIONAL_INFORMATION_CONFIG_PATH = Path(os.path.join(resources_path(), "libraries", "additional_information.yaml"))
class AttributeValueDataType(str, enum.Enum):
LOCALIZED_TEXT = "LocalizedText"
TEXT = "Text"
NUMERIC = "Numeric"
NUMERIC_RANGE = "NumericRange"
POSITIVE_NUMERIC = "PositiveNumeric"
POSITIVE_NUMERIC_RANGE = "PositiveNumericRange"
DECIMAL = "Decimal"
DECIMAL_RANGE = "DecimalRange"
POSITIVE_DECIMAL = "PositiveDecimal"
POSITIVE_DECIMAL_RANGE = "PositiveDecimalRange"
CODE = "Code"
IDENTIFIER = "Identifier"
SPOT_ELEVATION = "SpotElevation"
TIME_PERIOD = "TimePeriod"
TIME_PERIOD_DATE_ONLY = "TimePeriodDateOnly"
class TemplateSyntaxError(Exception):
def __init__(self, template_cls: str, message: str):
super().__init__(f"Invalid template syntax for {template_cls}: {message}")
@dataclass
class FeatureTemplateLibrary:
"""Describes the configuration of a feature template library"""
name: str
version: str | None
description: str | None
feature_templates: list[PlanFeature]
@classmethod
def find_matching_group_config(cls, group_name: str, regulation_group_libraries: list[RegulationGroupLibrary]):
for library in regulation_group_libraries:
for category in library.regulation_group_categories:
for group in category.regulation_groups:
if group.name == group_name:
return group
return None
@classmethod
def from_config_file(
cls, config_fp: Path, regulation_group_libraries: list[RegulationGroupLibrary]
) -> FeatureTemplateLibrary:
get_underground_id = UndergroundTypeLayer.get_attribute_value_by_another_attribute_value
with config_fp.open(encoding="utf-8") as f:
data = yaml.safe_load(f)
try:
return FeatureTemplateLibrary(
name=data["name"],
version=data.get("version"),
description=data.get("description"),
feature_templates=[
PlanFeature(
geom=None,
type_of_underground_id=(
get_underground_id("id", "value", feature_data.get("type_of_underground"))
if feature_data.get("type_of_underground")
else None
),
layer_name=feature_data.get("layer_name"),
name=feature_data.get("name"),
description=feature_data.get("description"),
regulation_groups=[
group
for group_name in feature_data.get("regulation_groups", [])
if (group := cls.find_matching_group_config(group_name, regulation_group_libraries))
],
lifecycles=[],
plan_id=None,
id_=None,
)
for feature_data in data["feature_templates"]
],
)
except KeyError as e:
raise TemplateSyntaxError(str(cls), str(e)) from e
@dataclass
class RegulationGroupCategory:
category_code: str | None
name: str | None
regulation_groups: list[RegulationGroup]
@dataclass
class RegulationGroupLibrary:
"""Describes the configuration of a plan regulation group library"""
name: str
version: int | None
description: str | None
regulation_group_categories: list[RegulationGroupCategory]
# regulation_groups: list[RegulationGroup]
@classmethod
def from_config_file(cls, config_fp: Path) -> RegulationGroupLibrary:
with config_fp.open(encoding="utf-8") as f:
data = yaml.safe_load(f)
return RegulationGroupLibrary(
name=data["name"],
version=data.get("version"),
description=data.get("description"),
regulation_group_categories=[
RegulationGroupCategory(
category_code=category_data["category_code"],
name=category_data.get("name"),
regulation_groups=[
RegulationGroup.from_config_data(config_data)
for config_data in category_data["plan_regulation_groups"]
],
)
for category_data in data["categories"]
],
)
@dataclass
class RegulationLibrary:
"""Describes the set of plan regulations."""
version: str
regulations: list[RegulationConfig]
regulations_dict: dict[str, RegulationConfig]
_instance: RegulationLibrary | None = None
@classmethod
def get_instance(cls) -> RegulationLibrary:
"""Get the singleton instance, if initialized."""
if cls._instance is None:
return cls.initialize()
return cls._instance
@classmethod
def get_regulations(cls) -> list[RegulationConfig]:
"""Get the list of top-level regulation configs, if instance is initialized."""
return cls.get_instance().regulations
@classmethod
def get_regulations_dict(cls) -> dict[str, RegulationConfig]:
"""Get all regulations in a dictionary where keys are regulations codes and values RegulationConfigs."""
return cls.get_instance().regulations_dict
@classmethod
def get_regulation_by_code(cls, regulation_code: str) -> RegulationConfig | None:
"""Get a regulation by it's regulation code (if exists)."""
return cls.get_instance().regulations_dict.get(regulation_code)
@classmethod
def initialize(
cls,
config_path: Path = DEFAULT_PLAN_REGULATIONS_CONFIG_PATH,
type_of_plan_regulations_layer_name="Kaavamääräyslaji",
) -> RegulationLibrary:
# Initialize RegulationLibrary and RegulationConfigs from QGIS layer and config file
# 1. Read config file into a dict
with config_path.open(encoding="utf-8") as f:
config_data = yaml.safe_load(f)
# 2. Read code layer
layer = get_layer_by_name(type_of_plan_regulations_layer_name)
if layer is None:
msg = f"Could not find layer {type_of_plan_regulations_layer_name}!"
raise KeyError(msg)
# 3. Initialize regulation configs from layer. Storing them by their ID is handy for adding childs later
id_to_regulation_map: dict[str, RegulationConfig] = {
feature["id"]: RegulationConfig.from_feature(feature) for feature in layer.getFeatures()
}
# 4. Add information from config file (value, unit, category only) and link child regulations
try:
regulation_data: dict = {data["regulation_code"]: data for data in config_data["plan_regulations"]}
top_level_regulations: list[RegulationConfig] = []
for regulation_config in id_to_regulation_map.values():
# Add possible information from config data file
data = regulation_data.get(regulation_config.regulation_code)
if data:
regulation_config.category_only = data.get("category_only", False)
regulation_config.default_value = AttributeValue(
value_data_type=(AttributeValueDataType(data["value_type"]) if "value_type" in data else None),
unit=data["unit"] if "unit" in data else None,
)
# Top-level, add to list
if not regulation_config.parent_id:
top_level_regulations.append(regulation_config)
else:
# Add as child of another regulation
id_to_regulation_map[regulation_config.parent_id].child_regulations.append(regulation_config)
except KeyError as e:
raise ConfigSyntaxError(str(e)) from e
# 5. Create dictionary, useful when creating PlanRegulationDefinitions at least
regulations_dict: dict[str, RegulationConfig] = {}
for reg in top_level_regulations:
reg.add_to_dictionary(regulations_dict)
# 5. Create instance
cls._instance = cls(
version=config_data["version"], regulations=top_level_regulations, regulations_dict=regulations_dict
)
logger.info("RegulationLibrary initialized successfully.")
return cls._instance
@dataclass
class RegulationConfig:
"""
Describes plan regulation type.
Initialized from DB/QGIS layer and extended with data from a config file.
"""
id: str
regulation_code: str
name: str | None
description: str | None
status: str
level: int
parent_id: str | None
child_regulations: list[RegulationConfig] = field(default_factory=list)
# Data from config file
category_only: bool = False
default_value: AttributeValue | None = None
# NOTE: Perhaps this ("model_from_feature") should be method of PlanTypeLayer class?
@classmethod
def from_feature(cls, feature: QgsFeature) -> RegulationConfig:
"""
Initialize PlanRegulationConfig from QgsFeature.
Child regulations, value type ,category only and unit need to be set separately.
"""
return cls(
id=feature["id"],
regulation_code=feature["value"],
name=deserialize_localized_text(feature["name"]),
description=deserialize_localized_text(feature["description"]),
status=feature["status"],
level=feature["level"],
parent_id=feature["parent_id"],
)
def add_to_dictionary(self, dictionary: dict[str, RegulationConfig]):
"""Add child regulations to dictionary too."""
dictionary[self.regulation_code] = self
for regulation in self.child_regulations:
regulation.add_to_dictionary(dictionary)
@dataclass
class AdditionalInformationConfig:
# From layer
id: str
additional_information_type: str
name: str | None
description: str | None
status: str
level: int
parent_id: str | None
children: list[str] = field(default_factory=list)
# From config file
default_value: AttributeValue | None = None
@dataclass
class AdditionalInformationConfigLibrary:
version: str
configs: dict[str, AdditionalInformationConfig] = field(default_factory=dict)
top_level_codes: list[str] = field(default_factory=list)
_id_to_configs: dict[str, AdditionalInformationConfig] = field(default_factory=dict)
_instance: AdditionalInformationConfigLibrary | None = None
@classmethod
def get_instance(cls) -> AdditionalInformationConfigLibrary:
"""Get the singleton instance, if initialized."""
if cls._instance is None:
cls._instance = cls.initialize(ADDITIONAL_INFORMATION_CONFIG_PATH)
return cls._instance
@classmethod
def initialize(cls, config_fp: Path = ADDITIONAL_INFORMATION_CONFIG_PATH) -> AdditionalInformationConfigLibrary:
with config_fp.open(encoding="utf-8") as f:
data = yaml.safe_load(f)
if data.get("version") != 1:
msg = "Version must be 1"
raise ConfigSyntaxError(msg)
config_file_configs = {
ai_config_data["code"]: ai_config_data for ai_config_data in data["additional_information"]
}
code_to_configs: dict[str, AdditionalInformationConfig] = {}
id_to_cofigs: dict[str, AdditionalInformationConfig] = {}
for feature in AdditionalInformationTypeLayer.get_features():
ai_code = feature["value"]
congig_file_config = config_file_configs.get(ai_code)
default_value = (
AttributeValue(
value_data_type=AttributeValueDataType(congig_file_config["data_type"]),
unit=congig_file_config.get("unit"),
)
if congig_file_config is not None
else None
)
ai_config = AdditionalInformationConfig(
id=feature["id"],
additional_information_type=ai_code,
name=deserialize_localized_text(feature["name"]),
description=deserialize_localized_text(feature["description"]),
status=feature["status"],
level=feature["level"],
parent_id=feature["parent_id"],
default_value=default_value,
)
code_to_configs[ai_code] = ai_config
id_to_cofigs[feature["id"]] = ai_config
top_level_codes = []
for ai_config in code_to_configs.values():
if ai_config.parent_id:
id_to_cofigs[ai_config.parent_id].children.append(ai_config.additional_information_type)
else:
top_level_codes.append(ai_config.additional_information_type)
return cls(
version=data["version"],
configs=code_to_configs,
top_level_codes=top_level_codes,
_id_to_configs=id_to_cofigs,
)
@classmethod
def get_config_by_code(cls, code: str) -> AdditionalInformationConfig:
"""Get a regulation by it's regulation code.
Raises a KeyError if code not exists.
"""
return cls.get_instance().configs[code]
@classmethod
def get_config_by_id(cls, id_: str) -> AdditionalInformationConfig:
"""Get a regulation by it's regulation code.
Raises a KeyError if code not exists.
"""
return cls.get_instance()._id_to_configs[id_] # noqa: SLF001
@dataclass
class AttributeValue:
value_data_type: AttributeValueDataType | None = None
numeric_value: int | float | None = None
numeric_range_min: int | float | None = None
numeric_range_max: int | float | None = None
unit: str | None = None
text_value: str | None = None
text_syntax: str | None = None
code_list: str | None = None
code_value: str | None = None
code_title: str | None = None
height_reference_point: str | None = None
@dataclass
class AdditionalInformation:
config: AdditionalInformationConfig # includes code and unit among other needed data for saving feature
id_: str | None = None
plan_regulation_id: str | None = None
type_additional_information_id: str | None = None
value: AttributeValue | None = None
@dataclass
class Regulation:
config: RegulationConfig # includes regulation_code and unit among other needed data for saving feature
value: AttributeValue | None = None
additional_information: list[AdditionalInformation] = field(default_factory=list)
regulation_number: int | None = None
files: list[str] = field(default_factory=list)
theme: str | None = None
topic_tag: str | None = None
verbal_regulation_type_id: str | None = None
regulation_group_id: str | None = None
id_: str | None = None
@dataclass
class Proposition:
value: str
theme_id: str | None = None
proposition_number: int | None = None
regulation_group_id: str | None = None
id_: str | None = None
@dataclass
class RegulationGroup:
type_code_id: str | None = None
name: str | None = None
short_name: str | None = None
color_code: str | None = None
group_number: int | None = None
regulations: list[Regulation] = field(default_factory=list)
propositions: list[Proposition] = field(default_factory=list)
id_: str | None = None
@staticmethod
def _additional_information_model_from_config(info_data: dict) -> AdditionalInformation:
ai_config = AdditionalInformationConfigLibrary.get_config_by_code(info_data["type"])
return AdditionalInformation(
config=ai_config,
value=AttributeValue(
value_data_type=info_data.get(
"value_data_type",
ai_config.default_value.value_data_type if ai_config.default_value is not None else None,
),
numeric_value=info_data.get("numeric_value"),
numeric_range_min=info_data.get("numeric_range_min"),
numeric_range_max=info_data.get("numeric_range_max"),
unit=info_data.get(
"unit",
ai_config.default_value.unit if ai_config.default_value is not None else None,
),
text_value=info_data.get("text_value"),
text_syntax=info_data.get("text_syntax"),
code_list=info_data.get("code_list"),
code_value=info_data.get("code_value"),
code_title=info_data.get("code_title"),
height_reference_point=info_data.get("height_reference_point"),
),
)
@classmethod
def from_config_data(cls, data: dict) -> RegulationGroup:
regulations = []
for reg_data in data["plan_regulations"]:
reg_code = reg_data["regulation_code"]
config = RegulationLibrary.get_regulation_by_code(reg_code)
if config:
regulations.append(
Regulation(
config=config,
value=reg_data.get("value"),
additional_information=[
cls._additional_information_model_from_config(info)
for info in reg_data.get("additional_information", [])
],
regulation_number=reg_data.get("regulation_number"),
files=reg_data.get("files") if reg_data.get("files") else [],
theme=reg_data.get("theme"),
topic_tag=reg_data.get("topic_tag"),
regulation_group_id=None,
id_=None,
)
)
else:
iface.messageBar().pushWarning("", f"Could not find plan regulation {reg_code}!")
return cls(
type_code_id=data.get("type"), # NOTE: Might need to convert type code into type code ID here when
# config file has type codes for regulation groups
name=data.get("name"),
short_name=data.get("short_name"),
color_code=data.get("color_code"),
group_number=data.get("group_number"),
regulations=regulations,
propositions=[],
id_=None,
)
@dataclass
class LifeCycle:
status_id: str | None = None
id_: str | None = None
plan_id: str | None = None
plan_regulation_id: str | None = None
plan_proposition_id: str | None = None
starting_at: QDate | None = None
ending_at: QDate | None = None
# might not need the following
land_use_are_id: str | None = None
other_area_id: str | None = None
line_id: str | None = None
land_use_point_id: str | None = None
other_point_id: str | None = None
@dataclass
class PlanFeature:
geom: QgsGeometry | None = None # Need to allow None for feature templates
type_of_underground_id: str | None = None
layer_name: str | None = None
name: str | None = None
description: str | None = None
regulation_groups: list[RegulationGroup] = field(default_factory=list)
lifecycles: list[LifeCycle] = field(default_factory=list)
plan_id: int | None = None
id_: str | None = None
@classmethod
def from_config_data(cls, data: dict) -> PlanFeature:
# TODO: Implement
return cls(**data)
@dataclass
class Plan:
name: str | None = None
description: str | None = None
plan_type_id: str | None = None
lifecycle_status_id: str | None = None
lifecycles: list[LifeCycle] = field(default_factory=list)
record_number: str | None = None
matter_management_identifier: str | None = None
permanent_plan_identifier: str | None = None
producers_plan_identifier: str | None = None
organisation_id: str | None = None
general_regulations: list[RegulationGroup] = field(default_factory=list)
documents: list[Document] = field(default_factory=list)
geom: QgsGeometry | None = None
id_: str | None = None
@dataclass
class Document:
name: str | None = None
url: str | None = None
type_of_document_id: str | None = None
decision: bool | None = None
# permanent_document_identifier: str | None = None
category_of_publicity_id: str | None = None
personal_data_content_id: str | None = None
retention_time_id: str | None = None
language_id: str | None = None
document_date: datetime | None = None
# exported_at: str | None = None
# exported_file_key:
confirmation_date: datetime | None = None
arrival_date: datetime | None = None
plan_id: int | None = None
id_: str | None = None