UnaryUnionOp.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import Geometry from '../../geom/Geometry'
  2. import PointGeometryUnion from './PointGeometryUnion'
  3. import hasInterface from '../../../../../hasInterface'
  4. import Collection from '../../../../../java/util/Collection'
  5. import SnapIfNeededOverlayOp from '../overlay/snap/SnapIfNeededOverlayOp'
  6. import InputExtracter from './InputExtracter'
  7. import OverlayOp from '../overlay/OverlayOp'
  8. import CascadedPolygonUnion from './CascadedPolygonUnion'
  9. export default class UnaryUnionOp {
  10. constructor() {
  11. UnaryUnionOp.constructor_.apply(this, arguments)
  12. }
  13. static constructor_() {
  14. this._geomFact = null
  15. this._extracter = null
  16. if (arguments.length === 1) {
  17. if (hasInterface(arguments[0], Collection)) {
  18. const geoms = arguments[0]
  19. this.extract(geoms)
  20. } else if (arguments[0] instanceof Geometry) {
  21. const geom = arguments[0]
  22. this.extract(geom)
  23. }
  24. } else if (arguments.length === 2) {
  25. const geoms = arguments[0], geomFact = arguments[1]
  26. this._geomFact = geomFact
  27. this.extract(geoms)
  28. }
  29. }
  30. static union() {
  31. if (arguments.length === 1) {
  32. if (hasInterface(arguments[0], Collection)) {
  33. const geoms = arguments[0]
  34. const op = new UnaryUnionOp(geoms)
  35. return op.union()
  36. } else if (arguments[0] instanceof Geometry) {
  37. const geom = arguments[0]
  38. const op = new UnaryUnionOp(geom)
  39. return op.union()
  40. }
  41. } else if (arguments.length === 2) {
  42. const geoms = arguments[0], geomFact = arguments[1]
  43. const op = new UnaryUnionOp(geoms, geomFact)
  44. return op.union()
  45. }
  46. }
  47. unionNoOpt(g0) {
  48. const empty = this._geomFact.createPoint()
  49. return SnapIfNeededOverlayOp.overlayOp(g0, empty, OverlayOp.UNION)
  50. }
  51. unionWithNull(g0, g1) {
  52. if (g0 === null && g1 === null) return null
  53. if (g1 === null) return g0
  54. if (g0 === null) return g1
  55. return g0.union(g1)
  56. }
  57. extract() {
  58. if (hasInterface(arguments[0], Collection)) {
  59. const geoms = arguments[0]
  60. this._extracter = InputExtracter.extract(geoms)
  61. } else if (arguments[0] instanceof Geometry) {
  62. const geom = arguments[0]
  63. this._extracter = InputExtracter.extract(geom)
  64. }
  65. }
  66. union() {
  67. if (this._geomFact === null) this._geomFact = this._extracter.getFactory()
  68. if (this._geomFact === null)
  69. return null
  70. if (this._extracter.isEmpty())
  71. return this._geomFact.createEmpty(this._extracter.getDimension())
  72. const points = this._extracter.getExtract(0)
  73. const lines = this._extracter.getExtract(1)
  74. const polygons = this._extracter.getExtract(2)
  75. let unionPoints = null
  76. if (points.size() > 0) {
  77. const ptGeom = this._geomFact.buildGeometry(points)
  78. unionPoints = this.unionNoOpt(ptGeom)
  79. }
  80. let unionLines = null
  81. if (lines.size() > 0) {
  82. const lineGeom = this._geomFact.buildGeometry(lines)
  83. unionLines = this.unionNoOpt(lineGeom)
  84. }
  85. let unionPolygons = null
  86. if (polygons.size() > 0)
  87. unionPolygons = CascadedPolygonUnion.union(polygons)
  88. const unionLA = this.unionWithNull(unionLines, unionPolygons)
  89. let union = null
  90. if (unionPoints === null) union = unionLA; else if (unionLA === null) union = unionPoints; else union = PointGeometryUnion.union(unionPoints, unionLA)
  91. if (union === null) return this._geomFact.createGeometryCollection()
  92. return union
  93. }
  94. }