Graph.d.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import Node from "./Node";
  2. import Edge from "./Edge";
  3. import EdgeRing from "./EdgeRing";
  4. import { FeatureCollection, LineString, MultiLineString, Feature } from "@turf/helpers";
  5. /**
  6. * Represents a planar graph of edges and nodes that can be used to compute a polygonization.
  7. *
  8. * Although, this class is inspired by GEOS's `geos::operation::polygonize::PolygonizeGraph`,
  9. * it isn't a rewrite. As regards algorithm, this class implements the same logic, but it
  10. * isn't a javascript transcription of the C++ source.
  11. *
  12. * This graph is directed (both directions are created)
  13. */
  14. export default class Graph {
  15. private nodes;
  16. private edges;
  17. /**
  18. * Creates a graph from a GeoJSON.
  19. *
  20. * @param {FeatureCollection<LineString>} geoJson - it must comply with the restrictions detailed in the index
  21. * @returns {Graph} - The newly created graph
  22. * @throws {Error} if geoJson is invalid.
  23. */
  24. static fromGeoJson(geoJson: FeatureCollection<LineString | MultiLineString> | LineString | MultiLineString | Feature<LineString | MultiLineString>): Graph;
  25. /**
  26. * Creates or get a Node.
  27. *
  28. * @param {number[]} coordinates - Coordinates of the node
  29. * @returns {Node} - The created or stored node
  30. */
  31. getNode(coordinates: number[]): Node;
  32. /**
  33. * Adds an Edge and its symetricall.
  34. *
  35. * Edges are added symetrically, i.e.: we also add its symetric
  36. *
  37. * @param {Node} from - Node which starts the Edge
  38. * @param {Node} to - Node which ends the Edge
  39. */
  40. addEdge(from: Node, to: Node): void;
  41. constructor();
  42. /**
  43. * Removes Dangle Nodes (nodes with grade 1).
  44. */
  45. deleteDangles(): void;
  46. /**
  47. * Check if node is dangle, if so, remove it.
  48. *
  49. * It calls itself recursively, removing a dangling node might cause another dangling node
  50. *
  51. * @param {Node} node - Node to check if it's a dangle
  52. */
  53. _removeIfDangle(node: Node): void;
  54. /**
  55. * Delete cut-edges (bridge edges).
  56. *
  57. * The graph will be traversed, all the edges will be labeled according the ring
  58. * in which they are. (The label is a number incremented by 1). Edges with the same
  59. * label are cut-edges.
  60. */
  61. deleteCutEdges(): void;
  62. /**
  63. * Set the `next` property of each Edge.
  64. *
  65. * The graph will be transversed in a CW form, so, we set the next of the symetrical edge as the previous one.
  66. * OuterEdges are sorted CCW.
  67. *
  68. * @param {Node} [node] - If no node is passed, the function calls itself for every node in the Graph
  69. */
  70. _computeNextCWEdges(node?: Node): void;
  71. /**
  72. * Computes the next edge pointers going CCW around the given node, for the given edgering label.
  73. *
  74. * This algorithm has the effect of converting maximal edgerings into minimal edgerings
  75. *
  76. * XXX: method literally transcribed from `geos::operation::polygonize::PolygonizeGraph::computeNextCCWEdges`,
  77. * could be written in a more javascript way.
  78. *
  79. * @param {Node} node - Node
  80. * @param {number} label - Ring's label
  81. */
  82. _computeNextCCWEdges(node: Node, label: number): void;
  83. /**
  84. * Finds rings and labels edges according to which rings are.
  85. *
  86. * The label is a number which is increased for each ring.
  87. *
  88. * @returns {Edge[]} edges that start rings
  89. */
  90. _findLabeledEdgeRings(): Edge[];
  91. /**
  92. * Computes the EdgeRings formed by the edges in this graph.
  93. *
  94. * @returns {EdgeRing[]} - A list of all the EdgeRings in the graph.
  95. */
  96. getEdgeRings(): EdgeRing[];
  97. /**
  98. * Find all nodes in a Maxima EdgeRing which are self-intersection nodes.
  99. *
  100. * @param {Node} startEdge - Start Edge of the Ring
  101. * @returns {Node[]} - intersection nodes
  102. */
  103. _findIntersectionNodes(startEdge: Edge): Node[];
  104. /**
  105. * Get the edge-ring which starts from the provided Edge.
  106. *
  107. * @param {Edge} startEdge - starting edge of the edge ring
  108. * @returns {EdgeRing} - EdgeRing which start Edge is the provided one.
  109. */
  110. _findEdgeRing(startEdge: Edge): EdgeRing;
  111. /**
  112. * Removes a node from the Graph.
  113. *
  114. * It also removes edges asociated to that node
  115. * @param {Node} node - Node to be removed
  116. */
  117. removeNode(node: Node): void;
  118. /**
  119. * Remove edge from the graph and deletes the edge.
  120. *
  121. * @param {Edge} edge - Edge to be removed
  122. */
  123. removeEdge(edge: Edge): void;
  124. }