MCIndexNoder.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import STRtree from '../index/strtree/STRtree'
  2. import NodedSegmentString from './NodedSegmentString'
  3. import MonotoneChainOverlapAction from '../index/chain/MonotoneChainOverlapAction'
  4. import MonotoneChainBuilder from '../index/chain/MonotoneChainBuilder'
  5. import ArrayList from '../../../../java/util/ArrayList'
  6. import SinglePassNoder from './SinglePassNoder'
  7. export default class MCIndexNoder extends SinglePassNoder {
  8. constructor() {
  9. super()
  10. MCIndexNoder.constructor_.apply(this, arguments)
  11. }
  12. static constructor_() {
  13. this._monoChains = new ArrayList()
  14. this._index = new STRtree()
  15. this._idCounter = 0
  16. this._nodedSegStrings = null
  17. this._nOverlaps = 0
  18. if (arguments.length === 0) {} else if (arguments.length === 1) {
  19. const si = arguments[0]
  20. SinglePassNoder.constructor_.call(this, si)
  21. }
  22. }
  23. getMonotoneChains() {
  24. return this._monoChains
  25. }
  26. getNodedSubstrings() {
  27. return NodedSegmentString.getNodedSubstrings(this._nodedSegStrings)
  28. }
  29. getIndex() {
  30. return this._index
  31. }
  32. add(segStr) {
  33. const segChains = MonotoneChainBuilder.getChains(segStr.getCoordinates(), segStr)
  34. for (let i = segChains.iterator(); i.hasNext(); ) {
  35. const mc = i.next()
  36. mc.setId(this._idCounter++)
  37. this._index.insert(mc.getEnvelope(), mc)
  38. this._monoChains.add(mc)
  39. }
  40. }
  41. computeNodes(inputSegStrings) {
  42. this._nodedSegStrings = inputSegStrings
  43. for (let i = inputSegStrings.iterator(); i.hasNext(); )
  44. this.add(i.next())
  45. this.intersectChains()
  46. }
  47. intersectChains() {
  48. const overlapAction = new SegmentOverlapAction(this._segInt)
  49. for (let i = this._monoChains.iterator(); i.hasNext(); ) {
  50. const queryChain = i.next()
  51. const overlapChains = this._index.query(queryChain.getEnvelope())
  52. for (let j = overlapChains.iterator(); j.hasNext(); ) {
  53. const testChain = j.next()
  54. if (testChain.getId() > queryChain.getId()) {
  55. queryChain.computeOverlaps(testChain, overlapAction)
  56. this._nOverlaps++
  57. }
  58. if (this._segInt.isDone()) return null
  59. }
  60. }
  61. }
  62. }
  63. class SegmentOverlapAction extends MonotoneChainOverlapAction {
  64. constructor() {
  65. super()
  66. SegmentOverlapAction.constructor_.apply(this, arguments)
  67. }
  68. static constructor_() {
  69. this._si = null
  70. const si = arguments[0]
  71. this._si = si
  72. }
  73. overlap() {
  74. if (arguments.length === 4) {
  75. const mc1 = arguments[0], start1 = arguments[1], mc2 = arguments[2], start2 = arguments[3]
  76. const ss1 = mc1.getContext()
  77. const ss2 = mc2.getContext()
  78. this._si.processIntersections(ss1, start1, ss2, start2)
  79. } else {
  80. return super.overlap.apply(this, arguments)
  81. }
  82. }
  83. }
  84. MCIndexNoder.SegmentOverlapAction = SegmentOverlapAction