Skip to content

Commit

Permalink
Fixed django#17736 -- Kept maximal floating-point accuracy in from_bbox
Browse files Browse the repository at this point in the history
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
claudep committed Jun 6, 2012
1 parent fedac99 commit 17824e2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -171,6 +171,7 @@ answer newbie questions, and generally made Django that much better:
Clint Ecker
Nick Efford <nick@efford.org>
eibaan@gmail.com
David Eklund
Julia Elman
enlight
Enrico <rico.bl@gmail.com>
Expand Down
7 changes: 5 additions & 2 deletions django/contrib/gis/geos/polygon.py
Expand Up @@ -55,8 +55,11 @@ def __len__(self):
def from_bbox(cls, bbox):
"Constructs a Polygon from a bounding box (4-tuple)."
x0, y0, x1, y1 = bbox
return GEOSGeometry( 'POLYGON((%s %s, %s %s, %s %s, %s %s, %s %s))' % (
x0, y0, x0, y1, x1, y1, x1, y0, x0, y0) )
for z in bbox:
if not isinstance(z, (int, long, float)):
return GEOSGeometry('POLYGON((%s %s, %s %s, %s %s, %s %s, %s %s))' %
(x0, y0, x0, y1, x1, y1, x1, y0, x0, y0))
return Polygon(((x0, y0), (x0, y1), (x1, y1), (x1, y0), (x0, y0)))

### These routines are needed for list-like operation w/ListMixin ###
def _create_polygon(self, length, items):
Expand Down
7 changes: 7 additions & 0 deletions django/contrib/gis/geos/tests/test_geos.py
Expand Up @@ -384,6 +384,13 @@ def test_polygons_from_bbox(self):
p = Polygon.from_bbox(bbox)
self.assertEqual(bbox, p.extent)

# Testing numerical precision
x = 3.14159265358979323
bbox = (0, 0, 1, x)
p = Polygon.from_bbox(bbox)
y = p.extent[-1]
self.assertEqual(format(x, '.13f'), format(y, '.13f'))

def test_polygons(self):
"Testing Polygon objects."

Expand Down

0 comments on commit 17824e2

Please sign in to comment.