| 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, sort_keys=True) |
|---|
| 19 | >>> json # doctest: +ELLIPSIS |
|---|
| 20 | '{"geometry": {"coordinates": [53..., -4...], "type": "Point"}, "id": "1", "properties": {"title": "Dict 1"}, "type": "Feature"}' |
|---|
| 21 | |
|---|
| 22 | Decoding |
|---|
| 23 | |
|---|
| 24 | >>> o = geojson.loads(json) |
|---|
| 25 | >>> geojson.dumps(o, sort_keys=True) # doctest: +ELLIPSIS |
|---|
| 26 | '{"geometry": {"coordinates": [53..., -4...], "type": "Point"}, "id": "1", "properties": {"title": "Dict 1"}, "type": "Feature"}' |
|---|
| 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, sort_keys=True) # doctest: +ELLIPSIS |
|---|
| 54 | '{"geometry": {"coordinates": [53..., -4...], "type": "Point"}, "id": "1", "properties": {"link": "http://example.org/features/1", "summary": "The first feature", "title": "Feature 1"}, "type": "Feature"}' |
|---|
| 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, encoding="utf-8") |
|---|
| 61 | >>> type(feature) |
|---|
| 62 | <class 'geojson.examples.SimpleWebFeature'> |
|---|
| 63 | >>> feature.id |
|---|
| 64 | '1' |
|---|
| 65 | >>> print feature.properties['title'] |
|---|
| 66 | Feature 1 |
|---|
| 67 | >>> print feature.properties['summary'] |
|---|
| 68 | 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, sort_keys=True) # doctest: +ELLIPSIS |
|---|
| 91 | '{"geometry": {"coordinates": [-106..., 40...], "type": "Point"}, "id": "1", "properties": {"title": "thingy one"}}' |
|---|