This repository has been archived by the owner on Aug 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to bound coordinates for world positions (refs #30)
- Loading branch information
Showing
2 changed files
with
24 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,9 +7,11 @@ | |
Int, | ||
Float, | ||
OneOf, | ||
Range, | ||
Length, | ||
Regex, | ||
null, | ||
Invalid, | ||
) | ||
|
||
|
||
|
@@ -140,22 +142,33 @@ def validation(cls, **kwargs): | |
|
||
class PointNode(SchemaNode): | ||
"""Represents a position (x, y, z, ...)""" | ||
coordinate = True | ||
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
def __init__(self, *args, **kwargs): | ||
defaults = dict(validator=Length(min=2)) | ||
defaults.update(**kwargs) | ||
super(PointNode, self).__init__(Sequence(), SchemaNode(Float()), **defaults) | ||
|
||
def deserialize(self, cstruct=null): | ||
deserialized = super(PointNode, self).deserialize(cstruct) | ||
if self.coordinate and not -180.0 <= deserialized[0] <= 180.0: | ||
This comment has been minimized.
Sorry, something went wrong.
almet
Member
|
||
raise Invalid(self, "Invalid longitude", cstruct) | ||
if self.coordinate and not -90.0 <= deserialized[1] <= 90.0: | ||
This comment has been minimized.
Sorry, something went wrong.
almet
Member
|
||
raise Invalid(self, "Invalid latitude", cstruct) | ||
return deserialized | ||
|
||
|
||
class GeometryField(TypeField): | ||
"""Base field for geometry values: basically a list of PointNode.""" | ||
node = Sequence | ||
subnode = PointNode | ||
dimension = None | ||
|
||
@classmethod | ||
def validation(cls, **kwargs): | ||
kwargs['validator'] = Length(min=cls.dimension + 1) | ||
validation = super(GeometryField, cls).validation(**kwargs) | ||
validation.children = [PointNode()] | ||
validation.add(cls.subnode()) | ||
return validation | ||
|
||
|
||
|
@@ -201,3 +214,4 @@ def __init__(self, definition): | |
for field in definition['fields']: | ||
fieldtype = field.pop('type') | ||
self.add(registry.validation(fieldtype, **field)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
maybe should be
is_coordinate
?