-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongo_script.py
40 lines (32 loc) · 1.11 KB
/
mongo_script.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
'''
Created on 17 giu 2021
@author: lorenzo
'''
from pymongo import MongoClient
import pprint, pandas
#ADDRESS="localhost"
#PORT="27017"
ADDRESS="infra.licit.local"
PORT="31184"
DB="promenade"
COLLECTION="areas"
INPUT_FILE="areas_full_with_nodes.csv"
DELIMITER=","
'''*** Reading input file***'''
df = pandas.read_csv(INPUT_FILE, delimiter=DELIMITER, dtype=str)
df
'''*** Connecting to mongo instance***'''
client = MongoClient("mongodb://"+ ADDRESS + ":" + PORT + "/")
db = client[DB]
areas = db[COLLECTION]
'''*** Adding new documents***'''
for i in range(0, len(df.index)):
print(f'Area {i}/{len(df.index)}')
line_area = df['GEOMETRY'][i][10:-2]
coordinates_list = []
line_area_splitted = line_area.split(',')
for pair in line_area_splitted:
coordinates = pair.strip().split(' ')
coordinates_list.append([float(coordinates[0]),float(coordinates[1])])
area = {"insee_com": int(df['INSEE_COM'][i]), "nom_com": df['NOM_COM'][i], "polygon": { "type": "Polygon", "coordinates":[coordinates_list]}, "nodes": int(df['nodes'][i])}
areas.insert_one(area)