Ticket #10368: gis_envelope_expand.diff
File gis_envelope_expand.diff, 5.2 KB (added by , 16 years ago) |
---|
-
django/contrib/gis/tests/test_gdal_envelope.py
1 1 import unittest 2 2 from django.contrib.gis.gdal import Envelope, OGRException 3 from django.contrib.gis.geos import Point 3 4 4 5 class EnvelopeTest(unittest.TestCase): 6 def setUp(self): 7 self.e = Envelope(0, 0, 5, 5) 5 8 6 9 def test01_init(self): 7 10 "Testing Envelope initilization." … … 36 39 self.assertEqual(e1, e2) 37 40 self.assertEqual((0.523, 0.217, 253.23, 523.69), e1) 38 41 42 def test04_expand_to_include_pt_2_params(self): 43 "Testing Envelope expand_to_include -- point as two parameters." 44 self.e.expand_to_include(2, 6) 45 self.assertEqual((0, 0, 5, 6), self.e) 46 self.e.expand_to_include(-1, -1) 47 self.assertEqual((-1, -1, 5, 6), self.e) 48 49 def test05_expand_to_include_pt_2_tuple(self): 50 "Testing Envelope expand_to_include -- point as a single 2-tuple parameter." 51 self.e.expand_to_include((10, 10)) 52 self.assertEqual((0, 0, 10, 10), self.e) 53 self.e.expand_to_include((-10, -10)) 54 self.assertEqual((-10, -10, 10, 10), self.e) 55 56 def test06_expand_to_include_extent_4_params(self): 57 "Testing Envelope expand_to_include -- extent as 4 parameters." 58 self.e.expand_to_include(-1, 1, 3, 7) 59 self.assertEqual((-1, 0, 5, 7), self.e) 60 61 def test06_expand_to_include_extent_4_tuple(self): 62 "Testing Envelope expand_to_include -- extent as a single 4-tuple parameter." 63 self.e.expand_to_include((-1, 1, 3, 7)) 64 self.assertEqual((-1, 0, 5, 7), self.e) 65 66 def test07_expand_to_include_envelope(self): 67 "Testing Envelope expand_to_include with Envelope as parameter." 68 self.e.expand_to_include(Envelope(-1, 1, 3, 7)) 69 self.assertEqual((-1, 0, 5, 7), self.e) 70 71 def test08_expand_to_include_point(self): 72 "Testing Envelope expand_to_include with Point as parameter." 73 self.e.expand_to_include(Point(-1, 1)) 74 self.assertEqual((-1, 0, 5, 5), self.e) 75 self.e.expand_to_include(Point(10, 10)) 76 self.assertEqual((-1, 0, 10, 10), self.e) 77 39 78 def suite(): 40 79 s = unittest.TestSuite() 41 80 s.addTest(unittest.makeSuite(EnvelopeTest)) -
django/contrib/gis/gdal/envelope.py
13 13 from ctypes import Structure, c_double 14 14 from types import TupleType, ListType 15 15 from django.contrib.gis.gdal.error import OGRException 16 from django.contrib.gis.geos import Point 16 17 17 18 # The OGR definition of an Envelope is a C structure containing four doubles. 18 19 # See the 'ogr_core.h' source file for more information: … … 132 133 (self.min_x, self.min_y, self.min_x, self.max_y, 133 134 self.max_x, self.max_y, self.max_x, self.min_y, 134 135 self.min_x, self.min_y) 136 137 def expand_to_include(self, *args): 138 """ 139 Modifies the envelope to expand to include the boundaries of 140 the passed-in 2-tuple (a point), 4-tuple (an extent) or 141 envelope. 142 """ 143 # We provide a number of different signatures for this method, 144 # and the logic here is all about converting them into a 145 # 4-tuple single parameter which does the actual work of 146 # expanding the envelope. 147 if len(args) == 1: 148 if isinstance(args[0], Envelope): 149 return self.expand_to_include(args[0].tuple) 150 elif isinstance(args[0], Point): 151 return self.expand_to_include(args[0].x, args[0].y, args[0].x, args[0].y) 152 elif isinstance(args[0], (TupleType, ListType)): 153 # A tuple was passed in. 154 if len(args[0]) == 2: 155 return self.expand_to_include((args[0][0], args[0][1], args[0][0], args[0][1])) 156 elif len(args[0]) == 4: 157 (minx, miny, maxx, maxy) = args[0] 158 if minx < self._envelope.MinX: 159 self._envelope.MinX = minx 160 if miny < self._envelope.MinY: 161 self._envelope.MinY = miny 162 if maxx > self._envelope.MaxX: 163 self._envelope.MaxX = maxx 164 if maxy > self._envelope.MaxY: 165 self._envelope.MaxY = maxy 166 else: 167 raise OGRException('Incorrect number of tuple elements (%d).' % len(args[0])) 168 else: 169 raise TypeError('Incorrect type of argument: %s' % str(type(args[0]))) 170 elif len(args) == 2: 171 # An x and an y parameter were passed in 172 return self.expand_to_include((args[0], args[1], args[0], args[1])) 173 elif len(args) == 4: 174 # Individiual parameters passed in. 175 return self.expand_to_include(args) 176 else: 177 raise OGRException('Incorrect number (%d) of arguments.' % len(args[0]))