I have a set of regions (geo-fences) which are polygons. This set of data is fixed; so there is no need for insertion and deletion of data. Which data structure can be used for searching for regions that a query point (longitude, latitude) is in it?
Note: I have implemented KD-Tree (In fact a 2D-Tree) successfully for a set of points. But it does not work for this problem. I have implemented an R-Tree then; and it solves the problem but it is slow (or my implementation sucks).
Thank you
Note: I have worked on R-Tree implementation and it works fine now.
Since you are not inserting/deleting and presumably have plenty of time to preprocess the data, you can use some additional memory to speed up computations. The basic idea for pre-processing:
Now, when you want to find regions that contain a point:
This works well for spread out and mostly non-intersecting polygons, particularly if you can choose a grid size fine enough so that there are only a few polygons per square. It will be slow in cases where you hit squares with lots of intersecting polygons. One additional optimization is to have a flag for each listed polygon at a square to indicate that the square is completely contained within the polygon; this allows you to avoid the slow containment test in many cases at the cost of a single bit per polygon entry. This is particularly valuable if your grid spacing is fine compared to the polygon sizes, as most squares will not be at intersections or edges.
If you need even more speed, you can start storing edge information at each square with the polygon reference. You only need to test against the polygon edges that actually intersect the area of the square. This can reduce the effort to only a handful of edge tests per polygon.
A R-Tree data structure can be used for this problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With