RepeatedPointTester.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import LineString from '../../geom/LineString'
  2. import Geometry from '../../geom/Geometry'
  3. import Point from '../../geom/Point'
  4. import Polygon from '../../geom/Polygon'
  5. import MultiPoint from '../../geom/MultiPoint'
  6. import GeometryCollection from '../../geom/GeometryCollection'
  7. import UnsupportedOperationException from '../../../../../java/lang/UnsupportedOperationException'
  8. export default class RepeatedPointTester {
  9. constructor() {
  10. RepeatedPointTester.constructor_.apply(this, arguments)
  11. }
  12. static constructor_() {
  13. this._repeatedCoord = null
  14. }
  15. getCoordinate() {
  16. return this._repeatedCoord
  17. }
  18. hasRepeatedPoint() {
  19. if (arguments[0] instanceof Geometry) {
  20. const g = arguments[0]
  21. if (g.isEmpty()) return false
  22. if (g instanceof Point) return false; else if (g instanceof MultiPoint) return false; else if (g instanceof LineString) return this.hasRepeatedPoint(g.getCoordinates()); else if (g instanceof Polygon) return this.hasRepeatedPoint(g); else if (g instanceof GeometryCollection) return this.hasRepeatedPoint(g); else throw new UnsupportedOperationException(g.getGeometryType())
  23. } else if (arguments[0] instanceof Array) {
  24. const coord = arguments[0]
  25. for (let i = 1; i < coord.length; i++)
  26. if (coord[i - 1].equals(coord[i])) {
  27. this._repeatedCoord = coord[i]
  28. return true
  29. }
  30. return false
  31. } else if (arguments[0] instanceof Polygon) {
  32. const p = arguments[0]
  33. if (this.hasRepeatedPoint(p.getExteriorRing().getCoordinates())) return true
  34. for (let i = 0; i < p.getNumInteriorRing(); i++)
  35. if (this.hasRepeatedPoint(p.getInteriorRingN(i).getCoordinates())) return true
  36. return false
  37. } else if (arguments[0] instanceof GeometryCollection) {
  38. const gc = arguments[0]
  39. for (let i = 0; i < gc.getNumGeometries(); i++) {
  40. const g = gc.getGeometryN(i)
  41. if (this.hasRepeatedPoint(g)) return true
  42. }
  43. return false
  44. }
  45. }
  46. }