bound.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. # 一个简单的二维向量 Vector2
  2. class V2:
  3. def __init__(self, x, y):
  4. self.x = x
  5. self.y = y
  6. pass
  7. pass
  8. # 一个简单的范围碰撞计算类
  9. class R2:
  10. def __init__(self, minimum, maximum):
  11. self._data = (minimum, maximum)
  12. pass
  13. @property
  14. def min(self): return self._data[0]
  15. @property
  16. def max(self): return self._data[1]
  17. @property
  18. def length(self): return self._data[1] - self._data[0]
  19. def intersect(self, other):
  20. if self.min > other.max or self.max < other.min:
  21. return None
  22. minimum = self.min if self.min > other.min else other.min
  23. maximum = self.max if self.max < other.max else other.max
  24. return R2(minimum, maximum)
  25. pass
  26. class Bound:
  27. class CartesianLH:
  28. @staticmethod
  29. def top (r): return r.min
  30. @staticmethod
  31. def bottom(r): return r.max
  32. pass
  33. class CartesianRH:
  34. @staticmethod
  35. def top (r): return r.max
  36. @staticmethod
  37. def bottom(r): return r.min
  38. pass
  39. def __init__(self, x_min, x_max, y_min, y_max, cartesian):
  40. self._rx = R2(x_min, x_max)
  41. self._ry = R2(y_min, y_max)
  42. self._cartesian = cartesian
  43. pass
  44. @property
  45. def left (self): return self._rx.min
  46. @property
  47. def right (self): return self._rx.max
  48. @property
  49. def top (self): return self._cartesian.top (self._ry)
  50. @property
  51. def bottom(self): return self._cartesian.bottom(self._ry)
  52. @property
  53. def west (self): return self.left
  54. @property
  55. def east (self): return self.right
  56. @property
  57. def north(self): return self.top
  58. @property
  59. def south(self): return self.bottom
  60. @property
  61. def x(self): return self._rx.min
  62. @property
  63. def y(self): return self._ry.min
  64. @property
  65. def width (self): return self._rx.length
  66. @property
  67. def height(self): return self._ry.length
  68. @property
  69. def value(self):
  70. return self.west, self.south, self.east, self.north
  71. @property
  72. def coordinates(self):
  73. return \
  74. [
  75. [self.west, self.south],
  76. [self.east, self.south],
  77. [self.east, self.north],
  78. [self.west, self.north],
  79. [self.west, self.south]
  80. ]
  81. def intersect(self, other):
  82. other: Bound = other
  83. ix = self._rx.intersect(other._rx)
  84. iy = self._ry.intersect(other._ry)
  85. if ix is None or iy is None:
  86. return None
  87. return Bound(ix.min, ix.max, iy.min, iy.max, self._cartesian)
  88. @staticmethod
  89. def LH(left, top, right, bottom):
  90. return Bound(left, right, top, bottom, Bound.CartesianLH)
  91. @staticmethod
  92. def RH(left, bottom, right, top):
  93. return Bound(left, right, bottom, top, Bound.CartesianRH)
  94. @staticmethod
  95. def LH_XYWH(x, y, width, height):
  96. return Bound.LH(x, y, x + width, y + height)
  97. @staticmethod
  98. def RH_XYWH(x, y, width, height):
  99. return Bound.RH(x, y, x + width, y + height)
  100. @staticmethod
  101. def from_transform(width: int, height: int, transform):
  102. """
  103. 使用从 GDAL 中获取的信息构造边界对象
  104. :param width: 图像宽度
  105. :param height: 图像高度
  106. :param transform: 空间变换数据
  107. :return: 边界对象
  108. """
  109. rx = transform[1]
  110. ry = transform[5]
  111. west = transform[0]
  112. north = transform[3]
  113. east = west + width * rx
  114. south = north + height * ry
  115. return Bound.RH(west, south, east, north)
  116. pass