views:

42

answers:

1

Hi, I need a data structure for doing 2d range counting queries (i.e. how many points are in a given rectangle).

I think my best bet is range tree (it can count in log^2, or even log after some optimizations). Does it sound like a good choice? Does anybody know about a python implementation or will I have to write one myself?

+1  A: 

See scipy.spatial.KDTree for one implementation.

There's also a less generic (but occasionally more useful, particularly with regards to what you have in mind) implementation using shapelib's quadtree. See this blog and the corresponding package in PyPi.

There are probably other implementations, too, but those are the two that I've used...

Joe Kington
I didn't use it because I thought kdtree is O(sqrt(n)). I was talking about a different data structure (range tree) which is O(log^2(n)). However, it seems they are using a really cool optimization for multiple queries, and the article (http://www.cs.cmu.edu/~agray/nips-final.ps) mentions exactly what I'm trying to to do as an application for the method (fast kernel density estimation). So I'll give it a try. Thanks!
Dani