Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions geojson_pydantic/features.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
"""pydantic models for GeoJSON Feature objects."""

from typing import Any, Dict, List, Optional
from typing import Dict, Generic, List, Optional, TypeVar

from pydantic import BaseModel, Field, validator
from pydantic import Field, validator
from pydantic.generics import GenericModel

from .geometries import Geometry
from .utils import BBox

T = TypeVar("T", bound=Dict)

class Feature(BaseModel):

class Feature(GenericModel, Generic[T]):
"""Feature Model"""

type: str = Field("Feature", const=True)
geometry: Geometry
properties: Optional[Dict[Any, Any]]
properties: Optional[T]
id: Optional[str]
bbox: Optional[BBox]

Expand All @@ -30,11 +33,11 @@ def set_geometry(cls, v):
return v


class FeatureCollection(BaseModel):
class FeatureCollection(GenericModel, Generic[T]):
"""FeatureCollection Model"""

type: str = Field("FeatureCollection", const=True)
features: List[Feature]
features: List[Feature[T]]
bbox: Optional[BBox]

def __iter__(self):
Expand Down
67 changes: 60 additions & 7 deletions test/test_features.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,67 @@
from random import randint
from uuid import uuid4

import pytest
from pydantic import BaseModel, ValidationError

from geojson_pydantic.features import Feature, FeatureCollection
from geojson_pydantic.geometries import Point


@pytest.mark.parametrize("coordinates", [(1, 2)])
def test_geometry_collection_iteration(coordinates):
class GenericProperties(BaseModel):
id: str
description: str
size: int


properties = {
"id": str(uuid4()),
"description": str(uuid4()),
"size": randint(0, 1000),
}

polygon = {
"type": "Polygon",
"coordinates": [
[
[13.38272, 52.46385],
[13.42786, 52.46385],
[13.42786, 52.48445],
[13.38272, 52.48445],
[13.38272, 52.46385],
]
],
}

test_feature = {
"type": "Feature",
"geometry": polygon,
"properties": properties,
}


def test_geometry_collection_iteration():
"""test if feature collection is iterable"""
print(coordinates, type(coordinates))
polygon = Point(coordinates=coordinates)
feature = Feature(geometry=polygon)
gc = FeatureCollection(features=[feature, feature])
gc = FeatureCollection(features=[test_feature, test_feature])
iter(gc)


def test_generic_properties_is_dict():
feature = Feature(**test_feature)
assert feature.properties["id"] == test_feature["properties"]["id"]
assert type(feature.properties) == dict
assert not hasattr(feature.properties, "id")


def test_generic_properties_is_object():
feature = Feature[GenericProperties](**test_feature)

assert feature.properties.id == test_feature["properties"]["id"]
assert type(feature.properties) == GenericProperties
assert hasattr(feature.properties, "id")


def test_generic_properties_should_raise_for_string():
with pytest.raises(ValidationError):
Feature(
**({"type": "Feature", "geometry": polygon, "properties": "should raise"})
)