OffsetCurveSetBuilder.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import Location from '../../geom/Location'
  2. import LineString from '../../geom/LineString'
  3. import Position from '../../geomgraph/Position'
  4. import Point from '../../geom/Point'
  5. import NodedSegmentString from '../../noding/NodedSegmentString'
  6. import Polygon from '../../geom/Polygon'
  7. import MultiPoint from '../../geom/MultiPoint'
  8. import LinearRing from '../../geom/LinearRing'
  9. import Orientation from '../../algorithm/Orientation'
  10. import MultiPolygon from '../../geom/MultiPolygon'
  11. import Label from '../../geomgraph/Label'
  12. import GeometryCollection from '../../geom/GeometryCollection'
  13. import UnsupportedOperationException from '../../../../../java/lang/UnsupportedOperationException'
  14. import CoordinateArrays from '../../geom/CoordinateArrays'
  15. import ArrayList from '../../../../../java/util/ArrayList'
  16. import Distance from '../../algorithm/Distance'
  17. import MultiLineString from '../../geom/MultiLineString'
  18. import Triangle from '../../geom/Triangle'
  19. export default class OffsetCurveSetBuilder {
  20. constructor() {
  21. OffsetCurveSetBuilder.constructor_.apply(this, arguments)
  22. }
  23. static constructor_() {
  24. this._inputGeom = null
  25. this._distance = null
  26. this._curveBuilder = null
  27. this._curveList = new ArrayList()
  28. const inputGeom = arguments[0], distance = arguments[1], curveBuilder = arguments[2]
  29. this._inputGeom = inputGeom
  30. this._distance = distance
  31. this._curveBuilder = curveBuilder
  32. }
  33. addRingSide(coord, offsetDistance, side, cwLeftLoc, cwRightLoc) {
  34. if (offsetDistance === 0.0 && coord.length < LinearRing.MINIMUM_VALID_SIZE) return null
  35. let leftLoc = cwLeftLoc
  36. let rightLoc = cwRightLoc
  37. if (coord.length >= LinearRing.MINIMUM_VALID_SIZE && Orientation.isCCW(coord)) {
  38. leftLoc = cwRightLoc
  39. rightLoc = cwLeftLoc
  40. side = Position.opposite(side)
  41. }
  42. const curve = this._curveBuilder.getRingCurve(coord, side, offsetDistance)
  43. this.addCurve(curve, leftLoc, rightLoc)
  44. }
  45. addRingBothSides(coord, distance) {
  46. this.addRingSide(coord, distance, Position.LEFT, Location.EXTERIOR, Location.INTERIOR)
  47. this.addRingSide(coord, distance, Position.RIGHT, Location.INTERIOR, Location.EXTERIOR)
  48. }
  49. addPoint(p) {
  50. if (this._distance <= 0.0) return null
  51. const coord = p.getCoordinates()
  52. const curve = this._curveBuilder.getLineCurve(coord, this._distance)
  53. this.addCurve(curve, Location.EXTERIOR, Location.INTERIOR)
  54. }
  55. addPolygon(p) {
  56. let offsetDistance = this._distance
  57. let offsetSide = Position.LEFT
  58. if (this._distance < 0.0) {
  59. offsetDistance = -this._distance
  60. offsetSide = Position.RIGHT
  61. }
  62. const shell = p.getExteriorRing()
  63. const shellCoord = CoordinateArrays.removeRepeatedPoints(shell.getCoordinates())
  64. if (this._distance < 0.0 && this.isErodedCompletely(shell, this._distance)) return null
  65. if (this._distance <= 0.0 && shellCoord.length < 3) return null
  66. this.addRingSide(shellCoord, offsetDistance, offsetSide, Location.EXTERIOR, Location.INTERIOR)
  67. for (let i = 0; i < p.getNumInteriorRing(); i++) {
  68. const hole = p.getInteriorRingN(i)
  69. const holeCoord = CoordinateArrays.removeRepeatedPoints(hole.getCoordinates())
  70. if (this._distance > 0.0 && this.isErodedCompletely(hole, -this._distance)) continue
  71. this.addRingSide(holeCoord, offsetDistance, Position.opposite(offsetSide), Location.INTERIOR, Location.EXTERIOR)
  72. }
  73. }
  74. isTriangleErodedCompletely(triangleCoord, bufferDistance) {
  75. const tri = new Triangle(triangleCoord[0], triangleCoord[1], triangleCoord[2])
  76. const inCentre = tri.inCentre()
  77. const distToCentre = Distance.pointToSegment(inCentre, tri.p0, tri.p1)
  78. return distToCentre < Math.abs(bufferDistance)
  79. }
  80. addLineString(line) {
  81. if (this._curveBuilder.isLineOffsetEmpty(this._distance)) return null
  82. const coord = CoordinateArrays.removeRepeatedPoints(line.getCoordinates())
  83. if (CoordinateArrays.isRing(coord) && !this._curveBuilder.getBufferParameters().isSingleSided()) {
  84. this.addRingBothSides(coord, this._distance)
  85. } else {
  86. const curve = this._curveBuilder.getLineCurve(coord, this._distance)
  87. this.addCurve(curve, Location.EXTERIOR, Location.INTERIOR)
  88. }
  89. }
  90. addCurve(coord, leftLoc, rightLoc) {
  91. if (coord === null || coord.length < 2) return null
  92. const e = new NodedSegmentString(coord, new Label(0, Location.BOUNDARY, leftLoc, rightLoc))
  93. this._curveList.add(e)
  94. }
  95. getCurves() {
  96. this.add(this._inputGeom)
  97. return this._curveList
  98. }
  99. add(g) {
  100. if (g.isEmpty()) return null
  101. if (g instanceof Polygon) this.addPolygon(g); else if (g instanceof LineString) this.addLineString(g); else if (g instanceof Point) this.addPoint(g); else if (g instanceof MultiPoint) this.addCollection(g); else if (g instanceof MultiLineString) this.addCollection(g); else if (g instanceof MultiPolygon) this.addCollection(g); else if (g instanceof GeometryCollection) this.addCollection(g); else throw new UnsupportedOperationException(g.getGeometryType())
  102. }
  103. isErodedCompletely(ring, bufferDistance) {
  104. const ringCoord = ring.getCoordinates()
  105. if (ringCoord.length < 4) return bufferDistance < 0
  106. if (ringCoord.length === 4) return this.isTriangleErodedCompletely(ringCoord, bufferDistance)
  107. const env = ring.getEnvelopeInternal()
  108. const envMinDimension = Math.min(env.getHeight(), env.getWidth())
  109. if (bufferDistance < 0.0 && 2 * Math.abs(bufferDistance) > envMinDimension) return true
  110. return false
  111. }
  112. addCollection(gc) {
  113. for (let i = 0; i < gc.getNumGeometries(); i++) {
  114. const g = gc.getGeometryN(i)
  115. this.add(g)
  116. }
  117. }
  118. }