App Engine Python SDK version 1.9.12
[gae.git] / python / lib / django-1.2 / django / contrib / gis / geometry / test_data.py
blob4e073487a5d7b8f37f266f7239d83cf9b70e2497
1 """
2 This module has the mock object definitions used to hold reference geometry
3 for the GEOS and GDAL tests.
4 """
5 import gzip
6 import os
8 from django.contrib import gis
9 from django.utils import simplejson
12 # This global used to store reference geometry data.
13 GEOMETRIES = None
15 # Path where reference test data is located.
16 TEST_DATA = os.path.join(os.path.dirname(gis.__file__), 'tests', 'data')
19 def tuplize(seq):
20 "Turn all nested sequences to tuples in given sequence."
21 if isinstance(seq, (list, tuple)):
22 return tuple([tuplize(i) for i in seq])
23 return seq
26 def strconvert(d):
27 "Converts all keys in dictionary to str type."
28 return dict([(str(k), v) for k, v in d.iteritems()])
31 def get_ds_file(name, ext):
32 return os.path.join(TEST_DATA,
33 name,
34 name + '.%s' % ext
38 class TestObj(object):
39 """
40 Base testing object, turns keyword args into attributes.
41 """
42 def __init__(self, **kwargs):
43 for key, value in kwargs.items():
44 setattr(self, key, value)
47 class TestDS(TestObj):
48 """
49 Object for testing GDAL data sources.
50 """
51 def __init__(self, name, **kwargs):
52 # Shapefile is default extension, unless specified otherwise.
53 ext = kwargs.pop('ext', 'shp')
54 self.ds = get_ds_file(name, ext)
55 super(TestDS, self).__init__(**kwargs)
58 class TestGeom(TestObj):
59 """
60 Testing object used for wrapping reference geometry data
61 in GEOS/GDAL tests.
62 """
63 def __init__(self, **kwargs):
64 # Converting lists to tuples of certain keyword args
65 # so coordinate test cases will match (JSON has no
66 # concept of tuple).
67 coords = kwargs.pop('coords', None)
68 if coords:
69 self.coords = tuplize(coords)
71 centroid = kwargs.pop('centroid', None)
72 if centroid:
73 self.centroid = tuple(centroid)
75 ext_ring_cs = kwargs.pop('ext_ring_cs', None)
76 if ext_ring_cs:
77 ext_ring_cs = tuplize(ext_ring_cs)
78 self.ext_ring_cs = ext_ring_cs
80 super(TestGeom, self).__init__(**kwargs)
83 class TestGeomSet(object):
84 """
85 Each attribute of this object is a list of `TestGeom` instances.
86 """
87 def __init__(self, **kwargs):
88 for key, value in kwargs.items():
89 setattr(self, key, [TestGeom(**strconvert(kw)) for kw in value])
92 class TestDataMixin(object):
93 """
94 Mixin used for GEOS/GDAL test cases that defines a `geometries`
95 property, which returns and/or loads the reference geometry data.
96 """
97 @property
98 def geometries(self):
99 global GEOMETRIES
100 if GEOMETRIES is None:
101 # Load up the test geometry data from fixture into global.
102 gzf = gzip.GzipFile(os.path.join(TEST_DATA, 'geometries.json.gz'))
103 geometries = simplejson.loads(gzf.read())
104 GEOMETRIES = TestGeomSet(**strconvert(geometries))
105 return GEOMETRIES