项目作者: hexogen

项目描述 :
PHP K-D Tree implementation with file system binary index
高级语言: PHP
项目地址: git://github.com/hexogen/kdtree.git
创建时间: 2016-12-18T05:21:46Z
项目社区:https://github.com/hexogen/kdtree

开源协议:MIT License

下载


K-D Tree

Latest Version on Packagist
Build Status
codecov
Software License
Total Downloads

PHP multidimensional K-D Tree implementation.

To receive all benefits from K-D Tree, use file system implementation(FSKDTree). FSKDTree stores tree in binary format and uses lazy loading while traversing through nodes. Current approach provides much higher performance compared
to deserialization.

Install

Via Composer

  1. $ composer require hexogen/kdtree

Usage

Tree creation

  1. //Item container with 2 dimensional points
  2. $itemList = new ItemList(2);
  3. //Adding 2 - dimension items to the list
  4. $itemList->addItem(new Item(1, [1.2, 4.3]));
  5. $itemList->addItem(new Item(2, [1.3, 3.4]));
  6. $itemList->addItem(new Item(3, [4.5, 1.2]));
  7. $itemList->addItem(new Item(4, [5.2, 3.5]));
  8. $itemList->addItem(new Item(5, [2.1, 3.6]));
  9. //Building tree with given item list
  10. $tree = new KDTree($itemList);

Searching nearest items to the given point

  1. //Creating search engine with custom algorithm (currently Nearest Search)
  2. $searcher = new NearestSearch($tree);
  3. //Retrieving a result ItemInterface[] array with given size (currently 2)
  4. $result = $searcher->search(new Point([1.25, 3.5]), 2);
  5. echo $result[0]->getId(); // 2
  6. echo $result[0]->getNthDimension(0); // 1.3
  7. echo $result[0]->getNthDimension(1); // 3.4
  8. echo $result[1]->getId(); // 1
  9. echo $result[1]->getNthDimension(0); // 1.2
  10. echo $result[1]->getNthDimension(1); // 4.3

Persist tree to a binary file

  1. //Init tree writer
  2. $persister = new FSTreePersister('/path/to/dir');
  3. //Save the tree to /path/to/dir/treeName.bin
  4. $persister->convert($tree, 'treeName.bin');

File system version of the tree

  1. //ItemInterface factory
  2. $itemFactory = new ItemFactory();
  3. //Then init new instance of file system version of the tree
  4. $fsTree = new FSKDTree('/path/to/dir/treeName.bin', $itemFactory);
  5. //Now use fs kdtree to search
  6. $fsSearcher = new NearestSearch($fsTree);
  7. //Retrieving a result ItemInterface[] array with given size (currently 2)
  8. $result = $fsSearcher->search(new Point([1.25, 3.5]), 2);
  9. echo $result[0]->getId(); // 2
  10. echo $result[1]->getId(); // 1

Change log

Please see CHANGELOG for more information on what has changed recently.

Testing

  1. $ composer test

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Security

If you discover any security related issues, please email volodymyrbas@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.