28 lines
849 B
Python
28 lines
849 B
Python
from dataclasses import dataclass, asdict
|
|
from typing import List, Dict, Any, TYPE_CHECKING, Optional
|
|
|
|
from models.geo_json.feature import GeoJSONFeature
|
|
|
|
if TYPE_CHECKING:
|
|
from models.circuit import Circuit
|
|
|
|
@dataclass
|
|
class GeoJSON:
|
|
type: str
|
|
bbox: Optional[list[float]]
|
|
features: List[GeoJSONFeature]
|
|
|
|
@classmethod
|
|
def from_dict(cls, circuit: 'Circuit', data: Dict[str, Any]) -> 'GeoJSON':
|
|
return cls(
|
|
type=data['type'],
|
|
bbox=data['bbox'] if 'bbox' in data else None,
|
|
features=[GeoJSONFeature.from_dict(circuit, f) for f in data['features']],
|
|
)
|
|
|
|
def to_dict(self) -> Dict[str, Any]:
|
|
"""Convert the GeoJSON object back to a dictionary"""
|
|
data = asdict(self)
|
|
# Remove None values
|
|
return {k: v for k, v in data.items() if v is not None}
|