PHP code example of hexogen / kdtree

1. Go to this page and download the library: Download hexogen/kdtree library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hexogen / kdtree example snippets

 php
//Creating search engine with custom algorithm (currently Nearest Search)
$searcher = new NearestSearch($tree);

//Retrieving a result ItemInterface[] array with given size (currently 2)
$result = $searcher->search(new Point([1.25, 3.5]), 2);

echo $result[0]->getId(); // 2
echo $result[0]->getNthDimension(0); // 1.3
echo $result[0]->getNthDimension(1); // 3.4

echo $result[1]->getId(); // 1
echo $result[1]->getNthDimension(0); // 1.2
echo $result[1]->getNthDimension(1); // 4.3
 php
$point = new Point([1.25, 3.5]);
foreach ($searcher->search($point, 2) as $item) {
    $squared = 0.0;
    for ($d = 0; $d < $point->getDimensionsCount(); $d++) {
        $delta = $item->getNthDimension($d) - $point->getNthDimension($d);
        $squared += $delta * $delta;
    }
    echo $item->getId(), ': ', sqrt($squared), PHP_EOL;
}
 php
//ItemInterface factory
$itemFactory = new ItemFactory();

//Then init new instance of file system version of the tree
$fsTree = new FSKDTree('/path/to/dir/treeName.bin', $itemFactory);

//Now use fs kdtree to search
$fsSearcher = new NearestSearch($fsTree);

//Retrieving a result ItemInterface[] array with given size (currently 2)
$result = $fsSearcher->search(new Point([1.25, 3.5]), 2);

echo $result[0]->getId(); // 2
echo $result[1]->getId(); // 1