LengthIndexedLine.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import LengthIndexOfPoint from './LengthIndexOfPoint'
  2. import LocationIndexOfLine from './LocationIndexOfLine'
  3. import LengthLocationMap from './LengthLocationMap'
  4. import ExtractLineByLocation from './ExtractLineByLocation'
  5. export default class LengthIndexedLine {
  6. constructor() {
  7. LengthIndexedLine.constructor_.apply(this, arguments)
  8. }
  9. static constructor_() {
  10. this._linearGeom = null
  11. const linearGeom = arguments[0]
  12. this._linearGeom = linearGeom
  13. }
  14. clampIndex(index) {
  15. const posIndex = this.positiveIndex(index)
  16. const startIndex = this.getStartIndex()
  17. if (posIndex < startIndex) return startIndex
  18. const endIndex = this.getEndIndex()
  19. if (posIndex > endIndex) return endIndex
  20. return posIndex
  21. }
  22. locationOf() {
  23. if (arguments.length === 1) {
  24. const index = arguments[0]
  25. return LengthLocationMap.getLocation(this._linearGeom, index)
  26. } else if (arguments.length === 2) {
  27. const index = arguments[0], resolveLower = arguments[1]
  28. return LengthLocationMap.getLocation(this._linearGeom, index, resolveLower)
  29. }
  30. }
  31. project(pt) {
  32. return LengthIndexOfPoint.indexOf(this._linearGeom, pt)
  33. }
  34. positiveIndex(index) {
  35. if (index >= 0.0) return index
  36. return this._linearGeom.getLength() + index
  37. }
  38. extractPoint() {
  39. if (arguments.length === 1) {
  40. const index = arguments[0]
  41. const loc = LengthLocationMap.getLocation(this._linearGeom, index)
  42. return loc.getCoordinate(this._linearGeom)
  43. } else if (arguments.length === 2) {
  44. const index = arguments[0], offsetDistance = arguments[1]
  45. const loc = LengthLocationMap.getLocation(this._linearGeom, index)
  46. const locLow = loc.toLowest(this._linearGeom)
  47. return locLow.getSegment(this._linearGeom).pointAlongOffset(locLow.getSegmentFraction(), offsetDistance)
  48. }
  49. }
  50. isValidIndex(index) {
  51. return index >= this.getStartIndex() && index <= this.getEndIndex()
  52. }
  53. getEndIndex() {
  54. return this._linearGeom.getLength()
  55. }
  56. getStartIndex() {
  57. return 0.0
  58. }
  59. indexOfAfter(pt, minIndex) {
  60. return LengthIndexOfPoint.indexOfAfter(this._linearGeom, pt, minIndex)
  61. }
  62. extractLine(startIndex, endIndex) {
  63. const startIndex2 = this.clampIndex(startIndex)
  64. const endIndex2 = this.clampIndex(endIndex)
  65. const resolveStartLower = startIndex2 === endIndex2
  66. const startLoc = this.locationOf(startIndex2, resolveStartLower)
  67. const endLoc = this.locationOf(endIndex2)
  68. return ExtractLineByLocation.extract(this._linearGeom, startLoc, endLoc)
  69. }
  70. indexOf(pt) {
  71. return LengthIndexOfPoint.indexOf(this._linearGeom, pt)
  72. }
  73. indicesOf(subLine) {
  74. const locIndex = LocationIndexOfLine.indicesOf(this._linearGeom, subLine)
  75. const index = [LengthLocationMap.getLength(this._linearGeom, locIndex[0]), LengthLocationMap.getLength(this._linearGeom, locIndex[1])]
  76. return index
  77. }
  78. }