def buildRtree(dataSetName, *B): global root global Bvalue Bvalue = 25 if len(B) == 1 and B[0] != None: Bvalue = B[0] f = open(dataSetName, 'rt') nextLine = f.readline() # size of date set size = int(nextLine.strip('\n')) # read the first point and build a root nextLine = f.readline() point = Rtree.Point(scanRange.getPoint(nextLine)) root = Rtree.Leaf(Bvalue, 1, point) root.addChild(point) # add the remained points nextLine = f.readline() while nextLine != '': point = Rtree.Point(scanRange.getPoint(nextLine)) insert(root, point) nextLine = f.readline() f.close() print('R-tree has been built. B is:', Bvalue, 'Highest level is:', root.level) return root
def buildRtree(dataSetName, *B): global root global Bvalue Bvalue = 25 # Upper limit of the node, initial 25. if len(B) == 1 and B[0] != None: Bvalue = B[0] f = open(dataSetName, 'rt') nextLine = f.readline() # size of date set size = int(nextLine.strip('\n')) # read the first point and build a root nextLine = f.readline() point = Rtree.Point(getPoint(nextLine)) root = Rtree.Leaf(Bvalue, 1, point) root.addChild(point) # add the remained points nextLine = f.readline() while nextLine == '\n': nextLine = f.readline() while nextLine != '': point = Rtree.Point(getPoint(nextLine)) insert(root, point) nextLine = f.readline() while nextLine == '\n': nextLine = f.readline() f.close() maintain(root) print('R-tree has been built. B is:', Bvalue,'. Highest level is:',root.level) '''print('第1层: ',root.attribute,' ',root.bitmap) for child in root.childList: print('第2层: ',child.attribute,' ',child.bitmap) for cc in child.childList: print('第3层: ', cc.attribute, ' ', cc.bitmap) for ccc in child.childList: print('第4层: ', ccc.attribute, ' ', ccc.bitmap) for aaa in ccc.childList: print(aaa.x,' ',aaa.y,' ',aaa.attribute,aaa.bitmap)''' return root
def handleOverFlow(node): global root global Bvalue # split node into two new nodes nodes = node.split() # if root node is overflow, new root need to build if node.paren == None: root = Rtree.Branch(Bvalue, node.level + 1, nodes[0]) root.addChild(nodes[0]) root.addChild(nodes[1]) root.childList[0].paren = root root.childList[1].paren = root else: # update the parent node parent = node.paren parent.childList.remove(node) parent.childList += nodes # check whether parent node is overflow if parent.isOverFlow(): handleOverFlow(parent)
def basicTest(): gen = MbrGenerator() mTree = Rtree(d=4, M=25, maxE=10 ** 6, reset=True, initOffset=0, partitionType=0) resFileName = "../results/Resultados Test.txt" f = open(resFileName, 'a+') f.write(datetime.datetime.now().strftime("%Y-%m-%d %H:%M")) f.write(" d=%d M=%d partitionType=%d\n" % (4, 25, 0)) f.write("meanPartitionsPerNode meanInsertTime meanTotalNodes meanInternalNodes\n") # Construyo el Rtree con los elementos for s in range (10 ** 3): mTree.insert(gen.next(d=4)) f.write("%f %f %f %f\n" % (mTree.getMeanNodePartitions(), mTree.meanInsertionTime, mTree.meanTotalNodes, mTree.meanInternalNodes)) f.write("meanVisited meanQueryTime\n") f.close() fileName = "../results/busqueda test " + str(mTree.treeType) + " M" + str(mTree.M()) + " d" + str(mTree.d()) + ".bin" mTree = Rtree(d=4, M=25, maxE=10 ** 6, reset=False, initOffset=0, partitionType=0) # Construyo los elementos de consulta f = open(resFileName, 'a+') fb = open(fileName, 'a+') for s in range (100): mTree.search(gen.nextRadial(d=4, r=0.25 * 2), fb,False, True) f.write("%f %f\n" % (mTree.getMeanVisitedNodes(), mTree.meanSearchTime)) f.close() fb.close()
from Rtree import * from random import uniform from time import time import csv root = Rtree(m=3, M=7) stations = [] i = 0 with open("2013NRSC.csv", "rU") as csvfile: radioCsv = csv.reader(csvfile, delimiter=",", quotechar='"') for station in radioCsv: y = float(station[7]) x = float(station[8]) stations.append( node( MBR={ "xmin": x - 0.01, "xmax": x + 0.01, "ymin": y - 0.01, "ymax": y + 0.01, }, index=i, )) i = i + 1 for j in range(i): root = Insert(root, stations[j]) print(type(root)) print(root.leaves)
#!/usr/bin/python # -*- coding: utf-8 -*- from Rtree import * from random import uniform from time import time numRuns = 1000 data = {} # Initialize 10000 coordinates (-1000, 1000), an area of a rectangle 0.01. for i in range(numRuns): x = uniform(-1000, 1000) y = uniform (-1000, 1000) data [i] = {'xmin': x, 'xmax': x + 0.01, 'ymin': y, 'ymax': y + 0.01} # Set a root node, m = 3, M = 7 root = Rtree(m = 3, M = 7) n = [] for i in range(numRuns): n.append(node(MBR = data[i], index = i)) t0 = time() # Insert for i in range(numRuns): root = Insert(root, n[i]) print root t1 = time() print 'Inserting ...' print t1 - t0 #search for x = root.Search(merge(n[0].MBR, n[1].MBR)) print x
mTree.search(gen.nextRadial(d=4, r=0.25 * 2), fb,False, True) f.write("%f %f\n" % (mTree.getMeanVisitedNodes(), mTree.meanSearchTime)) f.close() fb.close() if __name__ == '__main__': # basicTest() resFileName = "../results/Resultados.txt" for j in range(1, 11): k = 1 for partitionType in [0, 1]: # for d in [2, 4, 8, 16]: for d in [4, 8, 16]: for M in [50, 100]: gen = MbrGenerator() mTree = Rtree(d=d, M=M, maxE=10 ** 6, reset=True, initOffset=0, partitionType=partitionType) f = open(resFileName, 'a+') f.write(datetime.datetime.now().strftime("%Y-%m-%d %H:%M")) f.write(" d=%d M=%d partitionType=%d\n" % (d, M, partitionType)) f.write("meanPartitionsPerNode meanInsertTime meanTotalNodes meanInternalNodes\n") # Construyo el Rtree con los elementos for s in range (10 ** 5): mTree.insert(gen.next(d=d)) f.write("%f %f %f %f\n" % (mTree.getMeanNodePartitions(), mTree.meanInsertionTime, mTree.meanTotalNodes, mTree.meanInternalNodes)) f.write("meanVisited meanQueryTime\n") print("Insercion n:%d" % k) k += 1 f.close() fileName = "../results/busqueda " + str(mTree.treeType) + " M" + str(mTree.M()) + " d" + str(mTree.d()) + ".bin" # Construyo los elementos de consulta for radio in [0.25, 0.50, 0.75]: