LinearGeometryBuilder.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import CoordinateList from '../geom/CoordinateList'
  2. import IllegalArgumentException from '../../../../java/lang/IllegalArgumentException'
  3. import ArrayList from '../../../../java/util/ArrayList'
  4. export default class LinearGeometryBuilder {
  5. constructor() {
  6. LinearGeometryBuilder.constructor_.apply(this, arguments)
  7. }
  8. static constructor_() {
  9. this._geomFact = null
  10. this._lines = new ArrayList()
  11. this._coordList = null
  12. this._ignoreInvalidLines = false
  13. this._fixInvalidLines = false
  14. this._lastPt = null
  15. const geomFact = arguments[0]
  16. this._geomFact = geomFact
  17. }
  18. getGeometry() {
  19. this.endLine()
  20. return this._geomFact.buildGeometry(this._lines)
  21. }
  22. getLastCoordinate() {
  23. return this._lastPt
  24. }
  25. endLine() {
  26. if (this._coordList === null)
  27. return null
  28. if (this._ignoreInvalidLines && this._coordList.size() < 2) {
  29. this._coordList = null
  30. return null
  31. }
  32. const rawPts = this._coordList.toCoordinateArray()
  33. let pts = rawPts
  34. if (this._fixInvalidLines) pts = this.validCoordinateSequence(rawPts)
  35. this._coordList = null
  36. let line = null
  37. try {
  38. line = this._geomFact.createLineString(pts)
  39. } catch (ex) {
  40. if (ex instanceof IllegalArgumentException) {
  41. if (!this._ignoreInvalidLines) throw ex
  42. } else {
  43. throw ex
  44. }
  45. } finally {}
  46. if (line !== null) this._lines.add(line)
  47. }
  48. setFixInvalidLines(fixInvalidLines) {
  49. this._fixInvalidLines = fixInvalidLines
  50. }
  51. add() {
  52. if (arguments.length === 1) {
  53. const pt = arguments[0]
  54. this.add(pt, true)
  55. } else if (arguments.length === 2) {
  56. const pt = arguments[0], allowRepeatedPoints = arguments[1]
  57. if (this._coordList === null) this._coordList = new CoordinateList()
  58. this._coordList.add(pt, allowRepeatedPoints)
  59. this._lastPt = pt
  60. }
  61. }
  62. setIgnoreInvalidLines(ignoreInvalidLines) {
  63. this._ignoreInvalidLines = ignoreInvalidLines
  64. }
  65. validCoordinateSequence(pts) {
  66. if (pts.length >= 2) return pts
  67. const validPts = [pts[0], pts[0]]
  68. return validPts
  69. }
  70. }