Ticket #17959: 17959-1.diff
File 17959-1.diff, 12.8 KB (added by , 13 years ago) |
---|
-
django/contrib/gis/gdal/libgdal.py
diff --git a/django/contrib/gis/gdal/libgdal.py b/django/contrib/gis/gdal/libgdal.py index f991388..9bd775d 100644
a b 1 import logging 1 2 import os 2 3 import re 3 from ctypes import c_char_p, CDLL4 from ctypes import c_char_p, c_int, CDLL, CFUNCTYPE 4 5 from ctypes.util import find_library 5 6 from django.contrib.gis.gdal.error import OGRException 6 7 8 # Set module logging 9 logger = logging.getLogger('django.contrib.gis') 10 if not logger.handlers: 11 ch = logging.StreamHandler() 12 logger.addHandler(ch) 13 7 14 # Custom library path set? 8 15 try: 9 16 from django.conf import settings … … if GDAL_VERSION >= (1, 5): 103 110 else: 104 111 GEOJSON = False 105 112 113 CPLErrorHandler = CFUNCTYPE(None, c_int, c_int, c_char_p) 114 def err_handler(error_class, error_number, message): 115 logger.error('GDAL_ERROR %d: %s' % (error_number, message)) 116 err_handler = CPLErrorHandler(err_handler) 117 118 def function(name, args, restype): 119 func = std_call(name) 120 func.argtypes = args 121 func.restype = restype 122 return func 123 124 set_error_handler = function('CPLSetErrorHandler', [CPLErrorHandler], CPLErrorHandler) 125 set_error_handler(err_handler) -
django/contrib/gis/gdal/tests/test_ds.py
diff --git a/django/contrib/gis/gdal/tests/test_ds.py b/django/contrib/gis/gdal/tests/test_ds.py index 16d6e86..11f5c01 100644
a b import unittest 3 3 from django.contrib.gis.gdal import DataSource, Envelope, OGRGeometry, OGRException, OGRIndexError, GDAL_VERSION 4 4 from django.contrib.gis.gdal.field import OFTReal, OFTInteger, OFTString 5 5 from django.contrib.gis.geometry.test_data import get_ds_file, TestDS, TEST_DATA 6 from django.test.utils import capture_logging 6 7 7 8 # List of acceptable data sources. 8 9 ds_list = (TestDS('test_point', nfeat=5, nfld=3, geom='POINT', gtype=1, driver='ESRI Shapefile', … … class DataSourceTest(unittest.TestCase): 59 60 60 61 def test03a_layers(self): 61 62 "Testing Data Source Layers." 62 print "\nBEGIN - expecting out of range feature id error; safe to ignore.\n"63 63 for source in ds_list: 64 64 ds = DataSource(source.ds) 65 65 … … class DataSourceTest(unittest.TestCase): 89 89 flds = layer.fields 90 90 for f in flds: self.assertEqual(True, f in source.fields) 91 91 92 # Negative FIDs are not allowed. 93 self.assertRaises(OGRIndexError, layer.__getitem__, -1) 94 self.assertRaises(OGRIndexError, layer.__getitem__, 50000) 92 # Negative FIDs are not allowed. Capture logs as it is expected 93 # that an 'out of range' GDAL_ERROR will be emitted 94 with capture_logging('django.contrib.gis'): 95 self.assertRaises(OGRIndexError, layer.__getitem__, -1) 96 self.assertRaises(OGRIndexError, layer.__getitem__, 50000) 95 97 96 98 if hasattr(source, 'field_values'): 97 99 fld_names = source.field_values.keys() … … class DataSourceTest(unittest.TestCase): 108 110 # the feature values here while in this loop. 109 111 for fld_name in fld_names: 110 112 self.assertEqual(source.field_values[fld_name][i], feat.get(fld_name)) 111 print "\nEND - expecting out of range feature id error; safe to ignore."112 113 113 114 def test03b_layer_slice(self): 114 115 "Test indexing and slicing on Layers." -
django/contrib/gis/gdal/tests/test_geom.py
diff --git a/django/contrib/gis/gdal/tests/test_geom.py b/django/contrib/gis/gdal/tests/test_geom.py index 656901b..ce8c0e7 100644
a b from django.contrib.gis.gdal import (OGRGeometry, OGRGeomType, OGRException, 8 8 OGRIndexError, SpatialReference, CoordTransform, GDAL_VERSION) 9 9 from django.contrib.gis.gdal.prototypes.geom import GEOJSON 10 10 from django.contrib.gis.geometry.test_data import TestDataMixin 11 from django.test.utils import capture_logging 11 12 from django.utils import unittest 12 13 13 14 class OGRGeomTest(unittest.TestCase, TestDataMixin): … … class OGRGeomTest(unittest.TestCase, TestDataMixin): 234 235 # Both rings in this geometry are not closed. 235 236 poly = OGRGeometry('POLYGON((0 0, 5 0, 5 5, 0 5), (1 1, 2 1, 2 2, 2 1))') 236 237 self.assertEqual(8, poly.point_count) 237 print "\nBEGIN - expecting IllegalArgumentException; safe to ignore.\n" 238 try: 239 c = poly.centroid 240 except OGRException: 241 # Should raise an OGR exception, rings are not closed 242 pass 243 else: 244 self.fail('Should have raised an OGRException!') 245 print "\nEND - expecting IllegalArgumentException; safe to ignore.\n" 238 239 # Capture logging as an IllegalArgumentException will be output by GDAL 240 with capture_logging('django.contrib.gis'): 241 # Rings are not closed 242 self.assertRaises(OGRException, lambda: poly.centroid) 246 243 247 244 # Closing the rings -- doesn't work on GDAL versions 1.4.1 and below: 248 245 # http://trac.osgeo.org/gdal/ticket/1673 -
django/contrib/gis/geos/libgeos.py
diff --git a/django/contrib/gis/geos/libgeos.py b/django/contrib/gis/geos/libgeos.py index a4f5adf..672f2b9 100644
a b 6 6 This module also houses GEOS Pointer utilities, including 7 7 get_pointer_arr(), and GEOM_PTR. 8 8 """ 9 import logging 9 10 import os 10 11 import re 11 import sys12 12 from ctypes import c_char_p, Structure, CDLL, CFUNCTYPE, POINTER 13 13 from ctypes.util import find_library 14 14 from django.contrib.gis.geos.error import GEOSException 15 15 16 # Set module logging 17 logger = logging.getLogger('django.contrib.gis') 18 if not logger.handlers: 19 ch = logging.StreamHandler() 20 logger.addHandler(ch) 21 16 22 # Custom library path set? 17 23 try: 18 24 from django.conf import settings … … lgeos = CDLL(lib_path) 56 62 # Supposed to mimic the GEOS message handler (C below): 57 63 # typedef void (*GEOSMessageHandler)(const char *fmt, ...); 58 64 NOTICEFUNC = CFUNCTYPE(None, c_char_p, c_char_p) 59 def notice_h(fmt, lst , output_h=sys.stdout):65 def notice_h(fmt, lst): 60 66 try: 61 67 warn_msg = fmt % lst 62 68 except: 63 69 warn_msg = fmt 64 output_h.write('GEOS_NOTICE: %s\n' % warn_msg)70 logger.warn('GEOS_NOTICE: %s' % warn_msg) 65 71 notice_h = NOTICEFUNC(notice_h) 66 72 67 73 ERRORFUNC = CFUNCTYPE(None, c_char_p, c_char_p) 68 def error_h(fmt, lst , output_h=sys.stderr):74 def error_h(fmt, lst): 69 75 try: 70 76 err_msg = fmt % lst 71 77 except: 72 78 err_msg = fmt 73 output_h.write('GEOS_ERROR: %s\n' % err_msg)79 logger.error('GEOS_ERROR: %s' % err_msg) 74 80 error_h = ERRORFUNC(error_h) 75 81 76 82 #### GEOS Geometry C data structures, and utility functions. #### -
django/contrib/gis/geos/tests/test_geos.py
diff --git a/django/contrib/gis/geos/tests/test_geos.py b/django/contrib/gis/geos/tests/test_geos.py index a8372b4..5d09be8 100644
a b 1 1 import ctypes 2 2 import random 3 3 import unittest 4 4 5 from django.contrib.gis.geos import * 5 6 from django.contrib.gis.geos.base import gdal, numpy, GEOSBase 6 7 from django.contrib.gis.geos.libgeos import GEOS_PREPARE 7 8 from django.contrib.gis.geometry.test_data import TestDataMixin 9 from django.test.utils import capture_logging 10 8 11 9 12 class GEOSTest(unittest.TestCase, TestDataMixin): 10 13 … … class GEOSTest(unittest.TestCase, TestDataMixin): 134 137 def test01d_errors(self): 135 138 "Testing the Error handlers." 136 139 # string-based 137 print "\nBEGIN - expecting GEOS_ERROR; safe to ignore.\n"138 for err in self.geometries.errors:139 try:140 g = fromstr(err.wkt)141 except (GEOSException, ValueError):142 pass143 144 # Bad WKB145 self.assertRaises(GEOSException, GEOSGeometry, buffer('0')) 146 147 print "\nEND - expecting GEOS_ERROR; safe to ignore.\n"140 with capture_logging('django.contrib.gis') as out: 141 for err in self.geometries.errors: 142 try: 143 g = fromstr(err.wkt) 144 except (GEOSException, ValueError): 145 pass 146 # Two GEOS_ERRORs: 147 self.assertEqual(len(out), 2) 148 149 # Bad WKB 150 self.assertRaises(GEOSException, GEOSGeometry, buffer('0')) 148 151 149 152 class NotAGeometry(object): 150 153 pass … … class GEOSTest(unittest.TestCase, TestDataMixin): 439 442 440 443 def test05b_multipolygons(self): 441 444 "Testing MultiPolygon objects." 442 print "\nBEGIN - expecting GEOS_NOTICE; safe to ignore.\n"443 445 prev = fromstr('POINT (0 0)') 444 for mp in self.geometries.multipolygons: 445 mpoly = fromstr(mp.wkt) 446 self.assertEqual(mpoly.geom_type, 'MultiPolygon') 447 self.assertEqual(mpoly.geom_typeid, 6) 448 self.assertEqual(mp.valid, mpoly.valid) 449 450 if mp.valid: 451 self.assertEqual(mp.num_geom, mpoly.num_geom) 452 self.assertEqual(mp.n_p, mpoly.num_coords) 453 self.assertEqual(mp.num_geom, len(mpoly)) 454 self.assertRaises(GEOSIndexError, mpoly.__getitem__, len(mpoly)) 455 for p in mpoly: 456 self.assertEqual(p.geom_type, 'Polygon') 457 self.assertEqual(p.geom_typeid, 3) 458 self.assertEqual(p.valid, True) 459 self.assertEqual(mpoly.wkt, MultiPolygon(*tuple(poly.clone() for poly in mpoly)).wkt) 460 461 print "\nEND - expecting GEOS_NOTICE; safe to ignore.\n" 446 with capture_logging('django.contrib.gis') as out: 447 for mp in self.geometries.multipolygons: 448 mpoly = fromstr(mp.wkt) 449 self.assertEqual(mpoly.geom_type, 'MultiPolygon') 450 self.assertEqual(mpoly.geom_typeid, 6) 451 self.assertEqual(mp.valid, mpoly.valid) 452 453 if mp.valid: 454 self.assertEqual(mp.num_geom, mpoly.num_geom) 455 self.assertEqual(mp.n_p, mpoly.num_coords) 456 self.assertEqual(mp.num_geom, len(mpoly)) 457 self.assertRaises(GEOSIndexError, mpoly.__getitem__, len(mpoly)) 458 for p in mpoly: 459 self.assertEqual(p.geom_type, 'Polygon') 460 self.assertEqual(p.geom_typeid, 3) 461 self.assertEqual(p.valid, True) 462 self.assertEqual(mpoly.wkt, MultiPolygon(*tuple(poly.clone() for poly in mpoly)).wkt) 463 self.assertTrue(out[0].msg, 464 'GEOS_NOTICE: Duplicate Rings at or near point 60 300') 462 465 463 466 def test06a_memory_hijinks(self): 464 467 "Testing Geometry __del__() on rings and polygons." … … class GEOSTest(unittest.TestCase, TestDataMixin): 894 897 """ Testing `transform` method (no SRID) """ 895 898 # Raise a warning if SRID <0/None. 896 899 import warnings 897 print "\nBEGIN - expecting Warnings; safe to ignore.\n"898 900 899 901 # Test for do-nothing behavior. 900 try: 901 # Keeping line-noise down by only printing the relevant 902 # warnings once. 903 warnings.simplefilter('once', UserWarning) 904 warnings.simplefilter('once', FutureWarning) 902 with warnings.catch_warnings(): 903 warnings.simplefilter("ignore") 905 904 906 905 g = GEOSGeometry('POINT (-104.609 38.255)', srid=None) 907 906 g.transform(2774) … … class GEOSTest(unittest.TestCase, TestDataMixin): 921 920 g1 = g.transform(2774, clone=True) 922 921 self.assertTrue(g1 is None) 923 922 924 finally:925 warnings.simplefilter('default', UserWarning)926 warnings.simplefilter('default', FutureWarning)927 928 print "\nEND - expecting Warnings; safe to ignore.\n"929 930 931 923 # test warning is raised 932 924 try: 933 925 warnings.simplefilter('error', FutureWarning) … … class GEOSTest(unittest.TestCase, TestDataMixin): 1039 1031 self.assertTrue(isinstance(g.valid_reason, basestring)) 1040 1032 self.assertEqual(g.valid_reason, "Valid Geometry") 1041 1033 1042 print "\nBEGIN - expecting GEOS_NOTICE; safe to ignore.\n"1043 1044 1034 g = GEOSGeometry("LINESTRING(0 0, 0 0)") 1045 1035 1046 self.assertTrue(not g.valid) 1036 with capture_logging('django.contrib.gis') as out: 1037 self.assertTrue(not g.valid) 1038 self.assertEqual(out[0].msg, 1039 'GEOS_NOTICE: Too few points in geometry component at or near point 0 0') 1047 1040 self.assertTrue(isinstance(g.valid_reason, basestring)) 1048 1041 self.assertTrue(g.valid_reason.startswith("Too few points in geometry component")) 1049 1042 1050 print "\nEND - expecting GEOS_NOTICE; safe to ignore.\n"1051 1052 1043 def test28_geos_version(self): 1053 1044 "Testing the GEOS version regular expression." 1054 1045 from django.contrib.gis.geos.libgeos import version_regex