-
Notifications
You must be signed in to change notification settings - Fork 37
Enforce type geometry properties #94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
5f98bdb
8c52f31
1ccae1f
9c65e5c
9c801bf
cb6fbc4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
"""pydantic models for GeoJSON Feature objects.""" | ||
|
||
from typing import Any, Dict, Generic, Iterator, List, Optional, TypeVar, Union | ||
from typing import Any, Dict, Generic, Iterator, List, Literal, Optional, TypeVar, Union | ||
|
||
from pydantic import BaseModel, Field, validator | ||
from pydantic.generics import GenericModel | ||
|
@@ -15,9 +15,9 @@ | |
class Feature(GenericModel, Generic[Geom, Props]): | ||
"""Feature Model""" | ||
|
||
type: str = Field(default="Feature", const=True) | ||
geometry: Optional[Geom] = None | ||
properties: Optional[Props] = None | ||
type: Literal["Feature"] | ||
geometry: Union[Geom, None] = Field(...) | ||
properties: Union[Props, None] = Field(...) | ||
id: Optional[str] = None | ||
bbox: Optional[BBox] = None | ||
|
||
|
@@ -31,6 +31,7 @@ def set_geometry(cls, geometry: Any) -> Any: | |
"""set geometry from geo interface or input""" | ||
if hasattr(geometry, "__geo_interface__"): | ||
return geometry.__geo_interface__ | ||
|
||
return geometry | ||
|
||
@property | ||
|
@@ -44,23 +45,22 @@ def __geo_interface__(self) -> Dict[str, Any]: | |
"geometry": self.geometry.__geo_interface__ | ||
if self.geometry is not None | ||
else None, | ||
"properties": self.properties, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. properties should always be set to at least There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Technically GeoJSON can have null properties. But geo_interface says
So, I would say leave this how it was. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the issue then is that you wouldn't be able to do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤔 https://github.com/jazzband/geojson/blob/main/geojson/feature.py#L31 always default to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: we already allow There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is an interesting one. Not being able to put it back into itself is problematic. And
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Feature(type="Feature", geometry=None, properties=None).__geo_interface__
>> {'type': 'Feature', 'geometry': None, 'properties': None} we do add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @vincentsarago Sorry, I had started to edit that comment and apparently never saved it. Got lost in the 12 tabs open looking at too many parts at the same time. Based on a strict reading geometry may be wrong. However jazzband/geojson is just returning self as the Which actually seems like a good idea, its supposed to be modeled after geojson, so just return |
||
} | ||
|
||
if self.bbox: | ||
geo["bbox"] = self.bbox | ||
|
||
if self.id: | ||
geo["id"] = self.id | ||
|
||
if self.properties: | ||
geo["properties"] = self.properties | ||
|
||
return geo | ||
|
||
|
||
class FeatureCollection(GenericModel, Generic[Geom, Props]): | ||
"""FeatureCollection Model""" | ||
|
||
type: str = Field(default="FeatureCollection", const=True) | ||
type: Literal["FeatureCollection"] | ||
features: List[Feature[Geom, Props]] | ||
bbox: Optional[BBox] = None | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
name = "geojson-pydantic" | ||
description = "Pydantic data models for the GeoJSON spec." | ||
readme = "README.md" | ||
requires-python = ">=3.7" | ||
requires-python = ">=3.8" | ||
license = {file = "LICENSE"} | ||
authors = [ | ||
{name = "Drew Bollinger", email = "[email protected]"}, | ||
|
@@ -12,7 +12,6 @@ classifiers = [ | |
"Intended Audience :: Information Technology", | ||
"Intended Audience :: Science/Research", | ||
"License :: OSI Approved :: MIT License", | ||
"Programming Language :: Python :: 3.7", | ||
"Programming Language :: Python :: 3.8", | ||
"Programming Language :: Python :: 3.9", | ||
"Programming Language :: Python :: 3.10", | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's deprecate python 3.7: