Geometry.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import IllegalArgumentException from '../../../../java/lang/IllegalArgumentException'
  2. import GeometryComponentFilter from './GeometryComponentFilter'
  3. import Comparable from '../../../../java/lang/Comparable'
  4. import Cloneable from '../../../../java/lang/Cloneable'
  5. import Serializable from '../../../../java/io/Serializable'
  6. import Envelope from './Envelope'
  7. export default class Geometry {
  8. constructor() {
  9. Geometry.constructor_.apply(this, arguments)
  10. }
  11. isGeometryCollection() {
  12. return this.getTypeCode() === Geometry.TYPECODE_GEOMETRYCOLLECTION
  13. }
  14. getFactory() {
  15. return this._factory
  16. }
  17. getGeometryN(n) {
  18. return this
  19. }
  20. getArea() {
  21. return 0.0
  22. }
  23. isRectangle() {
  24. return false
  25. }
  26. equalsExact(other) {
  27. return this === other || this.equalsExact(other, 0)
  28. }
  29. geometryChanged() {
  30. this.apply(Geometry.geometryChangedFilter)
  31. }
  32. geometryChangedAction() {
  33. this._envelope = null
  34. }
  35. equalsNorm(g) {
  36. if (g === null) return false
  37. return this.norm().equalsExact(g.norm())
  38. }
  39. getLength() {
  40. return 0.0
  41. }
  42. getNumGeometries() {
  43. return 1
  44. }
  45. compareTo() {
  46. let other
  47. if (arguments.length === 1) {
  48. const o = arguments[0]
  49. other = o
  50. if (this.getTypeCode() !== other.getTypeCode()) return this.getTypeCode() - other.getTypeCode()
  51. if (this.isEmpty() && other.isEmpty()) return 0
  52. if (this.isEmpty()) return -1
  53. if (other.isEmpty()) return 1
  54. return this.compareToSameClass(o)
  55. } else if (arguments.length === 2) {
  56. const o = arguments[0]; const comp = arguments[1]
  57. other = o
  58. if (this.getTypeCode() !== other.getTypeCode()) return this.getTypeCode() - other.getTypeCode()
  59. if (this.isEmpty() && other.isEmpty()) return 0
  60. if (this.isEmpty()) return -1
  61. if (other.isEmpty()) return 1
  62. return this.compareToSameClass(o, comp)
  63. }
  64. }
  65. getUserData() {
  66. return this._userData
  67. }
  68. getSRID() {
  69. return this._SRID
  70. }
  71. getEnvelope() {
  72. return this.getFactory().toGeometry(this.getEnvelopeInternal())
  73. }
  74. checkNotGeometryCollection(g) {
  75. if (g.getTypeCode() === Geometry.TYPECODE_GEOMETRYCOLLECTION) throw new IllegalArgumentException('This method does not support GeometryCollection arguments')
  76. }
  77. equal(a, b, tolerance) {
  78. if (tolerance === 0) return a.equals(b)
  79. return a.distance(b) <= tolerance
  80. }
  81. norm() {
  82. const copy = this.copy()
  83. copy.normalize()
  84. return copy
  85. }
  86. reverse() {
  87. const res = this.reverseInternal()
  88. if (this.envelope != null) res.envelope = this.envelope.copy()
  89. res.setSRID(this.getSRID())
  90. return res
  91. }
  92. copy() {
  93. const copy = this.copyInternal()
  94. copy.envelope = this._envelope == null ? null : this._envelope.copy()
  95. copy._SRID = this._SRID
  96. copy._userData = this._userData
  97. return copy
  98. }
  99. getPrecisionModel() {
  100. return this._factory.getPrecisionModel()
  101. }
  102. getEnvelopeInternal() {
  103. if (this._envelope === null) this._envelope = this.computeEnvelopeInternal()
  104. return new Envelope(this._envelope)
  105. }
  106. setSRID(SRID) {
  107. this._SRID = SRID
  108. }
  109. setUserData(userData) {
  110. this._userData = userData
  111. }
  112. compare(a, b) {
  113. const i = a.iterator()
  114. const j = b.iterator()
  115. while (i.hasNext() && j.hasNext()) {
  116. const aElement = i.next()
  117. const bElement = j.next()
  118. const comparison = aElement.compareTo(bElement)
  119. if (comparison !== 0) return comparison
  120. }
  121. if (i.hasNext()) return 1
  122. if (j.hasNext()) return -1
  123. return 0
  124. }
  125. hashCode() {
  126. return this.getEnvelopeInternal().hashCode()
  127. }
  128. isEquivalentClass(other) {
  129. return this.getClass() === other.getClass()
  130. }
  131. isGeometryCollectionOrDerived() {
  132. if (this.getTypeCode() === Geometry.TYPECODE_GEOMETRYCOLLECTION || this.getTypeCode() === Geometry.TYPECODE_MULTIPOINT || this.getTypeCode() === Geometry.TYPECODE_MULTILINESTRING || this.getTypeCode() === Geometry.TYPECODE_MULTIPOLYGON) return true
  133. return false
  134. }
  135. get interfaces_() {
  136. return [Cloneable, Comparable, Serializable]
  137. }
  138. getClass() {
  139. return Geometry
  140. }
  141. static hasNonEmptyElements(geometries) {
  142. for (let i = 0; i < geometries.length; i++)
  143. if (!geometries[i].isEmpty()) return true
  144. return false
  145. }
  146. static hasNullElements(array) {
  147. for (let i = 0; i < array.length; i++)
  148. if (array[i] === null) return true
  149. return false
  150. }
  151. }
  152. Geometry.constructor_ = function(factory) {
  153. if (!factory) return
  154. this._envelope = null
  155. this._userData = null
  156. this._factory = factory
  157. this._SRID = factory.getSRID()
  158. }
  159. Geometry.TYPECODE_POINT = 0
  160. Geometry.TYPECODE_MULTIPOINT = 1
  161. Geometry.TYPECODE_LINESTRING = 2
  162. Geometry.TYPECODE_LINEARRING = 3
  163. Geometry.TYPECODE_MULTILINESTRING = 4
  164. Geometry.TYPECODE_POLYGON = 5
  165. Geometry.TYPECODE_MULTIPOLYGON = 6
  166. Geometry.TYPECODE_GEOMETRYCOLLECTION = 7
  167. Geometry.TYPENAME_POINT = 'Point'
  168. Geometry.TYPENAME_MULTIPOINT = 'MultiPoint'
  169. Geometry.TYPENAME_LINESTRING = 'LineString'
  170. Geometry.TYPENAME_LINEARRING = 'LinearRing'
  171. Geometry.TYPENAME_MULTILINESTRING = 'MultiLineString'
  172. Geometry.TYPENAME_POLYGON = 'Polygon'
  173. Geometry.TYPENAME_MULTIPOLYGON = 'MultiPolygon'
  174. Geometry.TYPENAME_GEOMETRYCOLLECTION = 'GeometryCollection'
  175. Geometry.geometryChangedFilter = {
  176. get interfaces_() {
  177. return [GeometryComponentFilter]
  178. },
  179. filter(geom) {
  180. geom.geometryChangedAction()
  181. }
  182. }