Node.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import NodeBase from './NodeBase'
  2. import Envelope from '../../geom/Envelope'
  3. import Assert from '../../util/Assert'
  4. import Key from './Key'
  5. export default class Node extends NodeBase {
  6. constructor() {
  7. super()
  8. Node.constructor_.apply(this, arguments)
  9. }
  10. static constructor_() {
  11. this._env = null
  12. this._centrex = null
  13. this._centrey = null
  14. this._level = null
  15. const env = arguments[0], level = arguments[1]
  16. this._env = env
  17. this._level = level
  18. this._centrex = (env.getMinX() + env.getMaxX()) / 2
  19. this._centrey = (env.getMinY() + env.getMaxY()) / 2
  20. }
  21. static createNode(env) {
  22. const key = new Key(env)
  23. const node = new Node(key.getEnvelope(), key.getLevel())
  24. return node
  25. }
  26. static createExpanded(node, addEnv) {
  27. const expandEnv = new Envelope(addEnv)
  28. if (node !== null) expandEnv.expandToInclude(node._env)
  29. const largerNode = Node.createNode(expandEnv)
  30. if (node !== null) largerNode.insertNode(node)
  31. return largerNode
  32. }
  33. find(searchEnv) {
  34. const subnodeIndex = NodeBase.getSubnodeIndex(searchEnv, this._centrex, this._centrey)
  35. if (subnodeIndex === -1) return this
  36. if (this._subnode[subnodeIndex] !== null) {
  37. const node = this._subnode[subnodeIndex]
  38. return node.find(searchEnv)
  39. }
  40. return this
  41. }
  42. isSearchMatch(searchEnv) {
  43. if (searchEnv === null) return false
  44. return this._env.intersects(searchEnv)
  45. }
  46. getSubnode(index) {
  47. if (this._subnode[index] === null)
  48. this._subnode[index] = this.createSubnode(index)
  49. return this._subnode[index]
  50. }
  51. getEnvelope() {
  52. return this._env
  53. }
  54. getNode(searchEnv) {
  55. const subnodeIndex = NodeBase.getSubnodeIndex(searchEnv, this._centrex, this._centrey)
  56. if (subnodeIndex !== -1) {
  57. const node = this.getSubnode(subnodeIndex)
  58. return node.getNode(searchEnv)
  59. } else {
  60. return this
  61. }
  62. }
  63. createSubnode(index) {
  64. let minx = 0.0
  65. let maxx = 0.0
  66. let miny = 0.0
  67. let maxy = 0.0
  68. switch (index) {
  69. case 0:
  70. minx = this._env.getMinX()
  71. maxx = this._centrex
  72. miny = this._env.getMinY()
  73. maxy = this._centrey
  74. break
  75. case 1:
  76. minx = this._centrex
  77. maxx = this._env.getMaxX()
  78. miny = this._env.getMinY()
  79. maxy = this._centrey
  80. break
  81. case 2:
  82. minx = this._env.getMinX()
  83. maxx = this._centrex
  84. miny = this._centrey
  85. maxy = this._env.getMaxY()
  86. break
  87. case 3:
  88. minx = this._centrex
  89. maxx = this._env.getMaxX()
  90. miny = this._centrey
  91. maxy = this._env.getMaxY()
  92. break
  93. }
  94. const sqEnv = new Envelope(minx, maxx, miny, maxy)
  95. const node = new Node(sqEnv, this._level - 1)
  96. return node
  97. }
  98. insertNode(node) {
  99. Assert.isTrue(this._env === null || this._env.contains(node._env))
  100. const index = NodeBase.getSubnodeIndex(node._env, this._centrex, this._centrey)
  101. if (node._level === this._level - 1) {
  102. this._subnode[index] = node
  103. } else {
  104. const childNode = this.createSubnode(index)
  105. childNode.insertNode(node)
  106. this._subnode[index] = childNode
  107. }
  108. }
  109. }