Пример #1
0
    def setup_rtree(self, ndim, clusters=None):
        if self._rtree:
            return self._rtree

        self._ndim = ndim
        if not ndim:

            class k(object):
                def __init__(self, graph):
                    self.graph = graph

                def insert(self, *args, **kwargs):
                    pass

                def delete(self, *args, **kwargs):
                    pass

                def intersection(self, *args, **kwargs):
                    return xrange(len(self.graph.clusters))

            self._rtree = k(self)
            return self._rtree

        p = RProp()
        p.dimension = max(2, ndim)
        p.dat_extension = 'data'
        p.idx_extension = 'index'

        if clusters:
            gen_func = ((i, self.bbox_rtree(c, enlarge=0.005), None)
                        for i, c in enumerate(clusters))
            self._rtree = RTree(gen_func, properties=p)
        else:
            self._rtree = RTree(properties=p)
        return self._rtree
Пример #2
0
  def setup_rtree(self, ndim, clusters=None):
    if self._rtree:
        return self._rtree

    self._ndim = ndim
    if not ndim:
        class k(object):
            def __init__(self, graph):
                self.graph = graph
            def insert(self, *args, **kwargs):
                pass
            def delete(self, *args, **kwargs):
                pass
            def intersection(self, *args, **kwargs):
                return xrange(len(self.graph.clusters))
        self._rtree = k(self)
        return self._rtree


    p = RProp()
    p.dimension = max(2, ndim)
    p.dat_extension = 'data'
    p.idx_extension = 'index'

    if clusters:
        gen_func = ((i, self.bbox_rtree(c, enlarge=0.005), None) for i, c in enumerate(clusters))
        self._rtree = RTree(gen_func, properties=p)
    else:
        self._rtree = RTree(properties=p)
    return self._rtree
 def __init__(self):
     self.obstacles = {}
     self.obstacle_index = rtree.index.Index(properties=Property(
         dimension=3))
     self.vertices = {}
     self.edges = {}
     self.vertex_index = rtree.index.Index(properties=Property(dimension=3))
Пример #4
0
    def test_tpr(self):
        # TODO : this freezes forever on some windows cloud builds
        if os.name == 'nt':
            return

        # Cartesians list for brute force
        objects = dict()
        tpr_tree = Index(properties=Property(type=RT_TPRTree))

        for operation, t_now, object_ in data_generator():
            if operation == "INSERT":
                tpr_tree.insert(object_.id, object_.get_coordinates())
                objects[object_.id] = object_
            elif operation == "DELETE":
                tpr_tree.delete(object_.id, object_.get_coordinates(t_now))
                del objects[object_.id]
            elif operation == "QUERY":
                tree_intersect = set(
                    tpr_tree.intersection(object_.get_coordinates()))

                # Brute intersect
                brute_intersect = set()
                for tree_object in objects.values():
                    x_low, y_low = tree_object.getXY(object_.start_time)
                    x_high, y_high = tree_object.getXY(object_.end_time)

                    if intersects(
                            x_low, y_low, x_high, y_high,  # Line
                            object_.x, object_.y, object_.dx, object_.dy):  # Rect
                        brute_intersect.add(tree_object.id)

                # Tree should match brute force approach
                assert tree_intersect == brute_intersect
Пример #5
0
    def create_rtree(self, clusters):
        if not len(clusters[0].bbox[0]):
            class k(object):
                def intersection(self, foo):
                    return xrange(len(clusters))
            return k()

        ndim = len(clusters[0].bbox[0]) + 1
        p = RProp()
        p.dimension = ndim
        p.dat_extension = 'data'
        p.idx_extension = 'index'

        rtree = RTree(properties=p)
        for idx, c in enumerate(clusters):
            rtree.insert(idx, c.bbox[0] + (0,) + c.bbox[1] + (1,))
        return rtree
Пример #6
0
 def __init__(self, resolution, basename=None, overwrite=False):
     # type: (float) -> None
     self._res = resolution
     self._cnt = 0
     if basename is None:
         self._index = Index(interleaved=True)
     else:
         p = Property(overwrite=overwrite)
         self._index = Index(basename, interleaved=True, properties=p)
Пример #7
0
    def create_rtree(self, clusters):
        if not len(clusters[0].bbox[0]):

            class k(object):
                def intersection(self, foo):
                    return xrange(len(clusters))

            return k()

        ndim = len(clusters[0].bbox[0]) + 1
        p = RProp()
        p.dimension = ndim
        p.dat_extension = 'data'
        p.idx_extension = 'index'

        rtree = RTree(properties=p)
        for idx, c in enumerate(clusters):
            rtree.insert(idx, c.bbox[0] + (0, ) + c.bbox[1] + (1, ))
        return rtree
