Development of an internal social media platform with personalised dashboards for students
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.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 = {0: 'Unknown',
  7. 1: 'Point',
  8. 2: 'LineString',
  9. 3: 'Polygon',
  10. 4: 'MultiPoint',
  11. 5: 'MultiLineString',
  12. 6: 'MultiPolygon',
  13. 7: 'GeometryCollection',
  14. 100: 'None',
  15. 101: 'LinearRing',
  16. 102: 'PointZ',
  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 = {v.lower(): k for k, v in _types.items()}
  27. def __init__(self, type_input):
  28. "Figure 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, str):
  32. type_input = type_input.lower()
  33. if type_input == 'geometry':
  34. type_input = 'unknown'
  35. num = self._str_types.get(type_input)
  36. if num is None:
  37. raise GDALException('Invalid OGR String Type "%s"' % type_input)
  38. elif isinstance(type_input, int):
  39. if type_input not in self._types:
  40. raise GDALException('Invalid OGR Integer Type: %d' % type_input)
  41. num = type_input
  42. else:
  43. raise TypeError('Invalid OGR input type given.')
  44. # Setting the OGR geometry type number.
  45. self.num = num
  46. def __str__(self):
  47. "Return the value of the name property."
  48. return self.name
  49. def __eq__(self, other):
  50. """
  51. Do an equivalence test on the OGR type with the given
  52. other OGRGeomType, the short-hand string, or the integer.
  53. """
  54. if isinstance(other, OGRGeomType):
  55. return self.num == other.num
  56. elif isinstance(other, str):
  57. return self.name.lower() == other.lower()
  58. elif isinstance(other, int):
  59. return self.num == other
  60. else:
  61. return False
  62. @property
  63. def name(self):
  64. "Return a short-hand string form of the OGR Geometry type."
  65. return self._types[self.num]
  66. @property
  67. def django(self):
  68. "Return the Django GeometryField for this OGR Type."
  69. s = self.name.replace('25D', '')
  70. if s in ('LinearRing', 'None'):
  71. return None
  72. elif s == 'Unknown':
  73. s = 'Geometry'
  74. elif s == 'PointZ':
  75. s = 'Point'
  76. return s + 'Field'
  77. def to_multi(self):
  78. """
  79. Transform Point, LineString, Polygon, and their 25D equivalents
  80. to their Multi... counterpart.
  81. """
  82. if self.name.startswith(('Point', 'LineString', 'Polygon')):
  83. self.num += 3