@mkerkstra/jsts-cjs
Version:
A CommonJS fork of JSTS: A JavaScript library of spatial predicates and functions for processing geometry
61 lines (60 loc) • 2.06 kB
JavaScript
import WKTWriter from '../io/WKTWriter.js'
import MCIndexNoder from './MCIndexNoder.js'
import TopologyException from '../geom/TopologyException.js'
import RobustLineIntersector from '../algorithm/RobustLineIntersector.js'
import NodingIntersectionFinder from './NodingIntersectionFinder.js'
export default class FastNodingValidator {
constructor() {
FastNodingValidator.constructor_.apply(this, arguments)
}
static constructor_() {
this._li = new RobustLineIntersector()
this._segStrings = null
this._findAllIntersections = false
this._segInt = null
this._isValid = true
const segStrings = arguments[0]
this._segStrings = segStrings
}
static computeIntersections(segStrings) {
const nv = new FastNodingValidator(segStrings)
nv.setFindAllIntersections(true)
nv.isValid()
return nv.getIntersections()
}
isValid() {
this.execute()
return this._isValid
}
setFindAllIntersections(findAllIntersections) {
this._findAllIntersections = findAllIntersections
}
checkInteriorIntersections() {
this._isValid = true
this._segInt = new NodingIntersectionFinder(this._li)
this._segInt.setFindAllIntersections(this._findAllIntersections)
const noder = new MCIndexNoder()
noder.setSegmentIntersector(this._segInt)
noder.computeNodes(this._segStrings)
if (this._segInt.hasIntersection()) {
this._isValid = false
return null
}
}
checkValid() {
this.execute()
if (!this._isValid) throw new TopologyException(this.getErrorMessage(), this._segInt.getIntersection())
}
getErrorMessage() {
if (this._isValid) return 'no intersections found'
const intSegs = this._segInt.getIntersectionSegments()
return 'found non-noded intersection between ' + WKTWriter.toLineString(intSegs[0], intSegs[1]) + ' and ' + WKTWriter.toLineString(intSegs[2], intSegs[3])
}
execute() {
if (this._segInt !== null) return null
this.checkInteriorIntersections()
}
getIntersections() {
return this._segInt.getIntersections()
}
}