Point.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import Geometry from './Geometry'
  2. import CoordinateFilter from './CoordinateFilter'
  3. import hasInterface from '../../../../hasInterface'
  4. import GeometryComponentFilter from './GeometryComponentFilter'
  5. import Dimension from './Dimension'
  6. import GeometryFilter from './GeometryFilter'
  7. import CoordinateSequenceFilter from './CoordinateSequenceFilter'
  8. import Puntal from './Puntal'
  9. import Envelope from './Envelope'
  10. import Assert from '../util/Assert'
  11. export default class Point extends Geometry {
  12. constructor() {
  13. super()
  14. Point.constructor_.apply(this, arguments)
  15. }
  16. static constructor_() {
  17. this._coordinates = null
  18. const coordinates = arguments[0], factory = arguments[1]
  19. Geometry.constructor_.call(this, factory)
  20. this.init(coordinates)
  21. }
  22. computeEnvelopeInternal() {
  23. if (this.isEmpty())
  24. return new Envelope()
  25. const env = new Envelope()
  26. env.expandToInclude(this._coordinates.getX(0), this._coordinates.getY(0))
  27. return env
  28. }
  29. getCoordinates() {
  30. return this.isEmpty() ? [] : [this.getCoordinate()]
  31. }
  32. copyInternal() {
  33. return new Point(this._coordinates.copy(), this._factory)
  34. }
  35. equalsExact() {
  36. if (arguments.length === 2 && (typeof arguments[1] === 'number' && arguments[0] instanceof Geometry)) {
  37. const other = arguments[0], tolerance = arguments[1]
  38. if (!this.isEquivalentClass(other))
  39. return false
  40. if (this.isEmpty() && other.isEmpty())
  41. return true
  42. if (this.isEmpty() !== other.isEmpty())
  43. return false
  44. return this.equal(other.getCoordinate(), this.getCoordinate(), tolerance)
  45. } else {
  46. return super.equalsExact.apply(this, arguments)
  47. }
  48. }
  49. normalize() {}
  50. getCoordinate() {
  51. return this._coordinates.size() !== 0 ? this._coordinates.getCoordinate(0) : null
  52. }
  53. getBoundaryDimension() {
  54. return Dimension.FALSE
  55. }
  56. reverseInternal() {
  57. return this.getFactory().createPoint(this._coordinates.copy())
  58. }
  59. getTypeCode() {
  60. return Geometry.TYPECODE_POINT
  61. }
  62. getDimension() {
  63. return 0
  64. }
  65. getNumPoints() {
  66. return this.isEmpty() ? 0 : 1
  67. }
  68. getX() {
  69. if (this.getCoordinate() === null)
  70. throw new IllegalStateException('getX called on empty Point')
  71. return this.getCoordinate().x
  72. }
  73. compareToSameClass() {
  74. if (arguments.length === 1) {
  75. const other = arguments[0]
  76. const point = other
  77. return this.getCoordinate().compareTo(point.getCoordinate())
  78. } else if (arguments.length === 2) {
  79. const other = arguments[0], comp = arguments[1]
  80. const point = other
  81. return comp.compare(this._coordinates, point._coordinates)
  82. }
  83. }
  84. apply() {
  85. if (hasInterface(arguments[0], CoordinateFilter)) {
  86. const filter = arguments[0]
  87. if (this.isEmpty())
  88. return null
  89. filter.filter(this.getCoordinate())
  90. } else if (hasInterface(arguments[0], CoordinateSequenceFilter)) {
  91. const filter = arguments[0]
  92. if (this.isEmpty()) return null
  93. filter.filter(this._coordinates, 0)
  94. if (filter.isGeometryChanged()) this.geometryChanged()
  95. } else if (hasInterface(arguments[0], GeometryFilter)) {
  96. const filter = arguments[0]
  97. filter.filter(this)
  98. } else if (hasInterface(arguments[0], GeometryComponentFilter)) {
  99. const filter = arguments[0]
  100. filter.filter(this)
  101. }
  102. }
  103. getBoundary() {
  104. return this.getFactory().createGeometryCollection()
  105. }
  106. getGeometryType() {
  107. return Geometry.TYPENAME_POINT
  108. }
  109. getCoordinateSequence() {
  110. return this._coordinates
  111. }
  112. getY() {
  113. if (this.getCoordinate() === null)
  114. throw new IllegalStateException('getY called on empty Point')
  115. return this.getCoordinate().y
  116. }
  117. isEmpty() {
  118. return this._coordinates.size() === 0
  119. }
  120. init(coordinates) {
  121. if (coordinates === null)
  122. coordinates = this.getFactory().getCoordinateSequenceFactory().create([])
  123. Assert.isTrue(coordinates.size() <= 1)
  124. this._coordinates = coordinates
  125. }
  126. isSimple() {
  127. return true
  128. }
  129. get interfaces_() {
  130. return [Puntal]
  131. }
  132. }