Skip to content
This repository has been archived by the owner on Aug 2, 2019. It is now read-only.

Commit

Permalink
Add ability to bound coordinates for world positions (refs #30)
Browse files Browse the repository at this point in the history
  • Loading branch information
leplatrem committed Dec 5, 2012
1 parent 83d45db commit 5494b2d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
16 changes: 15 additions & 1 deletion daybed/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
Int,
Float,
OneOf,
Range,
Length,
Regex,
null,
Invalid,
)


Expand Down Expand Up @@ -140,22 +142,33 @@ def validation(cls, **kwargs):

class PointNode(SchemaNode):
"""Represents a position (x, y, z, ...)"""
coordinate = True

This comment has been minimized.

Copy link
@almet

almet Dec 7, 2012

Member

maybe should be is_coordinate ?


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.

Copy link
@almet

almet Dec 7, 2012

Member

is it possible that deserialized is empty? if so, we need to take care of the possible KeyError.

This comment has been minimized.

Copy link
@leplatrem

leplatrem Dec 8, 2012

Author Contributor

super() goes through the base Length(min=2) validator.

raise Invalid(self, "Invalid longitude", cstruct)
if self.coordinate and not -90.0 <= deserialized[1] <= 90.0:

This comment has been minimized.

Copy link
@almet

almet Dec 7, 2012

Member

we can factorize the if self.coordinate here (and then check that we have at least two values in deserialized)

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


Expand Down Expand Up @@ -201,3 +214,4 @@ def __init__(self, definition):
for field in definition['fields']:
fieldtype = field.pop('type')
self.add(registry.validation(fieldtype, **field))

9 changes: 9 additions & 0 deletions daybed/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ def test_point_node(self):
self.assertRaises(colander.Invalid, schema.deserialize, '"0.4, 45.0"')
self.assertRaises(colander.Invalid, schema.deserialize, ["a", "b"])

def test_coordinate_node(self):
schema = schemas.PointNode(coordinate=False)
self.assertEquals([181.0, 91.0], schema.deserialize([181.0, 91.0]))
schema = schemas.PointNode()
self.assertRaises(colander.Invalid, schema.deserialize, [181.0, 91.0])
self.assertRaises(colander.Invalid, schema.deserialize, [-181.0, -91.0])
self.assertRaises(colander.Invalid, schema.deserialize, [120.0, -91.0])
self.assertEquals([0.4, 45.0, 181], schema.deserialize([0.4, 45.0, 181]))

def test_point(self):
schema = schemas.PointField.definition()
definition = schema.deserialize(
Expand Down

0 comments on commit 5494b2d

Please sign in to comment.