{"id":17036719,"url":"https://github.com/bast/flanders","last_synced_at":"2025-12-18T00:14:26.083Z","repository":{"id":19078194,"uuid":"86112048","full_name":"bast/flanders","owner":"bast","description":"Fast 2D nearest neighbor search with an angle.","archived":false,"fork":false,"pushed_at":"2023-05-30T14:35:42.000Z","size":650,"stargazers_count":7,"open_issues_count":1,"forks_count":1,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-03-26T07:51:16.726Z","etag":null,"topics":["angle","kd-tree","nearest-neighbor-search","python"],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bast.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":"2017-03-24T21:28:16.000Z","updated_at":"2023-05-30T14:17:45.000Z","dependencies_parsed_at":"2022-07-24T20:32:16.301Z","dependency_job_id":null,"html_url":"https://github.com/bast/flanders","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bast%2Fflanders","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bast%2Fflanders/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bast%2Fflanders/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bast%2Fflanders/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bast","download_url":"https://codeload.github.com/bast/flanders/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248102367,"owners_count":21048168,"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":["angle","kd-tree","nearest-neighbor-search","python"],"created_at":"2024-10-14T08:51:42.265Z","updated_at":"2025-12-18T00:14:26.037Z","avatar_url":"https://github.com/bast.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![GH Actions badge](https://github.com/bast/flanders/workflows/Test/badge.svg)](https://github.com/bast/flanders/actions)\n[![License](https://img.shields.io/badge/license-%20MPL--v2.0-blue.svg)](LICENSE)\n[![PyPI badge](https://badge.fury.io/py/flanders.svg)](https://badge.fury.io/py/flanders)\n\n\n# Flanders: Fast 2D nearest neighbor search with an angle\n\n```\n                                                        `.-:://////:-.`\n                                                 `-/oyhddddmmddddddNmdmdhs/`\n                                              -ohddddddddddddNddddddNddddmmds.\n                                            `hmmmmmdddddddddddNddddmmddddmmddm.\n                                           `hddddddmmddddddhyyysoossyhdmNmdddNs\n                                           sddddddddmmho/:-------------:odmmmmo\n                                          :mddddddddd+-------------------:hddd.\n                                          dddddddddm+---------------------:ms.\n                                         :mddddddddd-----------------------s/`\n                                         yddddddddds------------------------:s\n                                        `mddddddddm+-----://////+/:---://////y`\n                                        -Nddddddddm/----+:`     `./+-+-`    `.+:\n                                        /mddddddddN:---o`          -d-    -.   o-\n                                        omddddddddN////y  -d/      `m.    y+   +:\n                                        sdddhhNmmmN+/::o: `-`     `+so++//:. `:+`\n               `---.                    ydd+::mdddm:----/+-.````-:+:------:+s/-\n               o/::/+-                  sN:-oomdddm+------://+///:---------:s\n       `.-.`   s-----+/`                oN:---smddds------------:+oyhhysssydhs:.\n      -o/://+-`.o:----/o`               /mh:---hyyy/----------+hdmmmmdddddmmmddho.\n      /+-----:+/-o:----:+`       `..    .mddyyydo-----:///::ohdmmdmmdmddNdmmdmmddd/\n       :+/-----:+/y-----:+-`  .:+///+.   dddddddd----:mMMNNddddddmddmdddmmdmddmdddm:\n       ``-/+:----::-------:/+++:----/+   +mdddddN----:MMMMMNmmmmmmmdddddddhhhhhys+/.\n    .////+//+o:--------------/-----/o`    sdddddm/----oNmdmNNNNMMMm//os--...``\n    y:-----:/+o------------------:o:       :ydddm+-----:oyyyysydMMm:::o+.\n    :o/:----------------+o:-----:s`          .::+o--------://++oooo+:--:s\n      -:/+/:--------------s/----s.              -o------------------:/o+.\n          `-/+o------------/---+d-             `+o------------------::h\n              .s/---------:::ohhhs             y.-++:-----------------d:`\n               dhys+///+oyhhhhyhm/            `s````:/++o+//:::://+++/-.s.\n              /dddhhhhhhyyhhhddhym`           -s`````````..-:::/h/s-````.s`\n             :dyyhddddddddddhyyyym           `ydy-`````````````s:.-o/````sy/-`\n             yyyyyyyyyyyyyyyyyyyhs     `./+ooymhyhy/.`````````:o....++``:dmdhhhso:`\n             dyyyyyyyyyyyyyyyyyym:`-/oyddhyyyyhddyyhhs+-``````hso++ohd++dyyddyyyyhhyo/`\n             myyyyyyyyyyyyyyyyyyNhhhyyyyyyyyyyyyddyyyyyhdyso++ddhmhmddhhyyyyddyyyyyyyhdy+.\n```\n\n\n## Installation\n\n```\n$ pip install flanders\n```\n\n\n## Example\n\nIn this example we have 6 points (numbered 0 to 5) and two observer\npoints with a certain view vector and view angle (90 degrees). The first observer\npoint finds point 2. The second observer point does not find any\nneighbor within the view angle and returns -1.\n\n![Example](img/flanders.png)\n\nExample code:\n```python\nimport flanders\n\n\n# as a first step we build the search tree\n# we can later reuse the search tree many times\n\npoints = [\n    (60.4, 51.3),\n    (173.9, 143.8),\n    (132.9, 124.9),\n    (19.5, 108.9),\n    (196.5, 9.9),\n    (143.3, 53.3),\n]\n\ntree = flanders.build_search_tree(points)\n\n\n# now we will search the indices of nearest neighbor points\n# for two observer points\n\nobserver_coordinates = [(119.2, 59.7), (155.2, 30.2)]\nview_vectors = [(0.0, 1.0), (-1.0, -1.0)]\nview_angles_deg = [90.0, 90.0]\n\nindices = flanders.nearest_indices_from_coordinates(\n    tree, observer_coordinates, view_vectors, view_angles_deg\n)\n\nassert indices == [2, -1]\n\n\n# instead of using observer coordinates, also the original\n# points themselves can be observers and we can select them\n# by their index\n\nobserver_indices = [0, 1, 2, 3, 4, 5]\nview_vectors = [(1.0, 1.0) for _ in observer_indices]\nview_angles_deg = [90.0 for _ in observer_indices]\n\nindices = flanders.nearest_indices_from_indices(\n    tree, observer_indices, view_vectors, view_angles_deg\n)\n\nassert indices == [5, -1, 1, 2, -1, 1]\n```\n\n\n## Efficiency considerations\n\nThe above example is very small and simple but this library starts to shine\nonce you have very many points and/or very many observers where a noddy\nimplementation would take too long to compute.\n\nExample timing for 1 M points and 10 k observers (on i7-10710U):\n- constructing the search tree: 3.0 s\n- nearest neighbor search: 9.6 s\n\nIf you compute nearest neighbors for many observers it is a good idea to send\nin an entire batch of observers instead of computing one by one.  If you send\nin an entire batch, the code will shared-memory parallelize the loop over the\nobservers.\n\n\n## References used during development\n\n- https://en.wikipedia.org/wiki/Nearest_neighbor_search\n- https://en.wikipedia.org/wiki/K-d_tree\n- http://www.slideshare.net/awebneck/the-post-office-problem\n- http://www.cs.nyu.edu/~roweis/papers/Ahmed_msc_thesis.pdf\n- http://dl.acm.org/citation.cfm?doid=361002.361007\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbast%2Fflanders","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbast%2Fflanders","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbast%2Fflanders/lists"}