My fork of Miklós Koren's quadtree implementation in Python.
Which is based off of Malcolm Kesson's quadtree code.
A quadtree implementation in Python to count many points within complex geometric shapes.
- Install Shapely
-
Install
libgeosdependency.$ sudo apt-get install libgeos-c1 -
Install shapely.
$ pip install Shapely
-
import quadtree
from shapely.geometry import Polygon
qt = quadtree.QuadTree([(0, 0), (0, 1), (5, 1)])
# Test if the points exist in the bounding box established by the points above.
assert qt.contains_point((0, 0))
assert qt.contains_point((0.1, 0.1))
assert not qt.contains_point((-0.1, 0.1))
# Walk all the points in the quadtree
for point in qt.walk():
print point
> (0, 1)
> (5, 1)
> (0, 0)
# Find points that exist within the bouding box below.
rect = Polygon([(4, 0), (6, 0), (6, 1), (4, 1)])
print qt.get_overlapping_points(quadtree.Feature(rect))
> []
rect = Polygon([(0, 0), (6, 0), (6, 2), (4, 2)])
print qt.get_overlapping_points(quadtree.Feature(rect))
# Current implementation returns all points as fake geometry objects.
> [{'geometry': {'type': 'Point', 'coordinates': [5, 1]},
'type': 'Feature', 'properties': {'name': 'Dinagat Islands'}}]