-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindowquerie.py
159 lines (133 loc) · 5.25 KB
/
windowquerie.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import sys
def scan(file,filetype):
flag = 0
if(filetype == "dir"):
while True:
if(flag==1):
field = next(file).split(" ")
field = [f.strip() for f in field]
field = [int(f) for f in field]
yield field
else:
field = next(file).split(" ")
field = [f.strip() for f in field]
field = [float(f) for f in field]
flag = 1
yield field
elif(filetype == "grd"):
while True:
field = next(file).split(" ")
field = [f.strip() for f in field]
field = [float(f) for f in field]
field[0] = int(field[0])
yield field
def file_len(fname):
with open(fname) as f:
for i, l in enumerate(f):
pass
return i + 1
def getCellPos(x,y,limitsarray):
dx = (limitsarray[1] - limitsarray[0]) / 10
dy = (limitsarray[3] - limitsarray[2]) / 10
xposition = (x - limitsarray[0]) / dx
yposition = (y - limitsarray[2]) / dy
cellX =int( xposition )
cellY =int( yposition )
return [cellX,cellY,xposition,yposition]
def writeToFile(my_list):
with open('windowquerie.txt', 'w') as f:
for item in my_list:
f.write("%s\n" % item)
def windowQ(x_low,x_high,y_low,y_high,limitsarray,cellmatrix,pointsmatrix):
queryanswer = []
if(x_low>x_high):
print "Error x_low>x_high"
exit(0)
elif(y_low>y_high):
print "Error y_low>y_high"
exit(0)
firstcell = getCellPos(x_low,y_low,limitsarray)
secondcell = getCellPos(x_high,y_high,limitsarray)
cellsminX = firstcell[2]
cellsminY = firstcell[3]
cellsmaxX = secondcell[2]
cellsmaxY = secondcell[3]
intersectingcellsarray = []
for i in range(firstcell[0],secondcell[0]+1):
for j in range(firstcell[1],secondcell[1]+1):
if(i<10 and j<10 and i>=0 and j>=0):
intersectingcellsarray.append([i,j])
for cell in intersectingcellsarray:
if(cell[0]>=cellsminX and cell[0]+1<=cellsmaxX and cell[1]>=cellsminY and cell[1]+1<=cellsmaxY):
cell.append("full")
else:
cell.append("check")
numberofpointsadded = 0
for cell in intersectingcellsarray:
for cellmatrixcell in cellmatrix:
if(cellmatrixcell[0]==cell[0] and cellmatrixcell[1]==cell[1]):
if(cell[2]=="full"):
for i in range(cellmatrixcell[3]):
queryanswer.append(pointsmatrix[cellmatrixcell[2]+i])
numberofpointsadded+=1
elif(cell[2]=="check"):
for i in range(cellmatrixcell[3]):
point = pointsmatrix[cellmatrixcell[2]+i]
pointposition = getCellPos(point[1],point[2],limitsarray)
pointx = pointposition[2]
pointy = pointposition[3]
if(pointx>=cellsminX and pointx<=cellsmaxX and pointy>=cellsminY and pointy<=cellsmaxY):
queryanswer.append(point)
numberofpointsadded+=1
writeToFile(queryanswer)
#AESTHETIC PRINTS START
print "--------------------------------"
for point in queryanswer:
print point
print "--------------------------------"
print "--------------------------------------"
for cell in intersectingcellsarray:
if(cell[2]=="full"):
print "Completely covered by window : " +"["+ cell[0].__str__()+","+cell[1].__str__() +"]"
elif(cell[2]=="check"):
print "Partially covered by window : "+"["+ cell[0].__str__()+","+cell[1].__str__() +"]"
print "--------------------------------------"
print "-----------------------------"
print "WindowX_low : "+cellsminX.__str__()+"\nWindowX_high : "+cellsmaxX.__str__()+"\nWindowY_low : "+cellsminY.__str__()+"\nWindowY_max : "+cellsmaxY.__str__()
print "-----------------------------"+"\n"+"-----------------------------"
print " Points in Window : " + numberofpointsadded.__str__()
print "-----------------------------"
#END OF PRINTS
def initMatrices():
griddir = open("grid.dir","r")
gridgrd = open("grid.grd","r")
cellparser = scan(griddir,"dir")
pointparser = scan(gridgrd,"grd")
cellmatrix = []
pointsmatrix = []
numberofcells = file_len("grid.dir")
numberofpoints = file_len("grid.grd")
numberofrestaurants = numberofpoints
limitsarray = next(cellparser)
for i in range(numberofcells-1):
cell = next(cellparser)
cellmatrix.append(cell)
for i in range(numberofpoints):
point = next(pointparser)
pointsmatrix.append(point)
griddir.close()
gridgrd.close()
return [cellmatrix,pointsmatrix,limitsarray]
def main(argv1,argv2,argv3,argv4):
argv1 = float(argv1)
argv2 = float(argv2)
argv3 = float(argv3)
argv4 = float(argv4)
matrices = initMatrices()
cellmatrix = matrices[0]
pointsmatrix = matrices[1]
limitsarray = matrices[2]
matrices = []
windowQ(argv1,argv2,argv3,argv4,limitsarray,cellmatrix,pointsmatrix)
if __name__ == "__main__":
main(sys.argv[1],sys.argv[2],sys.argv[3],sys.argv[4])