| 1 |
|
|---|
| 2 |
Tests that addional members are allowed in any level of GeoJSON |
|---|
| 3 |
=============================================================== |
|---|
| 4 |
|
|---|
| 5 |
A dictionary can satisfy the protocol |
|---|
| 6 |
------------------------------------- |
|---|
| 7 |
|
|---|
| 8 |
>>> f = { |
|---|
| 9 |
... "type": "Feature", |
|---|
| 10 |
... "geometry": { |
|---|
| 11 |
... "type": "LineString", |
|---|
| 12 |
... "coordinates": [[100.0, 0.0], [101.0, 1.0]] |
|---|
| 13 |
... }, |
|---|
| 14 |
... "properties": { |
|---|
| 15 |
... "prop0": "value0", |
|---|
| 16 |
... "prop1": "value1" |
|---|
| 17 |
... }, |
|---|
| 18 |
... "foo": "bar" |
|---|
| 19 |
... } |
|---|
| 20 |
|
|---|
| 21 |
>>> import geojson |
|---|
| 22 |
|
|---|
| 23 |
Encoding |
|---|
| 24 |
|
|---|
| 25 |
>>> json = geojson.dumps(f) |
|---|
| 26 |
|
|---|
| 27 |
>>> json |
|---|
| 28 |
'{"geometry": {"type": "LineString", "coordinates": [[100.0, 0.0], [101.0, 1.0]]}, "foo": "bar", "type": "Feature", "properties": {"prop0": "value0", "prop1": "value1"}, "id": null}' |
|---|
| 29 |
|
|---|
| 30 |
Decoding |
|---|
| 31 |
|
|---|
| 32 |
>>> o = geojson.loads(json) |
|---|
| 33 |
|
|---|
| 34 |
>>> json |
|---|
| 35 |
'{"geometry": {"type": "LineString", "coordinates": [[100.0, 0.0], [101.0, 1.0]]}, "foo": "bar", "type": "Feature", "properties": {"prop0": "value0", "prop1": "value1"}, "id": null}' |
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
Custom objects can be decoded into a json structure containing valid geojson |
|---|
| 39 |
---------------------------------------------------------------------------- |
|---|
| 40 |
|
|---|
| 41 |
>>> class Route(object): |
|---|
| 42 |
... def __init__(self, title, description, waypoints): |
|---|
| 43 |
... self.title = title |
|---|
| 44 |
... self.descrpition = description |
|---|
| 45 |
... self.waypoints = waypoints |
|---|
| 46 |
... @property |
|---|
| 47 |
... def __geo_interface__(self): |
|---|
| 48 |
... return dict(type="Feature", geometry=dict(type="LineString", coordinates=self.waypoints), |
|---|
| 49 |
... title=self.title, description=self.descrpition) |
|---|
| 50 |
... |
|---|
| 51 |
|
|---|
| 52 |
>>> r = Route("Snowdonia circular", "A nice bike ride around some mountains", ((1.0, 2.0), (2.0, 3.2))) |
|---|
| 53 |
|
|---|
| 54 |
>>> json = geojson.dumps(r) |
|---|
| 55 |
|
|---|
| 56 |
>>> json # doctest: +ELLIPSIS |
|---|
| 57 |
'{"description": "A nice bike ride around some mountains", "title": "Snowdonia circular", "geometry": {"type": "LineString", "coordinates": [[1..., 2...], [2..., 3...]]}, "type": "Feature", "properties": {}, "id": null}' |
|---|
| 58 |
|
|---|
| 59 |
>>> r = geojson.loads(json) |
|---|
| 60 |
>>> r # doctest: +ELLIPSIS |
|---|
| 61 |
{u'description': u'A nice bike ride around some mountains', u'title': u'Snowdonia circular', u'geometry': {u'type': u'LineString', u'coordinates': [[1..., 2...], [2..., 3...]]}, u'id': None, u'type': u'Feature', u'properties': {}} |
|---|
| 62 |
|
|---|