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)
  >>> json  # doctest: +ELLIPSIS
  '{"geometry": {"type": "Point", "coordinates": [53..., -4...]}, "type": "Feature", "properties": {"title": "Dict 1"}, "id": "1"}'

  Decoding

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


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

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

  It satisfies the feature protocol

  >>> feature.id
  '1'
  >>> feature.properties['title']
  u'Feature 1'
  >>> feature.properties['summary']
  u'The first feature'
  >>> feature.properties['link']
  'http://example.org/features/1'
  >>> feature.geometry  # doctest: +ELLIPSIS
  {'type': 'Point', 'coordinates': [53..., -4...]}

  Encoding

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

  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)
  >>> type(feature)
  <class 'geojson.examples.SimpleWebFeature'>
  >>> feature.id
  '1'
  >>> feature.properties['title']
  u'Feature 1'
  >>> feature.properties['summary']
  u'The first feature'
  >>> feature.properties['link']
  'http://example.org/features/1'
  >>> feature.geometry  # doctest: +ELLIPSIS
  {'type': 'Point', 'coordinates': [53..., -4...]}

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)
  >>> ob.__geo_interface__['geometry']  # doctest: +ELLIPSIS
  {'type': 'Point', 'coordinates': (-106..., 40...)}
  >>> geojson.dumps(ob)  # doctest: +ELLIPSIS
  '{"geometry": {"type": "Point", "coordinates": [-106..., 40...]}, "id": "1", "properties": {"title": "thingy one"}}'
