RelateOp.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import RelateComputer from './RelateComputer'
  2. import GeometryGraphOperation from '../GeometryGraphOperation'
  3. import RectangleContains from '../predicate/RectangleContains'
  4. import RectangleIntersects from '../predicate/RectangleIntersects'
  5. export default class RelateOp extends GeometryGraphOperation {
  6. constructor() {
  7. super()
  8. RelateOp.constructor_.apply(this, arguments)
  9. }
  10. static constructor_() {
  11. this._relate = null
  12. if (arguments.length === 2) {
  13. const g0 = arguments[0], g1 = arguments[1]
  14. GeometryGraphOperation.constructor_.call(this, g0, g1)
  15. this._relate = new RelateComputer(this._arg)
  16. } else if (arguments.length === 3) {
  17. const g0 = arguments[0], g1 = arguments[1], boundaryNodeRule = arguments[2]
  18. GeometryGraphOperation.constructor_.call(this, g0, g1, boundaryNodeRule)
  19. this._relate = new RelateComputer(this._arg)
  20. }
  21. }
  22. static covers(g1, g2) {
  23. if (g2.getDimension() === 2 && g1.getDimension() < 2)
  24. return false
  25. if (g2.getDimension() === 1 && g1.getDimension() < 1 && g2.getLength() > 0.0)
  26. return false
  27. if (!g1.getEnvelopeInternal().covers(g2.getEnvelopeInternal())) return false
  28. if (g1.isRectangle())
  29. return true
  30. return new RelateOp(g1, g2).getIntersectionMatrix().isCovers()
  31. }
  32. static intersects(g1, g2) {
  33. if (!g1.getEnvelopeInternal().intersects(g2.getEnvelopeInternal())) return false
  34. if (g1.isRectangle())
  35. return RectangleIntersects.intersects(g1, g2)
  36. if (g2.isRectangle())
  37. return RectangleIntersects.intersects(g2, g1)
  38. if (g1.isGeometryCollection() || g2.isGeometryCollection()) {
  39. const r = false
  40. for (let i = 0; i < g1.getNumGeometries(); i++)
  41. for (let j = 0; j < g2.getNumGeometries(); j++)
  42. if (g1.getGeometryN(i).intersects(g2.getGeometryN(j)))
  43. return true
  44. return false
  45. }
  46. return new RelateOp(g1, g2).getIntersectionMatrix().isIntersects()
  47. }
  48. static touches(g1, g2) {
  49. if (!g1.getEnvelopeInternal().intersects(g2.getEnvelopeInternal())) return false
  50. return new RelateOp(g1, g2).getIntersectionMatrix().isTouches(g1.getDimension(), g2.getDimension())
  51. }
  52. static equalsTopo(g1, g2) {
  53. if (!g1.getEnvelopeInternal().equals(g2.getEnvelopeInternal())) return false
  54. return RelateOp.relate(g1, g2).isEquals(g1.getDimension(), g2.getDimension())
  55. }
  56. static relate() {
  57. if (arguments.length === 2) {
  58. const a = arguments[0], b = arguments[1]
  59. const relOp = new RelateOp(a, b)
  60. const im = relOp.getIntersectionMatrix()
  61. return im
  62. } else if (arguments.length === 3) {
  63. const a = arguments[0], b = arguments[1], boundaryNodeRule = arguments[2]
  64. const relOp = new RelateOp(a, b, boundaryNodeRule)
  65. const im = relOp.getIntersectionMatrix()
  66. return im
  67. }
  68. }
  69. static overlaps(g1, g2) {
  70. if (!g1.getEnvelopeInternal().intersects(g2.getEnvelopeInternal())) return false
  71. return new RelateOp(g1, g2).getIntersectionMatrix().isOverlaps(g1.getDimension(), g2.getDimension())
  72. }
  73. static crosses(g1, g2) {
  74. if (!g1.getEnvelopeInternal().intersects(g2.getEnvelopeInternal())) return false
  75. return new RelateOp(g1, g2).getIntersectionMatrix().isCrosses(g1.getDimension(), g2.getDimension())
  76. }
  77. static contains(g1, g2) {
  78. if (g2.getDimension() === 2 && g1.getDimension() < 2)
  79. return false
  80. if (g2.getDimension() === 1 && g1.getDimension() < 1 && g2.getLength() > 0.0)
  81. return false
  82. if (!g1.getEnvelopeInternal().contains(g2.getEnvelopeInternal())) return false
  83. if (g1.isRectangle())
  84. return RectangleContains.contains(g1, g2)
  85. return new RelateOp(g1, g2).getIntersectionMatrix().isContains()
  86. }
  87. getIntersectionMatrix() {
  88. return this._relate.computeIM()
  89. }
  90. }