forked from Kinrre/nestcollector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
266 lines (210 loc) · 7.44 KB
/
run.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
"""
Script to run nestcollector.
"""
import configparser
import logging
import os
import sys
from nestcollector.nest import Nest
from nestcollector.database import Database
from nestcollector.overpass import Overpass
CONFIG_PATH = 'config/config.ini'
CONFIG_EXAMPLE_PATH = 'config/config.ini.example'
CONFIG_AREAS_PATH = 'config/areas.json'
CONFIG_AREAS_EXAMPLE_PATH = 'config/areas.json.example'
class NestCollector:
"""
Class for running the NestCollector.
Attributes:
config (configparser.ConfigParser): The config parser.
overpass (Overpass): The Overpass API instance.
db (NestDatabase): The database instance.
"""
def __init__(self) -> None:
"""
Initializes the NestCollector class.
"""
# Set the logging level
logging.basicConfig(level = logging.INFO)
# Check that the config file exists
if not os.path.isfile(CONFIG_PATH):
logging.error(f'Missing {CONFIG_PATH} file, please copy {CONFIG_EXAMPLE_PATH} and fill it with your database settings!')
sys.exit(1)
# Check that the config areas file exists
if not os.path.isfile(CONFIG_AREAS_PATH):
logging.error(f"Missing {CONFIG_AREAS_PATH} file, please use 'https://fence.mcore-services.be' to create the geofence!")
logging.error(f'Example areas file: {CONFIG_AREAS_EXAMPLE_PATH}')
sys.exit(1)
# Config
self.config = configparser.ConfigParser()
self.config.read(CONFIG_PATH)
# Get the Overpass API instance
self.overpass = Overpass(areas_path=CONFIG_AREAS_PATH)
# Get the database instance
self.db = Database(
host=self.get_db_host(),
port=self.get_db_port(),
name=self.get_db_name(),
user=self.get_db_user(),
password=self.get_db_password(),
use_stats_db=self.get_stats_use_stats_db(),
stats_host=self.get_stats_db_host(),
stats_port=self.get_stats_db_port(),
stats_name=self.get_stats_db_name(),
stats_user=self.get_stats_db_user(),
stats_password=self.get_stats_db_password()
)
def run(self) -> None:
"""
Runs the NestCollector.
"""
# Get the OSM data
osm_data = self.overpass.get_osm_data()
# Get the nests
nest = Nest(
osm_data=osm_data,
area_names=self.overpass.area_names,
default_name=self.get_default_name(),
minimum_m2=self.get_minimum_m2(),
buffer_multipolygons=self.get_buffer_multipolygons()
)
nests = nest.get_nests()
# Save the nests to the database
previous_active_nests = self.db.count_active_nests()
self.db.save_nests(nests)
# Calculate the spawnpoints of the nests
self.db.create_spawnpoints_procedure(self.get_minimum_spawnpoints())
self.db.call_spawnpoints_procedure()
# Filter low coverage nests by mon area
if self.get_stats_use_stats_db():
self.db.create_low_coverage_procedure(self.get_stats_minimum_coverage())
self.db.call_low_coverage_procedure()
# Filter overlapping nests
self.db.create_overlapping_procedure(self.get_maximum_overlap())
self.db.call_overlappping_procedure()
# Count the final active nests
final_active_nests = self.db.count_active_nests()
new_nests = final_active_nests - previous_active_nests
logging.info(f'Final active nests: {final_active_nests} ({new_nests} new nests)')
def get_default_name(self) -> str:
"""
Returns the default name of a nest.
Returns:
str: The default name of a nest.
"""
return self.config['NESTS']['DEFAULT_NAME']
def get_minimum_spawnpoints(self) -> int:
"""
Returns the minimum spawnpoints of a nest.
Returns:
int: The minimum spawnpoints of a nest.
"""
return int(self.config['NESTS']['MINIMUM_SPAWNPOINTS'])
def get_minimum_m2(self) -> float:
"""
Returns the minimum m2 of a nest.
Returns:
float: The minimum m2 of a nest.
"""
return float(self.config['NESTS']['MINIMUM_M2'])
def get_maximum_overlap(self) -> int:
"""
Returns the maximum allowed overlap between nests.
Returns:
int: The maximum allowed overlap between nests.
"""
return int(self.config['NESTS']['MAXIMUM_OVERLAP'])
def get_buffer_multipolygons(self) -> bool:
"""
Returns whether to buffer multipolygons.
Returns:
bool: Whether to buffer multipolygons.
"""
return self.config['NESTS']['BUFFER_MULTIPOLYGONS'].capitalize() == 'True'
def get_db_host(self) -> str:
"""
Returns the IP host of the db.
Returns:
str: The IP host of the db.
"""
return self.config['DB']['HOST']
def get_db_port(self) -> str:
"""
Returns the port of the db.
Returns:
str: The port of the db.
"""
return self.config['DB']['PORT']
def get_db_name(self) -> str:
"""
Returns the database name.
Returns:
str: The database name.
"""
return self.config['DB']['NAME']
def get_db_user(self) -> str:
"""
Returns the database username.
Returns:
str: The database username.
"""
return self.config['DB']['USER']
def get_db_password(self) -> str:
"""
Returns the database username password.
Returns:
str: The database username password.
"""
return self.config['DB']['PASSWORD']
def get_stats_use_stats_db(self) -> bool:
"""
Returns if Stats should be used.
Returns:
bool: If Stats should be used.
"""
return self.config['STATS']['USE_STATS_DB'].capitalize() == 'True'
def get_stats_minimum_coverage(self) -> int:
"""
Returns the minimum coverage of a nest.
Returns:
int: The minimum coverage of a nest.
"""
return int(self.config['STATS']['MINIMUM_COVERAGE'])
def get_stats_db_host(self) -> str:
"""
Returns the IP host of the Stats db.
Returns:
str: The IP host of the Stats db.
"""
return self.config['STATS']['HOST']
def get_stats_db_port(self) -> str:
"""
Returns the port of the Stats db.
Returns:
str: The port of the Stats db.
"""
return self.config['STATS']['PORT']
def get_stats_db_name(self) -> str:
"""
Returns the Stats database name.
Returns:
str: The Stats database name.
"""
return self.config['STATS']['NAME']
def get_stats_db_user(self) -> str:
"""
Returns the Stats database username.
Returns:
str: The Stats database username.
"""
return self.config['STATS']['USER']
def get_stats_db_password(self) -> str:
"""
Returns the Stats database username password.
Returns:
str: The Stats database username password.
"""
return self.config['STATS']['PASSWORD']
if __name__ == '__main__':
nest_collector = NestCollector()
nest_collector.run()