## PySpart
[](https://github.com/habedi/spart/tree/main/pyspart/LICENSE)
[](https://github.com/habedi/spart/tree/main/pyspart)
[](https://pypi.org/project/pyspart)
Python bindings for the [Spart](https://github.com/habedi/spart) library.
### Installation
```bash
pip install pyspart
````
### Examples
Below are some examples of how to use the different trees in PySpart.
#### Quadtree (2D)
```python
from pyspart import Quadtree, Point2D
# Define the bounding area for the Quadtree.
boundary = {"x": 0.0, "y": 0.0, "width": 10.0, "height": 10.0}
# Create a new Quadtree with a maximum capacity of 3 points per node.
tree = Quadtree(boundary, 3)
# Define some 2D points.
point1 = Point2D(1.0, 2.0, "Point1")
point2 = Point2D(3.0, 4.0, "Point2")
point3 = Point2D(5.0, 6.0, "Point3")
point4 = Point2D(7.0, 8.0, "Point4")
point5 = Point2D(2.0, 3.0, "Point5")
# Insert points into the Quadtree.
tree.insert(point1)
tree.insert(point2)
tree.insert(point3)
tree.insert(point4)
tree.insert(point5)
# Perform a k-nearest neighbor (kNN) search.
neighbors = tree.knn_search(point1, 2)
print(f"kNN search results for {point1}: {neighbors}")
# Perform a range search with a radius of 5.0.
range_points = tree.range_search(point1, 5.0)
print(f"Range search results for {point1}: {range_points}")
# Remove a point from the tree.
tree.delete(point1)
```
#### Octree (3D)
```python
from pyspart import Octree, Point3D
# Define the bounding area for the Octree.
boundary = {"x": 0.0, "y": 0.0, "z": 0.0, "width": 10.0, "height": 10.0, "depth": 10.0}
# Create a new Octree with a maximum capacity of 3 points per node.
tree = Octree(boundary, 3)
# Define some 3D points.
point1 = Point3D(1.0, 2.0, 3.0, "Point1")
point2 = Point3D(3.0, 4.0, 5.0, "Point2")
point3 = Point3D(5.0, 6.0, 7.0, "Point3")
point4 = Point3D(7.0, 8.0, 9.0, "Point4")
point5 = Point3D(2.0, 3.0, 4.0, "Point5")
# Insert points into the Octree.
tree.insert(point1)
tree.insert(point2)
tree.insert(point3)
tree.insert(point4)
tree.insert(point5)
# Perform a kNN search.
neighbors = tree.knn_search(point1, 2)
print(f"kNN search results for {point1}: {neighbors}")
# Perform a range search with a radius of 5.0.
range_points = tree.range_search(point1, 5.0)
print(f"Range search results for {point1}: {range_points}")
# Remove a point from the tree.
tree.delete(point1)
```
#### Kd-tree (3D)
```python
from pyspart import KdTree3D, Point3D
# Create a new Kd-tree for 3D points.
tree = KdTree3D()
# Define some 3D points.
point1 = Point3D(1.0, 2.0, 3.0, "Point1")
point2 = Point3D(3.0, 4.0, 5.0, "Point2")
point3 = Point3D(5.0, 6.0, 7.0, "Point3")
point4 = Point3D(7.0, 8.0, 9.0, "Point4")
point5 = Point3D(2.0, 3.0, 4.0, "Point5")
# Insert points into the Kd-tree.
tree.insert(point1)
tree.insert(point2)
tree.insert(point3)
tree.insert(point4)
tree.insert(point5)
# Perform a kNN search.
neighbors = tree.knn_search(point1, 2)
print(f"kNN search results for {point1}: {neighbors}")
# Perform a range search with a radius of 5.0.
range_points = tree.range_search(point1, 5.0)
print(f"Range search results for {point1}: {range_points}")
# Remove a point from the tree.
tree.delete(point1)
```
#### R-tree (3D)
```python
from pyspart import RTree3D, Point3D
# Create a new R-tree with a maximum capacity of 4 points per node.
tree = RTree3D(4)
# Define some 3D points.
point1 = Point3D(1.0, 2.0, 3.0, "Point1")
point2 = Point3D(3.0, 4.0, 5.0, "Point2")
point3 = Point3D(5.0, 6.0, 7.0, "Point3")
point4 = Point3D(7.0, 8.0, 9.0, "Point4")
point5 = Point3D(2.0, 3.0, 4.0, "Point5")
# Insert points into the R-tree.
tree.insert(point1)
tree.insert(point2)
tree.insert(point3)
tree.insert(point4)
tree.insert(point5)
# Perform a kNN search.
neighbors = tree.knn_search(point1, 2)
print(f"kNN search results for {point1}: {neighbors}")
# Perform a range search with a radius of 5.0.
range_points = tree.range_search(point1, 5.0)
print(f"Range search results for {point1}: {range_points}")
# Remove a point from the tree.
tree.delete(point1)
```
#### R*-tree (3D)
```python
from pyspart import RStarTree3D, Point3D
# Create a new R*-tree with a maximum capacity of 4 points per node.
tree = RStarTree3D(4)
# Define some 3D points.
point1 = Point3D(1.0, 2.0, 3.0, "Point1")
point2 = Point3D(3.0, 4.0, 5.0, "Point2")
point3 = Point3D(5.0, 6.0, 7.0, "Point3")
point4 = Point3D(7.0, 8.0, 9.0, "Point4")
point5 = Point3D(2.0, 3.0, 4.0, "Point5")
# Insert points into the R*-tree.
tree.insert(point1)
tree.insert(point2)
tree.insert(point3)
tree.insert(point4)
tree.insert(point5)
# Perform a kNN search.
neighbors = tree.knn_search(point1, 2)
print(f"kNN search results for {point1}: {neighbors}")
# Perform a range search with a radius of 5.0.
range_points = tree.range_search(point1, 5.0)
print(f"Range search results for {point1}: {range_points}")
# Remove a point from the tree.
tree.delete(point1)
```
Check out the [examples](https://github.com/habedi/spart/tree/main/pyspart/examples) directory for more examples.
### Serialization
In Python, you can use the `save` and `load` methods to serialize and deserialize the tree to and from a file:
```python
from pyspart import Quadtree, Point2D
# Create a Quadtree and insert some points
boundary = {"x": 0.0, "y": 0.0, "width": 100.0, "height": 100.0}
qt = Quadtree(boundary, 4)
qt.insert(Point2D(10.0, 20.0, "point1"))
qt.insert(Point2D(50.0, 50.0, "point2"))
# Save the tree to a file
qt.save("quadtree.spart")
# Load the tree from the file
loaded_qt = Quadtree.load("quadtree.spart")
```
### License
PySpart is licensed under the [MIT License](https://github.com/habedi/spart/tree/main/pyspart/LICENSE).
Raw data
{
"_id": null,
"home_page": null,
"name": "pyspart",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": "Hassan Abedi <hassan.abedi.t+pyspart@gmail.com>",
"keywords": "quadtree, kdtree, r-tree, octree, spatial-index",
"author": null,
"author_email": "Hassan Abedi <hassan.abedi.t+pyspart@gmail.com>",
"download_url": null,
"platform": null,
"description": "## PySpart\n\n[](https://github.com/habedi/spart/tree/main/pyspart/LICENSE)\n[](https://github.com/habedi/spart/tree/main/pyspart)\n[](https://pypi.org/project/pyspart)\n\nPython bindings for the [Spart](https://github.com/habedi/spart) library.\n\n### Installation\n\n```bash\npip install pyspart\n````\n\n### Examples\n\nBelow are some examples of how to use the different trees in PySpart.\n\n#### Quadtree (2D)\n\n```python\nfrom pyspart import Quadtree, Point2D\n\n# Define the bounding area for the Quadtree.\nboundary = {\"x\": 0.0, \"y\": 0.0, \"width\": 10.0, \"height\": 10.0}\n\n# Create a new Quadtree with a maximum capacity of 3 points per node.\ntree = Quadtree(boundary, 3)\n\n# Define some 2D points.\npoint1 = Point2D(1.0, 2.0, \"Point1\")\npoint2 = Point2D(3.0, 4.0, \"Point2\")\npoint3 = Point2D(5.0, 6.0, \"Point3\")\npoint4 = Point2D(7.0, 8.0, \"Point4\")\npoint5 = Point2D(2.0, 3.0, \"Point5\")\n\n# Insert points into the Quadtree.\ntree.insert(point1)\ntree.insert(point2)\ntree.insert(point3)\ntree.insert(point4)\ntree.insert(point5)\n\n# Perform a k-nearest neighbor (kNN) search.\nneighbors = tree.knn_search(point1, 2)\nprint(f\"kNN search results for {point1}: {neighbors}\")\n\n# Perform a range search with a radius of 5.0.\nrange_points = tree.range_search(point1, 5.0)\nprint(f\"Range search results for {point1}: {range_points}\")\n\n# Remove a point from the tree.\ntree.delete(point1)\n```\n\n#### Octree (3D)\n\n```python\nfrom pyspart import Octree, Point3D\n\n# Define the bounding area for the Octree.\nboundary = {\"x\": 0.0, \"y\": 0.0, \"z\": 0.0, \"width\": 10.0, \"height\": 10.0, \"depth\": 10.0}\n\n# Create a new Octree with a maximum capacity of 3 points per node.\ntree = Octree(boundary, 3)\n\n# Define some 3D points.\npoint1 = Point3D(1.0, 2.0, 3.0, \"Point1\")\npoint2 = Point3D(3.0, 4.0, 5.0, \"Point2\")\npoint3 = Point3D(5.0, 6.0, 7.0, \"Point3\")\npoint4 = Point3D(7.0, 8.0, 9.0, \"Point4\")\npoint5 = Point3D(2.0, 3.0, 4.0, \"Point5\")\n\n# Insert points into the Octree.\ntree.insert(point1)\ntree.insert(point2)\ntree.insert(point3)\ntree.insert(point4)\ntree.insert(point5)\n\n# Perform a kNN search.\nneighbors = tree.knn_search(point1, 2)\nprint(f\"kNN search results for {point1}: {neighbors}\")\n\n# Perform a range search with a radius of 5.0.\nrange_points = tree.range_search(point1, 5.0)\nprint(f\"Range search results for {point1}: {range_points}\")\n\n# Remove a point from the tree.\ntree.delete(point1)\n```\n\n#### Kd-tree (3D)\n\n```python\nfrom pyspart import KdTree3D, Point3D\n\n# Create a new Kd-tree for 3D points.\ntree = KdTree3D()\n\n# Define some 3D points.\npoint1 = Point3D(1.0, 2.0, 3.0, \"Point1\")\npoint2 = Point3D(3.0, 4.0, 5.0, \"Point2\")\npoint3 = Point3D(5.0, 6.0, 7.0, \"Point3\")\npoint4 = Point3D(7.0, 8.0, 9.0, \"Point4\")\npoint5 = Point3D(2.0, 3.0, 4.0, \"Point5\")\n\n# Insert points into the Kd-tree.\ntree.insert(point1)\ntree.insert(point2)\ntree.insert(point3)\ntree.insert(point4)\ntree.insert(point5)\n\n# Perform a kNN search.\nneighbors = tree.knn_search(point1, 2)\nprint(f\"kNN search results for {point1}: {neighbors}\")\n\n# Perform a range search with a radius of 5.0.\nrange_points = tree.range_search(point1, 5.0)\nprint(f\"Range search results for {point1}: {range_points}\")\n\n# Remove a point from the tree.\ntree.delete(point1)\n```\n\n#### R-tree (3D)\n\n```python\nfrom pyspart import RTree3D, Point3D\n\n# Create a new R-tree with a maximum capacity of 4 points per node.\ntree = RTree3D(4)\n\n# Define some 3D points.\npoint1 = Point3D(1.0, 2.0, 3.0, \"Point1\")\npoint2 = Point3D(3.0, 4.0, 5.0, \"Point2\")\npoint3 = Point3D(5.0, 6.0, 7.0, \"Point3\")\npoint4 = Point3D(7.0, 8.0, 9.0, \"Point4\")\npoint5 = Point3D(2.0, 3.0, 4.0, \"Point5\")\n\n# Insert points into the R-tree.\ntree.insert(point1)\ntree.insert(point2)\ntree.insert(point3)\ntree.insert(point4)\ntree.insert(point5)\n\n# Perform a kNN search.\nneighbors = tree.knn_search(point1, 2)\nprint(f\"kNN search results for {point1}: {neighbors}\")\n\n# Perform a range search with a radius of 5.0.\nrange_points = tree.range_search(point1, 5.0)\nprint(f\"Range search results for {point1}: {range_points}\")\n\n# Remove a point from the tree.\ntree.delete(point1)\n```\n\n#### R*-tree (3D)\n\n```python\nfrom pyspart import RStarTree3D, Point3D\n\n# Create a new R*-tree with a maximum capacity of 4 points per node.\ntree = RStarTree3D(4)\n\n# Define some 3D points.\npoint1 = Point3D(1.0, 2.0, 3.0, \"Point1\")\npoint2 = Point3D(3.0, 4.0, 5.0, \"Point2\")\npoint3 = Point3D(5.0, 6.0, 7.0, \"Point3\")\npoint4 = Point3D(7.0, 8.0, 9.0, \"Point4\")\npoint5 = Point3D(2.0, 3.0, 4.0, \"Point5\")\n\n# Insert points into the R*-tree.\ntree.insert(point1)\ntree.insert(point2)\ntree.insert(point3)\ntree.insert(point4)\ntree.insert(point5)\n\n# Perform a kNN search.\nneighbors = tree.knn_search(point1, 2)\nprint(f\"kNN search results for {point1}: {neighbors}\")\n\n# Perform a range search with a radius of 5.0.\nrange_points = tree.range_search(point1, 5.0)\nprint(f\"Range search results for {point1}: {range_points}\")\n\n# Remove a point from the tree.\ntree.delete(point1)\n```\n\nCheck out the [examples](https://github.com/habedi/spart/tree/main/pyspart/examples) directory for more examples.\n\n### Serialization\n\nIn Python, you can use the `save` and `load` methods to serialize and deserialize the tree to and from a file:\n\n```python\nfrom pyspart import Quadtree, Point2D\n\n# Create a Quadtree and insert some points\nboundary = {\"x\": 0.0, \"y\": 0.0, \"width\": 100.0, \"height\": 100.0}\nqt = Quadtree(boundary, 4)\nqt.insert(Point2D(10.0, 20.0, \"point1\"))\nqt.insert(Point2D(50.0, 50.0, \"point2\"))\n\n# Save the tree to a file\nqt.save(\"quadtree.spart\")\n\n# Load the tree from the file\nloaded_qt = Quadtree.load(\"quadtree.spart\")\n```\n\n### License\n\nPySpart is licensed under the [MIT License](https://github.com/habedi/spart/tree/main/pyspart/LICENSE).\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Python bindings for Spart library",
"version": "0.2.0",
"project_urls": {
"documentation": "https://github.com/habedi/spart/tree/main/pyspart",
"homepage": "https://github.com/habedi/spart",
"repository": "https://github.com/habedi/spart/tree/main/pyspart"
},
"split_keywords": [
"quadtree",
" kdtree",
" r-tree",
" octree",
" spatial-index"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "88271240e944da61b6fe3a768af47e51cd5ff181485ed873fe8b74d3a1d9a71c",
"md5": "c3339467e42f636a104953ce4f20942c",
"sha256": "08d1bfa3fa728b9228b4a6973dd4a323f52590aa95de6ad1e8945b5bdef92e36"
},
"downloads": -1,
"filename": "pyspart-0.2.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
"has_sig": false,
"md5_digest": "c3339467e42f636a104953ce4f20942c",
"packagetype": "bdist_wheel",
"python_version": "cp310",
"requires_python": ">=3.10",
"size": 508473,
"upload_time": "2025-08-23T11:42:24",
"upload_time_iso_8601": "2025-08-23T11:42:24.268571Z",
"url": "https://files.pythonhosted.org/packages/88/27/1240e944da61b6fe3a768af47e51cd5ff181485ed873fe8b74d3a1d9a71c/pyspart-0.2.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-08-23 11:42:24",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "habedi",
"github_project": "spart",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "pyspart"
}