NodingValidator.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import GeometryFactory from '../geom/GeometryFactory'
  2. import RobustLineIntersector from '../algorithm/RobustLineIntersector'
  3. import RuntimeException from '../../../../java/lang/RuntimeException'
  4. export default class NodingValidator {
  5. constructor() {
  6. NodingValidator.constructor_.apply(this, arguments)
  7. }
  8. static constructor_() {
  9. this._li = new RobustLineIntersector()
  10. this._segStrings = null
  11. const segStrings = arguments[0]
  12. this._segStrings = segStrings
  13. }
  14. checkEndPtVertexIntersections() {
  15. if (arguments.length === 0) {
  16. for (let i = this._segStrings.iterator(); i.hasNext(); ) {
  17. const ss = i.next()
  18. const pts = ss.getCoordinates()
  19. this.checkEndPtVertexIntersections(pts[0], this._segStrings)
  20. this.checkEndPtVertexIntersections(pts[pts.length - 1], this._segStrings)
  21. }
  22. } else if (arguments.length === 2) {
  23. const testPt = arguments[0], segStrings = arguments[1]
  24. for (let i = segStrings.iterator(); i.hasNext(); ) {
  25. const ss = i.next()
  26. const pts = ss.getCoordinates()
  27. for (let j = 1; j < pts.length - 1; j++)
  28. if (pts[j].equals(testPt)) throw new RuntimeException('found endpt/interior pt intersection at index ' + j + ' :pt ' + testPt)
  29. }
  30. }
  31. }
  32. checkInteriorIntersections() {
  33. if (arguments.length === 0) {
  34. for (let i = this._segStrings.iterator(); i.hasNext(); ) {
  35. const ss0 = i.next()
  36. for (let j = this._segStrings.iterator(); j.hasNext(); ) {
  37. const ss1 = j.next()
  38. this.checkInteriorIntersections(ss0, ss1)
  39. }
  40. }
  41. } else if (arguments.length === 2) {
  42. const ss0 = arguments[0], ss1 = arguments[1]
  43. const pts0 = ss0.getCoordinates()
  44. const pts1 = ss1.getCoordinates()
  45. for (let i0 = 0; i0 < pts0.length - 1; i0++)
  46. for (let i1 = 0; i1 < pts1.length - 1; i1++)
  47. this.checkInteriorIntersections(ss0, i0, ss1, i1)
  48. } else if (arguments.length === 4) {
  49. const e0 = arguments[0], segIndex0 = arguments[1], e1 = arguments[2], segIndex1 = arguments[3]
  50. if (e0 === e1 && segIndex0 === segIndex1) return null
  51. const p00 = e0.getCoordinates()[segIndex0]
  52. const p01 = e0.getCoordinates()[segIndex0 + 1]
  53. const p10 = e1.getCoordinates()[segIndex1]
  54. const p11 = e1.getCoordinates()[segIndex1 + 1]
  55. this._li.computeIntersection(p00, p01, p10, p11)
  56. if (this._li.hasIntersection())
  57. if (this._li.isProper() || this.hasInteriorIntersection(this._li, p00, p01) || this.hasInteriorIntersection(this._li, p10, p11))
  58. throw new RuntimeException('found non-noded intersection at ' + p00 + '-' + p01 + ' and ' + p10 + '-' + p11)
  59. }
  60. }
  61. checkValid() {
  62. this.checkEndPtVertexIntersections()
  63. this.checkInteriorIntersections()
  64. this.checkCollapses()
  65. }
  66. checkCollapses() {
  67. if (arguments.length === 0) {
  68. for (let i = this._segStrings.iterator(); i.hasNext(); ) {
  69. const ss = i.next()
  70. this.checkCollapses(ss)
  71. }
  72. } else if (arguments.length === 1) {
  73. const ss = arguments[0]
  74. const pts = ss.getCoordinates()
  75. for (let i = 0; i < pts.length - 2; i++)
  76. this.checkCollapse(pts[i], pts[i + 1], pts[i + 2])
  77. }
  78. }
  79. hasInteriorIntersection(li, p0, p1) {
  80. for (let i = 0; i < li.getIntersectionNum(); i++) {
  81. const intPt = li.getIntersection(i)
  82. if (!(intPt.equals(p0) || intPt.equals(p1))) return true
  83. }
  84. return false
  85. }
  86. checkCollapse(p0, p1, p2) {
  87. if (p0.equals(p2)) throw new RuntimeException('found non-noded collapse at ' + NodingValidator.fact.createLineString([p0, p1, p2]))
  88. }
  89. }
  90. NodingValidator.fact = new GeometryFactory()