2 This module has the mock object definitions used to hold reference geometry
3 for the GEOS and GDAL tests.
8 from django
.contrib
import gis
9 from django
.utils
import simplejson
12 # This global used to store reference geometry data.
15 # Path where reference test data is located.
16 TEST_DATA
= os
.path
.join(os
.path
.dirname(gis
.__file
__), 'tests', 'data')
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
])
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
,
38 class TestObj(object):
40 Base testing object, turns keyword args into attributes.
42 def __init__(self
, **kwargs
):
43 for key
, value
in kwargs
.items():
44 setattr(self
, key
, value
)
47 class TestDS(TestObj
):
49 Object for testing GDAL data sources.
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
):
60 Testing object used for wrapping reference geometry data
63 def __init__(self
, **kwargs
):
64 # Converting lists to tuples of certain keyword args
65 # so coordinate test cases will match (JSON has no
67 coords
= kwargs
.pop('coords', None)
69 self
.coords
= tuplize(coords
)
71 centroid
= kwargs
.pop('centroid', None)
73 self
.centroid
= tuple(centroid
)
75 ext_ring_cs
= kwargs
.pop('ext_ring_cs', None)
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):
85 Each attribute of this object is a list of `TestGeom` instances.
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):
94 Mixin used for GEOS/GDAL test cases that defines a `geometries`
95 property, which returns and/or loads the reference geometry data.
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
))