Changeset 1054
- Timestamp:
- 02/26/08 14:34:38
- Files:
-
- GeoJSON/trunk/CHANGES.txt (modified) (1 diff)
- GeoJSON/trunk/geojson/base.py (modified) (1 diff)
- GeoJSON/trunk/geojson/crs.py (modified) (2 diffs)
- GeoJSON/trunk/geojson/feature.py (modified) (1 diff)
- GeoJSON/trunk/geojson/geometry.py (modified) (3 diffs)
- GeoJSON/trunk/tests/blog.txt (modified) (3 diffs)
- GeoJSON/trunk/tests/c2c_features.txt (modified) (1 diff)
- GeoJSON/trunk/tests/featurecollection.txt (modified) (5 diffs)
- GeoJSON/trunk/tests/features.txt (modified) (6 diffs)
- GeoJSON/trunk/tests/geometry.txt (modified) (9 diffs)
- GeoJSON/trunk/tests/including_additional_members.txt (modified) (1 diff)
- GeoJSON/trunk/tests/objects.txt (modified) (4 diffs)
- GeoJSON/trunk/tests/runalldoctests.py (modified) (2 diffs)
- GeoJSON/trunk/tests/rundoctests.dist (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
GeoJSON/trunk/CHANGES.txt
r1040 r1054 3 3 4 4 5 - Made all code work with Python 2.4.3, 2.5.1, will test with all variations. 6 (see tests/rundoctests.dist) 5 7 6 - Added respresentations of the default GeoJSON types in feauture.py and 7 geometry.py 8 - input to {dumps,dump} must be 'mapping' like objects or support __geo_interface__ 9 - default geometry crs of EPSG 4326 will added if none provided 10 - Can use geojson.<type> to implement a __geo_interface__ 11 - Can nest a geometry object anywhere in a json structure (see doctests including_additional_members.txt) 12 - Can encode and decode geojson.<type> objects in addition to mappings 13 - Better error reporting if incorrect geojson type specified. 14 - Added additional doc-tests for FeatureColletions and object API. 15 16 8 - Made tests use ELLIPSIS to avoid output transmogification due to floating point representation. 17 9 18 Last-Modified: MattRussell 2 5/01/200810 Last-Modified: MattRussell 26/02/2008 GeoJSON/trunk/geojson/base.py
r1040 r1054 54 54 geojson_factory = getattr(geojson, type_) 55 55 if not issubclass(geojson_factory, GeoJSON): 56 raise TypeError("Not a valid GeoJSON type: %r (geojson_factory: %r, cls: %r)" % (type_, geojson_factory, cls)) 56 raise TypeError("""\ 57 Not a valid GeoJSON type: 58 %r (geojson_factory: %r, cls: %r) 59 """ % (type_, geojson_factory, cls)) 57 60 instance = geojson_factory(**d) 58 61 except (AttributeError, KeyError), invalid: GeoJSON/trunk/geojson/crs.py
r1040 r1054 8 8 def __init__(self, properties=None, **extra): 9 9 super(CoordinateReferenceSystem, self).__init__(**extra) 10 self.properties = properties if properties else{}10 self.properties = properties or {} 11 11 12 12 @property … … 21 21 def __init__(self, properties=None, **extra): 22 22 super(EPSG, self).__init__(**extra) 23 self.properties = properties if properties else{"code": 4326}23 self.properties = properties or {"code": 4326} GeoJSON/trunk/geojson/feature.py
r1040 r1054 23 23 self.id = id 24 24 self.geometry = self.to_instance(geometry, strict=True) 25 if properties is None: 26 properties = {} 27 self.properties = properties 25 self.properties = properties or {} 28 26 29 27 @property GeoJSON/trunk/geojson/geometry.py
r1040 r1054 1 2 1 from geojson.base import GeoJSON 3 2 import geojson.crs … … 20 19 d.update(coordinates=self.coordinates, crs=crs) 21 20 return d 21 22 22 23 23 class GeometryCollection(GeoJSON): … … 59 59 class MultiPolygon(Geometry): pass 60 60 61 GeoJSON/trunk/tests/blog.txt
r935 r1054 3 3 4 4 5 >>> d = {"blog": {"posts": [{"atom:summary": "post 1", "type": "atom:item", "atom:description": "i love blogging"}, {"atom:summary": "post 2 from CA", "type": "atom:item", "location": {"type": "Point", "coordinates": [-120 , 40]}, "atom:description": "geoblogging in California"}], "location": {"type": "Polygon", "coordinates": [[[-121, 39], [-119, 39], [-119, 41], [-121, 41], [-121, 39]]]}}}5 >>> d = {"blog": {"posts": [{"atom:summary": "post 1", "type": "atom:item", "atom:description": "i love blogging"}, {"atom:summary": "post 2 from CA", "type": "atom:item", "location": {"type": "Point", "coordinates": [-120.2138, 40.1231]}, "atom:description": "geoblogging in California"}], "location": {"type": "Polygon", "coordinates": [[[-121.5627, 39.8173], [-119.5221, 39.8173], [-119.1232, 41.1231], [-121.5632, 41.3321], [-121.2156, 39.8103]]]}}} 6 6 7 7 >>> import geojson … … 10 10 11 11 >>> json = geojson.dumps(d) 12 >>> json 13 '{"blog": {"posts": [{"atom:summary": "post 1", "type": "atom:item", "atom:description": "i love blogging"}, {"atom:summary": "post 2 from CA", "type": "atom:item", "location": {"type": "Point", "coordinates": [-120 , 40]}, "atom:description": "geoblogging in California"}], "location": {"type": "Polygon", "coordinates": [[[-121, 39], [-119, 39], [-119, 41], [-121, 41], [-121, 39]]]}}}'12 >>> json # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE 13 '{"blog": {"posts": [{"atom:summary": "post 1", "type": "atom:item", "atom:description": "i love blogging"}, {"atom:summary": "post 2 from CA", "type": "atom:item", "location": {"type": "Point", "coordinates": [-120..., 40...]}, "atom:description": "geoblogging in California"}], "location": {"type": "Polygon", "coordinates": [[[-121..., 39...], [-119..., 39...], [-119..., 41...], [-121..., 41...], [-121..., 39...]]]}}}' 14 14 15 15 Decoding … … 18 18 >>> type(o) 19 19 <type 'dict'> 20 >>> o 21 {u'blog': {u'posts': [{u'atom:summary': u'post 1', u'type': u'atom:item', u'atom:description': u'i love blogging'}, {u'atom:summary': u'post 2 from CA', u'type': u'atom:item', u'location': {u'type': u'Point', u'coordinates': [-120 , 40]}, u'atom:description': u'geoblogging in California'}], u'location': {u'type': u'Polygon', u'coordinates': [[[-121, 39], [-119, 39], [-119, 41], [-121, 41], [-121, 39]]]}}}20 >>> o # doctest: +ELLIPSIS 21 {u'blog': {u'posts': [{u'atom:summary': u'post 1', u'type': u'atom:item', u'atom:description': u'i love blogging'}, {u'atom:summary': u'post 2 from CA', u'type': u'atom:item', u'location': {u'type': u'Point', u'coordinates': [-120..., 40...]}, u'atom:description': u'geoblogging in California'}], u'location': {u'type': u'Polygon', u'coordinates': [[[-121..., 39...], [-119..., 39...], [-119..., 41...], [-121..., 41...], [-121..., 39...]]]}}} 22 22 GeoJSON/trunk/tests/c2c_features.txt
r1040 r1054 27 27 ... } 28 28 29 >>> from shapely.geometry import Point 30 >>> f = Feature(12, Point(9, 5), foo='bar') 29 >>> class Point(object): 30 ... """Mock shapely point.""" 31 ... def __init__(self, x, y): 32 ... self.x = x; self.y = y 33 ... @property 34 ... def __geo_interface__(self): 35 ... return dict(type="Point", coordinates=[self.x, self.y]) 36 37 >>> f = Feature(12, Point(49.132323, 55.341411), foo='bar') 31 38 >>> import geojson 32 >>> geojson.dumps(f) 33 '{"geometry": {"crs": {"type": "EPSG", "properties": {"code": 4326}}, "type": "Point", "coordinates": [ 9.0, 5.0]}, "type": "Feature", "properties": {"foo": "bar"}, "id": 12}'39 >>> geojson.dumps(f) # doctest: +ELLIPSIS 40 '{"geometry": {"crs": {"type": "EPSG", "properties": {"code": 4326}}, "type": "Point", "coordinates": [49..., 55...]}, "type": "Feature", "properties": {"foo": "bar"}, "id": 12}' 34 41 >>> c = FeatureCollection([f]) 35 >>> geojson.dumps(c) 36 '{"type": "FeatureCollection", "features": [{"geometry": {"crs": {"type": "EPSG", "properties": {"code": 4326}}, "type": "Point", "coordinates": [ 9.0, 5.0]}, "type": "Feature", "properties": {"foo": "bar"}, "id": 12}]}'42 >>> geojson.dumps(c) # doctest: +ELLIPSIS 43 '{"type": "FeatureCollection", "features": [{"geometry": {"crs": {"type": "EPSG", "properties": {"code": 4326}}, "type": "Point", "coordinates": [49..., 55...]}, "type": "Feature", "properties": {"foo": "bar"}, "id": 12}]}' GeoJSON/trunk/tests/featurecollection.txt
r1040 r1054 10 10 ... "id": "1", 11 11 ... "geometry": {"type": "Point", 12 ... "coordinates": [ 0, 1]}}]}12 ... "coordinates": [44.556, 67.192]}}]} 13 13 14 14 … … 17 17 >>> import geojson 18 18 >>> json = geojson.dumps(fc) 19 >>> json 20 '{"type": "FeatureCollection", "features": [{"geometry": {"type": "Point", "coordinates": [ 0, 1]}, "type": "Feature", "id": "1"}]}'19 >>> json # doctest: +ELLIPSIS 20 '{"type": "FeatureCollection", "features": [{"geometry": {"type": "Point", "coordinates": [44..., 67...]}, "type": "Feature", "id": "1"}]}' 21 21 22 22 Decoding 23 23 24 24 >>> fcb = geojson.loads(json) 25 >>> fcb 26 {u'type': u'FeatureCollection', u'features': [{u'geometry': {u'type': u'Point', u'coordinates': [ 0, 1]}, u'type': u'Feature', u'id': u'1'}]}25 >>> fcb # doctest: +ELLIPSIS 26 {u'type': u'FeatureCollection', u'features': [{u'geometry': {u'type': u'Point', u'coordinates': [44..., 67...]}, u'type': u'Feature', u'id': u'1'}]} 27 27 28 28 … … 30 30 ----------------------------------------------------------- 31 31 32 >>> features = [geojson.Feature(id=1, geometry=geojson.Point(coordinates=( 0, 1)))]32 >>> features = [geojson.Feature(id=1, geometry=geojson.Point(coordinates=(53.04781795911469, -4.10888671875)))] 33 33 >>> fco = geojson.FeatureCollection(features=features) 34 >>> fco.features 35 [Feature(geometry={'crs': {'type': 'EPSG', 'properties': {'code': 4326}}, 'type': 'Point', 'coordinates': ( 0, 1)}, properties={}, id=1)]34 >>> fco.features # doctest: +ELLIPSIS 35 [Feature(geometry={'crs': {'type': 'EPSG', 'properties': {'code': 4326}}, 'type': 'Point', 'coordinates': (53..., -4...)}, properties={}, id=1)] 36 36 37 37 … … 39 39 40 40 >>> json = geojson.dumps(fco) 41 >>> json 42 '{"type": "FeatureCollection", "features": [{"geometry": {"crs": {"type": "EPSG", "properties": {"code": 4326}}, "type": "Point", "coordinates": [ 0, 1]}, "type": "Feature", "properties": {}, "id": 1}]}'41 >>> json # doctest: +ELLIPSIS 42 '{"type": "FeatureCollection", "features": [{"geometry": {"crs": {"type": "EPSG", "properties": {"code": 4326}}, "type": "Point", "coordinates": [53..., -4...]}, "type": "Feature", "properties": {}, "id": 1}]}' 43 43 44 44 … … 47 47 >>> hook = lambda ob: geojson.GeoJSON.to_instance(ob, geojson.feature) 48 48 >>> fc = geojson.loads(json, object_hook=hook) 49 >>> fc.features 50 [Feature(geometry={'crs': {'type': 'EPSG', 'properties': {u'code': 4326}}, 'type': 'Point', 'coordinates': [ 0, 1]}, properties={}, id=1)]49 >>> fc.features # doctest: +ELLIPSIS 50 [Feature(geometry={'crs': {'type': 'EPSG', 'properties': {u'code': 4326}}, 'type': 'Point', 'coordinates': [53..., -4...]}, properties={}, id=1)] 51 51 52 >>> fc.features[0].geometry 53 Point(crs={'type': 'EPSG', 'properties': {u'code': 4326}}, coordinates=[ 0, 1])52 >>> fc.features[0].geometry # doctest: +ELLIPSIS 53 Point(crs={'type': 'EPSG', 'properties': {u'code': 4326}}, coordinates=[53..., -4...]) 54 54 55 55 >>> geometry = fc.features[0].geometry 56 >>> geometry.coordinates 57 [ 0, 1]58 >>>56 >>> geometry.coordinates # doctest: +ELLIPSIS 57 [53..., -4...] 58 59 59 60 - It may be used from any object, consider: 61 62 >>> class Point(object): 63 ... """Mock shapely point.""" 64 ... def __init__(self, x, y): 65 ... self.x = x 66 ... self.y = y 67 ... @property 68 ... def __geo_interface__(self): 69 ... return geojson.Point(coordiantes=[self.x, self.y]) 70 71 >>> p = Point(53.04781795911469, -4.10888671875) 72 >>> geojson.dumps(p) # doctest: +ELLIPSIS 73 '{"type": "Point", "coordiantes": [53..., -4...], "coordinates": [], "crs": {"type": "EPSG", "properties": {"code": 4326}}}' GeoJSON/trunk/tests/features.txt
r1040 r1054 8 8 ... 'type': 'Feature', 9 9 ... 'id': '1', 10 ... 'geometry': {'type': 'Point', 'coordinates': [ 0, 0]},10 ... 'geometry': {'type': 'Point', 'coordinates': [53.04781795911469, -4.10888671875]}, 11 11 ... 'properties': {'title': 'Dict 1'}, 12 12 ... } … … 17 17 18 18 >>> json = geojson.dumps(f) 19 >>> json 20 '{"geometry": {"crs": {"type": "EPSG", "properties": {"code": 4326}}, "type": "Point", "coordinates": [ 0, 0]}, "type": "Feature", "properties": {"title": "Dict 1"}, "id": "1"}'19 >>> json # doctest: +ELLIPSIS 20 '{"geometry": {"crs": {"type": "EPSG", "properties": {"code": 4326}}, "type": "Point", "coordinates": [53..., -4...]}, "type": "Feature", "properties": {"title": "Dict 1"}, "id": "1"}' 21 21 22 22 Decoding 23 23 24 24 >>> o = geojson.loads(json) 25 >>> o 26 {u'geometry': {u'crs': {u'type': u'EPSG', u'properties': {u'code': 4326}}, u'type': u'Point', u'coordinates': [ 0, 0]}, u'type': u'Feature', u'properties': {u'title': u'Dict 1'}, u'id': u'1'}25 >>> o # doctest: +ELLIPSIS 26 {u'geometry': {u'crs': {u'type': u'EPSG', u'properties': {u'code': 4326}}, u'type': u'Point', u'coordinates': [53..., -4...]}, u'type': u'Feature', u'properties': {u'title': u'Dict 1'}, u'id': u'1'} 27 27 28 28 … … 32 32 >>> from geojson.examples import SimpleWebFeature 33 33 >>> feature = SimpleWebFeature(id='1', 34 ... geometry={'type': 'Point', 'coordinates': [ 0.0, 0.0]},34 ... geometry={'type': 'Point', 'coordinates': [53.04781795911469, -4.10888671875]}, 35 35 ... title=u'Feature 1', summary=u'The first feature', 36 36 ... link='http://example.org/features/1') … … 46 46 >>> feature.properties['link'] 47 47 'http://example.org/features/1' 48 >>> feature.geometry 49 {'type': 'Point', 'coordinates': [ 0.0, 0.0]}48 >>> feature.geometry # doctest: +ELLIPSIS 49 {'type': 'Point', 'coordinates': [53..., -4...]} 50 50 51 51 Encoding 52 52 53 >>> geojson.dumps(feature) 54 '{"geometry": {"crs": {"type": "EPSG", "properties": {"code": 4326}}, "type": "Point", "coordinates": [ 0.0, 0.0]}, "type": "Feature", "properties": {"summary": "The first feature", "link": "http:\\/\\/example.org\\/features\\/1", "title": "Feature 1"}, "id": "1"}'53 >>> geojson.dumps(feature) # doctest: +ELLIPSIS 54 '{"geometry": {"crs": {"type": "EPSG", "properties": {"code": 4326}}, "type": "Point", "coordinates": [53..., -4...]}, "type": "Feature", "properties": {"summary": "The first feature", "link": "http:\\/\\/example.org\\/features\\/1", "title": "Feature 1"}, "id": "1"}' 55 55 56 56 Decoding 57 57 58 >>> factory = geojson.examples.createSimpleWebFeature 59 >>> json = '{"geometry": {"type": "Point", "coordinates": [ 0.0, 0.0]}, "id": "1", "properties": {"summary": "The first feature", "link": "http:\\/\\/example.org\\/features\\/1", "title": "Feature 1"}}'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 60 >>> feature = geojson.loads(json, object_hook=factory) 61 61 >>> type(feature) … … 69 69 >>> feature.properties['link'] 70 70 'http://example.org/features/1' 71 >>> feature.geometry 72 {'type': 'Point', 'coordinates': [ 0.0, 0.0]}71 >>> feature.geometry # doctest: +ELLIPSIS 72 {'type': 'Point', 'coordinates': [53..., -4...]} 73 73 74 74 Test the geo interface … … 86 86 87 87 >>> ob = Thingy('1', 'thingy one', -106.0, 40.0) 88 >>> ob.__geo_interface__['geometry'] 89 {'type': 'Point', 'coordinates': (-106. 0, 40.0)}90 >>> geojson.dumps(ob) 91 '{"geometry": {"type": "Point", "coordinates": [-106. 0, 40.0]}, "id": "1", "properties": {"title": "thingy one"}}'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"}}' GeoJSON/trunk/tests/geometry.txt
r1040 r1054 17 17 18 18 >>> json = geojson.dumps(g) 19 >>> json 20 '{"crs": {"type": "EPSG", "properties": {"code": 4326}}, "type": "Point", "coordinates": [1. 3, -54.23242]}'19 >>> json # doctest: +ELLIPSIS 20 '{"crs": {"type": "EPSG", "properties": {"code": 4326}}, "type": "Point", "coordinates": [1..., -54...]}' 21 21 22 22 Decoding 23 23 24 24 >>> o = geojson.loads(json) 25 >>> o 26 {u'crs': {u'type': u'EPSG', u'properties': {u'code': 4326}}, u'type': u'Point', u'coordinates': [1. 3, -54.232419999999998]}25 >>> o # doctest: +ELLIPSIS 26 {u'crs': {u'type': u'EPSG', u'properties': {u'code': 4326}}, u'type': u'Point', u'coordinates': [1..., -54...]} 27 27 28 28 … … 32 32 >>> ls = geojson.geometry.LineString(((52.1, -34.131), (65.231, -34.234)), crs=geojson.crs.EPSG()) 33 33 34 >>> ls.__geo_interface__ 35 {'crs': {'type': 'EPSG', 'properties': {'code': 4326}}, 'type': 'LineString', 'coordinates': ((52.100000000000001, -34.131), (65. 230999999999995, -34.234000000000002))}34 >>> ls.__geo_interface__ # doctest: +ELLIPSIS 35 {'crs': {'type': 'EPSG', 'properties': {'code': 4326}}, 'type': 'LineString', 'coordinates': ((52.100000000000001, -34.131), (65..., -34...))} 36 36 37 37 >>> ls.type 38 38 'LineString' 39 >>> ls.coordinates 40 ((52. 100000000000001, -34.131), (65.230999999999995, -34.234000000000002))39 >>> ls.coordinates # doctest: +ELLIPSIS 40 ((52..., -34...), (65..., -34...)) 41 41 42 42 Encoding 43 43 44 44 >>> json = geojson.dumps(ls) 45 >>> json 46 '{"crs": {"type": "EPSG", "properties": {"code": 4326}}, "type": "LineString", "coordinates": [[52. 1, -34.131], [65.231, -34.234]]}'45 >>> json # doctest: +ELLIPSIS 46 '{"crs": {"type": "EPSG", "properties": {"code": 4326}}, "type": "LineString", "coordinates": [[52..., -34...], [65..., -34...]]}' 47 47 48 48 Decoding 49 >>> geojson.loads(json) 50 {u'crs': {u'type': u'EPSG', u'properties': {u'code': 4326}}, u'type': u'LineString', u'coordinates': [[52. 100000000000001, -34.131], [65.230999999999995, -34.234000000000002]]}49 >>> geojson.loads(json) # doctest: +ELLIPSIS 50 {u'crs': {u'type': u'EPSG', u'properties': {u'code': 4326}}, u'type': u'LineString', u'coordinates': [[52..., -34...], [65..., -34...]]} 51 51 52 52 >>> factory = lambda o: geojson.GeoJSON.to_instance(o, geojson.geometry) … … 56 56 >>> geom.type 57 57 'LineString' 58 >>> geom.coordinates 59 [[52. 100000000000001, -34.131], [65.230999999999995, -34.234000000000002]]58 >>> geom.coordinates # doctest: +ELLIPSIS 59 [[52..., -34...], [65..., -34...]] 60 60 61 61 … … 68 68 >>> ls = geojson.geometry.LineString(coords, crs=EPSG(properties=dict(code=2423))) 69 69 70 >>> ls.__geo_interface__ 71 {'crs': {'type': 'EPSG', 'properties': {'code': 2423}}, 'type': 'LineString', 'coordinates': ((-1918145. 0108183471, -4098018.9166399641), (-680004.67204747663, -3864394.3196185972))}70 >>> ls.__geo_interface__ # doctest: +ELLIPSIS 71 {'crs': {'type': 'EPSG', 'properties': {'code': 2423}}, 'type': 'LineString', 'coordinates': ((-1918145..., -4098018...), (-680004..., -3864394...))} 72 72 73 73 It satisfies the geometry protocol 74 74 75 75 >>> json = geojson.dumps(ls) 76 >>> json 77 '{"crs": {"type": "EPSG", "properties": {"code": 2423}}, "type": "LineString", "coordinates": [[-1918145. 01082, -4098018.91664], [-680004.672047, -3864394.31962]]}'76 >>> json # doctest: +ELLIPSIS 77 '{"crs": {"type": "EPSG", "properties": {"code": 2423}}, "type": "LineString", "coordinates": [[-1918145..., -4098018...], [-680004..., -3864394...]]}' 78 78 79 79 Decoding 80 >>> geojson.loads(json) 81 {u'crs': {u'type': u'EPSG', u'properties': {u'code': 2423}}, u'type': u'LineString', u'coordinates': [[-1918145. 01082, -4098018.91664], [-680004.67204700003, -3864394.3196200002]]}80 >>> geojson.loads(json) # doctest: +ELLIPSIS 81 {u'crs': {u'type': u'EPSG', u'properties': {u'code': 2423}}, u'type': u'LineString', u'coordinates': [[-1918145..., -4098018...], [-680004..., -3864394...]]} 82 82 83 83 … … 88 88 >>> geom.type 89 89 'LineString' 90 >>> geom.coordinates 91 [[-1918145. 01082, -4098018.91664], [-680004.67204700003, -3864394.3196200002]]90 >>> geom.coordinates # doctest: +ELLIPSIS 91 [[-1918145..., -4098018...], [-680004..., -3864394...]] 92 92 93 93 … … 100 100 >>> ls = geojson.geometry.LineString(coords, crs=EPSG(properties=dict(code=2423))) 101 101 102 >>> ls.__geo_interface__ 103 {'crs': {'type': 'EPSG', 'properties': {'code': 2423}}, 'type': 'LineString', 'coordinates': ((-1918145. 0108183471, -4098018.9166399641), (-680004.67204747663, -3864394.3196185972))}102 >>> ls.__geo_interface__ # doctest: +ELLIPSIS 103 {'crs': {'type': 'EPSG', 'properties': {'code': 2423}}, 'type': 'LineString', 'coordinates': ((-1918145..., -4098018...), (-680004..., -3864394...))} 104 104 105 105 It satisfies the geometry protocol … … 108 108 >>> ls.type 109 109 'LineString' 110 >>> ls.coordinates 111 ((-1918145. 0108183471, -4098018.9166399641), (-680004.67204747663, -3864394.3196185972))110 >>> ls.coordinates # doctest: +ELLIPSIS 111 ((-1918145..., -4098018...), (-680004..., -3864394...)) 112 112 113 113 Encoding 114 114 115 115 >>> json = geojson.dumps(ls) 116 >>> json 117 '{"crs": {"type": "EPSG", "properties": {"code": 2423}}, "type": "LineString", "coordinates": [[-1918145. 01082, -4098018.91664], [-680004.672047, -3864394.31962]]}'116 >>> json # doctest: +ELLIPSIS 117 '{"crs": {"type": "EPSG", "properties": {"code": 2423}}, "type": "LineString", "coordinates": [[-1918145..., -4098018...], [-680004..., -3864394...]]}' 118 118 119 119 Decoding 120 >>> geojson.loads(json) 121 {u'crs': {u'type': u'EPSG', u'properties': {u'code': 2423}}, u'type': u'LineString', u'coordinates': [[-1918145. 01082, -4098018.91664], [-680004.67204700003, -3864394.3196200002]]}120 >>> geojson.loads(json) # doctest: +ELLIPSIS 121 {u'crs': {u'type': u'EPSG', u'properties': {u'code': 2423}}, u'type': u'LineString', u'coordinates': [[-1918145..., -4098018...], [-680004..., -3864394...]]} 122 122 123 123 >>> factory = lambda o: geojson.GeoJSON.to_instance(o, geojson.geometry) … … 127 127 >>> geom.type 128 128 'LineString' 129 >>> geom.coordinates 130 [[-1918145. 01082, -4098018.91664], [-680004.67204700003, -3864394.3196200002]]129 >>> geom.coordinates # doctest: +ELLIPSIS 130 [[-1918145..., -4098018...], [-680004..., -3864394...]] 131 131 132 132 … … 143 143 144 144 >>> ob = PointThingy(-106.0, 40.0) 145 >>> ob.__geo_interface__['coordinates'] 146 (-106. 0, 40.0)147 >>> geojson.dumps(ob) 148 '{"crs": {"type": "EPSG", "properties": {"code": 4326}}, "type": "Point", "coordinates": [-106. 0, 40.0]}'145 >>> ob.__geo_interface__['coordinates'] # doctest: +ELLIPSIS 146 (-106..., 40...) 147 >>> geojson.dumps(ob) # doctest: +ELLIPSIS 148 '{"crs": {"type": "EPSG", "properties": {"code": 4326}}, "type": "Point", "coordinates": [-106..., 40...]}' GeoJSON/trunk/tests/including_additional_members.txt
r1040 r1054 54 54 >>> json = geojson.dumps(r) 55 55 56 >>> json 57 '{"description": "A nice bike ride around some mountains", "title": "Snowdonia circular", "geometry": {"crs": {"type": "EPSG", "properties": {"code": 4326}}, "type": "LineString", "coordinates": [[1. 0, 2.0], [2.0, 3.2]]}, "type": "Feature", "properties": {}, "id": null}'56 >>> json # doctest: +ELLIPSIS 57 '{"description": "A nice bike ride around some mountains", "title": "Snowdonia circular", "geometry": {"crs": {"type": "EPSG", "properties": {"code": 4326}}, "type": "LineString", "coordinates": [[1..., 2...], [2..., 3...]]}, "type": "Feature", "properties": {}, "id": null}' 58 58 59 >>> r = geojson.loads(json) 60 >>> r 61 {u'description': u'A nice bike ride around some mountains', u'title': u'Snowdonia circular', u'geometry': {u'crs': {u'type': u'EPSG', u'properties': {u'code': 4326}}, u'type': u'LineString', u'coordinates': [[1. 0, 2.0], [2.0, 3.2000000000000002]]}, u'id': None, u'type': u'Feature', u'properties': {}}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'crs': {u'type': u'EPSG', u'properties': {u'code': 4326}}, u'type': u'LineString', u'coordinates': [[1..., 2...], [2..., 3...]]}, u'id': None, u'type': u'Feature', u'properties': {}} 62 62 GeoJSON/trunk/tests/objects.txt
r1040 r1054 19 19 20 20 >>> json = geojson.dumps(lat_lon) 21 >>> json 22 '{"crs": {"type": "EPSG", "properties": {"code": 4326}}, "type": "Point", "coordinates": [4. 53242, -54.1231]}'21 >>> json # doctest: +ELLIPSIS 22 '{"crs": {"type": "EPSG", "properties": {"code": 4326}}, "type": "Point", "coordinates": [4..., -54...]}' 23 23 24 24 Objects with a __geo_interface__ attribute or property can be nested in geojson feature: … … 29 29 30 30 >>> json = geojson.dumps(f) 31 >>> json 32 '{"geometry": {"crs": {"type": "EPSG", "properties": {"code": 4326}}, "type": "Point", "coordinates": [4. 53242, -54.1231]}, "type": "Feature", "properties": {}, "id": null}'31 >>> json # doctest: +ELLIPSIS 32 '{"geometry": {"crs": {"type": "EPSG", "properties": {"code": 4326}}, "type": "Point", "coordinates": [4..., -54...]}, "type": "Feature", "properties": {}, "id": null}' 33 33 34 34 geojson types can be used to implemented a __geo_interface__: … … 50 50 >>> ll2 = LatLon2(-54.1231, 4.53242) 51 51 >>> json2 = geojson.dumps(ll2) 52 >>> json2 53 '{"crs": {"type": "EPSG", "properties": {"code": 4326}}, "type": "Point", "coordinates": [4. 53242, -54.1231]}'52 >>> json2 # doctest: +ELLIPSIS 53 '{"crs": {"type": "EPSG", "properties": {"code": 4326}}, "type": "Point", "coordinates": [4..., -54...]}' 54 54 55 55 … … 58 58 59 59 >>> feature_dict = geojson.loads(json) 60 >>> print feature_dict61 {u'geometry': {u'crs': {u'type': u'EPSG', u'properties': {u'code': 4326}}, u'type': u'Point', u'coordinates': [4. 5324200000000001, -54.123100000000001]}, u'type': u'Feature', u'properties': {}, u'id': None}60 >>> feature_dict # doctest: +ELLIPSIS 61 {u'geometry': {u'crs': {u'type': u'EPSG', u'properties': {u'code': 4326}}, u'type': u'Point', u'coordinates': [4..., -54...]}, u'type': u'Feature', u'properties': {}, u'id': None} 62 62 63 - or to an object, via a factory. Here we'll create GeoJSON object.63 - or to an object, via a factory. Here we'll create GeoJSON object. 64 64 65 >>> ll2 = LatLon2(1,1) 66 >>> json = geojson.dumps(ll2) 67 >>> json 68 '{"crs": {"type": "EPSG", "properties": {"code": 4326}}, "type": "Point", "coordinates": [1, 1]}' 69 >>> geojson.loads(json) 70 {u'crs': {u'type': u'EPSG', u'properties': {u'code': 4326}}, u'type': u'Point', u'coordinates': [1, 1]} 65 >>> ll2 = LatLon2(43.3,-154.1) 66 >>> json = geojson.dumps(ll2) 67 >>> json # doctest: +ELLIPSIS 68 '{"crs": {"type": "EPSG", "properties": {"code": 4326}}, "type": "Point", "coordinates": [-154..., 43...]}' 69 70 >>> geojson.loads(json) # doctest: +ELLIPSIS 71 {u'crs': {u'type': u'EPSG', u'properties': {u'code': 4326}}, u'type': u'Point', u'coordinates': [-154..., 43...]} 72 71 73 >>> factory = lambda ob: geojson.GeoJSON.to_instance(ob) 72 74 >>> geometry = geojson.loads(json, object_hook=factory) 73 >>> geometry 74 Point(crs={'type': 'EPSG', 'properties': {u'code': 4326}}, coordinates=[ 1, 1])75 >>> geometry # doctest: +ELLIPSIS 76 Point(crs={'type': 'EPSG', 'properties': {u'code': 4326}}, coordinates=[-154..., 43...]) 75 77 76 78 GeoJSON/trunk/tests/runalldoctests.py
r935 r1054 6 6 try: 7 7 import pkg_resources 8 pkg_resources.require( 'PCL-GeoJSON')8 pkg_resources.require("PCL-GeoJSON") 9 9 except: 10 10 pass 11 11 12 12 13 def run(pattern): 13 14 if pattern is None: 14 testfiles = glob.glob( '*.txt')15 testfiles = glob.glob("*.txt") 15 16 else: 16 17 testfiles = glob.glob(pattern) 17 for file in testfiles: 18 for file in testfiles: 18 19 doctest.testfile(file) 20 19 21 20 22 if __name__ == "__main__": … … 26 28 pattern = None 27 29 for o, a in opts: 28 if o == '-t':30 if o == "-t": 29 31 pattern = a 30 32 run(pattern) GeoJSON/trunk/tests/rundoctests.dist
r935 r1054 1 #!/bin/ sh1 #!/bin/bash 2 2 #export PYTHONPATH="/home/sean/Projects/GeoJSON/dev-packages" 3 python runalldoctests.py -v 3 4 for python_version in python python2.4 python2.5; do 5 py_exec_path=$(which "$python_version"); 6 if [ -e "$py_exec_path" ] && [ -x "$py_exec_path" ]; then 7 echo "Testing under $python_version ($py_exec_path)" 8 "$python_version" runalldoctests.py -v 9 fi 10 done
