root/geojson/trunk/tests/geometry.txt

Revision 1063 (checked in by seang, 8 months ago)

Get in step with GeoJSON draft version 6: implement named and linked CRS, do not serialize the default crs

  • Property svn:eol-style set to native
Line 
1
2 Tests of the geometry protocol
3 ==============================
4
5 A dictionary can satisfy the protocol
6 -------------------------------------
7
8   >>> g = {
9   ...    "type": "Point",
10   ...    "coordinates": [1.3, -54.23242]
11   ... }
12
13   >>> import geojson
14   >>> import geojson.geometry
15
16   Encoding
17
18   >>> json = geojson.dumps(g)
19   >>> json # doctest: +ELLIPSIS
20   '{"type": "Point", "coordinates": [1..., -54...]}'
21
22   Decoding
23
24   >>> o = geojson.loads(json)
25   >>> o # doctest: +ELLIPSIS
26   {u'type': u'Point', u'coordinates': [1..., -54...]}
27
28
29 geometry class
30 ---------------------
31
32   >>> ls = geojson.geometry.LineString(((52.1, -34.131), (65.231, -34.234)))
33
34   >>> ls.__geo_interface__ # doctest: +ELLIPSIS
35   {'type': 'LineString', 'coordinates': ((52.100000000000001, -34.131), (65..., -34...))}
36
37   >>> ls.type
38   'LineString'
39   >>> ls.coordinates # doctest: +ELLIPSIS
40   ((52..., -34...), (65..., -34...))
41
42   Encoding
43
44   >>> json = geojson.dumps(ls)
45   >>> json # doctest: +ELLIPSIS
46   '{"type": "LineString", "coordinates": [[52..., -34...], [65..., -34...]]}'
47
48   Decoding
49   >>> geojson.loads(json) # doctest: +ELLIPSIS
50   {u'type': u'LineString', u'coordinates': [[52..., -34...], [65..., -34...]]}
51
52   >>> factory = lambda o: geojson.GeoJSON.to_instance(o, geojson.geometry)
53   >>> geom = geojson.loads(json, object_hook=factory)
54   >>> type(geom)
55   <class 'geojson.geometry.LineString'>
56   >>> geom.type
57   'LineString'
58   >>> geom.coordinates # doctest: +ELLIPSIS
59   [[52..., -34...], [65..., -34...]]
60
61
62   Test custom crs
63  
64   >>> from geojson.crs import Named
65
66   >>> coords = ((-1918145.0108183471, -4098018.9166399641), (-680004.67204747663, -3864394.3196185972))
67
68   >>> ls = geojson.geometry.LineString(coords, crs=Named(properties=dict(name='EPSG:4326')))
69
70   >>> ls.__geo_interface__ # doctest: +ELLIPSIS
71   {'crs': {'type': 'name', 'properties': {'name': 'EPSG:4326'}}, 'type': 'LineString', 'coordinates': ((-1918145..., -4098018...), (-680004..., -3864394...))}
72
73   It satisfies the geometry protocol
74
75   >>> json = geojson.dumps(ls)
76   >>> json # doctest: +ELLIPSIS
77   '{"crs": {"type": "name", "properties": {"name": "EPSG:4326"}}, "type": "LineString", "coordinates": [[-1918145..., -4098018...], [-680004..., -3864394...]]}'
78
79   Decoding
80   >>> geojson.loads(json) # doctest: +ELLIPSIS
81   {u'crs': {u'type': u'name', u'properties': {u'name': u'EPSG:4326'}}, u'type': u'LineString', u'coordinates': [[-1918145..., -4098018...], [-680004..., -3864394...]]}
82  
83
84   >>> factory = lambda o: geojson.GeoJSON.to_instance(o, geojson.geometry)
85   >>> geom = geojson.loads(json, object_hook=factory)
86   >>> type(geom)
87   <class 'geojson.geometry.LineString'>
88   >>> geom.type
89   'LineString'
90   >>> geom.coordinates # doctest: +ELLIPSIS
91   [[-1918145..., -4098018...], [-680004..., -3864394...]]
92  
93 Test the geo interface
94 ----------------------
95
96   >>> class PointThingy(object):
97   ...     def __init__(self, x, y):
98   ...         self.x = x
99   ...         self.y = y
100   ...     @property
101   ...     def __geo_interface__(self):
102   ...        return {"type": "Point", "coordinates": (self.x, self.y)}
103
104   >>> ob = PointThingy(-106.0, 40.0)
105   >>> ob.__geo_interface__['coordinates']  # doctest: +ELLIPSIS
106   (-106..., 40...)
107   >>> geojson.dumps(ob)  # doctest: +ELLIPSIS
108   '{"type": "Point", "coordinates": [-106..., 40...]}'
Note: See TracBrowser for help on using the browser.