Label.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import StringBuffer from '../../../../java/lang/StringBuffer'
  2. import Location from '../geom/Location'
  3. import Position from './Position'
  4. import TopologyLocation from './TopologyLocation'
  5. export default class Label {
  6. constructor() {
  7. Label.constructor_.apply(this, arguments)
  8. }
  9. static constructor_() {
  10. this.elt = new Array(2).fill(null)
  11. if (arguments.length === 1) {
  12. if (Number.isInteger(arguments[0])) {
  13. const onLoc = arguments[0]
  14. this.elt[0] = new TopologyLocation(onLoc)
  15. this.elt[1] = new TopologyLocation(onLoc)
  16. } else if (arguments[0] instanceof Label) {
  17. const lbl = arguments[0]
  18. this.elt[0] = new TopologyLocation(lbl.elt[0])
  19. this.elt[1] = new TopologyLocation(lbl.elt[1])
  20. }
  21. } else if (arguments.length === 2) {
  22. const geomIndex = arguments[0], onLoc = arguments[1]
  23. this.elt[0] = new TopologyLocation(Location.NONE)
  24. this.elt[1] = new TopologyLocation(Location.NONE)
  25. this.elt[geomIndex].setLocation(onLoc)
  26. } else if (arguments.length === 3) {
  27. const onLoc = arguments[0], leftLoc = arguments[1], rightLoc = arguments[2]
  28. this.elt[0] = new TopologyLocation(onLoc, leftLoc, rightLoc)
  29. this.elt[1] = new TopologyLocation(onLoc, leftLoc, rightLoc)
  30. } else if (arguments.length === 4) {
  31. const geomIndex = arguments[0], onLoc = arguments[1], leftLoc = arguments[2], rightLoc = arguments[3]
  32. this.elt[0] = new TopologyLocation(Location.NONE, Location.NONE, Location.NONE)
  33. this.elt[1] = new TopologyLocation(Location.NONE, Location.NONE, Location.NONE)
  34. this.elt[geomIndex].setLocations(onLoc, leftLoc, rightLoc)
  35. }
  36. }
  37. static toLineLabel(label) {
  38. const lineLabel = new Label(Location.NONE)
  39. for (let i = 0; i < 2; i++)
  40. lineLabel.setLocation(i, label.getLocation(i))
  41. return lineLabel
  42. }
  43. getGeometryCount() {
  44. let count = 0
  45. if (!this.elt[0].isNull()) count++
  46. if (!this.elt[1].isNull()) count++
  47. return count
  48. }
  49. setAllLocations(geomIndex, location) {
  50. this.elt[geomIndex].setAllLocations(location)
  51. }
  52. isNull(geomIndex) {
  53. return this.elt[geomIndex].isNull()
  54. }
  55. setAllLocationsIfNull() {
  56. if (arguments.length === 1) {
  57. const location = arguments[0]
  58. this.setAllLocationsIfNull(0, location)
  59. this.setAllLocationsIfNull(1, location)
  60. } else if (arguments.length === 2) {
  61. const geomIndex = arguments[0], location = arguments[1]
  62. this.elt[geomIndex].setAllLocationsIfNull(location)
  63. }
  64. }
  65. isLine(geomIndex) {
  66. return this.elt[geomIndex].isLine()
  67. }
  68. merge(lbl) {
  69. for (let i = 0; i < 2; i++)
  70. if (this.elt[i] === null && lbl.elt[i] !== null)
  71. this.elt[i] = new TopologyLocation(lbl.elt[i])
  72. else
  73. this.elt[i].merge(lbl.elt[i])
  74. }
  75. flip() {
  76. this.elt[0].flip()
  77. this.elt[1].flip()
  78. }
  79. getLocation() {
  80. if (arguments.length === 1) {
  81. const geomIndex = arguments[0]
  82. return this.elt[geomIndex].get(Position.ON)
  83. } else if (arguments.length === 2) {
  84. const geomIndex = arguments[0], posIndex = arguments[1]
  85. return this.elt[geomIndex].get(posIndex)
  86. }
  87. }
  88. toString() {
  89. const buf = new StringBuffer()
  90. if (this.elt[0] !== null) {
  91. buf.append('A:')
  92. buf.append(this.elt[0].toString())
  93. }
  94. if (this.elt[1] !== null) {
  95. buf.append(' B:')
  96. buf.append(this.elt[1].toString())
  97. }
  98. return buf.toString()
  99. }
  100. isArea() {
  101. if (arguments.length === 0) {
  102. return this.elt[0].isArea() || this.elt[1].isArea()
  103. } else if (arguments.length === 1) {
  104. const geomIndex = arguments[0]
  105. return this.elt[geomIndex].isArea()
  106. }
  107. }
  108. isAnyNull(geomIndex) {
  109. return this.elt[geomIndex].isAnyNull()
  110. }
  111. setLocation() {
  112. if (arguments.length === 2) {
  113. const geomIndex = arguments[0], location = arguments[1]
  114. this.elt[geomIndex].setLocation(Position.ON, location)
  115. } else if (arguments.length === 3) {
  116. const geomIndex = arguments[0], posIndex = arguments[1], location = arguments[2]
  117. this.elt[geomIndex].setLocation(posIndex, location)
  118. }
  119. }
  120. isEqualOnSide(lbl, side) {
  121. return this.elt[0].isEqualOnSide(lbl.elt[0], side) && this.elt[1].isEqualOnSide(lbl.elt[1], side)
  122. }
  123. allPositionsEqual(geomIndex, loc) {
  124. return this.elt[geomIndex].allPositionsEqual(loc)
  125. }
  126. toLine(geomIndex) {
  127. if (this.elt[geomIndex].isArea()) this.elt[geomIndex] = new TopologyLocation(this.elt[geomIndex].location[0])
  128. }
  129. }