django/contrib/gis/geos/tests/pymutable_geometries.py
branchtrunk
changeset 138 466bece04a15
child 354 3fcbf6e3e599
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/django/contrib/gis/geos/tests/pymutable_geometries.py	Sat Jan 31 15:18:50 2009 -0600
     1.3 @@ -0,0 +1,184 @@
     1.4 +from django.contrib.gis.geos import *
     1.5 +from random import random
     1.6 +
     1.7 +SEQ_LENGTH = 10
     1.8 +SEQ_RANGE = (-1 * SEQ_LENGTH, SEQ_LENGTH)
     1.9 +SEQ_BOUNDS = (-1 * SEQ_LENGTH, -1, 0, SEQ_LENGTH - 1)
    1.10 +SEQ_OUT_OF_BOUNDS = (-1 * SEQ_LENGTH -1 , SEQ_LENGTH)
    1.11 +
    1.12 +def seqrange(): return xrange(*SEQ_RANGE)
    1.13 +
    1.14 +def random_coord(dim    = 2,           # coordinate dimensions
    1.15 +                 rng    = (-50,50),    # coordinate range
    1.16 +                 num_type     = float,
    1.17 +                 round_coords = True):
    1.18 +
    1.19 +    if round_coords:
    1.20 +        num = lambda: num_type(round(random() * (rng[1]-rng[0]) + rng[0]))
    1.21 +    else:
    1.22 +        num = lambda: num_type(random() * (rng[1]-rng[0]) + rng[0])
    1.23 +    
    1.24 +    return tuple( num() for axis in xrange(dim) )
    1.25 +
    1.26 +def random_list(length = SEQ_LENGTH, ring = False, **kwargs):
    1.27 +    result  = [ random_coord(**kwargs) for index in xrange(length) ]
    1.28 +    if ring:
    1.29 +        result[-1] = result[0]
    1.30 +
    1.31 +    return result
    1.32 +
    1.33 +random_list.single = random_coord
    1.34 +
    1.35 +def random_coll(count = SEQ_LENGTH, **kwargs):
    1.36 +    return [ tuple(random_list(**kwargs)) for i in xrange(count) ]
    1.37 +
    1.38 +random_coll.single = random_list
    1.39 +
    1.40 +class PyMutTestGeom:
    1.41 +    "The Test Geometry class container."
    1.42 +    def __init__(self, geom_type, coords_fcn=random_list, subtype=tuple, **kwargs):
    1.43 +        self.geom_type  = geom_type
    1.44 +        self.subtype    = subtype
    1.45 +        self.coords_fcn = coords_fcn
    1.46 +        self.fcn_args   = kwargs
    1.47 +        self.coords = self.coords_fcn(**kwargs)
    1.48 +        self.geom   = self.make_geom()
    1.49 +
    1.50 +    def newitem(self, **kwargs):
    1.51 +        a = self.coords_fcn.single(**kwargs)
    1.52 +        return self.subtype(a), tuple(a)
    1.53 +
    1.54 +    @property
    1.55 +    def tuple_coords(self):
    1.56 +        return tuple(self.coords)
    1.57 +
    1.58 +    def make_geom(self):
    1.59 +        return self.geom_type(map(self.subtype,self.coords))
    1.60 +
    1.61 +
    1.62 +def slice_geometries(ring=True): 
    1.63 +    testgeoms = [
    1.64 +        PyMutTestGeom(LineString),
    1.65 +        PyMutTestGeom(MultiPoint, subtype=Point),
    1.66 +        PyMutTestGeom(MultiLineString, coords_fcn=random_coll, subtype=LineString),
    1.67 +        ]
    1.68 +    if ring:
    1.69 +        testgeoms.append(PyMutTestGeom(LinearRing, ring=True))
    1.70 +
    1.71 +    return testgeoms
    1.72 +
    1.73 +def getslice_functions(): 
    1.74 +    def gs_01(x): x[0:4],   
    1.75 +    def gs_02(x): x[5:-1],  
    1.76 +    def gs_03(x): x[6:2:-1],
    1.77 +    def gs_04(x): x[:],     
    1.78 +    def gs_05(x): x[:3],    
    1.79 +    def gs_06(x): x[::2],   
    1.80 +    def gs_07(x): x[::-4],  
    1.81 +    def gs_08(x): x[7:7],   
    1.82 +    def gs_09(x): x[20:],   
    1.83 +
    1.84 +    # don't really care about ringy-ness here
    1.85 +    return mark_ring(vars(), 'gs_')
    1.86 +
    1.87 +def delslice_functions():
    1.88 +    def ds_01(x): del x[0:4]   
    1.89 +    def ds_02(x): del x[5:-1]  
    1.90 +    def ds_03(x): del x[6:2:-1]
    1.91 +    def ds_04(x): del x[:]     # should this be allowed?
    1.92 +    def ds_05(x): del x[:3]    
    1.93 +    def ds_06(x): del x[1:9:2]   
    1.94 +    def ds_07(x): del x[::-4]  
    1.95 +    def ds_08(x): del x[7:7]   
    1.96 +    def ds_09(x): del x[-7:-2]
    1.97 +
    1.98 +    return mark_ring(vars(), 'ds_')
    1.99 +
   1.100 +def setslice_extended_functions(g):
   1.101 +    a = g.coords_fcn(3, rng=(100,150))
   1.102 +    def maptype(x,a):
   1.103 +        if isinstance(x, list): return a
   1.104 +        else: return map(g.subtype, a)
   1.105 +
   1.106 +    def sse_00(x): x[:3:1]      = maptype(x, a)
   1.107 +    def sse_01(x): x[0:3:1]     = maptype(x, a)
   1.108 +    def sse_02(x): x[2:5:1]     = maptype(x, a)
   1.109 +    def sse_03(x): x[-3::1]     = maptype(x, a)
   1.110 +    def sse_04(x): x[-4:-1:1]   = maptype(x, a)
   1.111 +    def sse_05(x): x[8:5:-1]    = maptype(x, a)
   1.112 +    def sse_06(x): x[-6:-9:-1]  = maptype(x, a)
   1.113 +    def sse_07(x): x[:8:3]      = maptype(x, a)
   1.114 +    def sse_08(x): x[1::3]      = maptype(x, a)
   1.115 +    def sse_09(x): x[-2::-3]    = maptype(x, a)
   1.116 +    def sse_10(x): x[7:1:-2]    = maptype(x, a)
   1.117 +    def sse_11(x): x[2:8:2]     = maptype(x, a)
   1.118 +
   1.119 +    return mark_ring(vars(), 'sse_')
   1.120 +
   1.121 +def setslice_simple_functions(g):
   1.122 +    a = g.coords_fcn(3, rng=(100,150))
   1.123 +    def maptype(x,a):
   1.124 +        if isinstance(x, list): return a
   1.125 +        else: return map(g.subtype, a)
   1.126 +
   1.127 +    def ss_00(x): x[:0]  = maptype(x, a)
   1.128 +    def ss_01(x): x[:1]  = maptype(x, a)
   1.129 +    def ss_02(x): x[:2]  = maptype(x, a)
   1.130 +    def ss_03(x): x[:3]  = maptype(x, a)
   1.131 +    def ss_04(x): x[-4:] = maptype(x, a)
   1.132 +    def ss_05(x): x[-3:] = maptype(x, a)
   1.133 +    def ss_06(x): x[-2:] = maptype(x, a)
   1.134 +    def ss_07(x): x[-1:] = maptype(x, a)
   1.135 +    def ss_08(x): x[5:]  = maptype(x, a)
   1.136 +    def ss_09(x): x[:]   = maptype(x, a)
   1.137 +    def ss_10(x): x[4:4] = maptype(x, a)
   1.138 +    def ss_11(x): x[4:5] = maptype(x, a)
   1.139 +    def ss_12(x): x[4:7] = maptype(x, a)
   1.140 +    def ss_13(x): x[4:8] = maptype(x, a)
   1.141 +    def ss_14(x): x[10:] = maptype(x, a)
   1.142 +    def ss_15(x): x[20:30]  = maptype(x, a)
   1.143 +    def ss_16(x): x[-13:-8] = maptype(x, a)
   1.144 +    def ss_17(x): x[-13:-9] = maptype(x, a)
   1.145 +    def ss_18(x): x[-13:-10] = maptype(x, a)
   1.146 +    def ss_19(x): x[-13:-11] = maptype(x, a)
   1.147 +
   1.148 +    return mark_ring(vars(), 'ss_')
   1.149 +
   1.150 +def test_geos_functions():
   1.151 +
   1.152 +    return (
   1.153 +        lambda x: x.num_coords,
   1.154 +        lambda x: x.empty,
   1.155 +        lambda x: x.valid,
   1.156 +        lambda x: x.simple,
   1.157 +        lambda x: x.ring,
   1.158 +        lambda x: x.boundary,
   1.159 +        lambda x: x.convex_hull,
   1.160 +        lambda x: x.extend,
   1.161 +        lambda x: x.area,
   1.162 +        lambda x: x.length,
   1.163 +            )
   1.164 +
   1.165 +def mark_ring(locals, name_pat, length=SEQ_LENGTH):
   1.166 +    '''
   1.167 +    Accepts an array of functions which perform slice modifications
   1.168 +    and labels each function as to whether or not it preserves ring-ness
   1.169 +    '''
   1.170 +    func_array = [ val for name, val in locals.items()
   1.171 +                    if hasattr(val, '__call__')
   1.172 +                    and name.startswith(name_pat) ]
   1.173 +
   1.174 +    for i in xrange(len(func_array)):
   1.175 +        a = range(length)
   1.176 +        a[-1] = a[0]
   1.177 +        func_array[i](a)
   1.178 +        ring = len(a) == 0 or (len(a) > 3 and a[-1] == a[0])
   1.179 +        func_array[i].ring = ring
   1.180 +
   1.181 +    return func_array
   1.182 +
   1.183 +def getcoords(o):
   1.184 +    if hasattr(o, 'coords'):
   1.185 +        return o.coords
   1.186 +    else:
   1.187 +        return o