def test_insert(self): node_lst = self.generate_case()[0] r_tree = RTree() for item in node_lst: r_tree.insert(item) self.assertEqual(r_tree.size, 50) self.assertTrue(isinstance(r_tree.head, Node)) return r_tree
def test_add_without_root_should_add_root(self): bounds = MBR(Coordinate(10, 10), Coordinate(20, 0)) o = Obj('Tank', bounds) r_tree = RTree(4, 2) r_tree.insert(o) self.assertIsNotNone(r_tree.root) self.assertIsInstance(r_tree.root, RTree.Node) self.assertEqual(r_tree.root.mbr, MBR.generate(bounds)) self.assertEqual(len(r_tree.root.members), 1) self.assertEqual(r_tree.root.members[0], o)
def test_add_bigger_mbr_Obj_expands_root(self): r_tree = RTree(4, 2) s_Obj = Obj('SMALL_MAN', MBR(Coordinate(52, 43), Coordinate(68, 22))) expected_root_mbr = MBR( Coordinate(52 - POINT_OFFSET, 43 + POINT_OFFSET), Coordinate(68 + POINT_OFFSET, 22 - POINT_OFFSET)) r_tree.insert(s_Obj) self.assertEqual(r_tree.root.mbr, expected_root_mbr) # Add a bigger Obj expected_root_mbr = MBR( Coordinate(20 - POINT_OFFSET, 45 + POINT_OFFSET), Coordinate(70 + POINT_OFFSET, 20 - POINT_OFFSET)) b_Obj = Obj('BIG_MAN', MBR(Coordinate(20, 45), Coordinate(70, 20))) r_tree.insert(b_Obj) self.assertEqual(r_tree.root.mbr, expected_root_mbr) self.assertCountEqual(r_tree.root.members, [b_Obj, s_Obj])
# This is a command-line driver script to locate all the crimes within a user-supplied box. # example: # python find-crimes.py --section 2702 414970 123799 417057 126342 # will print all the aggravated assaults within the box defined by those coordinates. import argparse from Shapes import Rectangle, Circle, Line, Point, BoundingBox, ShapeUnion from RTree import RTree import numpy as np from data_analysis import crime_tree parser = argparse.ArgumentParser() parser.add_argument( '-X', '--Xdata', help='n*p data matrix to be loaded where n is sample size.', type=str) args = parser.parse_args() Tree = RTree(eval(args.Xdata), leafsize=args.leafsize)
def main(): #We first create a RTree where rt0 is the root rt5 = RTree('n5') rt11 = RTree('n11') rt10 = RTree('n10', [rt11]) rt4 = RTree('n4') rt3 = RTree('n3') rt2 = RTree('n2') rt7 = RTree('n7', [rt10]) rt9 = RTree('n9') rt6 = RTree('n6', [rt7]) rt8 = RTree('n8', [rt6]) rt1 = RTree('n1', [rt4, rt5, rt8, rt9]) rt0 = RTree('n0', [rt1, rt2, rt3]) #Display the Depth print("\nDisplay through Depth") rt0.displayDepth() #Display the Width print("\nDisplay through Width") rt0.displayWidth() #Get the childrens print("\nGet the childrens") for children in rt0.getChildrens(): print(children.getContent()) #Get the father print("\nGet the father") print(rt0.getFather(rt1).getContent()) #Get the descendents (childs, grand childs ect) print("\nGet the descendents") #Filter the RTree that are in more than once listToPrint = [] for children in rt0.getDescending(): if (children not in listToPrint): listToPrint.append(children) print(children.getContent()) #Get the ascendents (father, grand father) print("\nGet the Ascendents") for fathers in rt0.getAscending(rt0.getRoot(), rt11): print(fathers.getContent()) #Check if a RTree is a leaf or not print("\nKnow if this is a leaf") print(rt0.isLeaf()) print(rt2.isLeaf()) #Get the degree of a RTree print("\nGet the degree of the RTree") print(rt0.getDegree()) """
def main( ): # runs the files mentioned through command line arguments as mentioned above. Otherwise, exists with an error. In addition, runs and tests RTree against sequential queries. try: filePoints = argv[ 1] # filePoints stores the second argument of [python3 main.py {dataset}.txt {range_query}.txt] i.e. dataset.txt. fileQuery = argv[ 2] # fileQuery stores the third argument of [python3 main.py {dataset}.txt {range_query}.txt] i.e. range_query.txt. except: # in case, if error occurs print 'Incorrect Usage' print('Incorrect Usage.') exit(1) try: file = open(filePoints, "r") # open the "dataset.txt" file rows = file.read().split( '\n') # first splitting the file by next line, then reading it points = list( map(format_data_point, rows[1:int(rows[0]) + 1]) ) # storing the actual values of the data points in "points" list using map() except: # in case, if error occurs print 'filePoints: File Not Found' print('{}: File Not Found.'.format(filename)) exit(1) try: file = open(fileQuery, "r") # open the "range_query.txt" file queries = list( map(format_range_query, file.read().split('\n')) ) # first splitting the file by next line, then reading it and using map() queries = [q for q in queries if q != {}] except: print('{}: File Not Found.' ) # in case, if error occurs print 'fileQuery: File Not Found' exit(1) print('------------------Building RTree------------------\n') rtree = RTree() # making an object of RTree class for point in points: rtree.insert(rtree.root, point) # inserting a point in the RTree from root node start = time( ) # initializing a time variable for getting total time taken through sequential search for query in queries: query_sequential( points, query ) # running the sequential search function 100 times on 100000 data points t1 = time( ) - start # storing the total time taken to run 100 queries sequentially in "t1" variable print('Total time for sequential queries: {}'.format( t1)) # printing the total time taken to run 100 queries sequentially print('Average time for every sequential query: {}\n'.format( t1 / len(queries) )) # printing the average time taken to run a single query sequentially file = open( "./query_results.txt", "w+" ) # opening a new file i.e. "query_results.txt", where the final answers of all the 100 queries will be stored start = time( ) # initializing a time variable for getting total time taken through RTree search for query in queries: file.write( "{}\n".format(rtree.query_rtree(rtree.root, query)) ) # writing the final answers of all the 100 queries in "query_results.txt" t2 = time( ) - start # storing the total time taken to run 100 queries implementing RTree in "t2" variable file.close() # closing the "query_results.txt" file print( 'Total time for R-Tree queries: {}'.format(t2) ) # printing the total time taken to run 100 queries implementing RTree print( 'Average time for every R-Tree query: {}\n'.format(t2 / len(queries)) ) # printing the average time taken to run a single query implementing RTree print( 'R-Tree is {} times faster than sequential query'.format(t1 / t2) ) # making a comparison between the time taken to run 100 queries sequentially and implementing RTree
o2 = 0 o3 = 0 o4 = 0 _o1 = 0 _o2 = 0 _o3 = 0 _o4 = 0 _ratio = 0 for x, y in zip(qx, qy): _dataset = random.sample(dataset, num) w.writelines(str(_dataset)) node = RTree() class_num = {} _class = [] inx = 0 for data in _dataset: if not data[1] in class_num: class_num[data[1]] = inx _class.append(data[1]) inx = inx + 1 n = Node(data[2], data[3], data[2], data[3], 0, class_num[data[1]], data[0]) node.insert(n) #option = list(np.random.choice(list(dic.keys()), k+1, replace=False, p=p))
parser.add_argument('-lat', '--latitude', required=True, default=None) parser.add_argument('-s', '--size', required=False, default=None) parser.add_argument('-t', '--type', required=False, default='box') parser.add_argument('-near', '--object', required=False, default=None) # parse arguments args = parser.parse_args() x = float(args.longitude) y = float(args.latitude) if args.size: size = float(args.size) near = False if args.object: near = True # add points points = parse(args.file) rtree = RTree() for point in points: rtree.insert(point) # search nearest (Additional task) if near: # search for near objects as square if args.type == "box": default_size = 1 # set default serch radius (square) res = rtree.find_rect(x, y, default_size, rtree.head) # find all near objects objects = [x for x in res if x.type == args.object] # get near objects while len( objects ) == 0 and default_size < 15: # increase radius and continue searching
""" Class for non-leaf node. init: node = Node() """ def __init__(self, L=[]): self.children = L self.father = None self.bounding_box = None def update_bounding_box(self): return (1) if __name__ == '__main__': circ_1 = Circle(np.array([1, 2]), 1) rtree = RTree(5) rtree.insert(circ_1) print(rtree.head.elem) leaf_1 = LeafNode([circ_1]) # shape classes in two-dimension import numpy as np class Rectangle: """ Any rectangles, can be trivial(segment of line). @:param: Specify any three vertexes of the rectangle as numpy array @:param: id should be a list of all the entries with names """ def __init__(self, lower_left, upper_right, angle, id=None):
query_lst = [] with open('crime-boxes.csv', newline='') as csvfile: box_reader = list(csv.reader(csvfile, delimiter=',')) dim_name_query = box_reader[0] query_names = [] for i in range(1, len(box_reader)): query_names.append(box_reader[i][0]) new_line = box_reader[i][1:] x1, y1, x2, y2 = list(map(float, new_line)) new_rec = Rectangle(np.array([x1, y1]), np.array([x2, y2]), angle=0, id=None) query_lst.append(new_rec) crime_tree = RTree() with open('crimes.csv', newline='') as csvfile: crime_reader = list(csv.reader(csvfile, delimiter=',')) dim_name_crime = crime_reader[0] for i in range(1, len(crime_reader)): new_line = crime_reader[i] new_rec = Rectangle(np.array([0, 0]), np.array([1, 1]), angle=0, id=None) # crime_tree.insert(new_rec) # analysis of crime data in query list # The numbers of robberies and assaults within that region # The most recent crime of each type within that region
def main(update, showWorld, show3D, latitude = 50.0755381, longitude = 14.4378005, distance = 0.01): if not os.path.exists('flights'): os.makedirs('flights') if not os.path.exists('airports'): os.makedirs('airports') if not os.path.exists('airlines'): os.makedirs('airlines') flightDir = os.listdir('flights') if update or len(flightDir) == 0: airports = fr.get_airports() Update(airports,'airports') airlines = fr.get_airlines() Update(airlines,'airlines') UpdateCurrentFlights() flightDir = os.listdir('flights') flightDir.sort() with open('flights/' + flightDir[-1], 'r') as f: f = [ast.literal_eval(line.rstrip()) for line in f.readlines()] allFlights = [] for item in f: # 'EZY' in item[-3]: allFlights.append(item) sequence_containing_x_vals = [] sequence_containing_y_vals = [] sequence_containing_z_vals = [] lats = [] lons = [] myTree = RTree(8, 3, 'heuristic') for counter, airplane in enumerate(allFlights): latInRadians = airplane[1] * math.pi / 180 lonInRadians = airplane[2] * math.pi / 180 # lats.append(airplane[1]) # lons.append(airplane[2]) x, y, z = ConvertToXYZ(latInRadians, lonInRadians) # sequence_containing_x_vals.append(x) # sequence_containing_y_vals.append(y) # sequence_containing_z_vals.append(z) myTree.Insert(Value([x, y, z], counter)) listOfText = [] airlineShortcut = {} airportShortcut = {} # countries with open('airlines/' + os.listdir('airlines')[0]) as airlinesFile: airlinesList = [ast.literal_eval(line.rstrip()) for line in airlinesFile] for item in airlinesList: airlineShortcut.update({item['ICAO']: item['Name']}) with open('airports/' + os.listdir('airports')[0]) as airlinesFile: airportList = [ast.literal_eval(line.rstrip()) for line in airlinesFile] for item in airportList: airportShortcut.update({item['iata']: item['country']}) x, y, z = ConvertToXYZ(ConvertToRadians(latitude), ConvertToRadians(longitude)) resultL = myTree.SearchCloseKDist(Value([x, y, z], 0), distance) resultLons = [] resultLats = [] badCounter = 0 for item in resultL: try: airplane = allFlights[item.index] # print(airplane) latInRadians = airplane[1] * math.pi / 180 lonInRadians = airplane[2] * math.pi / 180 resultLats.append(airplane[1]) resultLons.append(airplane[2]) shortAirline = airplane[-1] fromShort = airplane[-8] toShort = airplane[-7] airplaneType = airplane[8] temporaryText = airlineShortcut[shortAirline] + ' flight from ' + airportShortcut[fromShort] + ' to ' + \ airportShortcut[toShort] + ' (' + airplaneType + ')' listOfText.append(temporaryText) except Exception as ex: badCounter += 1 print(100 * badCounter / float(len(resultL)), 'percent error (bad data)') if showWorld: world = ShowWorld(lats=resultLats, lons=resultLons, text=listOfText) world.Show() if show3D: world3D = Show3D() for i in range(len(resultLats)): x, y, z = ConvertToXYZ(resultLats[i], resultLons[i]) sequence_containing_x_vals.append(x) sequence_containing_y_vals.append(y) sequence_containing_z_vals.append(z) world3D.TestShow(sequence_containing_x_vals, sequence_containing_y_vals, sequence_containing_z_vals)
import numpy as np from Shapes import Rectangle, Circle, Line, Point, BoundingBox, ShapeUnion from RTree import RTree, Node, LeafNode node_lst = [] for i in range(500): circ = Circle(np.array([i, i]), 1, id={'name': 'node' + str(i)}) node_lst.append(circ) #for item in node_lst: # print(item.id['name']+':') # item.bounding_box.print() import random random.seed(42) r_tree = RTree(M=3) i = 1 for item in node_lst: r_tree.insert(item) head = r_tree.head print('\n') head.bounding_box.print() for c in head.children: c.bounding_box.print() print('\nlayer 1: ', c) if isinstance(c, LeafNode): for k in c.elem: print(k.id['name']) else: for e in c.children: