
Tests of the Feature collections protocol
-----------------------------------------

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

  >>> fc = {"type": "FeatureCollection",
  ...       "features": [{"type": "Feature",
  ...                      "id": "1",
  ...                      "geometry": {"type": "Point",
  ...                                   "coordinates": [44.556, 67.192]}}]}


  Encoding

  >>> import geojson
  >>> json = geojson.dumps(fc, sort_keys=True)
  >>> json  # doctest: +ELLIPSIS
  '{"features": [{"geometry": {"coordinates": [44..., 67...], "type": "Point"}, "id": "1", "type": "Feature"}], "type": "FeatureCollection"}'

  Decoding
  
  >>> fcb = geojson.loads(json)
  >>> geojson.dumps(fcb, sort_keys=True)  # doctest: +ELLIPSIS
  '{"features": [{"geometry": {"coordinates": [44..., 67...], "type": "Point"}, "id": "1", "properties": {}, "type": "Feature"}], "type": "FeatureCollection"}'



GeoJSON types thmeselves satisfy the protocol (of course!)
-----------------------------------------------------------
  
  >>> features = [geojson.Feature(id=1, geometry=geojson.Point(coordinates=(53.04781795911469, -4.10888671875)))]
  >>> fco = geojson.FeatureCollection(features)
  >>> fco.features  # doctest: +ELLIPSIS
  [{"geometry": {"coordinates": [53..., -4...], "type": "Point"}, "id": 1, "properties": {}, "type": "Feature"}]


  Encoding

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

   and can encode back into a instnace of the same kind, or supply your own hook:

  >>> hook = lambda ob: geojson.GeoJSON.to_instance(ob, geojson.feature)
  >>> fc = geojson.loads(json, object_hook=hook)
  >>> fc.features  # doctest: +ELLIPSIS
  [{"geometry": {"coordinates": [53..., -4...], "type": "Point"}, "id": 1, "properties": {}, "type": "Feature"}]
  
  >>> fc.features[0].geometry  # doctest: +ELLIPSIS
  {"coordinates": [53..., -4...], "type": "Point"}

  >>> len(fc["features"])
  1

  >>> len(fc.features)
  1

  Convert GeoJSON to regular dict
  >>> dfc = dict(fc)
  >>> dfc["type"] == "FeatureCollection"
  True

  >>> geometry = fc.features[0].geometry
  >>> geometry.coordinates  # doctest: +ELLIPSIS
  [53..., -4...]
 

- It may be used from any object, consider:

  >>> class Point(object):
  ...     """Mock shapely point."""
  ...     def __init__(self, x, y):
  ...         self.x = x
  ...         self.y = y
  ...     @property
  ...     def __geo_interface__(self):
  ...         return geojson.Point(coordinates=[self.x, self.y])

  >>> p = Point(53.04781795911469, -4.10888671875)
  >>> geojson.dumps(p, sort_keys=True)  # doctest: +ELLIPSIS
  '{"coordinates": [53..., -4...], "type": "Point"}'
