6

All,

There are a few examples on implementing a quadtree using Python but my question is, does anyone know of a class written in pure python as in a single .py file that I can easily include in my project? The three most popular packages are listed here Are any of these quad-tree libraries any good? but I have not had luck with using them because of all the dependencies required to run them. I am really going for something lighweight and relatively simple to use. I would like to call the script by passing in the bounds for the entire globe and work down from there. myMethod((-180,-90,180,90))

Thanks, Adam

Community
  • 1
  • 1
aeupinhere
  • 2,883
  • 6
  • 31
  • 39
  • 2
    What about the second library in the question you linked to (http://stackoverflow.com/questions/2298517/are-any-of-these-quad-tree-libraries-any-good)? The dependencies are trivial to none. – NPE May 19 '11 at 14:36

2 Answers2

4

PyQuadTree is a pretty lightweight module (that I built based on someone else's code). It's written in pure-Python, has no dependencies, and doesn't require any installation or compiling at all. It's a single .py file that can be easily included as part of bigger project, which sounds like what is being asked about here.

It also has documentation and supports both Python 2x and 3x.

Karim Bahgat
  • 2,781
  • 3
  • 21
  • 27
  • I have no experience with Quadtrees, but I have to implement it for a project on trajectory analysis. Please kindly let me know, if your module can be helpful with the analysis of trajectories (trajectory data which consists of sequence of x,y coordinates) – Liza May 09 '17 at 03:41
  • If by trajectory you simply mean a line geometry that represents movement, that works fine. But the lib doesnt care about type of geometry, all it needs is a bounding box (xmin,ymin,xmax,ymax). So you can insert many different trajectory bboxes into the tree, and then you can query the tree to quickly see which trajectories are located in an area. Go to the github repo linked to in the blogpost to read specifically how to use it. – Karim Bahgat May 09 '17 at 07:42
  • Thank you so much for replying. Please have a look here https://stackoverflow.com/questions/44147628/implementing-quadtree-on-a-data-frame I am trying to implement this on a data frame, also I am not sure how to query the tree to know which trajectories belongs to which quadrants. Even a little help will be very useful. Thanks. – Liza May 24 '17 at 01:49
  • Thank you for your library, I could partially achieve what I wanted. But I would want your suggestion on a special scenario, where there are two very close trajectories, and **both** of them are horizontal. So both of them will have their own bbox which will simply be two lines over the trajectories. So,if I give an Intersection box which targetting the area between these two trajectories, inspite of being very close, the two trajectories wont be a match. Solution to this might be creating a buffer box around the bounding box to address this scenario. Please suggest what can I do? – Liza Jun 06 '17 at 01:11
  • If the intersection bbox does not contain the two trajectory bboxes, then this is the expected behavior. Not sure why you mention them being horizontal, the behavior would be the same regardless. Or is your question more about how you can get matches that are near within a buffer? If so I suggest making a new question, the short answer being yes you should buffer the intersection bbox. Pyqtree so far doesnt have a method for getting x nearest. – Karim Bahgat Jun 11 '17 at 22:32
1

Take a look at Rect. You'll need 2 files. You can merge them into one.

moraes
  • 13,213
  • 7
  • 45
  • 59
  • The wiki pages (and indeed the entire project) on code.google.com for the [Rect](http://pypi.python.org/pypi/Rect) package seem to have gone missing. Anyone know of an updated link for the missing info? – Inactivist Jun 02 '13 at 16:01
  • 1
    That's unfortunate. The tar.gz is still available on PyPi though. – moraes Jul 10 '13 at 11:39