You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

geomtype.py 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. from django.contrib.gis.gdal.error import GDALException
  2. class OGRGeomType:
  3. "Encapsulate OGR Geometry Types."
  4. wkb25bit = -2147483648
  5. # Dictionary of acceptable OGRwkbGeometryType s and their string names.
  6. _types = {
  7. 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. 102: 'PointZ',
  18. 1 + wkb25bit: 'Point25D',
  19. 2 + wkb25bit: 'LineString25D',
  20. 3 + wkb25bit: 'Polygon25D',
  21. 4 + wkb25bit: 'MultiPoint25D',
  22. 5 + wkb25bit: 'MultiLineString25D',
  23. 6 + wkb25bit: 'MultiPolygon25D',
  24. 7 + wkb25bit: 'GeometryCollection25D',
  25. }
  26. # Reverse type dictionary, keyed by lowercase of the name.
  27. _str_types = {v.lower(): k for k, v in _types.items()}
  28. def __init__(self, type_input):
  29. "Figure out the correct OGR Type based upon the input."
  30. if isinstance(type_input, OGRGeomType):
  31. num = type_input.num
  32. elif isinstance(type_input, str):
  33. type_input = type_input.lower()
  34. if type_input == 'geometry':
  35. type_input = 'unknown'
  36. num = self._str_types.get(type_input)
  37. if num is None:
  38. raise GDALException('Invalid OGR String Type "%s"' % type_input)
  39. elif isinstance(type_input, int):
  40. if type_input not in self._types:
  41. raise GDALException('Invalid OGR Integer Type: %d' % type_input)
  42. num = type_input
  43. else:
  44. raise TypeError('Invalid OGR input type given.')
  45. # Setting the OGR geometry type number.
  46. self.num = num
  47. def __str__(self):
  48. "Return the value of the name property."
  49. return self.name
  50. def __eq__(self, other):
  51. """
  52. Do an equivalence test on the OGR type with the given
  53. other OGRGeomType, the short-hand string, or the integer.
  54. """
  55. if isinstance(other, OGRGeomType):
  56. return self.num == other.num
  57. elif isinstance(other, str):
  58. return self.name.lower() == other.lower()
  59. elif isinstance(other, int):
  60. return self.num == other
  61. else:
  62. return False
  63. @property
  64. def name(self):
  65. "Return a short-hand string form of the OGR Geometry type."
  66. return self._types[self.num]
  67. @property
  68. def django(self):
  69. "Return 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. elif s == 'PointZ':
  76. s = 'Point'
  77. return s + 'Field'
  78. def to_multi(self):
  79. """
  80. Transform Point, LineString, Polygon, and their 25D equivalents
  81. to their Multi... counterpart.
  82. """
  83. if self.name.startswith(('Point', 'LineString', 'Polygon')):
  84. self.num += 3