TopologyPreservingSimplifier.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import LineString from '../geom/LineString'
  2. import HashMap from '../../../../java/util/HashMap'
  3. import GeometryTransformer from '../geom/util/GeometryTransformer'
  4. import TaggedLinesSimplifier from './TaggedLinesSimplifier'
  5. import IllegalArgumentException from '../../../../java/lang/IllegalArgumentException'
  6. import GeometryComponentFilter from '../geom/GeometryComponentFilter'
  7. import TaggedLineString from './TaggedLineString'
  8. export default class TopologyPreservingSimplifier {
  9. constructor() {
  10. TopologyPreservingSimplifier.constructor_.apply(this, arguments)
  11. }
  12. static constructor_() {
  13. this._inputGeom = null
  14. this._lineSimplifier = new TaggedLinesSimplifier()
  15. this._linestringMap = null
  16. const inputGeom = arguments[0]
  17. this._inputGeom = inputGeom
  18. }
  19. static simplify(geom, distanceTolerance) {
  20. const tss = new TopologyPreservingSimplifier(geom)
  21. tss.setDistanceTolerance(distanceTolerance)
  22. return tss.getResultGeometry()
  23. }
  24. getResultGeometry() {
  25. if (this._inputGeom.isEmpty()) return this._inputGeom.copy()
  26. this._linestringMap = new HashMap()
  27. this._inputGeom.apply(new LineStringMapBuilderFilter(this))
  28. this._lineSimplifier.simplify(this._linestringMap.values())
  29. const result = new LineStringTransformer(this._linestringMap).transform(this._inputGeom)
  30. return result
  31. }
  32. setDistanceTolerance(distanceTolerance) {
  33. if (distanceTolerance < 0.0) throw new IllegalArgumentException('Tolerance must be non-negative')
  34. this._lineSimplifier.setDistanceTolerance(distanceTolerance)
  35. }
  36. }
  37. class LineStringTransformer extends GeometryTransformer {
  38. constructor() {
  39. super()
  40. LineStringTransformer.constructor_.apply(this, arguments)
  41. }
  42. static constructor_() {
  43. this._linestringMap = null
  44. const linestringMap = arguments[0]
  45. this._linestringMap = linestringMap
  46. }
  47. transformCoordinates(coords, parent) {
  48. if (coords.size() === 0) return null
  49. if (parent instanceof LineString) {
  50. const taggedLine = this._linestringMap.get(parent)
  51. return this.createCoordinateSequence(taggedLine.getResultCoordinates())
  52. }
  53. return super.transformCoordinates.call(this, coords, parent)
  54. }
  55. }
  56. class LineStringMapBuilderFilter {
  57. constructor() {
  58. LineStringMapBuilderFilter.constructor_.apply(this, arguments)
  59. }
  60. static constructor_() {
  61. this.tps = null
  62. const tps = arguments[0]
  63. this.tps = tps
  64. }
  65. filter(geom) {
  66. if (geom instanceof LineString) {
  67. const line = geom
  68. if (line.isEmpty()) return null
  69. const minSize = line.isClosed() ? 4 : 2
  70. const taggedLine = new TaggedLineString(line, minSize)
  71. this.tps._linestringMap.put(line, taggedLine)
  72. }
  73. }
  74. get interfaces_() {
  75. return [GeometryComponentFilter]
  76. }
  77. }
  78. TopologyPreservingSimplifier.LineStringTransformer = LineStringTransformer
  79. TopologyPreservingSimplifier.LineStringMapBuilderFilter = LineStringMapBuilderFilter