
Tests of the geometry protocol
==============================

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

  >>> g = {
  ...    "type": "Point",
  ...    "coordinates": [1.3, -54.23242]
  ... }

  >>> import geojson
  >>> import geojson.geometry

  Encoding

  >>> json = geojson.dumps(g, sort_keys=True)
  >>> json # doctest: +ELLIPSIS
  '{"coordinates": [1..., -54...], "type": "Point"}'

  Decoding

  >>> o = geojson.loads(json)
  >>> geojson.dumps(o, sort_keys=True) # doctest: +ELLIPSIS
  '{"coordinates": [1..., -54...], "type": "Point"}'


geometry class
--------------

  >>> ls = geojson.geometry.LineString(((52.1, -34.131), (65.231, -34.234)))

  >>> isinstance(ls.__geo_interface__, geojson.GeoJSON) # doctest: +ELLIPSIS
  True

  >>> geojson.dumps(ls, sort_keys=True) # doctest: +ELLIPSIS
  '{"coordinates": [[52..., -34...], [65..., -34...]], "type": "LineString"}'

  >>> ls.type
  'LineString'
  >>> ls.coordinates # doctest: +ELLIPSIS
  ((52..., -34...), (65..., -34...))

  Encoding

  >>> json = geojson.dumps(ls, sort_keys=True)
  >>> json # doctest: +ELLIPSIS
  '{"coordinates": [[52..., -34...], [65..., -34...]], "type": "LineString"}'

  Decoding
  >>> obj = geojson.loads(json) 
  >>> type(obj)
  <class 'geojson.geometry.LineString'>

  >>> geojson.dumps(obj, sort_keys=True) # doctest: +ELLIPSIS
  '{"coordinates": [[52..., -34.131], [65..., -34...]], "type": "LineString"}'

  >>> factory = lambda o: geojson.GeoJSON.to_instance(o, geojson.geometry)
  >>> geom = geojson.loads(json, object_hook=factory)
  >>> type(geom)
  <class 'geojson.geometry.LineString'>
  >>> geom.type
  'LineString'
  >>> geom.coordinates # doctest: +ELLIPSIS
  [[52..., -34...], [65..., -34...]]


  Test custom crs 
 
  >>> from geojson.crs import Named

  >>> coords = ((-1918145.0108183471, -4098018.9166399641), (-680004.67204747663, -3864394.3196185972))

  >>> ls = geojson.geometry.LineString(coords, crs=Named(properties=dict(name="EPSG:4326")))

  >>> isinstance(ls, geojson.GeoJSON) and hasattr(ls, "__geo_interface__")
  True

  >>> ls # doctest: +ELLIPSIS
  {"coordinates": [[-1918145..., -4098018...], [-680004..., -3864394...]], "crs": {"properties": {"name": "EPSG:4326"}, "type": "name"}, "type": "LineString"}

  It satisfies the geometry protocol

  >>> json = geojson.dumps(ls, sort_keys=True)
  >>> json # doctest: +ELLIPSIS
  '{"coordinates": [[-1918145..., -4098018...], [-680004..., -3864394...]], "crs": {"properties": {"name": "EPSG:4326"}, "type": "name"}, "type": "LineString"}'

  Decoding
  >>> obj = geojson.loads(json) 
  >>> type(obj)
  <class 'geojson.geometry.LineString'>

  >>> geojson.dumps(obj, sort_keys=True) # doctest: +ELLIPSIS
  '{"coordinates": [[-1918145..., -4098018...], [-680004..., -3864394...]], "crs": {"properties": {"name": "EPSG:4326"}, "type": "name"}, "type": "LineString"}'
 

  >>> factory = lambda o: geojson.GeoJSON.to_instance(o, geojson.geometry)
  >>> geom = geojson.loads(json, object_hook=factory)
  >>> type(geom)
  <class 'geojson.geometry.LineString'>
  >>> geom.type
  'LineString'
  >>> geom.coordinates # doctest: +ELLIPSIS
  [[-1918145..., -4098018...], [-680004..., -3864394...]]
 
Test the geo interface
----------------------

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

  >>> ob = PointThingy(-106.0, 40.0)
  >>> ob.__geo_interface__['coordinates']  # doctest: +ELLIPSIS
  (-106..., 40...)
  >>> geojson.dumps(ob, sort_keys=True)  # doctest: +ELLIPSIS
  '{"coordinates": [-106..., 40...], "type": "Point"}'
