PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/django/contrib/gis/geometry/test_data.py

https://code.google.com/p/mango-py/
Python | 105 lines | 54 code | 23 blank | 28 comment | 10 complexity | c4fca32d21e9fc20079fd1b867cd5819 MD5 | raw file
Possible License(s): BSD-3-Clause
  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
  7. from django.contrib import gis
  8. from django.utils import simplejson
  9. # This global used to store reference geometry data.
  10. GEOMETRIES = None
  11. # Path where reference test data is located.
  12. TEST_DATA = os.path.join(os.path.dirname(gis.__file__), 'tests', 'data')
  13. def tuplize(seq):
  14. "Turn all nested sequences to tuples in given sequence."
  15. if isinstance(seq, (list, tuple)):
  16. return tuple([tuplize(i) for i in seq])
  17. return seq
  18. def strconvert(d):
  19. "Converts all keys in dictionary to str type."
  20. return dict([(str(k), v) for k, v in d.iteritems()])
  21. def get_ds_file(name, ext):
  22. return os.path.join(TEST_DATA,
  23. name,
  24. name + '.%s' % ext
  25. )
  26. class TestObj(object):
  27. """
  28. Base testing object, turns keyword args into attributes.
  29. """
  30. def __init__(self, **kwargs):
  31. for key, value in kwargs.items():
  32. setattr(self, key, value)
  33. class TestDS(TestObj):
  34. """
  35. Object for testing GDAL data sources.
  36. """
  37. def __init__(self, name, **kwargs):
  38. # Shapefile is default extension, unless specified otherwise.
  39. ext = kwargs.pop('ext', 'shp')
  40. self.ds = get_ds_file(name, ext)
  41. super(TestDS, self).__init__(**kwargs)
  42. class TestGeom(TestObj):
  43. """
  44. Testing object used for wrapping reference geometry data
  45. in GEOS/GDAL tests.
  46. """
  47. def __init__(self, **kwargs):
  48. # Converting lists to tuples of certain keyword args
  49. # so coordinate test cases will match (JSON has no
  50. # concept of tuple).
  51. coords = kwargs.pop('coords', None)
  52. if coords:
  53. self.coords = tuplize(coords)
  54. centroid = kwargs.pop('centroid', None)
  55. if centroid:
  56. self.centroid = tuple(centroid)
  57. ext_ring_cs = kwargs.pop('ext_ring_cs', None)
  58. if ext_ring_cs:
  59. ext_ring_cs = tuplize(ext_ring_cs)
  60. self.ext_ring_cs = ext_ring_cs
  61. super(TestGeom, self).__init__(**kwargs)
  62. class TestGeomSet(object):
  63. """
  64. Each attribute of this object is a list of `TestGeom` instances.
  65. """
  66. def __init__(self, **kwargs):
  67. for key, value in kwargs.items():
  68. setattr(self, key, [TestGeom(**strconvert(kw)) for kw in value])
  69. class TestDataMixin(object):
  70. """
  71. Mixin used for GEOS/GDAL test cases that defines a `geometries`
  72. property, which returns and/or loads the reference geometry data.
  73. """
  74. @property
  75. def geometries(self):
  76. global GEOMETRIES
  77. if GEOMETRIES is None:
  78. # Load up the test geometry data from fixture into global.
  79. gzf = gzip.GzipFile(os.path.join(TEST_DATA, 'geometries.json.gz'))
  80. geometries = simplejson.loads(gzf.read())
  81. GEOMETRIES = TestGeomSet(**strconvert(geometries))
  82. return GEOMETRIES