test_envelope.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import unittest
  2. from unittest import skipUnless
  3. from django.contrib.gis.gdal import HAS_GDAL
  4. if HAS_GDAL:
  5. from django.contrib.gis.gdal import Envelope, GDALException
  6. class TestPoint(object):
  7. def __init__(self, x, y):
  8. self.x = x
  9. self.y = y
  10. @skipUnless(HAS_GDAL, "GDAL is required")
  11. class EnvelopeTest(unittest.TestCase):
  12. def setUp(self):
  13. self.e = Envelope(0, 0, 5, 5)
  14. def test01_init(self):
  15. "Testing Envelope initialization."
  16. e1 = Envelope((0, 0, 5, 5))
  17. Envelope(0, 0, 5, 5)
  18. Envelope(0, '0', '5', 5) # Thanks to ww for this
  19. Envelope(e1._envelope)
  20. self.assertRaises(GDALException, Envelope, (5, 5, 0, 0))
  21. self.assertRaises(GDALException, Envelope, 5, 5, 0, 0)
  22. self.assertRaises(GDALException, Envelope, (0, 0, 5, 5, 3))
  23. self.assertRaises(GDALException, Envelope, ())
  24. self.assertRaises(ValueError, Envelope, 0, 'a', 5, 5)
  25. self.assertRaises(TypeError, Envelope, 'foo')
  26. self.assertRaises(GDALException, Envelope, (1, 1, 0, 0))
  27. try:
  28. Envelope(0, 0, 0, 0)
  29. except GDALException:
  30. self.fail("shouldn't raise an exception for min_x == max_x or min_y == max_y")
  31. def test02_properties(self):
  32. "Testing Envelope properties."
  33. e = Envelope(0, 0, 2, 3)
  34. self.assertEqual(0, e.min_x)
  35. self.assertEqual(0, e.min_y)
  36. self.assertEqual(2, e.max_x)
  37. self.assertEqual(3, e.max_y)
  38. self.assertEqual((0, 0), e.ll)
  39. self.assertEqual((2, 3), e.ur)
  40. self.assertEqual((0, 0, 2, 3), e.tuple)
  41. self.assertEqual('POLYGON((0.0 0.0,0.0 3.0,2.0 3.0,2.0 0.0,0.0 0.0))', e.wkt)
  42. self.assertEqual('(0.0, 0.0, 2.0, 3.0)', str(e))
  43. def test03_equivalence(self):
  44. "Testing Envelope equivalence."
  45. e1 = Envelope(0.523, 0.217, 253.23, 523.69)
  46. e2 = Envelope((0.523, 0.217, 253.23, 523.69))
  47. self.assertEqual(e1, e2)
  48. self.assertEqual((0.523, 0.217, 253.23, 523.69), e1)
  49. def test04_expand_to_include_pt_2_params(self):
  50. "Testing Envelope expand_to_include -- point as two parameters."
  51. self.e.expand_to_include(2, 6)
  52. self.assertEqual((0, 0, 5, 6), self.e)
  53. self.e.expand_to_include(-1, -1)
  54. self.assertEqual((-1, -1, 5, 6), self.e)
  55. def test05_expand_to_include_pt_2_tuple(self):
  56. "Testing Envelope expand_to_include -- point as a single 2-tuple parameter."
  57. self.e.expand_to_include((10, 10))
  58. self.assertEqual((0, 0, 10, 10), self.e)
  59. self.e.expand_to_include((-10, -10))
  60. self.assertEqual((-10, -10, 10, 10), self.e)
  61. def test06_expand_to_include_extent_4_params(self):
  62. "Testing Envelope expand_to_include -- extent as 4 parameters."
  63. self.e.expand_to_include(-1, 1, 3, 7)
  64. self.assertEqual((-1, 0, 5, 7), self.e)
  65. def test06_expand_to_include_extent_4_tuple(self):
  66. "Testing Envelope expand_to_include -- extent as a single 4-tuple parameter."
  67. self.e.expand_to_include((-1, 1, 3, 7))
  68. self.assertEqual((-1, 0, 5, 7), self.e)
  69. def test07_expand_to_include_envelope(self):
  70. "Testing Envelope expand_to_include with Envelope as parameter."
  71. self.e.expand_to_include(Envelope(-1, 1, 3, 7))
  72. self.assertEqual((-1, 0, 5, 7), self.e)
  73. def test08_expand_to_include_point(self):
  74. "Testing Envelope expand_to_include with Point as parameter."
  75. self.e.expand_to_include(TestPoint(-1, 1))
  76. self.assertEqual((-1, 0, 5, 5), self.e)
  77. self.e.expand_to_include(TestPoint(10, 10))
  78. self.assertEqual((-1, 0, 10, 10), self.e)