UnionInteracting.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import GeometryCombiner from '../../geom/util/GeometryCombiner'
  2. import System from '../../../../../java/lang/System'
  3. import ArrayList from '../../../../../java/util/ArrayList'
  4. export default class UnionInteracting {
  5. constructor() {
  6. UnionInteracting.constructor_.apply(this, arguments)
  7. }
  8. static constructor_() {
  9. this._geomFactory = null
  10. this._g0 = null
  11. this._g1 = null
  12. this._interacts0 = null
  13. this._interacts1 = null
  14. const g0 = arguments[0], g1 = arguments[1]
  15. this._g0 = g0
  16. this._g1 = g1
  17. this._geomFactory = g0.getFactory()
  18. this._interacts0 = new Array(g0.getNumGeometries()).fill(null)
  19. this._interacts1 = new Array(g1.getNumGeometries()).fill(null)
  20. }
  21. static union(g0, g1) {
  22. const uue = new UnionInteracting(g0, g1)
  23. return uue.union()
  24. }
  25. extractElements(geom, interacts, isInteracting) {
  26. const extractedGeoms = new ArrayList()
  27. for (let i = 0; i < geom.getNumGeometries(); i++) {
  28. const elem = geom.getGeometryN(i)
  29. if (interacts[i] === isInteracting) extractedGeoms.add(elem)
  30. }
  31. return this._geomFactory.buildGeometry(extractedGeoms)
  32. }
  33. computeInteracting() {
  34. if (arguments.length === 0) {
  35. for (let i = 0; i < this._g0.getNumGeometries(); i++) {
  36. const elem = this._g0.getGeometryN(i)
  37. this._interacts0[i] = this.computeInteracting(elem)
  38. }
  39. } else if (arguments.length === 1) {
  40. const elem0 = arguments[0]
  41. let interactsWithAny = false
  42. for (let i = 0; i < this._g1.getNumGeometries(); i++) {
  43. const elem1 = this._g1.getGeometryN(i)
  44. const interacts = elem1.getEnvelopeInternal().intersects(elem0.getEnvelopeInternal())
  45. if (interacts) this._interacts1[i] = true
  46. if (interacts) interactsWithAny = true
  47. }
  48. return interactsWithAny
  49. }
  50. }
  51. union() {
  52. this.computeInteracting()
  53. const int0 = this.extractElements(this._g0, this._interacts0, true)
  54. const int1 = this.extractElements(this._g1, this._interacts1, true)
  55. if (int0.isEmpty() || int1.isEmpty())
  56. System.out.println('found empty!')
  57. const union = int0.union(int1)
  58. const disjoint0 = this.extractElements(this._g0, this._interacts0, false)
  59. const disjoint1 = this.extractElements(this._g1, this._interacts1, false)
  60. const overallUnion = GeometryCombiner.combine(union, disjoint0, disjoint1)
  61. return overallUnion
  62. }
  63. bufferUnion(g0, g1) {
  64. const factory = g0.getFactory()
  65. const gColl = factory.createGeometryCollection([g0, g1])
  66. const unionAll = gColl.buffer(0.0)
  67. return unionAll
  68. }
  69. }