{"id":16386511,"url":"https://github.com/rowanwins/visibility-graph","last_synced_at":"2025-03-21T02:31:25.339Z","repository":{"id":44621145,"uuid":"116345506","full_name":"rowanwins/visibility-graph","owner":"rowanwins","description":"Visibility graph implementation to support shortest path calculations such as dijkstra or a-star","archived":false,"fork":false,"pushed_at":"2023-06-14T14:38:36.000Z","size":3752,"stargazers_count":51,"open_issues_count":7,"forks_count":7,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-10-12T04:17:36.657Z","etag":null,"topics":["javascript","visibility-graph"],"latest_commit_sha":null,"homepage":"https://rowanwins.github.io/visibility-graph/debug/dist","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rowanwins.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-01-05T05:23:21.000Z","updated_at":"2024-10-04T13:19:59.000Z","dependencies_parsed_at":"2023-02-08T16:46:39.746Z","dependency_job_id":null,"html_url":"https://github.com/rowanwins/visibility-graph","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rowanwins%2Fvisibility-graph","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rowanwins%2Fvisibility-graph/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rowanwins%2Fvisibility-graph/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rowanwins%2Fvisibility-graph/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rowanwins","download_url":"https://codeload.github.com/rowanwins/visibility-graph/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221811386,"owners_count":16884305,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","visibility-graph"],"created_at":"2024-10-11T04:17:28.471Z","updated_at":"2024-10-28T09:07:44.779Z","avatar_url":"https://github.com/rowanwins.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# visibility-graph.js\nVisibility graph implementation to support shortest path calculations such as [dijkstra](https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm) or [a-star](https://en.wikipedia.org/wiki/A*_search_algorithm).\n\n### [Demo](https://rowanwins.github.io/visibility-graph/debug/dist/index.html)\n\n## Documentation\n\nThis library exposes a `VisibilityGraph` class\n\n#### API\n`new VisibilityGraph(geojson, ?existingGraph)` - creates a new instance using a [Polygon](http://geojson.win/#appendix-A.3) or [MultiPolygon](http://geojson.win/#appendix-A.6) feature or geometry. Optionally, if you have an existing graph that you've previously generated using the `.saveGraphToJson()` you can pass it in as a argument.\n\n`.saveGraphToJson()` - Returns a json representation of the visibility graph which can be saved to disk and then restored by passing a second argument to the class constructor.\n\n`.addStartAndEndPointsToGraph(origin, destination)` - Takes 2 geojson point features, one for the origin, and one for the destination, and returns the newly added nodes in an object `{startNode: ngraphNode, endNode: ngraphNode}`. Each time this is called any previously added start and end points are removed from the graph.\n\n`.getNodeIdByLatLon([lat, lon])` - Returns a graph node ID that matches the lat lon.\n\n\n\n## Example\n\n````js\n  import VisibilityGraph from 'visibility-graph.js'\n  import path from 'ngraph.path'\n\n  // Create the visibility graph from the geojson data\n  const vg = new VisibilityGraph(geojson)\n\n  // Use the 'ngraph.path' library to find a way \n  //through the newly created visibility graph\n  const pathFinder = path.nba(vg.graph, {\n    distance (fromNode, toNode) {\n      const dx = fromNode.data.x - toNode.data.x\n      const dy = fromNode.data.y - toNode.data.y\n      return Math.sqrt(dx * dx + dy * dy)\n    }\n  })\n\n  // Add the start and endpoints to the graph  \n  const startEndNodes = vg.addStartAndEndPointsToGraph(\n    {type: 'Feature', geometry: {type: 'Point', coordinates: [0, 0]}},\n    {type: 'Feature', geometry: {type: 'Point', coordinates: [10, 10]}}\n  )\n  \n  // And finally retrive the optimal path \n  const optimalPath = pathFinder.find(\n    startEndNodes.startNode.nodeId,\n    startEndNodes.endNode.nodeId\n  )\n\n  ````\n\n**NOTE:** If you get occassional issues with how your edges are being linked try reducing the precision of your coordinates (eg 8 decimal places). \n\n## Using with other packages\n- Path finding can be achieved with the [ngraph.path](https://github.com/anvaka/ngraph.path) package.\n\n\n## Performance\nThe process of creating a visibility graph can be slow depending on the number of vertices in your input geometry.\n\n| Scenario  | Nodes/Vertices  | Create Graph  | Time Reload Graph / Graph Size  |\n| --------- | --------------- | ------------- | ------------------------------- |\n| Australia | 250             | 1 second      | 300kb                           |\n| Asia      | 1400            | 4 seconds     | 100ms / 5.2MB                   |\n| World     | 4400            | 20 seconds    |                                 |\n\n\nDepending on your requirements you may also be able to convert your input data if it has concave polygons, to only having convex polygons, this may reduce redundant nodes in the graph.\n\n\n## References \u0026 Credits\n* Based on [pyvisgraph](https://github.com/TaipanRex/pyvisgraph) and associated blog posts\n  * [Distance Tables Part 1: Defining the Problem](https://taipanrex.github.io/2016/09/17/Distance-Tables-Part-1-Defining-the-Problem.html)\n  * [Distance Tables Part 2: Lee's Visibility Graph Algorithm](https://taipanrex.github.io/2016/10/19/Distance-Tables-Part-2-Lees-Visibility-Graph-Algorithm.html)\n* [Lee's o(n\u003csup\u003e2\u003c/sup\u003e log n) Visibility Graph Algorithm](https://github.com/davetcoleman/visibility_graph/blob/master/Visibility_Graph_Algorithm.pdf) paper by Dave Coleman\n* [Intro to path finding](https://www.redblobgames.com/pathfinding/)\n  * And specifically a bit about [visibility graphs](https://www.redblobgames.com/pathfinding/visibility-graphs/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frowanwins%2Fvisibility-graph","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frowanwins%2Fvisibility-graph","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frowanwins%2Fvisibility-graph/lists"}