Skip to content

Commit

Permalink
schemas: add geojson support
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablo Panero committed Oct 27, 2020
1 parent ae015ca commit ccaafd7
Show file tree
Hide file tree
Showing 10 changed files with 388 additions and 0 deletions.
20 changes: 20 additions & 0 deletions marshmallow_utils/schemas/__init__.py
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',
)
41 changes: 41 additions & 0 deletions marshmallow_utils/schemas/geojson.py
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
38 changes: 38 additions & 0 deletions marshmallow_utils/schemas/multipoint.py
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}})
37 changes: 37 additions & 0 deletions marshmallow_utils/schemas/point.py
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}})
40 changes: 40 additions & 0 deletions marshmallow_utils/schemas/polygon.py
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}})
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
'bleach>=3.0.0',
'edtf>=4.0.1,<5.0.0',
'ftfy>=4.4.3',
'geojson>=2.5.0',
'marshmallow>=3.0.0,<4.0.0',
'marshmallow-oneofschema>=2.1.0',
'pycountry>=18.12.8',
'uritemplate>=3.0.1',
'werkzeug>=1.0.0',
Expand Down
90 changes: 90 additions & 0 deletions tests/schemas/test_geojson.py
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)
39 changes: 39 additions & 0 deletions tests/schemas/test_multipoint.py
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)
42 changes: 42 additions & 0 deletions tests/schemas/test_point.py
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)
Loading

0 comments on commit ccaafd7

Please sign in to comment.