FastNodingValidator.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import WKTWriter from '../io/WKTWriter'
  2. import MCIndexNoder from './MCIndexNoder'
  3. import TopologyException from '../geom/TopologyException'
  4. import RobustLineIntersector from '../algorithm/RobustLineIntersector'
  5. import NodingIntersectionFinder from './NodingIntersectionFinder'
  6. export default class FastNodingValidator {
  7. constructor() {
  8. FastNodingValidator.constructor_.apply(this, arguments)
  9. }
  10. static constructor_() {
  11. this._li = new RobustLineIntersector()
  12. this._segStrings = null
  13. this._findAllIntersections = false
  14. this._segInt = null
  15. this._isValid = true
  16. const segStrings = arguments[0]
  17. this._segStrings = segStrings
  18. }
  19. static computeIntersections(segStrings) {
  20. const nv = new FastNodingValidator(segStrings)
  21. nv.setFindAllIntersections(true)
  22. nv.isValid()
  23. return nv.getIntersections()
  24. }
  25. execute() {
  26. if (this._segInt !== null) return null
  27. this.checkInteriorIntersections()
  28. }
  29. getIntersections() {
  30. return this._segInt.getIntersections()
  31. }
  32. isValid() {
  33. this.execute()
  34. return this._isValid
  35. }
  36. setFindAllIntersections(findAllIntersections) {
  37. this._findAllIntersections = findAllIntersections
  38. }
  39. checkInteriorIntersections() {
  40. this._isValid = true
  41. this._segInt = new NodingIntersectionFinder(this._li)
  42. this._segInt.setFindAllIntersections(this._findAllIntersections)
  43. const noder = new MCIndexNoder()
  44. noder.setSegmentIntersector(this._segInt)
  45. noder.computeNodes(this._segStrings)
  46. if (this._segInt.hasIntersection()) {
  47. this._isValid = false
  48. return null
  49. }
  50. }
  51. checkValid() {
  52. this.execute()
  53. if (!this._isValid) throw new TopologyException(this.getErrorMessage(), this._segInt.getIntersection())
  54. }
  55. getErrorMessage() {
  56. if (this._isValid) return 'no intersections found'
  57. const intSegs = this._segInt.getIntersectionSegments()
  58. return 'found non-noded intersection between ' + WKTWriter.toLineString(intSegs[0], intSegs[1]) + ' and ' + WKTWriter.toLineString(intSegs[2], intSegs[3])
  59. }
  60. }