root/geojson/trunk/tests/objects.txt

Revision 1504, 2.6 KB (checked in by mattrussell, 4 months ago)

Simplify code. Remove custom mapping object. add more tests for falling through to json, crs and strict json. Fixes #186

Line 
1Encoding objects with __geo_interface__
2------------------------------------------
3  >>> import geojson
4  >>> dumps = lambda **kwargs: geojson.dumps(sort_keys=True, **kwargs)
5
6  >>> class LatLon(object):
7  ...     
8  ...    def __init__(self, lat, lon):
9  ...        super(LatLon, self).__init__()
10  ...        self.lat = lat
11  ...        self.lon = lon
12  ...
13  ...    @property
14  ...    def __geo_interface__(self):
15  ...        return dict(type="Point", coordinates=(self.lon, self.lat))
16
17  >>> lat_lon = LatLon(-54.1231, 4.53242)
18 
19  Can be encoded into geojson geometry:
20
21  >>> json = geojson.dumps(lat_lon, sort_keys=True)
22  >>> json # doctest: +ELLIPSIS
23  '{"coordinates": [4..., -54...], "type": "Point"}'
24
25  Objects with a __geo_interface__ attribute or property can be nested in geojson feature:
26
27  >>> f = geojson.Feature(geometry=lat_lon)
28  >>> f # doctest: +ELLIPSIS
29  {"geometry": {"coordinates": [4..., -54...], "type": "Point"}, "id": null, "properties": {}, "type": "Feature"}
30
31
32  And feature will encode:
33  >>> json = geojson.dumps(f, sort_keys=True)
34  >>> json # doctest: +ELLIPSIS
35  '{"geometry": {"coordinates": [4..., -54...], "type": "Point"}, "id": null, "properties": {}, "type": "Feature"}'
36
37  geojson types can be used to implemented a __geo_interface__:
38
39  >>> class LatLon2(LatLon):
40  ...     @property
41  ...     def __geo_interface__(self):
42  ...             return geojson.Point((self.lon, self.lat))
43  ...
44
45
46  >>> class LatLon2(LatLon):
47  ...     @property
48  ...     def __geo_interface__(self):
49  ...         return geojson.Point((self.lon, self.lat))
50  ...     
51  ...   
52
53  >>> ll2 = LatLon2(-54.1231, 4.53242)
54  >>> json2 = geojson.dumps(ll2, sort_keys=True)
55  >>> json2 # doctest: +ELLIPSIS
56  '{"coordinates": [4..., -54...], "type": "Point"}'
57 
58  Decoding
59    - to a dict
60
61  >>> feature_dict = geojson.loads(json)
62  >>> geojson.dumps(feature_dict, sort_keys=True)  # doctest: +ELLIPSIS
63  '{"geometry": {"coordinates": [4.5324200000000001, -54.123100000000001], "type": "Point"}, "id": null, "properties": {}, "type": "Feature"}'
64
65  - or to an object, via a factory. Here we'll create GeoJSON object.
66
67  >>> ll2 = LatLon2(43.3,-154.1)
68  >>> json = geojson.dumps(ll2, sort_keys=True)
69  >>> json # doctest: +ELLIPSIS
70  '{"coordinates": [-154..., 43...], "type": "Point"}'
71 
72  >>> o = geojson.loads(json) # doctest: +ELLIPSIS
73  >>> geojson.dumps(o, sort_keys=True) # doctest: +ELLIPSIS
74  '{"coordinates": [-154..., 43...], "type": "Point"}'
75 
76  >>> factory = lambda ob: geojson.GeoJSON.to_instance(ob)
77  >>> geometry = geojson.loads(json, object_hook=factory)
78  >>> geometry   # doctest: +ELLIPSIS     
79  {"coordinates": [-154..., 43...], "type": "Point"}
80 
Note: See TracBrowser for help on using the browser.