{"id":13736253,"url":"https://github.com/jblindsay/kdtree","last_synced_at":"2025-02-26T08:19:29.077Z","repository":{"id":100606857,"uuid":"254766544","full_name":"jblindsay/kdtree","owner":"jblindsay","description":"A pure Nim k-d tree implementation for efficient spatial querying of point data","archived":false,"fork":false,"pushed_at":"2020-11-10T20:57:37.000Z","size":308,"stargazers_count":43,"open_issues_count":0,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-01-08T22:06:49.046Z","etag":null,"topics":["gis","spatial-analysis","spatial-data"],"latest_commit_sha":null,"homepage":null,"language":"Nim","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/jblindsay.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,"governance":null}},"created_at":"2020-04-11T00:59:00.000Z","updated_at":"2024-10-14T23:47:04.000Z","dependencies_parsed_at":"2023-06-19T17:13:08.792Z","dependency_job_id":null,"html_url":"https://github.com/jblindsay/kdtree","commit_stats":{"total_commits":17,"total_committers":1,"mean_commits":17.0,"dds":0.0,"last_synced_commit":"87e94490d7014328df60a46c9be4ec76c3807989"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jblindsay%2Fkdtree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jblindsay%2Fkdtree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jblindsay%2Fkdtree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jblindsay%2Fkdtree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jblindsay","download_url":"https://codeload.github.com/jblindsay/kdtree/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240815243,"owners_count":19862028,"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":["gis","spatial-analysis","spatial-data"],"created_at":"2024-08-03T03:01:18.252Z","updated_at":"2025-02-26T08:19:29.043Z","avatar_url":"https://github.com/jblindsay.png","language":"Nim","funding_links":[],"categories":["Data","Nim"],"sub_categories":["Data Structures"],"readme":"# kdtree\n\n**Contents**\n\n1. [Description](#1-description)\n2. [Documentation](#2-documentation)\n3. [Usage](#2-usage)\n\n## 1 Description\n\n**kdtree** is a pure Nim [k-d tree](https://en.wikipedia.org/wiki/K-d_tree) implementation. k-d trees are data structures for performing efficient spatial query operations on point data sets. This implementation is very flexible, allowing for nearest-neighbour (single and multiple), within-radius (circular search areas), and range (rectangular search areas) spatial queries.\n\n## 2 Documentation\n\nDocumentation for `kdtree` can be found [here](https://jblindsay.github.io/kdtree/kdtree.html).\n\n## 3 Usage\n\n```nim\nimport random, strformat\nimport kdtree\n \n# Generate 100,000 random points\nlet numPoints = 100_000\nvar\n  points = newSeqOfCap[array[2, float]](numPoints)\n  values = newSeqOfCap[int](numPoints) \n  x: float\n  y: float\n  r = initRand(34)\n \nfor a in 0..\u003cnumPoints:\n  x = r.rand(100.0)\n  y = r.rand(100.0)\n  points.add([x, y])\n  values.add(a)\n \necho fmt\"Building tree of {numPoints} random points...\"\nvar tree = newKdTree[int](points, values)\n# Notice that our 'values' are of int type here; the data associated with points can be of any generic data type.\n\n# The preferred method of tree construction is bulk loading of point data using 'newKdTree'. However, you\n# may also add individual points.\nx = r.rand(100.0)\ny = r.rand(100.0)\nlet value = numPoints\ntree.add([x, y], value)\n\n# However, adding many individual points can result in an unbalanced tree, which can result in inefficient queries. \n# You may check the tree balance and re-balance the tree if necessary.\nlet balance = tree.isBalanced() # The larger the value magnitude, the more unbalanced the tree is. The sign indicates \n                                # the direction of skew, with negative values indicating a left-skewed tree and positive \n                                # values indicated a right-skewed tree.\n\nif abs(balance) \u003e 1:\n    tree.rebalance()\n\n\n############################ \n# Spatial query operations #\n############################ \n\n# Perform nearestNeighour searches\nlet numSearches = 10_000\nfor a in 0..\u003cnumSearches:\n  x = r.rand(100.0)\n  y = r.rand(100.0)\n  let (pt, values, dist) = tree.nearestNeighbour([x, y])\n  echo fmt\"point={pt}, value={value}, dist={dist}\"\n \n# Perform nearestNeighours searches\nlet n = 10\nfor a in 0..\u003cnumSearches:\n  x = r.rand(100.0)\n  y = r.rand(100.0)\n  let ret = tree.nearestNeighbours([x, y], n)\n  for (pt, value, dist) in ret:\n    echo fmt\"point={pt}, value={value}, dist={dist}\"\n\n# Perform a withinRadius search\nx = 50.0\ny = 50.0\nvar ret2 = tree.withinRadius([x, y], radius=5.0, sortResults=true)\nfor (pt, value, dist) in ret2:\n  echo fmt\"point={pt}, value={value}, dist={dist}\"\n \n# Perform a withinRange search\nvar \n  min: array[2, float] = [0.0, 0.0]\n  max: array[2, float] = [10.0, 10.0]\n  hyperRect = newHyperRectangle(min, max)\n\nvar ret = tree.withinRange(hyperRect)\nfor (pt, value) in ret:\n  echo fmt\"point={pt}, value={value}\"\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjblindsay%2Fkdtree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjblindsay%2Fkdtree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjblindsay%2Fkdtree/lists"}