Skip to main content

Marshmallow schema validation for GeoJson

Project description

GitHub Actions status for master branch Latest PyPI package version Codecov Downloads

marshmallow_geojson 🌍

GeoJSON Objects

Status

Point

MultiPoint

LineString

MultiLineString

Polygon

MultiPolygon

GeometryCollection

Feature

FeatureCollection

Bbox

Installation

marshmallow_geojson is compatible with Python 3.7 and 3.8. The recommended way to install is via poetry:

poetry add marshmallow_geojson

Using pip to install is also possible.

pip install marshmallow_geojson

GEOJSON

GeoJSON is a format for encoding a variety of geographic data structures.

{
  "type": "Feature",
  "geometry": {
    "type": "Point",
    "coordinates": [125.6, 10.1]
  },
  "properties": {
    "name": "Dinagat Islands"
  }
}

GeoJSON supports the following geometry types: Point, LineString, Polygon, MultiPoint, MultiLineString, and MultiPolygon. Geometric objects with additional properties are Feature objects. Sets of features are contained by FeatureCollection objects.

Examples of using

Custom properties:

from marshmallow.fields import Str, Nested
from marshmallow_geojson import GeoJSONSchema, PropertiesSchema, FeatureSchema

class MyPropertiesSchema(PropertiesSchema):
    name = Str(required=True)

class MyFeatureSchema(FeatureSchema):
    properties = Nested(
        MyPropertiesSchema,
        required=True,
    )

class MyGeoJSONSchema(GeoJSONSchema):
    feature_schema = MyFeatureSchema
>>> geojson_text = '{"type": "Feature", "geometry": {"type": "Polygon", "coordinates": [[[-80.724878, 35.265454], [-80.722646, 35.260338]]]}, "properties": {"name": "foo name"}}'
>>> geojson_schema = MyGeoJSONSchema()
>>> geojson_schema.loads(geojson_text)
{'geometry': {'coordinates': [[(-80.724878, 35.265454), (-80.722646, 35.260338)]], 'type': 'Polygon'}, 'properties': {'name': 'foo name'}, 'type': 'Feature'}

Point

Simple example data:

>>> from marshmallow_geojson import GeoJSONSchema
>>> geojson_text = '{"type": "Point","coordinates": [-105.01621, 39.57422]}'
>>> geojson_many_text = '[{"type": "Point","coordinates": [-105.01621, 39.57422]}]'
>>> geojson_dc = {"type": "Point","coordinates": [-105.01621, 39.57422]}

>>> geojson_schema = GeoJSONSchema()
>>> geojson_schema.loads(geojson_text)
{'type': 'Point', 'coordinates': (-105.01621, 39.57422)}

>>> geojson_schema.load(geojson_dc)
{'type': 'Point', 'coordinates': (-105.01621, 39.57422)}

>>> geojson_schema.dumps(geojson_dc)
{'type': 'Point', 'coordinates': (-105.01621, 39.57422)}

>>> geojson_schema.dump(geojson_dc)
{'type': 'Point', 'coordinates': (-105.01621, 39.57422)}

>>> geojson_schema = GeoJSONSchema(many=True)
>>> geojson_schema.loads(geojson_many_text)
[{'type': 'Point', 'coordinates': (-105.01621, 39.57422)}]

MultiPoint

Simple example data:

>>> from marshmallow_geojson import GeoJSONSchema
>>> geojson_text = '{"type": "MultiPoint", "coordinates": [ [-105.01621, 39.57422], [-80.666513, 35.053994] ]}'
>>> geojson_schema = GeoJSONSchema()
>>> geojson_schema.loads(geojson_text)
{'type': 'MultiPoint', 'coordinates': [(-105.01621, 39.57422), (-80.666513, 35.053994)]}

LineString

Simple example data:

>>> from marshmallow_geojson import GeoJSONSchema
>>> geojson_text = '{"type": "LineString", "coordinates": [ [-99.113159, 38.869651], [-99.0802, 38.85682], [-98.822021, 38.85682], [-98.448486, 38.848264] ]}'
>>> geojson_schema = GeoJSONSchema()
>>> geojson_schema.loads(geojson_text)
{'type': 'LineString', 'coordinates': [(-99.113159, 38.869651), (-99.0802, 38.85682), (-98.822021, 38.85682), (-98.448486, 38.848264)]}

