[Thuban-commits] r2698 - in trunk/thuban: . Thuban/Model test

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Mon Sep 18 02:56:27 CEST 2006


Author: bernhard
Date: 2006-09-18 02:56:26 +0200 (Mon, 18 Sep 2006)
New Revision: 2698

Modified:
   trunk/thuban/ChangeLog
   trunk/thuban/NEWS
   trunk/thuban/Thuban/Model/proj.py
   trunk/thuban/test/test_proj.py
Log:
Fixed behaviour with the proj bug and python >=2.4 
when decimal_point != '.'. So de_DE locales will work fine again.


Modified: trunk/thuban/ChangeLog
===================================================================
--- trunk/thuban/ChangeLog	2006-09-18 00:45:37 UTC (rev 2697)
+++ trunk/thuban/ChangeLog	2006-09-18 00:56:26 UTC (rev 2698)
@@ -1,10 +1,16 @@
 2006-09-18 Bernhard Reiter <bernhard at intevation.de>
 
+	Fixed proj behaviour with python >=2.4 when decimal_point != '.'.
+
 	* Thuban/UI/application.py: Fixed warning dialog when gdal is missing.
 
 	* test/test_proj.py: new test_lc_numeric_robustness(). Added author
 	Bernhard Reiter and new copyright year 2006.
 
+	* Thuban/Model/proj.py: New _do_we_have_to_work_around_broken_proj()
+	and Projection.assuregoodlocale(), Projection.assureinitlocale().
+	Added author Bernhard Reiter and (c) 2006.
+
 2006-09-15 Bernhard Reiter <bernhard at intevation.de>
 
 	* README: added RXP and pyRXP as optional for tests.

Modified: trunk/thuban/NEWS
===================================================================
--- trunk/thuban/NEWS	2006-09-18 00:45:37 UTC (rev 2697)
+++ trunk/thuban/NEWS	2006-09-18 00:56:26 UTC (rev 2698)
@@ -1,5 +1,8 @@
 Changes in Thuban 1.1.0+CVS
 ===========================
+	
+ - Fixed behaviour with the proj bug and python >=2.4 
+   when decimal_point != '.'. So de_DE locales will work fine again.
 
  - Startup improved: We fail right away if the internal encoding could
    not be determined. In this case, try to set the LANGUAGE variable to

Modified: trunk/thuban/Thuban/Model/proj.py
===================================================================
--- trunk/thuban/Thuban/Model/proj.py	2006-09-18 00:45:37 UTC (rev 2697)
+++ trunk/thuban/Thuban/Model/proj.py	2006-09-18 00:56:26 UTC (rev 2698)
@@ -1,13 +1,14 @@
-# Copyright (c) 2001, 2003 by Intevation GmbH
+# Copyright (c) 2001, 2003, 2006 by Intevation GmbH
 # Authors:
 # Bernhard Herzog <bh at intevation.de>
+# Bernhard Reiter <bernhard at intevation.de>
 #
 # This program is free software under the GPL (>=v2)
 # Read the file COPYING coming with Thuban for details.
-
 __version__ = "$Revision$"
 
 from types import StringTypes
+import locale
 
 from Thuban import _
 from Thuban.Lib.connector import Publisher
@@ -21,10 +22,37 @@
 PROJ_UNITS_METERS  = 1
 PROJ_UNITS_DEGREES = 2
 
+def _do_we_have_to_work_around_broken_proj():
+    """ If we have a problematic locale, check if proj results are good. """
+    if locale.localeconv()['decimal_point'] != '.':
+        params = ["proj=latlong", "to_meter=0.01745", "ellps=clrk66"]
+        proj = BaseProjection(params)
+        result1 = proj.Forward(1,1)
+
+        savedlocale = locale.getlocale(locale.LC_NUMERIC)
+        locale.setlocale(locale.LC_NUMERIC, "C")
+
+        proj = BaseProjection(params)
+        result2 = proj.Forward(1,1)
+
+        locale.setlocale(locale.LC_NUMERIC, savedlocale)
+        if result1 != result2:
+            return True
+    return False
+
 class Projection(BaseProjection):
+    """A proj4 projection object that remembers the parameters.
 
-    """A proj4 projection object that remembers the parameters"""
+    Note: it seems that calling
+        self.assuregoodlocale()
+        self.assureinitlocale()
+    before BaseProjection.__init__() is enough to work around the bug.
 
+    We assuming that the locale stays the same after a projection
+    has been initialised 
+    and thus we can return to it in self.assureinitlocale().
+    """
+
     def __init__(self, params, name = None, epsg = None):
         """Initialize the Projection
 
@@ -32,12 +60,17 @@
 
         params -- a list of 'parameter=value' strings
 
-        name -- (optional) The name of the projectin. If None or omitted
+        name -- (optional) The name of the projection. If None or omitted
                 it defaults to 'Unknown' in the local language.
 
         epsg -- (optional) The EPSG code as a string.
         """
+        self.initlocale = locale.getlocale(locale.LC_NUMERIC)
+        self.work_around_broken_proj = _do_we_have_to_work_around_broken_proj()
+
+        self.assuregoodlocale()
         BaseProjection.__init__(self, params)
+        self.assureinitlocale()
 
         if name is None:
             self.name = _("Unknown")
@@ -47,6 +80,14 @@
         self.epsg = epsg
         self.params = params
 
+    def assuregoodlocale(self):
+        if self.work_around_broken_proj:
+            locale.setlocale(locale.LC_NUMERIC, "C")
+
+    def assureinitlocale(self):
+        if self.work_around_broken_proj:
+            locale.setlocale(locale.LC_NUMERIC, self.initlocale)
+
     def _transform_bbox(self, trafo, bbox):
         # This is not really the correct way to determine the bbox of a
         # projected bbox, but for now it works well enough

Modified: trunk/thuban/test/test_proj.py
===================================================================
--- trunk/thuban/test/test_proj.py	2006-09-18 00:45:37 UTC (rev 2697)
+++ trunk/thuban/test/test_proj.py	2006-09-18 00:56:26 UTC (rev 2698)
@@ -1,6 +1,7 @@
-# Copyright (c) 2002, 2003 by Intevation GmbH
+# Copyright (c) 2002, 2003, 2006 by Intevation GmbH
 # Authors:
 # Bernhard Herzog <bh at intevation.de>
+# Bernhard Reiter <bernhard at intevation.de>
 #
 # This program is free software under the GPL (>=v2)
 # Read the file COPYING coming with Thuban for details.
@@ -114,11 +115,12 @@
                     "No locale with comma as decimal_point found.")
 
         proj = Projection(params)
-        result1 =  proj.Forward(1,1)
+        #print proj.work_around_broken_proj
+        result1 =  proj.Forward(1.2,3.2)
 
         locale.setlocale(locale.LC_NUMERIC, "C")
         proj = Projection(params)
-        result2= proj.Forward(1,1)
+        result2= proj.Forward(1.2,3.2)
 
         locale.setlocale(locale.LC_NUMERIC, oldlocale)
         self.assertFloatSeqEqual(result1, result2, epsilon = 1e-5 )



More information about the Thuban-commits mailing list