EdgeGraphBuilder.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import LineString from '../geom/LineString'
  2. import Geometry from '../geom/Geometry'
  3. import hasInterface from '../../../../hasInterface'
  4. import Collection from '../../../../java/util/Collection'
  5. import EdgeGraph from './EdgeGraph'
  6. import GeometryComponentFilter from '../geom/GeometryComponentFilter'
  7. export default class EdgeGraphBuilder {
  8. constructor() {
  9. EdgeGraphBuilder.constructor_.apply(this, arguments)
  10. }
  11. static constructor_() {
  12. this._graph = new EdgeGraph()
  13. }
  14. static build(geoms) {
  15. const builder = new EdgeGraphBuilder()
  16. builder.add(geoms)
  17. return builder.getGraph()
  18. }
  19. add() {
  20. if (arguments[0] instanceof Geometry) {
  21. const geometry = arguments[0]
  22. geometry.apply(new (class {
  23. get interfaces_() {
  24. return [GeometryComponentFilter]
  25. }
  26. filter(component) {
  27. if (component instanceof LineString)
  28. this.add(component)
  29. }
  30. })())
  31. } else if (hasInterface(arguments[0], Collection)) {
  32. const geometries = arguments[0]
  33. for (let i = geometries.iterator(); i.hasNext(); ) {
  34. const geometry = i.next()
  35. this.add(geometry)
  36. }
  37. } else if (arguments[0] instanceof LineString) {
  38. const lineString = arguments[0]
  39. const seq = lineString.getCoordinateSequence()
  40. for (let i = 1; i < seq.size(); i++)
  41. this._graph.addEdge(seq.getCoordinate(i - 1), seq.getCoordinate(i))
  42. }
  43. }
  44. getGraph() {
  45. return this._graph
  46. }
  47. }