RelateComputer.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import PointLocator from '../../algorithm/PointLocator'
  2. import Location from '../../geom/Location'
  3. import IntersectionMatrix from '../../geom/IntersectionMatrix'
  4. import EdgeEndBuilder from './EdgeEndBuilder'
  5. import NodeMap from '../../geomgraph/NodeMap'
  6. import RelateNodeFactory from './RelateNodeFactory'
  7. import ArrayList from '../../../../../java/util/ArrayList'
  8. import RobustLineIntersector from '../../algorithm/RobustLineIntersector'
  9. import Assert from '../../util/Assert'
  10. export default class RelateComputer {
  11. constructor() {
  12. RelateComputer.constructor_.apply(this, arguments)
  13. }
  14. static constructor_() {
  15. this._li = new RobustLineIntersector()
  16. this._ptLocator = new PointLocator()
  17. this._arg = null
  18. this._nodes = new NodeMap(new RelateNodeFactory())
  19. this._im = null
  20. this._isolatedEdges = new ArrayList()
  21. this._invalidPoint = null
  22. const arg = arguments[0]
  23. this._arg = arg
  24. }
  25. insertEdgeEnds(ee) {
  26. for (let i = ee.iterator(); i.hasNext(); ) {
  27. const e = i.next()
  28. this._nodes.add(e)
  29. }
  30. }
  31. computeProperIntersectionIM(intersector, im) {
  32. const dimA = this._arg[0].getGeometry().getDimension()
  33. const dimB = this._arg[1].getGeometry().getDimension()
  34. const hasProper = intersector.hasProperIntersection()
  35. const hasProperInterior = intersector.hasProperInteriorIntersection()
  36. if (dimA === 2 && dimB === 2) {
  37. if (hasProper) im.setAtLeast('212101212')
  38. } else if (dimA === 2 && dimB === 1) {
  39. if (hasProper) im.setAtLeast('FFF0FFFF2')
  40. if (hasProperInterior) im.setAtLeast('1FFFFF1FF')
  41. } else if (dimA === 1 && dimB === 2) {
  42. if (hasProper) im.setAtLeast('F0FFFFFF2')
  43. if (hasProperInterior) im.setAtLeast('1F1FFFFFF')
  44. } else if (dimA === 1 && dimB === 1) {
  45. if (hasProperInterior) im.setAtLeast('0FFFFFFFF')
  46. }
  47. }
  48. labelIsolatedEdges(thisIndex, targetIndex) {
  49. for (let ei = this._arg[thisIndex].getEdgeIterator(); ei.hasNext(); ) {
  50. const e = ei.next()
  51. if (e.isIsolated()) {
  52. this.labelIsolatedEdge(e, targetIndex, this._arg[targetIndex].getGeometry())
  53. this._isolatedEdges.add(e)
  54. }
  55. }
  56. }
  57. labelIsolatedEdge(e, targetIndex, target) {
  58. if (target.getDimension() > 0) {
  59. const loc = this._ptLocator.locate(e.getCoordinate(), target)
  60. e.getLabel().setAllLocations(targetIndex, loc)
  61. } else {
  62. e.getLabel().setAllLocations(targetIndex, Location.EXTERIOR)
  63. }
  64. }
  65. computeIM() {
  66. const im = new IntersectionMatrix()
  67. im.set(Location.EXTERIOR, Location.EXTERIOR, 2)
  68. if (!this._arg[0].getGeometry().getEnvelopeInternal().intersects(this._arg[1].getGeometry().getEnvelopeInternal())) {
  69. this.computeDisjointIM(im)
  70. return im
  71. }
  72. this._arg[0].computeSelfNodes(this._li, false)
  73. this._arg[1].computeSelfNodes(this._li, false)
  74. const intersector = this._arg[0].computeEdgeIntersections(this._arg[1], this._li, false)
  75. this.computeIntersectionNodes(0)
  76. this.computeIntersectionNodes(1)
  77. this.copyNodesAndLabels(0)
  78. this.copyNodesAndLabels(1)
  79. this.labelIsolatedNodes()
  80. this.computeProperIntersectionIM(intersector, im)
  81. const eeBuilder = new EdgeEndBuilder()
  82. const ee0 = eeBuilder.computeEdgeEnds(this._arg[0].getEdgeIterator())
  83. this.insertEdgeEnds(ee0)
  84. const ee1 = eeBuilder.computeEdgeEnds(this._arg[1].getEdgeIterator())
  85. this.insertEdgeEnds(ee1)
  86. this.labelNodeEdges()
  87. this.labelIsolatedEdges(0, 1)
  88. this.labelIsolatedEdges(1, 0)
  89. this.updateIM(im)
  90. return im
  91. }
  92. labelNodeEdges() {
  93. for (let ni = this._nodes.iterator(); ni.hasNext(); ) {
  94. const node = ni.next()
  95. node.getEdges().computeLabelling(this._arg)
  96. }
  97. }
  98. copyNodesAndLabels(argIndex) {
  99. for (let i = this._arg[argIndex].getNodeIterator(); i.hasNext(); ) {
  100. const graphNode = i.next()
  101. const newNode = this._nodes.addNode(graphNode.getCoordinate())
  102. newNode.setLabel(argIndex, graphNode.getLabel().getLocation(argIndex))
  103. }
  104. }
  105. labelIntersectionNodes(argIndex) {
  106. for (let i = this._arg[argIndex].getEdgeIterator(); i.hasNext(); ) {
  107. const e = i.next()
  108. const eLoc = e.getLabel().getLocation(argIndex)
  109. for (let eiIt = e.getEdgeIntersectionList().iterator(); eiIt.hasNext(); ) {
  110. const ei = eiIt.next()
  111. const n = this._nodes.find(ei.coord)
  112. if (n.getLabel().isNull(argIndex))
  113. if (eLoc === Location.BOUNDARY) n.setLabelBoundary(argIndex); else n.setLabel(argIndex, Location.INTERIOR)
  114. }
  115. }
  116. }
  117. labelIsolatedNode(n, targetIndex) {
  118. const loc = this._ptLocator.locate(n.getCoordinate(), this._arg[targetIndex].getGeometry())
  119. n.getLabel().setAllLocations(targetIndex, loc)
  120. }
  121. computeIntersectionNodes(argIndex) {
  122. for (let i = this._arg[argIndex].getEdgeIterator(); i.hasNext(); ) {
  123. const e = i.next()
  124. const eLoc = e.getLabel().getLocation(argIndex)
  125. for (let eiIt = e.getEdgeIntersectionList().iterator(); eiIt.hasNext(); ) {
  126. const ei = eiIt.next()
  127. const n = this._nodes.addNode(ei.coord)
  128. if (eLoc === Location.BOUNDARY) n.setLabelBoundary(argIndex); else
  129. if (n.getLabel().isNull(argIndex)) n.setLabel(argIndex, Location.INTERIOR)
  130. }
  131. }
  132. }
  133. labelIsolatedNodes() {
  134. for (let ni = this._nodes.iterator(); ni.hasNext(); ) {
  135. const n = ni.next()
  136. const label = n.getLabel()
  137. Assert.isTrue(label.getGeometryCount() > 0, 'node with empty label found')
  138. if (n.isIsolated())
  139. if (label.isNull(0)) this.labelIsolatedNode(n, 0); else this.labelIsolatedNode(n, 1)
  140. }
  141. }
  142. updateIM(im) {
  143. for (let ei = this._isolatedEdges.iterator(); ei.hasNext(); ) {
  144. const e = ei.next()
  145. e.updateIM(im)
  146. }
  147. for (let ni = this._nodes.iterator(); ni.hasNext(); ) {
  148. const node = ni.next()
  149. node.updateIM(im)
  150. node.updateIMFromEdges(im)
  151. }
  152. }
  153. computeDisjointIM(im) {
  154. const ga = this._arg[0].getGeometry()
  155. if (!ga.isEmpty()) {
  156. im.set(Location.INTERIOR, Location.EXTERIOR, ga.getDimension())
  157. im.set(Location.BOUNDARY, Location.EXTERIOR, ga.getBoundaryDimension())
  158. }
  159. const gb = this._arg[1].getGeometry()
  160. if (!gb.isEmpty()) {
  161. im.set(Location.EXTERIOR, Location.INTERIOR, gb.getDimension())
  162. im.set(Location.EXTERIOR, Location.BOUNDARY, gb.getBoundaryDimension())
  163. }
  164. }
  165. }