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