PHPackages                             hexogen/kdtree - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Caching](/categories/caching)
4. /
5. hexogen/kdtree

ActiveLibrary[Caching](/categories/caching)

hexogen/kdtree
==============

file system KDTree index

v0.2.6(1y ago)2086.7k↓14.7%3MITPHPPHP ^7.1|^8.0

Since Jan 10Pushed 1y ago1 watchersCompare

[ Source](https://github.com/hexogen/kdtree)[ Packagist](https://packagist.org/packages/hexogen/kdtree)[ Docs](https://github.com/hexogen/kdtree)[ RSS](/packages/hexogen-kdtree/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (4)Versions (11)Used By (0)

K-D Tree
========

[](#k-d-tree)

[![Latest Version on Packagist](https://camo.githubusercontent.com/18503cdb39455b495376a7a708b17df3853dae6e819077c6ed9fd6d95a1749f0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6865786f67656e2f6b64747265652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/hexogen/kdtree)[![Build Status](https://camo.githubusercontent.com/6ac38f8e9ef680564e6bce38cf479e1c146dc00f5c4d000232015b4a1ba1161c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6865786f67656e2f6b64747265652f74657374732e796d6c3f6272616e63683d6d6173746572)](https://github.com/hexogen/kdtree/actions?query=workflow%3ATests)[![codecov](https://camo.githubusercontent.com/4bf7e1fe3ea5c56a7b6072ad1f6fcc395671e043623e197aba8bd4a6e14cc4a6/68747470733a2f2f636f6465636f762e696f2f67682f6865786f67656e2f6b64747265652f67726170682f62616467652e7376673f746f6b656e3d3137364c345541305931)](https://codecov.io/gh/hexogen/kdtree)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Total Downloads](https://camo.githubusercontent.com/1d176b19c0bb4dac4ed73daa9691c10461cf46a55ccd83979c694616f80553c4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6865786f67656e2f6b64747265652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/hexogen/kdtree)

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
-------

[](#install)

Via Composer

```
$ composer require hexogen/kdtree
```

Usage
-----

[](#usage)

### Tree creation

[](#tree-creation)

```
//Item container with 2 dimensional points
$itemList = new ItemList(2);

//Adding 2 - dimension items to the list
$itemList->addItem(new Item(1, [1.2, 4.3]));
$itemList->addItem(new Item(2, [1.3, 3.4]));
$itemList->addItem(new Item(3, [4.5, 1.2]));
$itemList->addItem(new Item(4, [5.2, 3.5]));
$itemList->addItem(new Item(5, [2.1, 3.6]));

//Building tree with given item list
$tree = new KDTree($itemList);
```

### Searching nearest items to the given point

[](#searching-nearest-items-to-the-given-point)

```
//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
```

### Persist tree to a binary file

[](#persist-tree-to-a-binary-file)

```
//Init tree writer
$persister = new FSTreePersister('/path/to/dir');

//Save the tree to /path/to/dir/treeName.bin
$persister->convert($tree, 'treeName.bin');
```

### File system version of the tree

[](#file-system-version-of-the-tree)

```
//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
```

Change log
----------

[](#change-log)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Testing
-------

[](#testing)

```
$ composer test
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](.github/CONTRIBUTING.md) and [CONDUCT](CONDUCT.md) for details.

Security
--------

[](#security)

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

Credits
-------

[](#credits)

- [Volodymyr Basarab](https://github.com/hexogen)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

44

—

FairBetter than 92% of packages

Maintenance36

Infrequent updates — may be unmaintained

Popularity40

Moderate usage in the ecosystem

Community22

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~311 days

Recently: every ~236 days

Total

10

Last Release

610d ago

PHP version history (3 changes)0.1.0PHP ~7.0

0.2.0PHP ~7.1

0.2.1PHP ^7.1|^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/fbe2c119d7d9b5123a151d05120fd8e07bea6830a4e19831c54252dc41377ea3?d=identicon)[hexogen](/maintainers/hexogen)

---

Top Contributors

[![hexogen](https://avatars.githubusercontent.com/u/4229736?v=4)](https://github.com/hexogen "hexogen (112 commits)")[![philsturgeon](https://avatars.githubusercontent.com/u/67381?v=4)](https://github.com/philsturgeon "philsturgeon (44 commits)")[![frankdejonge](https://avatars.githubusercontent.com/u/534693?v=4)](https://github.com/frankdejonge "frankdejonge (20 commits)")[![ravage84](https://avatars.githubusercontent.com/u/625761?v=4)](https://github.com/ravage84 "ravage84 (10 commits)")[![jotaelesalinas](https://avatars.githubusercontent.com/u/2042875?v=4)](https://github.com/jotaelesalinas "jotaelesalinas (8 commits)")[![RobLoach](https://avatars.githubusercontent.com/u/25086?v=4)](https://github.com/RobLoach "RobLoach (7 commits)")[![hansott](https://avatars.githubusercontent.com/u/3886384?v=4)](https://github.com/hansott "hansott (6 commits)")[![bcrowe](https://avatars.githubusercontent.com/u/752603?v=4)](https://github.com/bcrowe "bcrowe (5 commits)")[![reinink](https://avatars.githubusercontent.com/u/882133?v=4)](https://github.com/reinink "reinink (5 commits)")[![colinodell](https://avatars.githubusercontent.com/u/202034?v=4)](https://github.com/colinodell "colinodell (5 commits)")[![EmanueleMinotto](https://avatars.githubusercontent.com/u/417201?v=4)](https://github.com/EmanueleMinotto "EmanueleMinotto (5 commits)")[![marcqualie](https://avatars.githubusercontent.com/u/101022?v=4)](https://github.com/marcqualie "marcqualie (4 commits)")[![hassankhan](https://avatars.githubusercontent.com/u/1781985?v=4)](https://github.com/hassankhan "hassankhan (3 commits)")[![browner12](https://avatars.githubusercontent.com/u/5232313?v=4)](https://github.com/browner12 "browner12 (3 commits)")[![alexbilbie](https://avatars.githubusercontent.com/u/77991?v=4)](https://github.com/alexbilbie "alexbilbie (2 commits)")[![assertchris](https://avatars.githubusercontent.com/u/200609?v=4)](https://github.com/assertchris "assertchris (2 commits)")[![barryvdh](https://avatars.githubusercontent.com/u/973269?v=4)](https://github.com/barryvdh "barryvdh (2 commits)")[![kdubuc](https://avatars.githubusercontent.com/u/895661?v=4)](https://github.com/kdubuc "kdubuc (2 commits)")[![mAAdhaTTah](https://avatars.githubusercontent.com/u/4371429?v=4)](https://github.com/mAAdhaTTah "mAAdhaTTah (2 commits)")[![sanmai](https://avatars.githubusercontent.com/u/139488?v=4)](https://github.com/sanmai "sanmai (1 commits)")

---

Tags

algorithmscachedata-structuresindexkd-treemultidimensionalitysearch-algorithmsearchdata structuresalgorithmskdtreehexogen

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/hexogen-kdtree/health.svg)

```
[![Health](https://phpackages.com/badges/hexogen-kdtree/health.svg)](https://phpackages.com/packages/hexogen-kdtree)
```

###  Alternatives

[react/cache

Async, Promise-based cache interface for ReactPHP

444112.4M40](/packages/react-cache)[wp-media/wp-rocket

Performance optimization plugin for WordPress

7431.3M3](/packages/wp-media-wp-rocket)[illuminate/cache

The Illuminate Cache package.

12835.6M1.4k](/packages/illuminate-cache)[colinmollenhour/php-redis-session-abstract

A Redis-based session handler with optimistic locking

6325.6M14](/packages/colinmollenhour-php-redis-session-abstract)[doganoo/php-algorithms

A collection of common algorithms implemented in PHP. The collection is based on "Cracking the Coding Interview" by Gayle Laakmann McDowell

9485.4k7](/packages/doganoo-php-algorithms)[cheprasov/php-redis-client

Php client for Redis. It is a fast, fully-functional and user-friendly client for Redis, optimized for performance. RedisClient supports the latest versions of Redis starting from 2.6 to 6.0

1281.2M21](/packages/cheprasov-php-redis-client)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
