PointGeometryUnion.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import PointLocator from '../../algorithm/PointLocator'
  2. import Location from '../../geom/Location'
  3. import TreeSet from '../../../../../java/util/TreeSet'
  4. import GeometryCombiner from '../../geom/util/GeometryCombiner'
  5. import CoordinateArrays from '../../geom/CoordinateArrays'
  6. export default class PointGeometryUnion {
  7. constructor() {
  8. PointGeometryUnion.constructor_.apply(this, arguments)
  9. }
  10. static constructor_() {
  11. this._pointGeom = null
  12. this._otherGeom = null
  13. this._geomFact = null
  14. const pointGeom = arguments[0], otherGeom = arguments[1]
  15. this._pointGeom = pointGeom
  16. this._otherGeom = otherGeom
  17. this._geomFact = otherGeom.getFactory()
  18. }
  19. static union(pointGeom, otherGeom) {
  20. const unioner = new PointGeometryUnion(pointGeom, otherGeom)
  21. return unioner.union()
  22. }
  23. union() {
  24. const locater = new PointLocator()
  25. const exteriorCoords = new TreeSet()
  26. for (let i = 0; i < this._pointGeom.getNumGeometries(); i++) {
  27. const point = this._pointGeom.getGeometryN(i)
  28. const coord = point.getCoordinate()
  29. const loc = locater.locate(coord, this._otherGeom)
  30. if (loc === Location.EXTERIOR) exteriorCoords.add(coord)
  31. }
  32. if (exteriorCoords.size() === 0) return this._otherGeom
  33. let ptComp = null
  34. const coords = CoordinateArrays.toCoordinateArray(exteriorCoords)
  35. if (coords.length === 1)
  36. ptComp = this._geomFact.createPoint(coords[0])
  37. else
  38. ptComp = this._geomFact.createMultiPointFromCoords(coords)
  39. return GeometryCombiner.combine(ptComp, this._otherGeom)
  40. }
  41. }