from .geometry import Geometry
from typing import Any, Optional
from dataclasses import dataclass, field, InitVar
[docs]
@dataclass(slots=True, kw_only=True)
class Image:
"""
Represents an image in a geographical context.
Attributes:
id (int): The ID of the image.
file_name (str): The filename of the image.
width (int): The width of the image.
height (int): The height of the image.
severity (str | None): The severity level associated with the image (if any).
site_type (str | None): The type of site depicted in the image (if any).
valid_fine_grain (bool | None): Indicates if the image is a valid fine grain (if any).
evidence (bool | None): Indicates if the image is marked as evidence (if any).
is_candidate_location (bool | None): Indicates if the image is a candidate location (if any).
geometry (Geometry | None): The geographical geometry associated with the image (if any).
annotations (list | None): List of annotations associated with the image (if any).
"""
# Properties
id: int = field(init=False)
file_name: str = field(init=False)
width: int = field(init=False)
height: int = field(init=False)
# Optional properties
severity: Optional[str] = field(init=False, default=None)
site_type: Optional[str] = field(init=False, default=None)
valid_fine_grain: Optional[bool] = field(init=False, default=None)
evidence: Optional[bool] = field(init=False, default=None)
is_candidate_location: Optional[bool] = field(init=False, default=None)
geometry: Optional[Geometry] = field(init=False, default=None)
annotations: Optional[str] = field(init=False, default=None)
# Init-only fields
image: InitVar[dict[str, str]] = field(kw_only=False)
input_geometry: InitVar[Optional[dict[str, Any]]] = field(default=None)
def __post_init__(self, image: dict[str, str], input_geometry: Optional[dict[str, Any]]):
# All GTs have these properties
self.id = int(image["id"])
self.file_name = image["file_name"]
self.width = int(image["width"])
self.height = int(image["height"])
# These properties are found in the expert GTs only
self.severity = image.get("severity")
self.site_type = image.get("site_type")
self.valid_fine_grain = bool(image.get("valid_fine_grain"))
self.evidence = bool(image.get("evidence"))
self.is_candidate_location = bool(image.get("is_candidate_location"))
# We are not processing these, we are only saving them
# in order to not have to look for them again
self.annotations = image.get("annotations")
if input_geometry:
self.geometry = Geometry(type=str(input_geometry.get("type")), coords=str(input_geometry.get("coordinates")))
def __str__(self):
image_string = "Image filename: " + self.file_name + "\n"
image_string += "Image ID: " + str(self.id) + "\n"
image_string += "Image width: " + str(self.width) + "\n"
image_string += "Image height: " + str(self.height) + "\n"
if any([self.severity, self.site_type, self.valid_fine_grain, self.evidence, self.is_candidate_location, self.geometry]):
image_string += "Severity: " + str(self.severity) + "\n"
image_string += "Site type: " + str(self.site_type) + "\n"
image_string += "Valid fine grain: " + str(self.valid_fine_grain) + "\n"
image_string += "Is evidence: " + str(self.evidence) + "\n"
image_string += "Is candidate location: " + str(self.is_candidate_location) + "\n"
if self.geometry:
image_string += self.geometry.__str__() + "\n"
else:
image_string += "No geometry provided\n"
return image_string