-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruleparser.py
More file actions
executable file
·65 lines (48 loc) · 1.94 KB
/
ruleparser.py
File metadata and controls
executable file
·65 lines (48 loc) · 1.94 KB
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
#!/usr/bin/python
import codecs
import sys
import os.path
sys.dont_write_bytecode=True
sys.path.append('./src')
from csv_to_array import csv_to_array, print_grid
from metadata import parse_md
from inputs_to_code import inputs_to_code
from outputs_to_code import outputs_to_code
from rp_settings import output_sql_filename, output_calc_filename
if (len(sys.argv) < 3):
print
print "Ruleparser: please provide a CSV spreadsheet and database table name."
print "For information about the format, please see README.md"
print "./ruleparser.py input.csv my_postgres_table"
print
sys.exit()
spreadsheet = sys.argv[1]
layer = sys.argv[2]
if not os.path.isfile(spreadsheet):
print
print "Error. The spreadsheet file does not exist: "+spreadsheet
print
sys.exit()
print "\nParsing spreadsheet: ", spreadsheet, "\n"
# 0. Load csv to grid
grid_tuple=csv_to_array(spreadsheet)
# 1. parse grid for metadata (top 3 lines)
metadata_bash_string=parse_md(*grid_tuple)
# 2. parse grid for inputs. ( filter for in_, generate to row number and raster calc)
formula_string=inputs_to_code(*grid_tuple)
# put metadata and formula into 'input' rule formula file.
print "------------ CUT HERE: "+output_calc_filename+" ----------------\n"
print metadata_bash_string, "\n"
print formula_string, "\n\n"
with codecs.open(output_calc_filename, 'w', 'utf-8') as f:
f.write(metadata_bash_string + "\n")
f.write(formula_string+"\n")
# 3. parse grid for outputs.
# write a second option for raster outputs later. for now, geom.
# filter for out_ columns. then simply substitute all values in the row, with quoting
# finally output a list of output values found so user can check.
sql_transform=outputs_to_code(*grid_tuple, input_filename=spreadsheet, layer_name=layer)
print "------------ CUT HERE: "+output_sql_filename+" ----------------\n"
print sql_transform, "\n\n"
with codecs.open(output_sql_filename, 'w', 'utf-8') as f:
f.write(sql_transform + "\n")