Discussion:
[Community] Not removing correctly from RTree index
Juan Pablo Caram
2014-12-27 21:23:20 UTC
Permalink
Hi,

I'm having trouble deleting from an rtree index. In the script below I add
3 points and then try to delete them:

from rtree import index as rtindex
pts = [[0.0, 0.0], [0.0, 1.0], [1.0, 1.0]]
rt = rtindex.Index()
print rt.interleaved

[rt.add(0, [x[0], x[1], x[0], x[1]]) for x in pts]
print [x.bbox for x in list(rt.nearest([0, 0], 10, True))]

for x in pts:
rt.delete(0, [x[0], x[1], x[0], x[1]])
print x, [x.bbox for x in list(rt.nearest([0, 0], 10, True))]



And the result is:

True
[[0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 1.0], [1.0, 1.0, 1.0, 1.0]]
[0.0, 0.0] [[0.0, 1.0, 0.0, 1.0], [1.0, 1.0, 1.0, 1.0]]
[0.0, 1.0] [[0.0, 1.0, 0.0, 1.0]]
[1.0, 1.0] [[0.0, 1.0, 0.0, 1.0]]


Basically, when trying to delete [0, 1] it's deleting the wrong thing. Is
this a bug or am I using it wrong?

Thanks,

JP
Juan Pablo Caram
2014-12-28 17:18:12 UTC
Permalink
I started a stackoverflow question on the issue:
http://stackoverflow.com/questions/27660298/store-2d-points-in-rtree-in-python
Post by Juan Pablo Caram
Hi,
I'm having trouble deleting from an rtree index. In the script below I add
from rtree import index as rtindex
pts = [[0.0, 0.0], [0.0, 1.0], [1.0, 1.0]]
rt = rtindex.Index()
print rt.interleaved
[rt.add(0, [x[0], x[1], x[0], x[1]]) for x in pts]
print [x.bbox for x in list(rt.nearest([0, 0], 10, True))]
rt.delete(0, [x[0], x[1], x[0], x[1]])
print x, [x.bbox for x in list(rt.nearest([0, 0], 10, True))]
True
[[0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 1.0], [1.0, 1.0, 1.0, 1.0]]
[0.0, 0.0] [[0.0, 1.0, 0.0, 1.0], [1.0, 1.0, 1.0, 1.0]]
[0.0, 1.0] [[0.0, 1.0, 0.0, 1.0]]
[1.0, 1.0] [[0.0, 1.0, 0.0, 1.0]]
Basically, when trying to delete [0, 1] it's deleting the wrong thing. Is
this a bug or am I using it wrong?
Thanks,
JP
Howard Butler
2014-12-30 14:36:03 UTC
Permalink
Post by Juan Pablo Caram
[rt.add(0, [x[0], x[1], x[0], x[1]]) for x in pts]
The problem is you're adding all entries with the same id. Rtree indexes are not databases, and the delete method will simply delete the first entry where both the ID and the bbox of the entry match.
Post by Juan Pablo Caram
from rtree import index as rtindex
pts = [[0.0, 0.0], [0.0, 1.0], [1.0, 1.0]]
rt = rtindex.Index()
rt.add(pts.index(x),[x[0], x[1], x[0], x[1]])
print 'available points', [x.bbox for x in list(rt.nearest([0, 0], 10, True))]
x = pts[0]
rt.delete(0, [x[0], x[1], x[0], x[1]])
print 'deleted', x, 'nearest', [x.bbox for x in list(rt.nearest([0, 0], 10, True))]
Hope this helps,

Howard
Howard Butler
2014-12-30 14:40:45 UTC
Permalink
Post by Howard Butler
delete method will simply delete the first entry where both the ID and the bbox of the entry match.
To be more exact, after seeing your SO post.... it will delete the first entry *it finds* where both the ID and bbox match. Not the first inserted one.

http://stackoverflow.com/questions/27660298/store-2d-points-in-rtree-in-python

Howard
Juan Pablo Caram
2014-12-31 20:45:32 UTC
Permalink
Hi Howard,

Yes, I was able to finally figure out the behavior. I just want to point
out that the python rtree documantation is very misleading of not
completely wrong.

Thanks,

JP
Post by Howard Butler
Post by Howard Butler
delete method will simply delete the first entry where both the ID and
the bbox of the entry match.
To be more exact, after seeing your SO post.... it will delete the first
entry *it finds* where both the ID and bbox match. Not the first inserted
one.
http://stackoverflow.com/questions/27660298/store-2d-points-in-rtree-in-python
Howard
_______________________________________________
Community mailing list
http://lists.gispython.org/mailman/listinfo/community
Howard Butler
2015-01-05 16:12:18 UTC
Permalink
Please submit a pull request that updates the documentation to reflect the software behavior.

Howard
Post by Juan Pablo Caram
Hi Howard,
Yes, I was able to finally figure out the behavior. I just want to point out that the python rtree documantation is very misleading of not completely wrong.
Thanks,
JP
Post by Howard Butler
delete method will simply delete the first entry where both the ID and the bbox of the entry match.
To be more exact, after seeing your SO post.... it will delete the first entry *it finds* where both the ID and bbox match. Not the first inserted one.
http://stackoverflow.com/questions/27660298/store-2d-points-in-rtree-in-python
Howard
_______________________________________________
Community mailing list
http://lists.gispython.org/mailman/listinfo/community
_______________________________________________
Community mailing list
http://lists.gispython.org/mailman/listinfo/community
Loading...