From e547386649872ad0a964c7066ba9054994b673e3 Mon Sep 17 00:00:00 2001 From: Juan Pablo Caram Date: Sun, 28 Dec 2014 18:06:54 -0500 Subject: [PATCH] Custom RTree wrapper. FlatCAMRTree and FlatCAMRTreeStorage. --- camlib.py | 116 ++++++++++++++++++++++++++++++++++++++++++++++- tests/test_rt.py | 9 ++-- upgrade_geos.sh | 7 +++ 3 files changed, 127 insertions(+), 5 deletions(-) create mode 100644 upgrade_geos.sh diff --git a/camlib.py b/camlib.py index d49a6165..575cfadc 100644 --- a/camlib.py +++ b/camlib.py @@ -3184,4 +3184,118 @@ def three_point_circle(p1, p2, p3): def distance(pt1, pt2): - return sqrt((pt1[0] - pt2[0]) ** 2 + (pt1[1] - pt2[1]) ** 2) \ No newline at end of file + return sqrt((pt1[0] - pt2[0]) ** 2 + (pt1[1] - pt2[1]) ** 2) + + +class FlatCAMRTree(object): + def __init__(self): + self.rti = rtindex.Index() + self.obj2points = [] + self.points2obj = [] + + def grow_obj2points(self, idx): + if len(self.obj2points) > idx: + # len == 2, idx == 1, ok. + return + else: + # len == 2, idx == 2, need 1 more. + # range(2, 3) + for i in range(len(self.obj2points), idx + 1): + self.obj2points.append([]) + + def insert(self, objid, obj): + self.grow_obj2points(objid) + self.obj2points[objid] = [] + + for pt in obj.coords: + self.rti.insert(len(self.points2obj), (pt[0], pt[1], pt[0], pt[1]), obj=objid) + self.obj2points[objid].append(len(self.points2obj)) + self.points2obj.append(objid) + + def remove_obj(self, objid, obj): + # Use all ptids to delete from index + for i in range(len(self.obj2points[objid])): + pt = obj.coords[i] + self.rti.delete(self.obj2points[objid][i], (pt[0], pt[1], pt[0], pt[1])) + + def nearest(self, pt): + return self.rti.nearest(pt, objects=True).next() + + +class FlatCAMRTreeStorage(FlatCAMRTree): + def __init__(self): + super(FlatCAMRTreeStorage, self).__init__() + + self.objects = [] + + def insert(self, obj): + self.objects.append(obj) + super(FlatCAMRTreeStorage, self).insert(len(self.objects) - 1, obj) + + def remove(self, obj): + objidx = self.objects.index(obj) + self.objects[objidx] = None + self.remove_obj(objidx, obj) + + def get_objects(self): + return (o for o in self.objects if o is not None) + + def nearest(self, pt): + tidx = super(FlatCAMRTreeStorage, self).nearest(pt) + return (tidx.bbox[0], tidx.bbox[1]), self.objects[tidx.object] + +class myO: + def __init__(self, coords): + self.coords = coords + + +def test_rti(): + + o1 = myO([(0, 0), (0, 1), (1, 1)]) + o2 = myO([(2, 0), (2, 1), (2, 1)]) + o3 = myO([(2, 0), (2, 1), (3, 1)]) + + os = [o1, o2] + + idx = FlatCAMRTree() + + for o in range(len(os)): + idx.insert(o, os[o]) + + print [x.bbox for x in idx.rti.nearest((0, 0), num_results=20, objects=True)] + + idx.remove_obj(0, o1) + + print [x.bbox for x in idx.rti.nearest((0, 0), num_results=20, objects=True)] + + idx.remove_obj(1, o2) + + print [x.bbox for x in idx.rti.nearest((0, 0), num_results=20, objects=True)] + + +def test_rtis(): + + o1 = myO([(0, 0), (0, 1), (1, 1)]) + o2 = myO([(2, 0), (2, 1), (2, 1)]) + o3 = myO([(2, 0), (2, 1), (3, 1)]) + + os = [o1, o2] + + idx = FlatCAMRTreeStorage() + + for o in range(len(os)): + idx.insert(os[o]) + + #os = None + #o1 = None + #o2 = None + + print [x.bbox for x in idx.rti.nearest((0, 0), num_results=20, objects=True)] + + idx.remove(idx.nearest((2,0))[1]) + + print [x.bbox for x in idx.rti.nearest((0, 0), num_results=20, objects=True)] + + idx.remove(idx.nearest((0,0))[1]) + + print [x.bbox for x in idx.rti.nearest((0, 0), num_results=20, objects=True)] \ No newline at end of file diff --git a/tests/test_rt.py b/tests/test_rt.py index 018781e6..cb80b3f3 100644 --- a/tests/test_rt.py +++ b/tests/test_rt.py @@ -6,10 +6,11 @@ def pt2rect(pt): pts = [(0.0, 0.0), (1.0, 1.0), (0.0, 1.0)] -#p = rtindex.Property() -#p.buffering_capacity = 1 -#rt = rtindex.Index(properties=p) -rt = rtindex.Index() +p = rtindex.Property() +p.buffering_capacity = 1 +p.dimension = 2 +rt = rtindex.Index(properties=p) +#rt = rtindex.Index() # If interleaved is True, the coordinates must be in # the form [xmin, ymin, ..., kmin, xmax, ymax, ..., kmax]. diff --git a/upgrade_geos.sh b/upgrade_geos.sh new file mode 100644 index 00000000..8c309a67 --- /dev/null +++ b/upgrade_geos.sh @@ -0,0 +1,7 @@ +#!/bin/sh +wget http://download.osgeo.org/geos/geos-3.4.2.tar.bz2 +tar xjvf geos-3.4.2.tar.bz2 +cd geos-3.4.2 +./configure --prefix=/usr +make +make install \ No newline at end of file