MultiLineString

Simple example data:

>>> from marshmallow_geojson import GeoJSONSchema
>>> geojson_text = '{"type": "MultiLineString", "coordinates": [ [ [-105.019898, 39.574997], [-105.019598, 39.574898], [-105.019061, 39.574782] ], [ [-105.017173, 39.574402], [-105.01698, 39.574385], [-105.016636, 39.574385], [-105.016508, 39.574402], [-105.01595, 39.57427] ], [ [-105.014276, 39.573972], [-105.014126, 39.574038], [-105.013825, 39.57417], [-105.01331, 39.574452] ] ]}'
>>> geojson_schema = GeoJSONSchema()
>>> geojson_schema.loads(geojson_text)
{'type': 'MultiLineString', 'coordinates': [[(-105.019898, 39.574997), (-105.019598, 39.574898), (-105.019061, 39.574782)], [(-105.017173, 39.574402), (-105.01698, 39.574385), (-105.016636, 39.574385), (-105.016508, 39.574402), (-105.01595, 39.57427)], [(-105.014276, 39.573972), (-105.014126, 39.574038), (-105.013825, 39.57417), (-105.01331, 39.574452)]]}

Polygon

Simple example data:

>>> from marshmallow_geojson import GeoJSONSchema
>>> geojson_text = '{"type": "Polygon", "coordinates": [ [ [100, 0], [101, 0], [101, 1], [100, 1], [100, 0] ] ]}'
>>> geojson_schema = GeoJSONSchema()
>>> geojson_schema.loads(geojson_text)
{'type': 'Polygon', 'coordinates': [[(100.0, 0.0), (101.0, 0.0), (101.0, 1.0), (100.0, 1.0), (100.0, 0.0)]]}

MultiPolygon

Simple example data:

>>> from marshmallow_geojson import GeoJSONSchema
>>> geojson_text = '{"type": "MultiPolygon", "coordinates": [ [ [ [107, 7], [108, 7], [108, 8], [107, 8], [107, 7] ] ], [ [ [100, 0], [101, 0], [101, 1], [100, 1], [100, 0] ] ] ]}'
>>> geojson_schema = GeoJSONSchema()
>>> geojson_schema.loads(geojson_text)
{'type': 'MultiPolygon', 'coordinates': [[[(107.0, 7.0), (108.0, 7.0), (108.0, 8.0), (107.0, 8.0), (107.0, 7.0)]], [[(100.0, 0.0), (101.0, 0.0), (101.0, 1.0), (100.0, 1.0), (100.0, 0.0)]]]}

GeometryCollection

Simple example data:

>>> from marshmallow_geojson import GeoJSONSchema
>>> geojson_text = '{"type": "GeometryCollection", "geometries": [{"type": "Point", "coordinates": [-80.660805, 35.049392]}, {"type": "Polygon", "coordinates": [ [ [-80.664582, 35.044965], [-80.663874, 35.04428], [-80.662586, 35.04558], [-80.663444, 35.046036], [-80.664582, 35.044965] ] ]}, {"type": "LineString", "coordinates": [[-80.662372, 35.059509], [-80.662693, 35.059263], [-80.662844, 35.05893] ]}]}'
>>> geojson_schema = GeoJSONSchema()
>>> geojson_schema.loads(geojson_text)
{'type': 'GeometryCollection', 'geometries': [{'type': 'Point', 'coordinates': (-80.660805, 35.049392)}, {'type': 'Polygon', 'coordinates': [[(-80.664582, 35.044965), (-80.663874, 35.04428), (-80.662586, 35.04558), (-80.663444, 35.046036), (-80.664582, 35.044965)]]}, {'type': 'LineString', 'coordinates': [(-80.662372, 35.059509), (-80.662693, 35.059263), (-80.662844, 35.05893)]}]}

Feature

Simple example data:

