2013-07-01 20:22:27 +08:00
|
|
|
import unittest
|
|
|
|
from unittest import skipUnless
|
|
|
|
|
2013-05-11 11:08:45 +08:00
|
|
|
from django.contrib.gis.gdal import HAS_GDAL
|
|
|
|
|
|
|
|
if HAS_GDAL:
|
2014-12-18 05:00:05 +08:00
|
|
|
from django.contrib.gis.gdal import Envelope, GDALException
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2009-03-08 07:02:48 +08:00
|
|
|
|
|
|
|
class TestPoint(object):
|
|
|
|
def __init__(self, x, y):
|
|
|
|
self.x = x
|
|
|
|
self.y = y
|
|
|
|
|
2013-05-11 11:08:45 +08:00
|
|
|
|
|
|
|
@skipUnless(HAS_GDAL, "GDAL is required")
|
2009-03-08 07:02:48 +08:00
|
|
|
class EnvelopeTest(unittest.TestCase):
|
|
|
|
|
2010-10-11 20:55:17 +08:00
|
|
|
def setUp(self):
|
|
|
|
self.e = Envelope(0, 0, 5, 5)
|
2013-07-01 20:22:27 +08:00
|
|
|
|
2009-03-08 07:02:48 +08:00
|
|
|
def test01_init(self):
|
2014-03-02 22:25:53 +08:00
|
|
|
"Testing Envelope initialization."
|
2009-03-08 07:02:48 +08:00
|
|
|
e1 = Envelope((0, 0, 5, 5))
|
2013-08-05 00:17:10 +08:00
|
|
|
Envelope(0, 0, 5, 5)
|
2013-11-03 05:02:56 +08:00
|
|
|
Envelope(0, '0', '5', 5) # Thanks to ww for this
|
2013-08-05 00:17:10 +08:00
|
|
|
Envelope(e1._envelope)
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(GDALException):
|
|
|
|
Envelope((5, 5, 0, 0))
|
|
|
|
with self.assertRaises(GDALException):
|
|
|
|
Envelope(5, 5, 0, 0)
|
|
|
|
with self.assertRaises(GDALException):
|
|
|
|
Envelope((0, 0, 5, 5, 3))
|
|
|
|
with self.assertRaises(GDALException):
|
|
|
|
Envelope(())
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
Envelope(0, 'a', 5, 5)
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
Envelope('foo')
|
|
|
|
with self.assertRaises(GDALException):
|
|
|
|
Envelope((1, 1, 0, 0))
|
2016-06-28 23:21:26 +08:00
|
|
|
# Shouldn't raise an exception for min_x == max_x or min_y == max_y
|
|
|
|
Envelope(0, 0, 0, 0)
|
2009-03-08 07:02:48 +08:00
|
|
|
|
|
|
|
def test02_properties(self):
|
|
|
|
"Testing Envelope properties."
|
|
|
|
e = Envelope(0, 0, 2, 3)
|
|
|
|
self.assertEqual(0, e.min_x)
|
|
|
|
self.assertEqual(0, e.min_y)
|
|
|
|
self.assertEqual(2, e.max_x)
|
|
|
|
self.assertEqual(3, e.max_y)
|
|
|
|
self.assertEqual((0, 0), e.ll)
|
|
|
|
self.assertEqual((2, 3), e.ur)
|
|
|
|
self.assertEqual((0, 0, 2, 3), e.tuple)
|
|
|
|
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)
|
|
|
|
self.assertEqual('(0.0, 0.0, 2.0, 3.0)', str(e))
|
|
|
|
|
|
|
|
def test03_equivalence(self):
|
|
|
|
"Testing Envelope equivalence."
|
|
|
|
e1 = Envelope(0.523, 0.217, 253.23, 523.69)
|
|
|
|
e2 = Envelope((0.523, 0.217, 253.23, 523.69))
|
|
|
|
self.assertEqual(e1, e2)
|
|
|
|
self.assertEqual((0.523, 0.217, 253.23, 523.69), e1)
|
|
|
|
|
2010-10-11 20:55:17 +08:00
|
|
|
def test04_expand_to_include_pt_2_params(self):
|
|
|
|
"Testing Envelope expand_to_include -- point as two parameters."
|
|
|
|
self.e.expand_to_include(2, 6)
|
|
|
|
self.assertEqual((0, 0, 5, 6), self.e)
|
|
|
|
self.e.expand_to_include(-1, -1)
|
|
|
|
self.assertEqual((-1, -1, 5, 6), self.e)
|
|
|
|
|
|
|
|
def test05_expand_to_include_pt_2_tuple(self):
|
|
|
|
"Testing Envelope expand_to_include -- point as a single 2-tuple parameter."
|
|
|
|
self.e.expand_to_include((10, 10))
|
|
|
|
self.assertEqual((0, 0, 10, 10), self.e)
|
|
|
|
self.e.expand_to_include((-10, -10))
|
|
|
|
self.assertEqual((-10, -10, 10, 10), self.e)
|
|
|
|
|
|
|
|
def test06_expand_to_include_extent_4_params(self):
|
|
|
|
"Testing Envelope expand_to_include -- extent as 4 parameters."
|
|
|
|
self.e.expand_to_include(-1, 1, 3, 7)
|
|
|
|
self.assertEqual((-1, 0, 5, 7), self.e)
|
|
|
|
|
|
|
|
def test06_expand_to_include_extent_4_tuple(self):
|
|
|
|
"Testing Envelope expand_to_include -- extent as a single 4-tuple parameter."
|
|
|
|
self.e.expand_to_include((-1, 1, 3, 7))
|
|
|
|
self.assertEqual((-1, 0, 5, 7), self.e)
|
|
|
|
|
|
|
|
def test07_expand_to_include_envelope(self):
|
|
|
|
"Testing Envelope expand_to_include with Envelope as parameter."
|
|
|
|
self.e.expand_to_include(Envelope(-1, 1, 3, 7))
|
|
|
|
self.assertEqual((-1, 0, 5, 7), self.e)
|
2009-03-08 07:02:48 +08:00
|
|
|
|
2010-10-11 20:55:17 +08:00
|
|
|
def test08_expand_to_include_point(self):
|
|
|
|
"Testing Envelope expand_to_include with Point as parameter."
|
|
|
|
self.e.expand_to_include(TestPoint(-1, 1))
|
|
|
|
self.assertEqual((-1, 0, 5, 5), self.e)
|
|
|
|
self.e.expand_to_include(TestPoint(10, 10))
|
|
|
|
self.assertEqual((-1, 0, 10, 10), self.e)
|