PageRenderTime 27ms CodeModel.GetById 14ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/django/contrib/gis/gdal/geomtype.py

https://code.google.com/p/mango-py/
Python | 85 lines | 67 code | 10 blank | 8 comment | 10 complexity | ec4b720e19d1f3799d50171dc8d699df MD5 | raw file
 1from django.contrib.gis.gdal.error import OGRException
 2
 3#### OGRGeomType ####
 4class OGRGeomType(object):
 5    "Encapulates OGR Geometry Types."
 6
 7    wkb25bit = -2147483648
 8
 9    # Dictionary of acceptable OGRwkbGeometryType s and their string names.
10    _types = {0 : 'Unknown',
11              1 : 'Point',
12              2 : 'LineString',
13              3 : 'Polygon',
14              4 : 'MultiPoint',
15              5 : 'MultiLineString',
16              6 : 'MultiPolygon',
17              7 : 'GeometryCollection',
18              100 : 'None',
19              101 : 'LinearRing',
20              1 + wkb25bit: 'Point25D',
21              2 + wkb25bit: 'LineString25D',
22              3 + wkb25bit: 'Polygon25D',
23              4 + wkb25bit: 'MultiPoint25D',
24              5 + wkb25bit : 'MultiLineString25D',
25              6 + wkb25bit : 'MultiPolygon25D',
26              7 + wkb25bit : 'GeometryCollection25D',
27              }
28    # Reverse type dictionary, keyed by lower-case of the name.
29    _str_types = dict([(v.lower(), k) for k, v in _types.items()])
30
31    def __init__(self, type_input):
32        "Figures out the correct OGR Type based upon the input."
33        if isinstance(type_input, OGRGeomType):
34            num = type_input.num
35        elif isinstance(type_input, basestring):
36            type_input = type_input.lower()
37            if type_input == 'geometry': type_input='unknown'
38            num = self._str_types.get(type_input, None)
39            if num is None:
40                raise OGRException('Invalid OGR String Type "%s"' % type_input)
41        elif isinstance(type_input, int):
42            if not type_input in self._types:
43                raise OGRException('Invalid OGR Integer Type: %d' % type_input)
44            num = type_input
45        else:
46            raise TypeError('Invalid OGR input type given.')
47        
48        # Setting the OGR geometry type number.
49        self.num = num
50
51    def __str__(self):
52        "Returns the value of the name property."
53        return self.name
54
55    def __eq__(self, other):
56        """
57        Does an equivalence test on the OGR type with the given
58        other OGRGeomType, the short-hand string, or the integer.
59        """
60        if isinstance(other, OGRGeomType):
61            return self.num == other.num
62        elif isinstance(other, basestring):
63            return self.name.lower() == other.lower()
64        elif isinstance(other, int):
65            return self.num == other
66        else:
67            return False
68
69    def __ne__(self, other):
70        return not (self == other)
71
72    @property
73    def name(self):
74        "Returns a short-hand string form of the OGR Geometry type."
75        return self._types[self.num]
76
77    @property
78    def django(self):
79        "Returns the Django GeometryField for this OGR Type."
80        s = self.name.replace('25D', '')
81        if s in ('LinearRing', 'None'):
82            return None
83        elif s == 'Unknown':
84            s = 'Geometry'
85        return s + 'Field'