monkey.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import WKTWriter from './io/WKTWriter'
  2. import IsValidOp from './operation/valid/IsValidOp'
  3. import InteriorPointArea from './algorithm/InteriorPointArea'
  4. import UnaryUnionOp from './operation/union/UnaryUnionOp'
  5. import UnionOp from './operation/union/UnionOp'
  6. import InteriorPointLine from './algorithm/InteriorPointLine'
  7. import IsSimpleOp from './operation/IsSimpleOp'
  8. import BufferOp from './operation/buffer/BufferOp'
  9. import ConvexHull from './algorithm/ConvexHull'
  10. import Centroid from './algorithm/Centroid'
  11. import RelateOp from './operation/relate/RelateOp'
  12. import InteriorPointPoint from './algorithm/InteriorPointPoint'
  13. import DistanceOp from './operation/distance/DistanceOp'
  14. import OverlayOp from './operation/overlay/OverlayOp'
  15. import BoundaryOp from './operation/BoundaryOp'
  16. import Geometry from './geom/Geometry'
  17. import LineString from './geom/LineString'
  18. import MultiLineString from './geom/MultiLineString'
  19. LineString.prototype.getBoundary = function() {
  20. return BoundaryOp.getBoundary(this)
  21. }
  22. MultiLineString.prototype.getBoundary = function() {
  23. return BoundaryOp.getBoundary(this)
  24. }
  25. Geometry.prototype.equalsTopo = function(g) {
  26. return RelateOp.equalsTopo(this, g)
  27. }
  28. Geometry.prototype.equals = function(g) {
  29. if (g === null) return false
  30. return RelateOp.equalsTopo(this, g)
  31. }
  32. Geometry.prototype.union = function() {
  33. if (arguments.length === 0) {
  34. return UnaryUnionOp.union(this)
  35. } else if (arguments.length === 1) {
  36. const other = arguments[0]
  37. return UnionOp.union(this, other)
  38. }
  39. }
  40. Geometry.prototype.isValid = function() {
  41. return IsValidOp.isValid(this)
  42. }
  43. Geometry.prototype.intersection = function(other) {
  44. return OverlayOp.intersection(this, other)
  45. }
  46. Geometry.prototype.covers = function(g) {
  47. return RelateOp.covers(this, g)
  48. }
  49. Geometry.prototype.coveredBy = function(g) {
  50. return RelateOp.covers(g, this)
  51. }
  52. Geometry.prototype.touches = function(g) {
  53. return RelateOp.touches(this, g)
  54. }
  55. Geometry.prototype.intersects = function(g) {
  56. return RelateOp.intersects(this, g)
  57. }
  58. Geometry.prototype.within = function(g) {
  59. return RelateOp.contains(g, this)
  60. }
  61. Geometry.prototype.overlaps = function(g) {
  62. return RelateOp.overlaps(this, g)
  63. }
  64. Geometry.prototype.disjoint = function(g) {
  65. return RelateOp.disjoint(this, g)
  66. }
  67. Geometry.prototype.crosses = function(g) {
  68. return RelateOp.crosses(this, g)
  69. }
  70. Geometry.prototype.buffer = function() {
  71. if (arguments.length === 1) {
  72. const distance = arguments[0]
  73. return BufferOp.bufferOp(this, distance)
  74. } else if (arguments.length === 2) {
  75. const distance = arguments[0]; const quadrantSegments = arguments[1]
  76. return BufferOp.bufferOp(this, distance, quadrantSegments)
  77. } else if (arguments.length === 3) {
  78. const distance = arguments[0]; const quadrantSegments = arguments[1]; const endCapStyle = arguments[2]
  79. return BufferOp.bufferOp(this, distance, quadrantSegments, endCapStyle)
  80. }
  81. }
  82. Geometry.prototype.convexHull = function() {
  83. return new ConvexHull(this).getConvexHull()
  84. }
  85. Geometry.prototype.relate = function() {
  86. if (arguments.length === 1) {
  87. const geometry = arguments[0]
  88. return RelateOp.relate(this, geometry)
  89. } else if (arguments.length === 2) {
  90. const geometry = arguments[0]; const intersectionPattern = arguments[1]
  91. return RelateOp.relate(this, geometry).matches(intersectionPattern)
  92. }
  93. }
  94. Geometry.prototype.getCentroid = function() {
  95. if (this.isEmpty()) return this._factory.createPoint()
  96. const centPt = Centroid.getCentroid(this)
  97. return this.createPointFromInternalCoord(centPt, this)
  98. }
  99. Geometry.prototype.getInteriorPoint = function() {
  100. if (this.isEmpty()) return this._factory.createPoint()
  101. let intPt = null
  102. const dim = this.getDimension()
  103. if (dim === 0)
  104. intPt = new InteriorPointPoint(this)
  105. else if (dim === 1)
  106. intPt = new InteriorPointLine(this)
  107. else intPt = new InteriorPointArea(this)
  108. const interiorPt = intPt.getInteriorPoint()
  109. return this.createPointFromInternalCoord(interiorPt, this)
  110. }
  111. Geometry.prototype.symDifference = function(other) {
  112. return OverlayOp.symDifference(this, other)
  113. }
  114. Geometry.prototype.createPointFromInternalCoord = function(coord, exemplar) {
  115. exemplar.getPrecisionModel().makePrecise(coord)
  116. return exemplar.getFactory().createPoint(coord)
  117. }
  118. Geometry.prototype.toText = function() {
  119. const writer = new WKTWriter()
  120. return writer.write(this)
  121. }
  122. Geometry.prototype.toString = function() {
  123. this.toText()
  124. }
  125. Geometry.prototype.contains = function(g) {
  126. return RelateOp.contains(this, g)
  127. }
  128. Geometry.prototype.difference = function(other) {
  129. return OverlayOp.difference(this, other)
  130. }
  131. Geometry.prototype.isSimple = function() {
  132. const op = new IsSimpleOp(this)
  133. return op.isSimple()
  134. }
  135. Geometry.prototype.isWithinDistance = function(geom, distance) {
  136. const envDist = this.getEnvelopeInternal().distance(geom.getEnvelopeInternal())
  137. if (envDist > distance) return false
  138. return DistanceOp.isWithinDistance(this, geom, distance)
  139. }
  140. Geometry.prototype.distance = function(g) {
  141. return DistanceOp.distance(this, g)
  142. }