Tests of the feature protocol
=============================

A dictionary can satisfy the protocol
-------------------------------------

  >>> f = {
  ...   'type': 'Feature',
  ...   'id': '1',
  ...   'geometry': {'type': 'Point', 'coordinates': [53.04781795911469, -4.10888671875]},
  ...   'properties': {'title': 'Dict 1'},
  ... }

  >>> import geojson

  Encoding

  >>> json = geojson.dumps(f, sort_keys=True)
  >>> json  # doctest: +ELLIPSIS
  '{"geometry": {"coordinates": [53..., -4...], "type": "Point"}, "id": "1", "properties": {"title": "Dict 1"}, "type": "Feature"}'

  Decoding

  >>> o = geojson.loads(json)
  >>> geojson.dumps(o, sort_keys=True)  # doctest: +ELLIPSIS
  '{"geometry": {"coordinates": [53..., -4...], "type": "Point"}, "id": "1", "properties": {"title": "Dict 1"}, "type": "Feature"}'


feature class
---------------------

  >>> from geojson.examples import SimpleWebFeature
  >>> feature = SimpleWebFeature(id='1',
  ...             geometry={'type': 'Point', 'coordinates': [53.04781795911469, -4.10888671875]},
  ...             title='Feature 1', summary='The first feature',
  ...             link='http://example.org/features/1')

  It satisfies the feature protocol

  >>> feature.id
  '1'
  >>> print(feature.properties['title'])
  Feature 1
  >>> print(feature.properties['summary'])
  The first feature
  >>> feature.properties['link']
  'http://example.org/features/1'
  >>> geojson.dumps(feature.geometry, sort_keys=True)  # doctest: +ELLIPSIS
  '{"coordinates": [53..., -4...], "type": "Point"}'

  Encoding

  >>> geojson.dumps(feature, sort_keys=True)  # doctest: +ELLIPSIS
  '{"geometry": {"coordinates": [53..., -4...], "type": "Point"}, "id": "1", "properties": {"link": "http://example.org/features/1", "summary": "The first feature", "title": "Feature 1"}, "type": "Feature"}'

  Decoding

  >>> factory = geojson.examples.createSimpleWebFeature 
  >>> json = '{"geometry": {"type": "Point", "coordinates": [53.04781795911469, -4.10888671875]}, "id": "1", "properties": {"summary": "The first feature", "link": "http://example.org/features/1", "title": "Feature 1"}}'
  >>> feature = geojson.loads(json, object_hook=factory, encoding="utf-8")
  >>> type(feature)
  <class 'geojson.examples.SimpleWebFeature'>
  >>> feature.id
  '1'
  >>> print(feature.properties['title'])
  Feature 1
  >>> print(feature.properties['summary'])
  The first feature
  >>> feature.properties['link']
  'http://example.org/features/1'
  >>> geojson.dumps(feature.geometry, sort_keys=True)  # doctest: +ELLIPSIS
  '{"coordinates": [53..., -4...], "type": "Point"}'

Test the geo interface
----------------------

  >>> class Thingy(object):
  ...     def __init__(self, id, title, x, y):
  ...         self.id = id
  ...         self.title = title
  ...         self.x = x
  ...         self.y = y
  ...     @property
  ...     def __geo_interface__(self):
  ...        return {"id": self.id, "properties": {"title": self.title}, "geometry": {"type": "Point", "coordinates": (self.x, self.y)}}

  >>> ob = Thingy('1', 'thingy one', -106.0, 40.0)
  >>> geojson.dumps(ob.__geo_interface__['geometry'], sort_keys=True)  # doctest: +ELLIPSIS
  '{"coordinates": [-106..., 40...], "type": "Point"}'
  >>> geojson.dumps(ob, sort_keys=True)  # doctest: +ELLIPSIS
  '{"geometry": {"coordinates": [-106..., 40...], "type": "Point"}, "id": "1", "properties": {"title": "thingy one"}}'
