def test_Delete(self): # Deleting non existing object point1 = rtree.Point(5, 1, 0, 0, 2, 3) point2 = rtree.Point(5, 2, 0, 0, 2, 3) point3 = rtree.Point(5, 3, 0, 0, 1, 1) point4 = rtree.Point(5, 4, 0, 0, 1, 1) dummyPoints = [point1, point2, point3, point4] # List of points objectDummy = self.rtree.createObject(dummyPoints) self.assertEqual(self.rtree.Delete(objectDummy), 0) # Deleting existing object # Add all objects self.rtree.Insert(self.f1) self.rtree.Insert(self.f2) self.rtree.Insert(self.f3) self.rtree.Insert(self.f4) self.rtree.Insert(self.f5) # FOR REPORT # self.assertEqual(self.rtree.Delete(self.f1), 1) # # Delete 2nd point # self.assertEqual(self.rtree.Delete(self.f2), 1) # # Delete 3rd point # self.assertEqual(self.rtree.Delete(self.f3), 1) # # Delete 4th point # self.assertEqual(self.rtree.Delete(self.f4), 1) # # Delete 5th point # self.assertEqual(self.rtree.Delete(self.f5), 1) # Find them -> Delete them -> Find them self.assertEqual(self.rtree.Query(self.f1), self.f1) self.assertEqual(self.rtree.Delete(self.f1), 1) self.assertEqual(self.rtree.Query(self.f1), 0) self.assertEqual(self.rtree.Query(self.f2), self.f2) self.assertEqual(self.rtree.Delete(self.f2), 1) self.assertEqual(self.rtree.Query(self.f2), 0) self.assertEqual(self.rtree.Query(self.f3), self.f3) self.assertEqual(self.rtree.Delete(self.f3), 1) self.assertEqual(self.rtree.Query(self.f3), 0) self.assertEqual(self.rtree.Query(self.f4), self.f4) self.assertEqual(self.rtree.Delete(self.f4), 1) self.assertEqual(self.rtree.Query(self.f4), 0) self.assertEqual(self.rtree.Query(self.f5), self.f5) self.assertEqual(self.rtree.Delete(self.f5), 1) self.assertEqual(self.rtree.Query(self.f5), 0)
def test_Insert50(self): # Creating object with 50 points points = [] i = 0 while i != 50: point = rtree.Point(1, i, 1, 1, 1, 1) points.append(point) i += 1 pointsObject = self.rtree.createObject(points) self.assertEqual(self.rtree.Insert(pointsObject), 1)
def getPointsStructures(self, structure): if structure == "quad" or structure == "kd": return self.getPoints() elif structure == "r": plansObjectList = [] # Get points plans = self.getPoints() # Convert them to RTree object for plan in plans.values(): plansObjectList.append(RTree.createObject(plan)) return plansObjectList else: return None
def untangle_dimensions_from_ocr(arr, dims, ocr): """ filter out ocrs based on context dimensions """ ocr_tree = RTree(arr.img.shape) ocr_tree.add_objs(ocr) for d in dims: #sumpt1 = d.tri1 #print(d.tri1) center1 = (np.sum(d.tri1, axis=0) // 3) center2 = (np.sum(d.tri2, axis=0) // 3) ocrs = ocr_tree.intersectPoint(center1) ocrs += ocr_tree.intersectPoint(center2) for o in ocrs: o.trash = True
def context_aware_correction(orig, ins): orig = np.copy(orig) T = Timer() T.enable(0) arr = ins['arr'] circles = ins['circles'] lines = ins['lines'] T.TIME() Updates.triangles(ins['triangles']) Updates.lines(ins['lines']) Updates.rectangles(ins['rectangles']) tri_tree = RTree(arr.img.shape) line_tree = RTree(arr.img.shape, False) tri_tree.add_objs(ins['triangles']) line_tree.add_objs(ins['lines']) T.TIME() T.echo('tree creation:') T.TIME() triangles = ins['triangles'] triangles, merges, merged = coalesce_triangles(arr['img'], triangles, tri_tree) ins['triangles'] = triangles T.TIME() T.echo('triangle coalesce time:') T.TIME() clamp_slopes(lines) merged = coalesce_lines(arr['img'], lines, line_tree) newlines = flatten(merged) clamp_slopes(newlines) ins['lines'] = newlines T.TIME() T.echo('line coalesce time:') #T.TIME() grow_lines(arr['img'], ins['lines']) Updates.lines(ins['lines']) merged = remove_overlapping_lines(merged) ins['lines'] = flatten(merged) ins['colinear_groups'] = merged line_tree = RTree(arr.img.shape, False) line_tree.add_objs(ins['lines']) #T.TIME() #T.echo('line grow time:') line_tree.test_coherency(ins['lines']) T.TIME() allpts = generate_line_girths(arr['img'], ins['lines']) colines = [Shape().init_from_line_group(g) for g in merged] dims = [] #for x in lines: for x in colines: # TODO also look for --> <-- types via colinear groups dims += TriangleHumps.get_dimensions(x, im=orig) ins['dimensions'] = dims T.TIME() T.echo('context-aware dimension detection:') # get line paths from dims T.TIME() ocr = ins['ocr'] untangle_dimensions_from_ocr(arr, dims, ocr) new_horz, new_verz = infer_ocr_groups(arr, ocr) new_horz = block_marked_groups(new_horz) new_verz = block_marked_groups(new_verz) T.TIME() T.echo('ocr inferring time:') T.TIME() ins['circles'], ocr_rejects = untangle_circles(circles, new_horz + new_verz) new_horz = remove_ocr_groups(new_horz, ocr_rejects) new_verz = remove_ocr_groups(new_verz) Updates.circles(ins['circles']) new_horz = [ocrgroup_to_textbox(x) for x in new_horz] new_verz = [ocrgroup_to_textbox(x, True) for x in new_verz] ins['ocr_groups_horz'] = new_horz ins['ocr_groups_verz'] = new_verz T.TIME() T.echo('circle untangle time:') boxtree = RTree(arr.img.shape, False) #left,bottom,right,top = self.get_bounds(x,) boxtree.add_objs( new_horz, lambda x: (x.p[0], x.p[1], x.p[0] + x.width, x.p[1] + x.height)) boxtree.add_objs( new_verz, lambda x: (x.p[0], x.p[1], x.p[0] + x.width, x.p[1] + x.height)) for tbox in new_horz + new_verz: print(tbox.text) for d in dims: print(d.line, ' intersects:') pad = d.base_len if d.type == structures.HORIZONTAL: xmin = min(d.line[0][0], d.line[1][0]) xmax = max(d.line[0][0], d.line[1][0]) #box = boxtree.xmin, d.line[0][1] - pad, boxtree.xmax, d.line[0][1] + pad box = xmin, d.line[0][1] - pad, xmax, d.line[0][1] + pad elif d.type == structures.VERTICAL: ymin = min(d.line[0][1], d.line[1][1]) ymax = max(d.line[0][1], d.line[1][1]) #box = d.line[0][0], boxtree.ymin - pad, d.line[0][0], boxtree.ymax + pad box = d.line[0][0] - pad, ymin, d.line[0][0] + pad, ymax else: # skip print('DIAGANOL') continue texts = boxtree.intersectBox(box) for t in texts: print(' %s' % t.text) #dump_plotly(ins['lines'], plotfuncs.side_traces) #dump_plotly(colines,plotfuncs.colinear_groups) #ins['lines'] = [l for l in ins['lines'] if l['id'] == 331] #draw_pts(orig,allpts) #draw_ocr_group_rects(orig, new_horz, new_verz) return ins
def test_createObject(self): # HORIZONTAL OBJECT point1 = rtree.Point(1, 1, 1, 1, 2, 3) point2 = rtree.Point(1, 2, 1, 2, 2, 3) point3 = rtree.Point(1, 3, 1, 3, 1, 1) point4 = rtree.Point(1, 4, 1, 4, 1, 1) pointsH = [point1, point2, point3, point4] # List of points, HORIZONTAL start = (pointsH[0].getLong(), pointsH[0].getLat()) # Start coords end = (pointsH[-1].getLong(), pointsH[-1].getLat()) # End coords self.assertEqual( self.rtree.createObject(pointsH).getPoints(), rtree.Object(start[0], start[1], end[0], end[1], 1, points=pointsH).getPoints()) # VERTICAL OBJECT point1 = rtree.Point(1, 1, 1, 1, 2, 3) point2 = rtree.Point(1, 2, 2, 1, 2, 3) point3 = rtree.Point(1, 3, 3, 1, 1, 1) point4 = rtree.Point(1, 4, 4, 1, 1, 1) pointsV = [point1, point2, point3, point4] # List of points, VERTICAL start = (pointsV[0].getLong(), pointsV[0].getLat()) # Start coords end = (pointsV[-1].getLong(), pointsV[-1].getLat()) # End coords self.assertEqual( self.rtree.createObject(pointsV).getPoints(), rtree.Object(start[0], start[1], end[0], end[1], 0, points=pointsV).getPoints()) # NON VERTICAL OR HORIZONTAL OBJECT point1 = rtree.Point(1, 1, 1, 9, 2, 3) point2 = rtree.Point(1, 2, 2, 1, 2, 3) point3 = rtree.Point(1, 3, 3, 1, 1, 1) point4 = rtree.Point(1, 4, 4, 2, 1, 1) # Altered latitude points = [point1, point2, point3, point4] # List of points, VERTICAL self.assertEqual(self.rtree.createObject(points), None)
def setUp(self): # Initial params longitude1 = 0 latitude1 = 0 longitude2 = 10 latitude2 = 10 fanout = 2 self.rtree = rtree.RTree(longitude1, latitude1, longitude2, latitude2, fanout) # self.rtree.root.purgeChildren() # self.rtree.root.purgeObjects() # Creating objects # Object 1 point1 = rtree.Point(1, 1, 1, 1, 2, 3) point2 = rtree.Point(1, 2, 1, 2, 2, 3) point3 = rtree.Point(1, 3, 1, 3, 1, 1) point4 = rtree.Point(1, 4, 1, 4, 1, 1) f1Points = [point1, point2, point3, point4] # List of points self.f1 = self.rtree.createObject(f1Points) # Object 2 point1 = rtree.Point(2, 1, 2, 1, 2, 3) point2 = rtree.Point(2, 2, 2, 2, 2, 3) point3 = rtree.Point(2, 3, 2, 3, 1, 1) point4 = rtree.Point(2, 4, 2, 4, 1, 1) f2Points = [point1, point2, point3, point4] # List of points self.f2 = self.rtree.createObject(f2Points) # Object 3 point1 = rtree.Point(3, 1, 3, 1, 2, 3) point2 = rtree.Point(3, 2, 3, 2, 2, 3) point3 = rtree.Point(3, 3, 3, 3, 1, 1) point4 = rtree.Point(3, 4, 3, 4, 1, 1) f3Points = [point1, point2, point3, point4] # List of points self.f3 = self.rtree.createObject(f3Points) # Object 4 point1 = rtree.Point(4, 1, 4, 1, 2, 3) point2 = rtree.Point(4, 2, 4, 2, 2, 3) point3 = rtree.Point(4, 3, 4, 3, 1, 1) point4 = rtree.Point(4, 4, 4, 4, 1, 1) f4Points = [point1, point2, point3, point4] # List of points self.f4 = self.rtree.createObject(f4Points) # Object 5 point1 = rtree.Point(5, 1, 5, 1, 2, 3) point2 = rtree.Point(5, 2, 5, 2, 2, 3) point3 = rtree.Point(5, 3, 5, 3, 1, 1) point4 = rtree.Point(5, 4, 5, 4, 1, 1) f5Points = [point1, point2, point3, point4] # List of points self.f5 = self.rtree.createObject(f5Points) # Negative params longitude3 = -1 latitude3 = -1 longitude4 = -10 latitude4 = -10 self.rtreeNegative = rtree.RTree(longitude3, latitude3, longitude4, latitude4, fanout) # Negative Object point1Neg = rtree.Point(5, 1, -5, -1, 2, 3) point2Neg = rtree.Point(5, 2, -5, -2, 2, 3) point3Neg = rtree.Point(5, 3, -5, -3, 1, 1) point4Neg = rtree.Point(5, 4, -5, -4, 1, 1) fnegPoints = [point1Neg, point2Neg, point3Neg, point4Neg] # List of points self.fneg = self.rtree.createObject(fnegPoints)
def main(): # GLOBAL PARAMETERS maxPoints = 1 # QLD Coverage longitude1 = 138 latitude1 = 28 longitude2 = 153 latitude2 = 10 # Generator Params numPlans = 50 numPoints = 10 maxAltitude = 20 # Quadtree Initialization quadtree = Quadtree.Quadtree(longitude1, latitude1, longitude2, latitude2, maxPoints) # R-Tree Initialization FANOUT = 4 rtree = RTree.RTree(longitude1, latitude1, longitude2, latitude2, FANOUT) # K-D Tree Initialization kdtree = KDTree.KDTree() # Flight Plan Generator Initialization fpg = FlightPlanGenerator.FlightPlanGenerator(longitude1, latitude1, longitude2, latitude2, numPlans, numPoints, maxAltitude) ################## FLIGHT PLAN GENERATIONS ##################### # TAKE OFF AND LAND FLIGHT fpg.manualGenerator() quadtreePoints = fpg.getPointsStructures("quad") if fpg.sendRequests() == 1: # Quadtree insertResultQuad = [] for plan in quadtreePoints.values(): for point in plan: # print(point.getAll()) insertResultQuad.append(quadtree.Insert(point)) # RANDOM FLIGHT fpg.randomGenerator() kdtreePoints = fpg.getPointsStructures("kd") rtreePoints = fpg.getPointsStructures("r") if fpg.sendRequests() == 1: # KDTree insertResultKD = [] for plan in kdtreePoints.values(): for point in plan: insertResultKD.append(kdtree.Insert(point)) # RTree insertResultR = [] for plan in rtreePoints: insertResultR.append(rtree.Insert(plan)) x = 1