-
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
Pablo Panero
committed
Oct 27, 2020
1 parent
ae015ca
commit ccaafd7
Showing
10 changed files
with
388 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,20 @@ | ||
# -*- 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 GeoJSONSchema | ||
from .multipoint import MultiPointSchema | ||
from .point import PointSchema | ||
from .polygon import PolygonSchema | ||
|
||
__all__ = ( | ||
'GeoJSONSchema', | ||
'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,41 @@ | ||
# -*- 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 marshmallow import validate | ||
from marshmallow_oneofschema import OneOfSchema | ||
|
||
from ..fields import SanitizedUnicode | ||
from .multipoint import MultiPointSchema | ||
from .point import PointSchema | ||
from .polygon import PolygonSchema | ||
|
||
|
||
class GeoJSONSchema(OneOfSchema): | ||
"""GeoJSON schema. | ||
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 dictoionary. | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# -*- 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 MultiPoint Schema.""" | ||
|
||
from geojson import MultiPoint | ||
from marshmallow import Schema, validates | ||
from marshmallow.exceptions import ValidationError | ||
from marshmallow.fields import Constant, Float, List | ||
|
||
|
||
class MultiPointSchema(Schema): | ||
"""GeoJSON MultiPoint schema.""" | ||
|
||
coordinates = List(List(Float), required=True) | ||
type = Constant('multipoint') | ||
|
||
@validates("coordinates") | ||
def validate_data(self, data, **kwargs): | ||
"""Validate point. | ||
From the `coordinates`, the first two elements are longitude | ||
and latitude, or easting and northing, precisely in that order and | ||
using decimal numbers. Altitude or elevation MAY be included as an | ||
optional third element. | ||
""" | ||
if not data: | ||
raise ValidationError({"geojson": "coordinates must be present."}) | ||
|
||
multipoint = MultiPoint(data) | ||
if not multipoint.is_valid: | ||
errors = multipoint.errors() | ||
raise ValidationError( | ||
{"geojson": {"coordinates": errors}}) |
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,37 @@ | ||
# -*- 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 Point Schema.""" | ||
|
||
from geojson import Point | ||
from marshmallow import Schema, validates | ||
from marshmallow.exceptions import ValidationError | ||
from marshmallow.fields import Constant, Float, List | ||
|
||
|
||
class PointSchema(Schema): | ||
"""GeoJSON Point schema.""" | ||
|
||
coordinates = List(Float, required=True) | ||
type = Constant('point') | ||
|
||
@validates("coordinates") | ||
def validate_coordinates(self, data, **kwargs): | ||
"""Validate point. | ||
From the `coordinates`, the first two elements are longitude | ||
and latitude, or easting and northing, precisely in that order and | ||
using decimal numbers. Altitude or elevation MAY be included as an | ||
optional third element. | ||
""" | ||
if not data: | ||
raise ValidationError({"geojson": "coordinates must be present."}) | ||
point = Point(data) | ||
if not point.is_valid: | ||
errors = point.errors() | ||
raise ValidationError( | ||
{"geojson": {"coordinates": errors}}) |
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,40 @@ | ||
# -*- 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 Polygon Schema.""" | ||
|
||
from geojson import Polygon | ||
from marshmallow import Schema, validates | ||
from marshmallow.exceptions import ValidationError | ||
from marshmallow.fields import Constant, Float, List | ||
|
||
|
||
class PolygonSchema(Schema): | ||
"""GeoJSON Point schema.""" | ||
|
||
coordinates = List(List(List(Float, required=True))) | ||
type = Constant('polygon') | ||
|
||
@validates("coordinates") | ||
def validate_coordinates(self, data, **kwargs): | ||
"""Validate point. | ||
For type "Polygon", the "coordinates" member MUST be an array of | ||
linear ring coordinate arrays. | ||
For Polygons with more than one of these rings, the first MUST be | ||
the exterior ring, and any others MUST be interior rings. The | ||
exterior ring bounds the surface, and the interior rings (if | ||
present) bound holes within the surface. | ||
""" | ||
if not data: | ||
raise ValidationError({"geojson": "coordinates must be present."}) | ||
polygon = Polygon(data) | ||
if not polygon.is_valid: | ||
errors = polygon.errors() | ||
raise ValidationError( | ||
{"geojson": {"coordinates": errors}}) |
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,90 @@ | ||
# -*- 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 Schema, ValidationError | ||
|
||
from marshmallow_utils.schemas import GeoJSONSchema | ||
|
||
|
||
def test_point(): | ||
valid_full = { | ||
"type": "point", | ||
"coordinates": [-32.94682, -60.63932] | ||
} | ||
|
||
point = GeoJSONSchema().load(valid_full) | ||
assert valid_full == point | ||
|
||
|
||
def test_point_fail(): | ||
invalid_input = { | ||
"type": "point", | ||
"coordinates": [-32.94682] | ||
} | ||
pytest.raises(ValidationError, GeoJSONSchema().load, invalid_input) | ||
|
||
|
||
def test_multipoint(): | ||
valid_full = { | ||
"type": "multipoint", | ||
"coordinates": [[-32.94682, -60.63932], [-32.94682, -60.63932, 10.0]] | ||
} | ||
|
||
multipoint = GeoJSONSchema().load(valid_full) | ||
assert valid_full == multipoint | ||
|
||
|
||
def test_multipoint_fail(): | ||
invalid_input = { | ||
"type": "multipoint", | ||
"coordinates": [-32.94682, -60.63932] | ||
} | ||
pytest.raises(ValidationError, GeoJSONSchema().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] | ||
]] | ||
} | ||
|
||
point = GeoJSONSchema().load(valid_full) | ||
assert valid_full == point | ||
|
||
|
||
def test_polygon_fail(): | ||
invalid_input = { | ||
"type": "polygon", | ||
"coordinates": [[[2.38, 57.322], [23.194, -20.28], [2.38, 57.322]]] | ||
} | ||
pytest.raises(ValidationError, GeoJSONSchema().load, invalid_input) | ||
|
||
|
||
@pytest.mark.parametrize("geotype", [("point"), ("multipoint")]) | ||
def test_point_no_coordinates_fail(geotype): | ||
invalid_point = {"type": geotype} | ||
|
||
pytest.raises(ValidationError, GeoJSONSchema().load, invalid_point) | ||
|
||
|
||
def test_not_supported_type_fail(): | ||
invalid_type = {"type": "invalid"} | ||
|
||
pytest.raises(ValidationError, GeoJSONSchema().load, invalid_type) | ||
|
||
|
||
def test_no_type_fail(): | ||
invalid_no_type = { | ||
"coordinates": [-32.94682, -60.63932] | ||
} | ||
|
||
pytest.raises(ValidationError, GeoJSONSchema().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,39 @@ | ||
# -*- 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 Schema, 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 | ||
} | ||
assert valid_full == MultiPointSchema().load(valid_full) | ||
|
||
|
||
@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,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 Point schema.""" | ||
|
||
import pytest | ||
from marshmallow import Schema, 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 | ||
} | ||
assert valid_full == PointSchema().load(valid_full) | ||
|
||
|
||
# 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) |
Oops, something went wrong.