Debian Codegraph @

geojson_pydantic/features.py

raw symbols SCIP
1 """pydantic models for GeoJSON Feature objects."""
2
3 from typing import Any, Dict, Generic, Iterator, List, Literal, Optional, TypeVar, Union
4
5 from pydantic import BaseModel, Field, StrictInt, StrictStr, field_validator
6
7 from geojson_pydantic.base import _GeoJsonBase
8 from geojson_pydantic.geometries import Geometry
9
10 Props = TypeVar("Props", bound=Union[Dict[str, Any], BaseModel])
11 Geom = TypeVar("Geom", bound=Geometry)
12
13
14 class Feature(_GeoJsonBase, Generic[Geom, Props]):
15 """Feature Model"""
16
17 type: Literal["Feature"]
18 geometry: Union[Geom, None] = Field(...)
19 properties: Union[Props, None] = Field(...)
20 id: Optional[Union[StrictInt, StrictStr]] = None
21
22 __geojson_exclude_if_none__ = {"bbox", "id"}
23
24 @field_validator("geometry", mode="before")
25 def set_geometry(cls, geometry: Any) -> Any:
26 """set geometry from geo interface or input"""
27 if hasattr(geometry, "__geo_interface__"):
28 return geometry.__geo_interface__
29
30 return geometry
31
32
33 Feat = TypeVar("Feat", bound=Feature)
34
35
36 class FeatureCollection(_GeoJsonBase, Generic[Feat]):
37 """FeatureCollection Model"""
38
39 type: Literal["FeatureCollection"]
40 features: List[Feat]
41
42 def iter(self) -> Iterator[Feat]:
43 """iterate over features"""
44 return iter(self.features)
45
46 @property
47 def length(self) -> int:
48 """return features length"""
49 return len(self.features)