Ticket #10368: gis_envelope_expand.diff

File gis_envelope_expand.diff, 5.2 KB (added by psmith, 16 years ago)

Patch to add expand_to_include to Envelope (with unit tests)

  • django/contrib/gis/tests/test_gdal_envelope.py

     
    11import unittest
    22from django.contrib.gis.gdal import Envelope, OGRException
     3from django.contrib.gis.geos import Point
    34
    45class EnvelopeTest(unittest.TestCase):
     6    def setUp(self):
     7        self.e = Envelope(0, 0, 5, 5)
    58
    69    def test01_init(self):
    710        "Testing Envelope initilization."
     
    3639        self.assertEqual(e1, e2)
    3740        self.assertEqual((0.523, 0.217, 253.23, 523.69), e1)
    3841
     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
    3978def suite():
    4079    s = unittest.TestSuite()
    4180    s.addTest(unittest.makeSuite(EnvelopeTest))
  • django/contrib/gis/gdal/envelope.py

     
    1313from ctypes import Structure, c_double
    1414from types import TupleType, ListType
    1515from django.contrib.gis.gdal.error import OGRException
     16from django.contrib.gis.geos import Point
    1617
    1718# The OGR definition of an Envelope is a C structure containing four doubles.
    1819#  See the 'ogr_core.h' source file for more information:
     
    132133               (self.min_x, self.min_y, self.min_x, self.max_y,
    133134                self.max_x, self.max_y, self.max_x, self.min_y,
    134135                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]))
Back to Top