-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.py
471 lines (360 loc) · 17.8 KB
/
utilities.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
import random
import os
import re
import string
import uuid
import datetime
import subprocess
import json
import aiohttp
import pandas as pd
from fastapi import FastAPI
import config
from routers import imports
def get_new_table_id() -> str:
"""
Method to return a new table id
"""
letters = string.ascii_lowercase
return ''.join(random.choice(letters) for i in range(50))
def get_new_process_id() -> str:
"""
Method to return a new process id
"""
return str(uuid.uuid4())
def remove_bad_characters(string: str) -> str:
regex = re.compile('[^a-zA-Z0-9_]')
return regex.sub('_', string).lower()
async def upload_csv_to_db_with_latitude_and_longitude(file_path: str, new_table_id: str, database: str,
latitude: str, longitude: str, table_columns: list, app: FastAPI):
"""
Method to upload data from from a csv file with latitude and longitude columns into db.
"""
pd.options.display.max_rows = 10
df = pd.read_csv(file_path)
columns = ""
formatted_table_columns = ""
for col in table_columns:
formatted_table_columns += f"{remove_bad_characters(col)},"
formatted_table_columns = formatted_table_columns[:-1]
create_table_sql = f"CREATE TABLE {new_table_id} ("
for name, dtype in df.dtypes.iteritems():
columns += f"{remove_bad_characters(name)},"
create_table_sql += f'"{remove_bad_characters(name)}"'
if dtype == "object" or dtype == "datetime64":
create_table_sql += " text,"
if dtype == "int64":
create_table_sql += " integer,"
if dtype == "float64":
create_table_sql += " double precision,"
create_table_sql = create_table_sql[:-1]
columns = columns[:-1]
create_table_sql += ");"
pool = app.state.databases[f'{database}_pool']
async with pool.acquire() as con:
await con.fetch(f"""DROP TABLE IF EXISTS "{new_table_id}";""")
await con.fetch(create_table_sql)
insert_sql = f"""COPY {new_table_id}({columns})
FROM '{file_path}'
DELIMITER ','
CSV HEADER;"""
await con.fetch(insert_sql)
add_geom_sql = f"""
SELECT AddGeometryColumn ('public','{new_table_id}','geom',4326,'POINT',2);
"""
await con.fetch(add_geom_sql)
update_geom_sql = f"""
UPDATE "{new_table_id}"
SET geom = ST_SetSRID(ST_MakePoint({longitude},{latitude}), 4326);
"""
await con.fetch(update_geom_sql)
delete_bad_geom_sql = f"""
DELETE FROM "{new_table_id}" WHERE geom IS NULL;
"""
await con.fetch(delete_bad_geom_sql)
async def upload_csv_to_db_with_geographic_data(file_path: str, new_table_id: str, database: str,
map: str, map_column: str, table_column: str, table_columns: list, map_columns: list, app: FastAPI):
"""
Method to upload data from from a csv file with geographic data into db.
"""
pd.options.display.max_rows = 10
df = pd.read_csv(file_path)
columns = ""
formatted_table_columns = ""
formatted_map_columns = ""
for col in table_columns:
if col not in map_columns:
formatted_table_columns += f"a.{remove_bad_characters(col)},"
for column in map_columns:
formatted_map_columns += f"b.{remove_bad_characters(column)},"
create_table_sql = f"CREATE TABLE {new_table_id}_temp ("
for name, dtype in df.dtypes.iteritems():
columns += f"{remove_bad_characters(name)},"
create_table_sql += f'"{remove_bad_characters(name)}"'
if dtype == "object" or dtype == "datetime64":
create_table_sql += " text,"
if dtype == "int64":
create_table_sql += " integer,"
if dtype == "float64":
create_table_sql += " double precision,"
create_table_sql = create_table_sql[:-1]
columns = columns[:-1]
create_table_sql += ");"
pool = app.state.databases[f'{database}_pool']
async with pool.acquire() as con:
await con.fetch(f"""DROP TABLE IF EXISTS "{new_table_id}_temp";""")
await con.fetch(create_table_sql)
insert_sql = f"""COPY {new_table_id}_temp({columns})
FROM '{file_path}'
DELIMITER ','
CSV HEADER;"""
await con.fetch(insert_sql)
join_sql = f"""CREATE TABLE "{new_table_id}" AS
SELECT {formatted_table_columns} {formatted_map_columns} geom
FROM "{new_table_id}_temp" as a
LEFT JOIN "{map}" as b
ON a."{table_column}" = b."{map_column}";
"""
await con.fetch(join_sql)
await con.fetch(f"""DROP TABLE IF EXISTS "{new_table_id}_temp";""")
async def get_arcgis_data(url: str, new_table_id: str, process_id: str, database: str, token: str=None):
"""
Method get arcgis data from a given url and load it into a database.
"""
start = datetime.datetime.now()
try:
service_url = f"{url}?f=json"
if token is not None:
service_url += f"&token={token}"
async with aiohttp.ClientSession() as session:
async with session.get(service_url) as resp:
data = await resp.json()
max_number_of_features_per_query = data['maxRecordCount']
feature_stats_url = f"{url}/query?where=1%3D1&returnGeometry=false&returnIdsOnly=true&f=json&resultRecordCount=10"
async with session.get(feature_stats_url) as feature_resp:
data = await feature_resp.text()
data = json.loads(data)
object_ids = data['objectIds']
number_of_features = len(data['objectIds'])
if number_of_features <= max_number_of_features_per_query:
async with session.get(f"{url}/query?where=1=1&outFields=*&returnGeometry=true&geometryPrecision=6&outSR=4326&f=geojson") as resp:
data = await resp.json()
with open(f'{new_table_id}.geojson', 'w') as json_file:
json.dump(data, json_file)
load_geographic_data_to_server(
table_id=new_table_id,
file_path=f'{new_table_id}.geojson',
database=database
)
else:
start_counter = 0
feature_collection = {
"type": "FeatureCollection",
"features": []
}
for x in range( start_counter, number_of_features, max_number_of_features_per_query ):
ids_requested = object_ids[x: x + max_number_of_features_per_query ]
payload = { 'f': 'geojson', 'where': '1=1',
'objectIds': str( ids_requested )[1:-1], 'outSR': '4326',
'returnGeometry': 'true', 'outFields': '*',
'geometryPrecision': '4'}
async with session.post( f"{url}/query", data=payload ) as resp:
data = await resp.text()
data = json.loads(data)
if 'error' in data:
print(data['error'])
feature_collection['features'] += data['features']
with open(f'{new_table_id}.geojson', 'w') as json_file:
json.dump(feature_collection, json_file)
load_geographic_data_to_server(
table_id=new_table_id,
file_path=f'{new_table_id}.geojson',
database=database
)
os.remove(f'{new_table_id}.geojson')
imports.import_processes[process_id]['status'] = "SUCCESS"
imports.import_processes[process_id]['new_table_id'] = new_table_id
imports.import_processes[process_id]['completion_time'] = datetime.datetime.now()
imports.import_processes[process_id]['run_time_in_seconds'] = datetime.datetime.now()-start
except Exception as error:
if os.path.exists(f'{new_table_id}.geojson'):
os.remove(f'{new_table_id}.geojson')
imports.import_processes[process_id]['status'] = "FAILURE"
imports.import_processes[process_id]['error'] = str(error)
imports.import_processes[process_id]['completion_time'] = datetime.datetime.now()
imports.import_processes[process_id]['run_time_in_seconds'] = datetime.datetime.now()-start
async def upload_geographic_file(file_path: str, new_table_id: str, process_id: str, database: str):
"""
Method to upload data from geographic file.
"""
start = datetime.datetime.now()
try:
load_geographic_data_to_server(
table_id=new_table_id,
file_path=file_path,
database=database
)
media_directory = os.listdir(f"{os.getcwd()}/media/")
for file in media_directory:
if new_table_id in file:
os.remove(f"{os.getcwd()}/media/{file}")
imports.import_processes[process_id]['status'] = "SUCCESS"
imports.import_processes[process_id]['new_table_id'] = new_table_id
imports.import_processes[process_id]['completion_time'] = datetime.datetime.now()
imports.import_processes[process_id]['run_time_in_seconds'] = datetime.datetime.now()-start
except Exception as error:
media_directory = os.listdir(f"{os.getcwd()}/media/")
for file in media_directory:
if new_table_id in file:
os.remove(f"{os.getcwd()}/media/{file}")
imports.import_processes[process_id]['status'] = "FAILURE"
imports.import_processes[process_id]['error'] = str(error)
imports.import_processes[process_id]['completion_time'] = datetime.datetime.now()
imports.import_processes[process_id]['run_time_in_seconds'] = datetime.datetime.now()-start
async def import_geographic_data_from_csv(file_path: str, new_table_id: str, process_id: str, database: str,
map: str, map_column: str, table_column: str, table_columns: list, map_columns: list, app: FastAPI):
"""
Method to upload data from from a csv file with geographic data.
"""
start = datetime.datetime.now()
try:
await upload_csv_to_db_with_geographic_data(
file_path=file_path,
new_table_id=new_table_id,
database=database,
map=map,
map_column=map_column,
table_column=table_column,
table_columns=table_columns,
map_columns=map_columns,
app=app
)
media_directory = os.listdir(f"{os.getcwd()}/media/")
for file in media_directory:
if new_table_id in file:
os.remove(f"{os.getcwd()}/media/{file}")
imports.import_processes[process_id]['status'] = "SUCCESS"
imports.import_processes[process_id]['new_table_id'] = new_table_id
imports.import_processes[process_id]['completion_time'] = datetime.datetime.now()
imports.import_processes[process_id]['run_time_in_seconds'] = datetime.datetime.now()-start
except Exception as error:
media_directory = os.listdir(f"{os.getcwd()}/media/")
for file in media_directory:
if new_table_id in file:
os.remove(f"{os.getcwd()}/media/{file}")
imports.import_processes[process_id]['status'] = "FAILURE"
imports.import_processes[process_id]['error'] = str(error)
imports.import_processes[process_id]['completion_time'] = datetime.datetime.now()
imports.import_processes[process_id]['run_time_in_seconds'] = datetime.datetime.now()-start
async def import_point_data_from_csv(file_path: str, new_table_id: str, process_id: str, database: str,
latitude: str, longitude: str, table_columns: list, app: FastAPI):
"""
Method to upload data from csv with lat lng columns.
"""
start = datetime.datetime.now()
try:
await upload_csv_to_db_with_latitude_and_longitude(
file_path=file_path,
new_table_id=new_table_id,
database=database,
latitude=latitude,
longitude=longitude,
table_columns=table_columns,
app=app
)
media_directory = os.listdir(f"{os.getcwd()}/media/")
for file in media_directory:
if new_table_id in file:
os.remove(f"{os.getcwd()}/media/{file}")
imports.import_processes[process_id]['status'] = "SUCCESS"
imports.import_processes[process_id]['new_table_id'] = new_table_id
imports.import_processes[process_id]['completion_time'] = datetime.datetime.now()
imports.import_processes[process_id]['run_time_in_seconds'] = datetime.datetime.now()-start
except Exception as error:
media_directory = os.listdir(f"{os.getcwd()}/media/")
for file in media_directory:
if new_table_id in file:
os.remove(f"{os.getcwd()}/media/{file}")
imports.import_processes[process_id]['status'] = "FAILURE"
imports.import_processes[process_id]['error'] = str(error)
imports.import_processes[process_id]['completion_time'] = datetime.datetime.now()
imports.import_processes[process_id]['run_time_in_seconds'] = datetime.datetime.now()-start
async def import_point_data_from_json_file(file_path: str, new_table_id: str, process_id: str, database: str,
latitude: str, longitude: str, table_columns: list, app: FastAPI):
"""
Method to upload data from csv with lat lng columns.
"""
start = datetime.datetime.now()
try:
df = pd.read_json(file_path)
df.to_csv(f"{os.getcwd()}/media/{new_table_id}.csv", index=False, sep=',', encoding="utf-8")
await upload_csv_to_db_with_latitude_and_longitude(
file_path=f"{os.getcwd()}/media/{new_table_id}.csv",
new_table_id=new_table_id,
database=database,
latitude=latitude,
longitude=longitude,
table_columns=table_columns,
app=app
)
media_directory = os.listdir(f"{os.getcwd()}/media/")
for file in media_directory:
if new_table_id in file:
os.remove(f"{os.getcwd()}/media/{file}")
imports.import_processes[process_id]['status'] = "SUCCESS"
imports.import_processes[process_id]['new_table_id'] = new_table_id
imports.import_processes[process_id]['completion_time'] = datetime.datetime.now()
imports.import_processes[process_id]['run_time_in_seconds'] = datetime.datetime.now()-start
except Exception as error:
media_directory = os.listdir(f"{os.getcwd()}/media/")
for file in media_directory:
if new_table_id in file:
os.remove(f"{os.getcwd()}/media/{file}")
imports.import_processes[process_id]['status'] = "FAILURE"
imports.import_processes[process_id]['error'] = str(error)
imports.import_processes[process_id]['completion_time'] = datetime.datetime.now()
imports.import_processes[process_id]['run_time_in_seconds'] = datetime.datetime.now()-start
async def import_geographic_data_from_json_file(file_path: str, new_table_id: str, process_id: str, database: str,
map: str, map_column: str, table_column: str, table_columns: list, map_columns: list, app: FastAPI):
"""
Method to upload data from from a json file with geographic data.
"""
start = datetime.datetime.now()
try:
df = pd.read_json(file_path)
df.to_csv(f"{os.getcwd()}/media/{new_table_id}.csv", index=False, sep=',', encoding="utf-8")
await upload_csv_to_db_with_geographic_data(
file_path=f"{os.getcwd()}/media/{new_table_id}.csv",
new_table_id=new_table_id,
database=database,
map=map,
map_column=map_column,
table_column=table_column,
table_columns=table_columns,
map_columns=map_columns,
app=app
)
media_directory = os.listdir(f"{os.getcwd()}/media/")
for file in media_directory:
if new_table_id in file:
os.remove(f"{os.getcwd()}/media/{file}")
imports.import_processes[process_id]['status'] = "SUCCESS"
imports.import_processes[process_id]['new_table_id'] = new_table_id
imports.import_processes[process_id]['completion_time'] = datetime.datetime.now()
imports.import_processes[process_id]['run_time_in_seconds'] = datetime.datetime.now()-start
except Exception as error:
media_directory = os.listdir(f"{os.getcwd()}/media/")
for file in media_directory:
if new_table_id in file:
os.remove(f"{os.getcwd()}/media/{file}")
imports.import_processes[process_id]['status'] = "FAILURE"
imports.import_processes[process_id]['error'] = str(error)
imports.import_processes[process_id]['completion_time'] = datetime.datetime.now()
imports.import_processes[process_id]['run_time_in_seconds'] = datetime.datetime.now()-start
def load_geographic_data_to_server(table_id: str, file_path: str, database: object):
db = config.DATABASES[database]
host = db['host']
username = db['username']
password = db['password']
database = db['database']
subprocess.call(f'ogr2ogr -f "PostgreSQL" "PG:host={host} user={username} dbname={database} password={password}" "{file_path}" -lco GEOMETRY_NAME=geom -lco FID=gid -lco PRECISION=no -nln {table_id} -overwrite', shell=True)