InputExtracter.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import LineString from '../../geom/LineString'
  2. import Geometry from '../../geom/Geometry'
  3. import hasInterface from '../../../../../hasInterface'
  4. import Collection from '../../../../../java/util/Collection'
  5. import Point from '../../geom/Point'
  6. import Polygon from '../../geom/Polygon'
  7. import GeometryCollection from '../../geom/GeometryCollection'
  8. import Dimension from '../../geom/Dimension'
  9. import ArrayList from '../../../../../java/util/ArrayList'
  10. import GeometryFilter from '../../geom/GeometryFilter'
  11. import Assert from '../../util/Assert'
  12. export default class InputExtracter {
  13. constructor() {
  14. InputExtracter.constructor_.apply(this, arguments)
  15. }
  16. static constructor_() {
  17. this._geomFactory = null
  18. this._polygons = new ArrayList()
  19. this._lines = new ArrayList()
  20. this._points = new ArrayList()
  21. this._dimension = Dimension.FALSE
  22. }
  23. static extract() {
  24. if (hasInterface(arguments[0], Collection)) {
  25. const geoms = arguments[0]
  26. const extracter = new InputExtracter()
  27. extracter.add(geoms)
  28. return extracter
  29. } else if (arguments[0] instanceof Geometry) {
  30. const geom = arguments[0]
  31. const extracter = new InputExtracter()
  32. extracter.add(geom)
  33. return extracter
  34. }
  35. }
  36. getFactory() {
  37. return this._geomFactory
  38. }
  39. recordDimension(dim) {
  40. if (dim > this._dimension) this._dimension = dim
  41. }
  42. getDimension() {
  43. return this._dimension
  44. }
  45. filter(geom) {
  46. this.recordDimension(geom.getDimension())
  47. if (geom instanceof GeometryCollection)
  48. return null
  49. if (geom.isEmpty()) return null
  50. if (geom instanceof Polygon) {
  51. this._polygons.add(geom)
  52. return null
  53. } else if (geom instanceof LineString) {
  54. this._lines.add(geom)
  55. return null
  56. } else if (geom instanceof Point) {
  57. this._points.add(geom)
  58. return null
  59. }
  60. Assert.shouldNeverReachHere('Unhandled geometry type: ' + geom.getGeometryType())
  61. }
  62. getExtract(dim) {
  63. switch (dim) {
  64. case 0:
  65. return this._points
  66. case 1:
  67. return this._lines
  68. case 2:
  69. return this._polygons
  70. }
  71. Assert.shouldNeverReachHere('Invalid dimension: ' + dim)
  72. return null
  73. }
  74. isEmpty() {
  75. return this._polygons.isEmpty() && this._lines.isEmpty() && this._points.isEmpty()
  76. }
  77. add() {
  78. if (hasInterface(arguments[0], Collection)) {
  79. const geoms = arguments[0]
  80. for (const geom of geoms)
  81. this.add(geom)
  82. } else if (arguments[0] instanceof Geometry) {
  83. const geom = arguments[0]
  84. if (this._geomFactory === null) this._geomFactory = geom.getFactory()
  85. geom.apply(this)
  86. }
  87. }
  88. get interfaces_() {
  89. return [GeometryFilter]
  90. }
  91. }