1
- import json
1
+ from typing import Dict
2
2
3
3
import geojson
4
- from geojson import FeatureCollection
4
+ from geojson_pydantic import Feature , FeatureCollection , MultiPolygon , Polygon
5
5
from pydantic import BaseModel , ConfigDict , Field , field_validator
6
6
7
7
from ohsome_quality_api .attributes .definitions import AttributeEnum
8
8
from ohsome_quality_api .topics .definitions import TopicEnum
9
9
from ohsome_quality_api .topics .models import TopicData
10
10
from ohsome_quality_api .utils .helper import snake_to_lower_camel
11
- from ohsome_quality_api .utils .validators import validate_geojson
12
11
13
12
14
13
class BaseConfig (BaseModel ):
@@ -20,8 +19,11 @@ class BaseConfig(BaseModel):
20
19
)
21
20
22
21
22
+ FeatureCollection_ = FeatureCollection [Feature [Polygon | MultiPolygon , Dict ]]
23
+
24
+
23
25
class BaseBpolys (BaseConfig ):
24
- bpolys : dict = Field (
26
+ bpolys : FeatureCollection_ = Field (
25
27
{
26
28
"type" : "FeatureCollection" ,
27
29
"features" : [
@@ -46,12 +48,11 @@ class BaseBpolys(BaseConfig):
46
48
47
49
@field_validator ("bpolys" )
48
50
@classmethod
49
- def validate_bpolys (cls , value ) -> FeatureCollection :
50
- obj = geojson .loads (json .dumps (value ))
51
- if not isinstance (obj , FeatureCollection ):
52
- raise ValueError ("must be of type FeatureCollection" )
53
- validate_geojson (obj ) # Check if exceptions are raised
54
- return obj
51
+ def transform (cls , value ) -> geojson .FeatureCollection :
52
+ # NOTE: `geojson_pydantic` library is used only for validation and openAPI-spec
53
+ # generation. To avoid refactoring all code the FeatureCollection object of
54
+ # the `geojson` library is still used every else.
55
+ return geojson .loads (value .model_dump_json ())
55
56
56
57
57
58
class IndicatorRequest (BaseBpolys ):
0 commit comments