-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
148 lines (134 loc) · 5.49 KB
/
utils.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
import os
import pymongo
import logging
from logging.handlers import TimedRotatingFileHandler
from shapely.geometry import Polygon
from config import config
def setup_logging():
log_file = config['log_file']
log_folder = os.path.split(log_file)[0]
if not os.path.exists(log_folder):
os.makedirs(log_folder)
file_handler = TimedRotatingFileHandler(log_file, when='D', interval=30)
# Set the log level and formatter
file_handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
file_handler.setFormatter(formatter)
# Add the file handler to the root logger
logging.getLogger().setLevel(logging.INFO)
logging.getLogger().addHandler(file_handler)
def connectDb():
try:
database_details = config['database_details']
client = pymongo.MongoClient(database_details['host'],
username=database_details['username'],
password=database_details['password'],
authSource=database_details['auth_source'],
authMechanism=database_details['auth_mechanism'])
collection = client[database_details['database']][database_details[
'collection']]
logging.info({
'service': 'database connection',
'message': 'connection started'
})
except Exception as e:
logging.error({
'service': 'database connection',
'message': e
})
return None, None
return client, collection
def check_intersection(source, spatialQuery):
logging.info({
'service': 'check-intersection',
'message': 'spatial query coordinates received'
})
p1 = Polygon(source)
for query in spatialQuery:
p2 = Polygon(query)
if p2.intersects(p1):
return True
return False
def modifyGridLayout(grid, rows, cols, start_point, deadheaded=True):
logging.info({
'grid_id': 'feature not added',
'service': 'modify grid',
'message': 'modifying plot grid layout according to walk pattern'
})
modified_layout = []
count = 0
if start_point == 'tl' and deadheaded:
return grid
elif start_point == 'tl' and not deadheaded:
for i in range(rows):
if i % 2 == 0:
modified_layout.extend(grid[count:count + cols])
count += cols
else:
current_row = grid[count:count + cols]
plot_numbers = reversed([x['plot_num'] for x in current_row])
for grid, number in zip(current_row, plot_numbers):
grid['plot_num'] = number
grid['plot_name'] = f'Plot {number}'
modified_layout.extend(current_row)
count += cols
elif start_point == 'tr' and not deadheaded:
for i in range(rows):
if i % 2 != 0:
modified_layout.extend(grid[count:count + cols])
count += cols
else:
current_row = grid[count:count + cols]
plot_numbers = reversed([x['plot_num'] for x in current_row])
for plot, number in zip(current_row, plot_numbers):
plot['plot_num'] = number
plot['plot_name'] = f'Plot {number}'
modified_layout.extend(current_row)
count += cols
elif start_point == 'tr' and deadheaded:
for i in range(rows):
current_row = grid[count: (count + cols)]
plot_numbers = reversed([x['plot_num'] for x in current_row])
for plot, number in zip(current_row, plot_numbers):
plot['plot_num'] = number
plot['plot_name'] = f'Plot {number}'
modified_layout.extend(current_row)
count += cols
elif start_point == 'bl' and deadheaded:
for i in reversed(range(rows)):
current_row = grid[i * cols: (i + 1) * cols]
for plot in current_row:
plot['plot_num'] = count + 1
plot['plot_name'] = f'Plot {count + 1}'
count += 1
modified_layout.extend(current_row)
elif start_point == 'bl' and not deadheaded:
for i in reversed(range(rows)):
current_row = grid[i * cols: (i + 1) * cols]
if (rows - i) % 2 == 0:
current_row = current_row[::-1]
for plot in current_row:
plot['plot_num'] = count + 1
plot['plot_name'] = f'Plot {count + 1}'
count += 1
modified_layout.extend(current_row)
elif start_point == 'br' and deadheaded:
for i in reversed(range(rows)):
current_row = grid[i * cols: (i + 1) * cols]
current_row = current_row[::-1]
for plot in current_row:
plot['plot_num'] = count + 1
plot['plot_name'] = f'Plot {count + 1}'
count += 1
modified_layout.extend(current_row)
elif start_point == 'br' and not deadheaded:
for i in reversed(range(rows)):
current_row = grid[i * cols: (i + 1) * cols]
if (rows - i) % 2 != 0:
current_row = current_row[::-1]
for plot in current_row:
plot['plot_num'] = count + 1
plot['plot_name'] = f'Plot {count + 1}'
count += 1
modified_layout.extend(current_row)
return modified_layout