Search code examples
pythonr-tree

Python Rtree get/set coordinates for an id


I'm using the Rtree index in libspatialindex to insert some values as indicated here:

>>> idx = index.Index()
>>> idx.insert(42, (0, 0, 1, 1))

how can I change the value for the id 42? I'm looking for something less costly than delete+add. Also, how can I get the coordinate value of a given id?


Solution

  • I came across the same issue and there doesn't seem to be a generic solution. So here is my hack around, hopefully can be of help:

    My set up:

    class Point:
        def __init__(self,x,y):
            self.x = x
            self.y = y
    

    When I index all my points, I run this code:

    idx = index.Index()
    # input_points is a list of Point object
    for ind, pt in enumerate(input_points):
        idx.insert(ind, (pt.x, pt.y, pt.x, pt.y))
    return idx
    

    Here you may have noticed that the id I assigned to each point is the index of that point inside my input_points list.

    So later when I want to get a specific point by id, I run:

    target_point = input_points[id] 
    

    Then you can do things with the target_point because that should be the point you are looking for.

    This is ONLY a hack around, so please let me know if you found a generic solution, I am curious too.