| 1 |
Tests of the feature protocol |
|---|
| 2 |
============================= |
|---|
| 3 |
|
|---|
| 4 |
A dictionary can satisfy the protocol |
|---|
| 5 |
------------------------------------- |
|---|
| 6 |
|
|---|
| 7 |
>>> f = { |
|---|
| 8 |
... 'type': 'Feature', |
|---|
| 9 |
... 'id': '1', |
|---|
| 10 |
... 'geometry': {'type': 'Point', 'coordinates': [53.04781795911469, -4.10888671875]}, |
|---|
| 11 |
... 'properties': {'title': 'Dict 1'}, |
|---|
| 12 |
... } |
|---|
| 13 |
|
|---|
| 14 |
>>> import geojson |
|---|
| 15 |
|
|---|
| 16 |
Encoding |
|---|
| 17 |
|
|---|
| 18 |
>>> json = geojson.dumps(f) |
|---|
| 19 |
>>> json # doctest: +ELLIPSIS |
|---|
| 20 |
'{"geometry": {"type": "Point", "coordinates": [53..., -4...]}, "type": "Feature", "properties": {"title": "Dict 1"}, "id": "1"}' |
|---|
| 21 |
|
|---|
| 22 |
Decoding |
|---|
| 23 |
|
|---|
| 24 |
>>> o = geojson.loads(json) |
|---|
| 25 |
>>> o # doctest: +ELLIPSIS |
|---|
| 26 |
{u'geometry': {u'type': u'Point', u'coordinates': [53..., -4...]}, u'type': u'Feature', u'properties': {u'title': u'Dict 1'}, u'id': u'1'} |
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
feature class |
|---|
| 30 |
--------------------- |
|---|
| 31 |
|
|---|
| 32 |
>>> from geojson.examples import SimpleWebFeature |
|---|
| 33 |
>>> feature = SimpleWebFeature(id='1', |
|---|
| 34 |
... geometry={'type': 'Point', 'coordinates': [53.04781795911469, -4.10888671875]}, |
|---|
| 35 |
... title=u'Feature 1', summary=u'The first feature', |
|---|
| 36 |
... link='http://example.org/features/1') |
|---|
| 37 |
|
|---|
| 38 |
It satisfies the feature protocol |
|---|
| 39 |
|
|---|
| 40 |
>>> feature.id |
|---|
| 41 |
'1' |
|---|
| 42 |
>>> feature.properties['title'] |
|---|
| 43 |
u'Feature 1' |
|---|
| 44 |
>>> feature.properties['summary'] |
|---|
| 45 |
u'The first feature' |
|---|
| 46 |
>>> feature.properties['link'] |
|---|
| 47 |
'http://example.org/features/1' |
|---|
| 48 |
>>> feature.geometry # doctest: +ELLIPSIS |
|---|
| 49 |
{'type': 'Point', 'coordinates': [53..., -4...]} |
|---|
| 50 |
|
|---|
| 51 |
Encoding |
|---|
| 52 |
|
|---|
| 53 |
>>> geojson.dumps(feature) # doctest: +ELLIPSIS |
|---|
| 54 |
'{"geometry": {"type": "Point", "coordinates": [53..., -4...]}, "type": "Feature", "properties": {"summary": "The first feature", "link": "http:\\/\\/example.org\\/features\\/1", "title": "Feature 1"}, "id": "1"}' |
|---|
| 55 |
|
|---|
| 56 |
Decoding |
|---|
| 57 |
|
|---|
| 58 |
>>> factory = geojson.examples.createSimpleWebFeature |
|---|
| 59 |
>>> json = '{"geometry": {"type": "Point", "coordinates": [53.04781795911469, -4.10888671875]}, "id": "1", "properties": {"summary": "The first feature", "link": "http:\\/\\/example.org\\/features\\/1", "title": "Feature 1"}}' |
|---|
| 60 |
>>> feature = geojson.loads(json, object_hook=factory) |
|---|
| 61 |
>>> type(feature) |
|---|
| 62 |
<class 'geojson.examples.SimpleWebFeature'> |
|---|
| 63 |
>>> feature.id |
|---|
| 64 |
'1' |
|---|
| 65 |
>>> feature.properties['title'] |
|---|
| 66 |
u'Feature 1' |
|---|
| 67 |
>>> feature.properties['summary'] |
|---|
| 68 |
u'The first feature' |
|---|
| 69 |
>>> feature.properties['link'] |
|---|
| 70 |
'http://example.org/features/1' |
|---|
| 71 |
>>> feature.geometry # doctest: +ELLIPSIS |
|---|
| 72 |
{'type': 'Point', 'coordinates': [53..., -4...]} |
|---|
| 73 |
|
|---|
| 74 |
Test the geo interface |
|---|
| 75 |
---------------------- |
|---|
| 76 |
|
|---|
| 77 |
>>> class Thingy(object): |
|---|
| 78 |
... def __init__(self, id, title, x, y): |
|---|
| 79 |
... self.id = id |
|---|
| 80 |
... self.title = title |
|---|
| 81 |
... self.x = x |
|---|
| 82 |
... self.y = y |
|---|
| 83 |
... @property |
|---|
| 84 |
... def __geo_interface__(self): |
|---|
| 85 |
... return {"id": self.id, "properties": {"title": self.title}, "geometry": {"type": "Point", "coordinates": (self.x, self.y)}} |
|---|
| 86 |
|
|---|
| 87 |
>>> ob = Thingy('1', 'thingy one', -106.0, 40.0) |
|---|
| 88 |
>>> ob.__geo_interface__['geometry'] # doctest: +ELLIPSIS |
|---|
| 89 |
{'type': 'Point', 'coordinates': (-106..., 40...)} |
|---|
| 90 |
>>> geojson.dumps(ob) # doctest: +ELLIPSIS |
|---|
| 91 |
'{"geometry": {"type": "Point", "coordinates": [-106..., 40...]}, "id": "1", "properties": {"title": "thingy one"}}' |
|---|