>>> from marshmallow_geojson import GeoJSONSchema
>>> geojson_text = '{"type": "Feature", "geometry": { "type": "Polygon", "coordinates": [ [ [-80.724878, 35.265454], [-80.722646, 35.260338], [-80.720329, 35.260618], [-80.71681, 35.255361], [-80.704793, 35.268397], [-82.715179, 35.267696], [-80.721359, 35.267276], [-80.724878, 35.265454] ] ] }, "properties": {} }'
>>> geojson_schema = GeoJSONSchema()
>>> geojson_schema.loads(geojson_text)
{'type': 'Feature', 'properties': {}, 'geometry': {'type': 'Polygon', 'coordinates': [[(-80.724878, 35.265454), (-80.722646, 35.260338), (-80.720329, 35.260618), (-80.71681, 35.255361), (-80.704793, 35.268397), (-82.715179, 35.267696), (-80.721359, 35.267276), (-80.724878, 35.265454)]]}}

FeatureCollection

Simple example data:

>>> from marshmallow_geojson import GeoJSONSchema
>>> geojson_text = '{"type": "FeatureCollection", "features": [{"type": "Feature", "geometry": {"type": "Point", "coordinates": [-80.870885, 35.215151] }, "properties": {} }, {"type": "Feature", "geometry": {"type": "Polygon", "coordinates": [ [ [-80.724878, 35.265454], [-80.722646, 35.260338], [-80.720329, 35.260618], [-80.704793, 35.268397], [-80.724878, 35.265454] ] ]}, "properties": {}} ] }'
>>> geojson_schema = GeoJSONSchema()
>>> geojson_schema.loads(geojson_text)
{'type': 'FeatureCollection', 'features': [{'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': (-80.870885, 35.215151)}, 'properties': {}}, {'type': 'Feature', 'geometry': {'type': 'Polygon', 'coordinates': [[(-80.724878, 35.265454), (-80.722646, 35.260338), (-80.720329, 35.260618), (-80.704793, 35.268397), (-80.724878, 35.265454)]]}, 'properties': {}}]}

Bbox

Set bbox:

from marshmallow.fields import List, Number
from marshmallow_geojson import PolygonSchema
from marshmallow_geojson.validate import Bbox

  class MyBboxSchema(PolygonSchema):
      bbox = List(
          Number(
              required=True,
          ),
          required=True,
          validate=Bbox(
              min_lon=-180,
              max_lon=180,
              min_lat=-90,
              max_lat=90,
          ),
      )
>>> geojson_text = '{"type": "Polygon", "bbox": [10, 10, 10, 10] , "coordinates": [[[100, 0], [101, 0], [101, 1], [100, 1], [100, 0]]]}'
>>> my_bbox_schema = MyBboxSchema()
>>> my_bbox_schema.loads(geojson_text)
{'type': 'Polygon', 'bbox': [10, 10, 10, 10] , 'coordinates': [[[100, 0], [101, 0], [101, 1], [100, 1], [100, 0]]]}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

marshmallow_geojson-0.5.0.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

marshmallow_geojson-0.5.0-py3-none-any.whl (15.2 kB view details)

Uploaded Python 3

File details

Details for the file marshmallow_geojson-0.5.0.tar.gz.

File metadata

  • Download URL: marshmallow_geojson-0.5.0.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.5 Darwin/21.6.0

File hashes

Hashes for marshmallow_geojson-0.5.0.tar.gz
Algorithm Hash digest
SHA256 c5f7a0e79ef368ab81633a3fe5a954f74ad4853010cbd9a817a95d6740c29ad3
MD5 c4282498f0a63801f6b5f8f7c8c19601
BLAKE2b-256 d4ef30312c2b525b016a676a4458f1a845d89329764e36d5b62c29082f54f792

See more details on using hashes here.

File details

Details for the file marshmallow_geojson-0.5.0-py3-none-any.whl.

File metadata

File hashes

Hashes for marshmallow_geojson-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a028b837809731afb27c1180a18692225c2a677a5eb362357ae28759a9eaa1da
MD5 4e537193115dc0429d70a250e1f34e52
BLAKE2b-256 1613235a7a4261f23afc060080e3af404bdddfdb44c5a388d1cb0fa179f59542

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page