Пример #8
0
def demo_delete():
    seed = 1  # Seed for random points

    countries = get_countries()

    country_id_to_remove = 170  # United States of America
    country_uuids_to_remove = []  # Polygons' ids to remove from the index

    properties = Property()
    # properties.writethrough = True
    # properties.leaf_capacity = 1000
    # properties.fill_factor = 0.5
    index = Index(properties=properties)

    points_per_polygon = 1
    points = []

    # Inserts countries data to the index
    for i, (country_name, geometry) in enumerate(countries):
        for polygon in get_polygons(geometry):
            temp_uuid = uuid.uuid1().int
            index.insert(temp_uuid, polygon.bounds, country_name)

            if i == country_id_to_remove:
                # Saves index ids of the polygon to be removed later
                country_uuids_to_remove.append(temp_uuid)

            # Generates random points in every polygon and saves them
            random_points = gen_random_point(points_per_polygon, polygon, seed)
            points.append((country_name, random_points))

    # Checks every generated point has matches
    for (country_name, country_points) in points:
        for point in country_points:
            hits = list(index.intersection(point.bounds, objects=True))
            assert any(hit.object == country_name for hit in hits)

    # Remove geometry
    geometry = countries[country_id_to_remove][1]
    for i, polygon in enumerate(get_polygons(geometry)):
        index.delete(country_uuids_to_remove[i], polygon.bounds)

    points_missing = []

    # Checks (again) if every generated point has matches
    for (country_name, country_points) in points:
        for point in country_points:
            hits = list(index.intersection(point.bounds, objects=True))
            # Save any point without matches
            if not any(hit.object == country_name for hit in hits):
                points_missing.append(str(point) + " - " + country_name)

    # Print missing points
    for point in points_missing:
        print(point)
Пример #9
0
    def construct_rtree(self, clusters):
        if not len(clusters[0].bbox[0]):
            class k(object):
                def intersection(self, foo):
                    return xrange(len(clusters))
            return k()
        ndim = max(2, len(clusters[0].centroid))
        p = RProp()
        p.dimension = ndim
        p.dat_extension = 'data'
        p.idx_extension = 'index'

        rtree = RTree(properties=p)
        for idx, c in enumerate(clusters):
            box = c.bbox #self.scale_box(c.bbox)
            if ndim == 1:
                rtree.insert(idx, box[0] + [0] + box[1] + [1])
            else:
                rtree.insert(idx, box[0] + box[1])
        return rtree
Пример #10
0
def make_index(shapes):
    """Creates an index for fast and efficient spatial queries.

    Args:
      shapes: shapely shapes to bulk-insert bounding boxes for into the spatial index.

    Returns:
      The spatial index created from the shape's bounding boxes.
    """

    # Todo: benchmark these for our use-cases
    prop = Property()
    prop.dimension = 2
    prop.leaf_capacity = 1000
    prop.fill_factor = 0.9

    def bounded():
        for i, shape in enumerate(shapes):
            yield (i, shape.bounds, None)

    return Index(bounded(), properties=prop)
def make_index(shapes):
    '''Creates an index for fast and efficient spatial queries.

    Args:
      shapes: shapely shapes to bulk-insert bounding boxes for into the spatial index.

    Returns:
      The spatial index created from the shape's bounding boxes.
    '''

    # Todo: benchmark these for our use-cases
    prop = Property()
    prop.dimension = 2
    prop.leaf_capacity = 1000
    prop.fill_factor = 0.9

    def bounded():
        for i, shape in enumerate(shapes):
            yield (i, shape.bounds, None)

    return Index(bounded(), properties=prop)
Пример #12
0
 def build_rtree(self):
     '''
     Construct an R-tree for the domain. This may reduce the
     computational complexity of the methods `intersection_count`,
     `contains`, `orient_simplices`, and `snap`.
     '''
     # create a bounding box for each simplex and add those
     # bounding boxes to the R-tree
     if self.rtree is not None:
         # do nothing because the R-tree already exists
         logger.debug('R-tree already exists')
         return
         
     smp_min = self.vertices[self.simplices].min(axis=1)
     smp_max = self.vertices[self.simplices].max(axis=1)
     bounds = np.hstack((smp_min, smp_max))
     
     p = Property()
     p.dimension = self.dim
     self.rtree = Index(properties=p)
     for i, bnd in enumerate(bounds):
         self.rtree.add(i, bnd)
Пример #13
0
                return 0
            else:
                obj.data = data
                return page

    def deleteByteArray(self, page, returnError):
        """ Deletes a page """
        try:
            RTreePage.objects(name=self.name, page=page).delete(safe=True)
        except:
            returnError.contents.value = self.InvalidPageError

    hasData = property( lambda self: RTreePage.objects(name=self.name).first() is not None )

if __name__=='__main__':
    settings = Property()
    settings.writethrough= True
    settings.buffering_capacity=1

    storage = MongoStorage('test')
    storage.clear()
    r = Rtree( storage, properties=settings)

    r.add(123,(0,0,1,1))
    
    print "test 1 should be true"
    item = list(r.nearest((0,0), 1, objects=True))[0]
    print item.id
    print r.valid()

    print "test 2 should be true"
Пример #14
0
                obj.data = data
                return page

    def deleteByteArray(self, page, returnError):
        """ Deletes a page """
        try:
            RTreePage.objects(name=self.name, page=page).delete(safe=True)
        except:
            returnError.contents.value = self.InvalidPageError

    hasData = property(
        lambda self: RTreePage.objects(name=self.name).first() is not None)


if __name__ == '__main__':
    settings = Property()
    settings.writethrough = True
    settings.buffering_capacity = 1

    storage = MongoStorage('test')
    storage.clear()
    r = Rtree(storage, properties=settings)

    r.add(123, (0, 0, 1, 1))

    print "test 1 should be true"
    item = list(r.nearest((0, 0), 1, objects=True))[0]
    print item.id
    print r.valid()

    print "test 2 should be true"
Пример #15
0
def tpr_tree(request):
    # Create tree
    from rtree.index import Index, Property, RT_TPRTree
    return Index(properties=Property(type=RT_TPRTree))