This repository has been archived by the owner on Jun 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathinp2sql
executable file
·107 lines (85 loc) · 3 KB
/
inp2sql
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
#!/usr/bin/python
# converts epanet file into tables
import sys
import codecs
inp = sys.stdin;
if len(sys.argv) > 1:
inp = codecs.open(sys.argv[1],'r', encoding='latin-1' )
out = sys.stdout;
if len(sys.argv) > 2:
out = codecs.open(sys.argv[2],'w', encoding='utf-8')
point_tables = [
'[JUNCTIONS]',
'[RESERVOIRS]',
'[TANKS]',
'[PUMPS]',
'[VALVES]',
'[EMITTERS]',
'[SOURCES]'
]
line_tables = [
'[PIPES]'
]
table_sections = point_tables+line_tables+[
'[DEMANDS]',
'[STATUS]',
'[PATTERNS]',
'[CURVES]',
'[CONTROLS]',
'[QUALITY]',
'[REACTIONS]',
'[MIXING]',
'[COORDINATES]',
'[VERTICES]',
'[LABELS]'
]
line = inp.readline()
while line:
if line.rstrip() in table_sections:
table_name = line.rstrip()[1:-1]
line = inp.readline()
if line[0] == ';': # this is a commend for column def
column_names = line[1:].split('\t')
for i,c in enumerate(column_names): column_names[i] = c.rstrip()
values = []
line = ';'
while line and line[0] != '[':
line = inp.readline()
line = line.rstrip()
if not line: break
pos = line.find(';')
if pos == 0: continue
if pos >0: line = line[:pos] ;
values.append(line.split('\t'))
# detect the column type: varchar by default, float if all values can be float
typ = []
for i,c in enumerate(column_names):
typ.append('float')
if c[:2] == 'ID':
typ[i] = 'varchar'
continue
for v in values:
try:
float(v[i])
except:
typ[i] = 'varchar'
break
# now create the table
decl = ""
for i,c in enumerate(column_names): decl+='"'+c+'" '+typ[i]+', '
decl = decl[:-2]
out.write('CREATE TABLE "'+table_name+'" ('+decl+');\n')
for r in values:
vals = ""
for i in range(len(typ)):
if i >= len(r): break
if typ[i] == 'varchar': vals += "'"+r[i].strip().rstrip()+"'"+", "
else : vals += str(r[i])+", "
out.write('INSERT INTO "'+table_name+'" VALUES ('+vals[:-2]+');\n')
if '['+table_name+']' in point_tables:
out.write('ALTER TABLE "'+table_name+'" ADD COLUMN fid serial PRIMARY KEY;\n')
out.write('ALTER TABLE "'+table_name+'" ADD COLUMN geom geometry(''POINT'',2154);\n')
if '['+table_name+']' in line_tables:
out.write('ALTER TABLE "'+table_name+'" ADD COLUMN fid serial PRIMARY KEY;\n')
out.write('ALTER TABLE "'+table_name+'" ADD COLUMN geom geometry(''LINESTRING'',2154);\n')
line = inp.readline()