Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #17736 -- Kept maximal floating-point accuracy in from_bbox

When constructing a polygon with Polygon.from_bbox, do not convert
parameters to strings at this stage (str defaults to 12 significant
digits).
Thanks tdihp@hotmail.com for the report and David Eklung for the patch.
  • Loading branch information...
commit 17824e2b74ca02b9914e853c818fa512a0f9ef34 1 parent fedac99
Claude Paroz authored June 06, 2012
1  AUTHORS
@@ -171,6 +171,7 @@ answer newbie questions, and generally made Django that much better:
171 171
     Clint Ecker
172 172
     Nick Efford <nick@efford.org>
173 173
     eibaan@gmail.com
  174
+    David Eklund
174 175
     Julia Elman
175 176
     enlight
176 177
     Enrico <rico.bl@gmail.com>
7  django/contrib/gis/geos/polygon.py
@@ -55,8 +55,11 @@ def __len__(self):
55 55
     def from_bbox(cls, bbox):
56 56
         "Constructs a Polygon from a bounding box (4-tuple)."
57 57
         x0, y0, x1, y1 = bbox
58  
-        return GEOSGeometry( 'POLYGON((%s %s, %s %s, %s %s, %s %s, %s %s))' %  (
59  
-                x0, y0, x0, y1, x1, y1, x1, y0, x0, y0) )
  58
+        for z in bbox:
  59
+            if not isinstance(z, (int, long, float)):
  60
+                return GEOSGeometry('POLYGON((%s %s, %s %s, %s %s, %s %s, %s %s))' %
  61
+                                    (x0, y0, x0, y1, x1, y1, x1, y0, x0, y0))
  62
+        return Polygon(((x0, y0), (x0, y1), (x1, y1), (x1, y0), (x0, y0)))
60 63
 
61 64
     ### These routines are needed for list-like operation w/ListMixin ###
62 65
     def _create_polygon(self, length, items):
7  django/contrib/gis/geos/tests/test_geos.py
@@ -384,6 +384,13 @@ def test_polygons_from_bbox(self):
384 384
         p = Polygon.from_bbox(bbox)
385 385
         self.assertEqual(bbox, p.extent)
386 386
 
  387
+        # Testing numerical precision
  388
+        x = 3.14159265358979323
  389
+        bbox = (0, 0, 1, x)
  390
+        p = Polygon.from_bbox(bbox)
  391
+        y = p.extent[-1]
  392
+        self.assertEqual(format(x, '.13f'), format(y, '.13f'))
  393
+
387 394
     def test_polygons(self):
388 395
         "Testing Polygon objects."
389 396
 

0 notes on commit 17824e2

Please sign in to comment.
Something went wrong with that request. Please try again.