Move mbr implementation to seperate file

This commit is contained in:
stefiosif
2022-04-14 17:58:28 +03:00
parent 2e2f7b02b6
commit 04319456dd
5 changed files with 119 additions and 161 deletions

View File

@@ -1,28 +1,9 @@
import sys
import ast
from mbr import MBR, Node
class MBR:
def __init__(self, id, xlow, xhigh, ylow, yhigh):
self.xlow = xlow
self.xhigh = xhigh
self.ylow = ylow
self.yhigh = yhigh
self.id = id
def intersects(self, other):
if self.xlow > other.xhigh or self.xhigh < other.xlow:
return False
if self.ylow > other.yhigh or self.yhigh < other.ylow:
return False
return True
class Node:
def __init__(self, leaf, id, list_of_mbrs):
self.leaf = leaf
self.id = id
self.list_of_mbrs = list_of_mbrs
def parseRtree(filename):
# Read R-Tree from data
def parse_tree(filename):
with open(filename, 'r') as f:
nodes = []
for line in f:
@@ -35,14 +16,14 @@ def parseRtree(filename):
return nodes
# Read and execute all the range queries assigned to the r tree
def parseQuery(rtree, filename):
# Read and execute all the range queries
def parse_query(rtree, filename):
with open(filename, 'r') as f:
i = 0
for line in f:
[xlow, ylow, xhigh, yhigh] = line.split()
results = []
rangeQuery(rtree, MBR(int(i), float(xlow), float(xhigh),
range_query(rtree, MBR(int(i), float(xlow), float(xhigh),
float(ylow), float(yhigh)), results)
print("{} ({}):".format(i, len(results)), end=' ')
@@ -55,18 +36,18 @@ def parseQuery(rtree, filename):
i += 1
# Find the intersecting rectangles
def rangeQuery(rtree, window, results, node=None):
def range_query(rtree, window, results, node=None):
# At the root node
if node == None:
node = rtree[-1]
for n in node.list_of_mbrs:
if window.intersects(n) or n.intersects(window):
rangeQuery(rtree, window, results, rtree[n.id])
range_query(rtree, window, results, rtree[n.id])
# At an intermediate node
elif node.leaf == 1:
for n in node.list_of_mbrs:
if window.intersects(n) or n.intersects(window):
rangeQuery(rtree, window, results, rtree[n.id])
range_query(rtree, window, results, rtree[n.id])
# At a leaf node
else:
for mbr in node.list_of_mbrs:
@@ -74,5 +55,5 @@ def rangeQuery(rtree, window, results, node=None):
results.append(mbr)
if __name__ == '__main__':
rtree = parseRtree(sys.argv[1])
parseQuery(rtree, sys.argv[2])
rtree = parse_tree(sys.argv[1])
parse_query(rtree, sys.argv[2])