RectangleContains.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import LineString from '../../geom/LineString'
  2. import Coordinate from '../../geom/Coordinate'
  3. import Point from '../../geom/Point'
  4. import Polygon from '../../geom/Polygon'
  5. export default class RectangleContains {
  6. constructor() {
  7. RectangleContains.constructor_.apply(this, arguments)
  8. }
  9. static constructor_() {
  10. this._rectEnv = null
  11. const rectangle = arguments[0]
  12. this._rectEnv = rectangle.getEnvelopeInternal()
  13. }
  14. static contains(rectangle, b) {
  15. const rc = new RectangleContains(rectangle)
  16. return rc.contains(b)
  17. }
  18. isContainedInBoundary(geom) {
  19. if (geom instanceof Polygon) return false
  20. if (geom instanceof Point) return this.isPointContainedInBoundary(geom)
  21. if (geom instanceof LineString) return this.isLineStringContainedInBoundary(geom)
  22. for (let i = 0; i < geom.getNumGeometries(); i++) {
  23. const comp = geom.getGeometryN(i)
  24. if (!this.isContainedInBoundary(comp)) return false
  25. }
  26. return true
  27. }
  28. isLineSegmentContainedInBoundary(p0, p1) {
  29. if (p0.equals(p1)) return this.isPointContainedInBoundary(p0)
  30. if (p0.x === p1.x) {
  31. if (p0.x === this._rectEnv.getMinX() || p0.x === this._rectEnv.getMaxX()) return true
  32. } else if (p0.y === p1.y) {
  33. if (p0.y === this._rectEnv.getMinY() || p0.y === this._rectEnv.getMaxY()) return true
  34. }
  35. return false
  36. }
  37. isLineStringContainedInBoundary(line) {
  38. const seq = line.getCoordinateSequence()
  39. const p0 = new Coordinate()
  40. const p1 = new Coordinate()
  41. for (let i = 0; i < seq.size() - 1; i++) {
  42. seq.getCoordinate(i, p0)
  43. seq.getCoordinate(i + 1, p1)
  44. if (!this.isLineSegmentContainedInBoundary(p0, p1)) return false
  45. }
  46. return true
  47. }
  48. isPointContainedInBoundary() {
  49. if (arguments[0] instanceof Point) {
  50. const point = arguments[0]
  51. return this.isPointContainedInBoundary(point.getCoordinate())
  52. } else if (arguments[0] instanceof Coordinate) {
  53. const pt = arguments[0]
  54. return pt.x === this._rectEnv.getMinX() || pt.x === this._rectEnv.getMaxX() || pt.y === this._rectEnv.getMinY() || pt.y === this._rectEnv.getMaxY()
  55. }
  56. }
  57. contains(geom) {
  58. if (!this._rectEnv.contains(geom.getEnvelopeInternal())) return false
  59. if (this.isContainedInBoundary(geom)) return false
  60. return true
  61. }
  62. }