0

I have a set of points with their coordinates (latitudes, longigutes), and I also have a region (a box). All the points are inside the box. Now I want to distribute the points into small cells (rectangles) considering the density of points. Basically, a cell containing many points should be subdivided into smaller ones, while keeping larger cells with small number of points.

I have check this question, which has almost the same problem with me but I couldn't find a good answer. I think I should use a quad tree, but all implementations I found doesn't provide that.

For example, this library allows making a tree associated with a box, and then we can insert boxes as follows:

spindex = Index(bbox=(0, 0, 100, 100))
for item in items:
    spindex.insert(item, item.bbox)

But it doens't allow inserting points. Moreover, I'll need to get the IDs of cells (or names), and check if a point belongs to a given cell.

Here is another lib I found. It does allow inserting points, but it doens't give me the cell's ID (or name), so I cannot check if a point belongs to a certain cell. Notice that the tree should do the decomposition automatically.

Could you please suggest me a solution? Thanks.

lenhhoxung
  • 2,530
  • 2
  • 30
  • 61
  • Got a downvote without understanding :-| – lenhhoxung Jul 26 '17 at 09:53
  • Like the answer you linked suggests, it's usually easier to use something like postgis. But asking for library suggestions is off-topic on SO, as you probably know. – pvg Jul 26 '17 at 09:56

1 Answers1

1

Finally, I ended up using Google's s2-geometry-library with Python wrapper. In fact, each cell created by this lib is not a rectangle (it's a projection), but it satisfies my need. The lib already divided the earth surface into cells at different levels (quad tree). Given an point (lat,lng), I can easily get a corresponding cell at leaf level. From these leaf nodes, I go up and merge cells based on what I need (number of points in a cell). This tutorial explains everything in details.

Here is my result: enter image description here

lenhhoxung
  • 2,530
  • 2
  • 30
  • 61