-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
335 additions
and
0 deletions.
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (C) 2020 CERN. | ||
# | ||
# Marshmallow-Utils is free software; you can redistribute it and/or modify | ||
# it under the terms of the MIT License; see LICENSE file for more details. | ||
|
||
"""Marshmallow schemas.""" | ||
|
||
from .geojson import GeometryObjectSchema, MultiPointSchema, PointSchema, \ | ||
PolygonSchema | ||
|
||
__all__ = ( | ||
'GeometryObjectSchema', | ||
'MultiPointSchema', | ||
'PointSchema', | ||
'PolygonSchema', | ||
) |
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 |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (C) 2020 CERN. | ||
# | ||
# Marshmallow-Utils is free software; you can redistribute it and/or modify | ||
# it under the terms of the MIT License; see LICENSE file for more details. | ||
|
||
"""GeoJSON Schema.""" | ||
|
||
from geojson import MultiPoint, Point, Polygon | ||
from marshmallow import Schema, ValidationError | ||
from marshmallow.fields import Constant, Float, List | ||
from marshmallow.validate import Validator | ||
from marshmallow_oneofschema import OneOfSchema | ||
|
||
|
||
class GeometryValidator(Validator): | ||
"""Validator for GeoJSON geometry objects.""" | ||
|
||
def __init__(self, geometry_cls): | ||
"""Initialize the validator. | ||
:param geometry_cls: The GeoJSON geometry class to validate against. | ||
""" | ||
self.geometry_cls = geometry_cls | ||
|
||
def __call__(self, value): | ||
"""Validate a geometry object.""" | ||
obj = self.geometry_cls(value) | ||
if not obj.is_valid: | ||
errors = obj.errors() | ||
raise ValidationError({"geojson": {"coordinates": errors}}) | ||
return value | ||
|
||
|
||
class PointSchema(Schema): | ||
"""GeoJSON Point schema. | ||
See https://tools.ietf.org/html/rfc7946#section-3.1.2 | ||
""" | ||
|
||
coordinates = List(Float, required=True, validate=GeometryValidator(Point)) | ||
type = Constant('Point') | ||
|
||
|
||
class MultiPointSchema(Schema): | ||
"""GeoJSON MultiPoint schema. | ||
See https://tools.ietf.org/html/rfc7946#section-3.1.3 | ||
""" | ||
|
||
coordinates = List( | ||
List(Float), required=True, validate=GeometryValidator(MultiPoint)) | ||
type = Constant('MultiPoint') | ||
|
||
|
||
class PolygonSchema(Schema): | ||
"""GeoJSON Polygon schema. | ||
See https://tools.ietf.org/html/rfc7946#section-3.1.6 | ||
""" | ||
|
||
coordinates = List( | ||
List(List(Float)), required=True, validate=GeometryValidator(Polygon)) | ||
type = Constant('Polygon') | ||
|
||
|
||
class GeometryObjectSchema(OneOfSchema): | ||
"""A GeoJSON Geometry Object schema. | ||
See https://tools.ietf.org/html/rfc7946#section-3.1 | ||
Only Point, MultiPoint and Polygon are supported. | ||
""" | ||
|
||
type_schemas = { | ||
"Point": PointSchema, | ||
"MultiPoint": MultiPointSchema, | ||
"Polygon": PolygonSchema, | ||
} | ||
|
||
def get_obj_type(self, obj): | ||
"""Finds the object type in the dictionary. | ||
This function is needed because the schemas return the dict itself. | ||
Not a geojson object. | ||
""" | ||
obj_type = obj.get("type") | ||
if not obj_type or obj_type not in self.type_schemas.keys(): | ||
raise Exception(f"Unknown GeoJSON object type: {obj_type}") | ||
|
||
return obj_type |
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
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 |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (C) 2020 CERN. | ||
# | ||
# Marshmallow-Utils is free software; you can redistribute it and/or modify | ||
# it under the terms of the MIT License; see LICENSE file for more details. | ||
|
||
"""Tests for marshmallow GeoJSON schema.""" | ||
|
||
import pytest | ||
from marshmallow import ValidationError | ||
|
||
from marshmallow_utils.schemas import GeometryObjectSchema | ||
|
||
|
||
def test_point(): | ||
valid_full = { | ||
"type": "Point", | ||
"coordinates": [-32.94682, -60.63932] | ||
} | ||
|
||
point = GeometryObjectSchema().load(valid_full) | ||
assert valid_full == point == GeometryObjectSchema().dump(point) | ||
|
||
|
||
def test_point_fail(): | ||
invalid_input = { | ||
"type": "Point", | ||
"coordinates": [-32.94682] | ||
} | ||
pytest.raises(ValidationError, GeometryObjectSchema().load, invalid_input) | ||
|
||
|
||
def test_multipoint(): | ||
valid_full = { | ||
"type": "MultiPoint", | ||
"coordinates": [[-32.94682, -60.63932], [-32.94682, -60.63932, 10.0]] | ||
} | ||
|
||
multipoint = GeometryObjectSchema().load(valid_full) | ||
assert valid_full == multipoint == GeometryObjectSchema().dump(multipoint) | ||
|
||
|
||
def test_multipoint_fail(): | ||
invalid_input = { | ||
"type": "MultiPoint", | ||
"coordinates": [-32.94682, -60.63932] | ||
} | ||
pytest.raises(ValidationError, GeometryObjectSchema().load, invalid_input) | ||
|
||
|
||
def test_polygon(): | ||
valid_full = { | ||
"type": "Polygon", | ||
"coordinates": [[ | ||
[2.38, 57.322], [23.194, -20.28], [-120.43, 19.15], [2.38, 57.322] | ||
]] | ||
} | ||
|
||
polygon = GeometryObjectSchema().load(valid_full) | ||
assert valid_full == polygon == GeometryObjectSchema().dump(polygon) | ||
|
||
|
||
def test_polygon_fail(): | ||
invalid_input = { | ||
"type": "Polygon", | ||
"coordinates": [[[2.38, 57.322], [23.194, -20.28], [2.38, 57.322]]] | ||
} | ||
pytest.raises(ValidationError, GeometryObjectSchema().load, invalid_input) | ||
|
||
|
||
@pytest.mark.parametrize("geotype", [("Point"), ("MultiPoint")]) | ||
def test_point_no_coordinates_fail(geotype): | ||
invalid_point = {"type": geotype} | ||
|
||
pytest.raises(ValidationError, GeometryObjectSchema().load, invalid_point) | ||
|
||
|
||
def test_not_supported_type_fail(): | ||
invalid_type = {"type": "invalid"} | ||
|
||
pytest.raises(ValidationError, GeometryObjectSchema().load, invalid_type) | ||
|
||
|
||
def test_no_type_fail(): | ||
invalid_no_type = { | ||
"coordinates": [-32.94682, -60.63932] | ||
} | ||
|
||
pytest.raises( | ||
ValidationError, GeometryObjectSchema().load, invalid_no_type) |
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (C) 2020 CERN. | ||
# | ||
# Marshmallow-Utils is free software; you can redistribute it and/or modify | ||
# it under the terms of the MIT License; see LICENSE file for more details. | ||
|
||
"""Tests for marshmallow GeoJSON MultiPoint schema.""" | ||
|
||
import pytest | ||
from marshmallow import ValidationError | ||
|
||
from marshmallow_utils.schemas import MultiPointSchema | ||
|
||
|
||
@pytest.mark.parametrize("coordinates", [ | ||
([[-32.94682, -60.63932], [-32.94682, -60.63932, 10.0]]), | ||
([[-32.99, -60.63], [-32.94, -60.63], [-32.92, -60.32, 10.0]]), | ||
]) | ||
def test_multipoint(coordinates): | ||
valid_full = { | ||
"type": "MultiPoint", | ||
"coordinates": coordinates | ||
} | ||
loaded = MultiPointSchema().load(valid_full) | ||
# NOTE: Since the schemas return the dict itself, the loaded object | ||
# is the same than the input and dumped objects (dicts) | ||
assert valid_full == loaded == MultiPointSchema().dump(loaded) | ||
|
||
|
||
@pytest.mark.parametrize("coordinates", [ | ||
("-32.94682,-60.63932, -32.94682, -60.63932, 10.0"), | ||
(["-32.94682,-60.63932", "-32.94682, -60.63932, 10.0"]), | ||
([-32.94682, -60.63932, 10.0, 10.0]), | ||
([[-32.99, -60.63], [-32.94], [-32.92, -60.32, 10.0]]) | ||
]) | ||
def test_multipoint_fail(coordinates): | ||
invalid_point = { | ||
"type": "MultiPoint", | ||
"coordinates": coordinates | ||
} | ||
pytest.raises(ValidationError, MultiPointSchema().load, invalid_point) |
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (C) 2020 CERN. | ||
# | ||
# Marshmallow-Utils is free software; you can redistribute it and/or modify | ||
# it under the terms of the MIT License; see LICENSE file for more details. | ||
|
||
"""Tests for marshmallow GeoJSON Point schema.""" | ||
|
||
import pytest | ||
from marshmallow import ValidationError | ||
|
||
from marshmallow_utils.schemas import PointSchema | ||
|
||
|
||
@pytest.mark.parametrize("coordinates", [ | ||
([-32.94682, -60.63932]), | ||
([-32.94682, -60.63932, 10.0]), | ||
]) | ||
def test_point(coordinates): | ||
valid_full = { | ||
"type": "Point", | ||
"coordinates": coordinates | ||
} | ||
|
||
loaded = PointSchema().load(valid_full) | ||
# NOTE: Since the schemas return the dict itself, the loaded object | ||
# is the same than the input and dumped objects (dicts) | ||
assert valid_full == loaded == PointSchema().dump(loaded) | ||
|
||
|
||
# NOTE: ["-32.94682", "-60.63932"] is valid because the Float field | ||
# deserializes properly the string into float, so it becomes | ||
# [-32.94682, -60.63932] | ||
@pytest.mark.parametrize("coordinates", [ | ||
("-32.94682,-60.63932"), | ||
(["-32.94682,-60.63932"]), | ||
([-32.94682, -60.63932, 10.0, 10.0]), | ||
([-32.94682]) | ||
]) | ||
def test_point_fail(coordinates): | ||
invalid_point = { | ||
"type": "Point", | ||
"coordinates": coordinates | ||
} | ||
pytest.raises(ValidationError, PointSchema().load, invalid_point) |
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (C) 2020 CERN. | ||
# | ||
# Marshmallow-Utils is free software; you can redistribute it and/or modify | ||
# it under the terms of the MIT License; see LICENSE file for more details. | ||
|
||
"""Tests for marshmallow GeoJSON Polygon schema.""" | ||
|
||
import pytest | ||
from marshmallow import ValidationError | ||
|
||
from marshmallow_utils.schemas import PolygonSchema | ||
|
||
|
||
@pytest.mark.parametrize("coordinates", [ | ||
([[[2.38, 57.322], [23.194, -20.28], [-120.43, 19.15], [2.38, 57.322]]]), | ||
]) | ||
def test_polygon(coordinates): | ||
print(coordinates) | ||
valid_full = { | ||
"type": "Polygon", | ||
"coordinates": coordinates | ||
} | ||
|
||
loaded = PolygonSchema().load(valid_full) | ||
# NOTE: Since the schemas return the dict itself, the loaded object | ||
# is the same than the input and dumped objects (dicts) | ||
assert valid_full == loaded == PolygonSchema().dump(loaded) | ||
|
||
|
||
@pytest.mark.parametrize("coordinates", [ | ||
([2.38, 57.322, -120.43, 19.15]), | ||
([[2.38, 57.322], [-120.43, 19.15]]), | ||
([[[2.38, 57.322], [23.194, -20.28], [24.194, -19.2]]]), | ||
([[[2.38, 57.322], [2.38, 57.322]]]), | ||
([[[2.38, 57.322], [23.194, -20.28], [2.38, 57.322]]]), | ||
]) | ||
def test_polygon_fail(coordinates): | ||
invalid_point = { | ||
"type": "Polygon", | ||
"coordinates": coordinates | ||
} | ||
pytest.raises(ValidationError, PolygonSchema().load, invalid_point) |