LinearIterator.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import hasInterface from '../../../../hasInterface'
  2. import IllegalArgumentException from '../../../../java/lang/IllegalArgumentException'
  3. import Lineal from '../geom/Lineal'
  4. export default class LinearIterator {
  5. constructor() {
  6. LinearIterator.constructor_.apply(this, arguments)
  7. }
  8. static constructor_() {
  9. this._linearGeom = null
  10. this._numLines = null
  11. this._currentLine = null
  12. this._componentIndex = 0
  13. this._vertexIndex = 0
  14. if (arguments.length === 1) {
  15. const linear = arguments[0]
  16. LinearIterator.constructor_.call(this, linear, 0, 0)
  17. } else if (arguments.length === 2) {
  18. const linear = arguments[0], start = arguments[1]
  19. LinearIterator.constructor_.call(this, linear, start.getComponentIndex(), LinearIterator.segmentEndVertexIndex(start))
  20. } else if (arguments.length === 3) {
  21. const linearGeom = arguments[0], componentIndex = arguments[1], vertexIndex = arguments[2]
  22. if (!hasInterface(linearGeom, Lineal)) throw new IllegalArgumentException('Lineal geometry is required')
  23. this._linearGeom = linearGeom
  24. this._numLines = linearGeom.getNumGeometries()
  25. this._componentIndex = componentIndex
  26. this._vertexIndex = vertexIndex
  27. this.loadCurrentLine()
  28. }
  29. }
  30. static segmentEndVertexIndex(loc) {
  31. if (loc.getSegmentFraction() > 0.0) return loc.getSegmentIndex() + 1
  32. return loc.getSegmentIndex()
  33. }
  34. getComponentIndex() {
  35. return this._componentIndex
  36. }
  37. getLine() {
  38. return this._currentLine
  39. }
  40. getVertexIndex() {
  41. return this._vertexIndex
  42. }
  43. getSegmentEnd() {
  44. if (this._vertexIndex < this.getLine().getNumPoints() - 1) return this._currentLine.getCoordinateN(this._vertexIndex + 1)
  45. return null
  46. }
  47. next() {
  48. if (!this.hasNext()) return null
  49. this._vertexIndex++
  50. if (this._vertexIndex >= this._currentLine.getNumPoints()) {
  51. this._componentIndex++
  52. this.loadCurrentLine()
  53. this._vertexIndex = 0
  54. }
  55. }
  56. loadCurrentLine() {
  57. if (this._componentIndex >= this._numLines) {
  58. this._currentLine = null
  59. return null
  60. }
  61. this._currentLine = this._linearGeom.getGeometryN(this._componentIndex)
  62. }
  63. getSegmentStart() {
  64. return this._currentLine.getCoordinateN(this._vertexIndex)
  65. }
  66. isEndOfLine() {
  67. if (this._componentIndex >= this._numLines) return false
  68. if (this._vertexIndex < this._currentLine.getNumPoints() - 1) return false
  69. return true
  70. }
  71. hasNext() {
  72. if (this._componentIndex >= this._numLines) return false
  73. if (this._componentIndex === this._numLines - 1 && this._vertexIndex >= this._currentLine.getNumPoints()) return false
  74. return true
  75. }
  76. }