Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
295 lines (279 loc) · 7.75 KB
/
Copy pathmain.go
File metadata and controls
295 lines (279 loc) · 7.75 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
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
package main
import (
"database/sql"
"errors"
"fmt"
"io/ioutil"
"os"
cfg "shapeRuler/config"
"shapeRuler/rules"
"shapeRuler/shapeUtils"
"strconv"
"strings"
"time"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
"github.com/urfave/cli"
)
func writelog(filename string, lines []string) error {
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
for _, s := range lines {
f.WriteString(s + "\n")
}
return nil
}
func process(baseDir string, conf *cfg.ConfJson, db *sql.DB, logs *[]string, errors *[]string) {
parcelFile := baseDir + conf.ParcelShape.Name
parcellData, err := shapeUtils.ReadInputParcel(parcelFile, conf)
noMatchingIds := 0
if err != nil {
*errors = append(*errors, parcelFile+" : Error reading shapefile "+err.Error())
return
}
log := shapeUtils.DetectSimilarKeys(parcellData)
if log != "" {
*logs = append(*logs, "--Multiple same idpar in "+parcelFile+" : "+log)
}
log = ""
for k, _ := range parcellData {
key, err := rules.PgGetRulesFor(&parcellData[k], conf, db /*, baseDir*/)
if err != nil {
*errors = append(*errors, parcelFile+" : Error getting rules "+err.Error())
}
if key != "" {
log += key + " "
noMatchingIds++
//fmt.Println(log)
//*logs = append(*logs, log)
}
}
//list of keys not found
if log != "" {
norows := ""
if noMatchingIds == len(parcellData) {
norows = "!Empty! "
}
log = norows + baseDir + " --> missing rules (" +
strconv.Itoa(noMatchingIds) + "/" + strconv.Itoa(len(parcellData)) + ") for : " + log
*logs = append(*logs, log)
}
err = shapeUtils.WriteShape(baseDir, conf, parcellData)
if err != nil {
*errors = append(*errors, parcelFile+" : Error writing shapefile "+err.Error())
return
}
}
func processCsv(baseDir string, conf *cfg.ConfJson, logs *[]string, errors *[]string) {
parcelFile := baseDir + conf.ParcelShape.Name
parcellData, err := shapeUtils.ReadInputParcel(parcelFile, conf)
noMatchingIds := 0
if err != nil {
*errors = append(*errors, parcelFile+" : Error reading shapefile "+err.Error())
return
}
log := shapeUtils.DetectSimilarKeys(parcellData)
if log != "" {
*logs = append(*logs, "--Multiple same idpar in "+parcelFile+" : "+log)
}
log = ""
for k, _ := range parcellData {
csvFile, err := os.Open(conf.Rules.Access)
if err != nil {
*errors = append(*errors, parcelFile+" : Error Opening csvfile "+err.Error())
return
}
defer csvFile.Close()
key, err := rules.CsvGetRulesFor(&parcellData[k], conf, csvFile /*, baseDir*/)
if err != nil {
*errors = append(*errors, parcelFile+" : Error getting rules "+err.Error())
}
if key != "" {
log += key + " "
noMatchingIds++
}
}
//list of keys not found
if log != "" {
norows := ""
if noMatchingIds == len(parcellData) {
norows = "!Empty! "
}
log = norows + baseDir + " --> missing rules (" +
strconv.Itoa(noMatchingIds) + "/" + strconv.Itoa(len(parcellData)) + ") for : " + log
*logs = append(*logs, log)
}
err = shapeUtils.WriteShape(baseDir, conf, parcellData)
if err != nil {
*errors = append(*errors, parcelFile+" : Error writing shapefile "+err.Error())
return
}
}
func createCsvForIdparAndDir(baseDir string, conf *cfg.ConfJson, csvslice *[]string) {
parcelFile := baseDir + conf.ParcelShape.Name
pathElems := strings.Split(baseDir, "/")
dir := pathElems[len(pathElems)-2]
dep := pathElems[len(pathElems)-3][3:]
parcellData, err := shapeUtils.ReadInputParcel(parcelFile, conf)
if err != nil {
fmt.Println(parcelFile + " : Error reading shapefile " + err.Error())
return
}
for _, row := range parcellData {
*csvslice = append(*csvslice, row.Key+";"+dir+";"+dep)
}
}
func createStub(c *cli.Context) error {
if c.String("shape") == "" || c.String("rules") == "" {
fmt.Println("need options, --help, -h for help")
return errors.New("bad arguments")
}
cfg.CreateStub(shapeFile, rulesFile, csvSep, confOut)
return nil
}
func execute(c *cli.Context) error {
if c.String("dir") == "" {
fmt.Println("need argument for dir, --help, -h for help")
return errors.New("bad arguments")
}
var conf cfg.ConfJson
var stats []string
var errs []string
logfile := "logstats.log"
errfile := "logerrs.log"
/* Loading JSON conf file */
err := conf.LoadFile(confPath)
if err != nil {
fmt.Println("error reading conf file " + err.Error())
panic(err)
}
isDb := false
var db *sql.DB
if conf.Rules.Type == "db" {
/* DB object to query the rules*/
db, err = conf.GetDb() //sql.Open("postgres", dbCreds)
if err != nil {
panic(err)
}
isDb = true
defer db.Close()
}
/* Loading subdirectories from base path*/
baseDirPath = sanitizePath(baseDirPath)
subdirs, err := ioutil.ReadDir(baseDirPath)
nbDirs := len(subdirs)
if err != nil {
panic(err)
}
/* Launching the process for all subdirs */
fmt.Println("Beginning processing for ", nbDirs, " directories")
start := time.Now()
for c, d := range subdirs {
if isDb {
process(baseDirPath+d.Name()+"/", &conf, db, &stats, &errs)
} else {
processCsv(baseDirPath+d.Name()+"/", &conf, &stats, &errs)
}
if c > 0 && c%350 == 0 {
fmt.Print("iter ", c, " - log: ", len(stats), " - errs: ", len(errs), " after ")
fmt.Printf("%.4f minutes\n", time.Now().Sub(start).Minutes())
}
}
end := time.Now()
fmt.Print("Time passed before writing logs : ")
fmt.Printf("%.4f minutes\n", end.Sub(start).Minutes())
fmt.Println("log size: ", len(stats))
fmt.Println("errs size: ", len(errs))
logPath = sanitizePath(logPath)
err = writelog(logPath+logfile, stats)
if err != nil {
fmt.Println("error writing logfile ", logPath+logfile)
}
fmt.Println("log file written in ", logPath+logfile)
err = writelog(logPath+errfile, errs)
if err != nil {
fmt.Println("error writing logfile ", logPath+errfile)
}
fmt.Println("log file written in ", logPath+errfile)
return nil
}
func sanitizePath(dirPath string) string {
size := len(dirPath)
if size == 0 {
return dirPath
}
if dirPath[size-1:] != "/" {
return dirPath + "/"
}
return dirPath
}
var confPath, baseDirPath, logPath, rulesFile, shapeFile, csvSep, confOut string
func main() {
/************** beginning cli parser **************/
app := cli.NewApp()
app.EnableBashCompletion = true
app.Commands = []cli.Command{
{
Name: "createStub",
Aliases: []string{"cs"},
Usage: "create a stub conf file from a typical parcelle shapefile and a rules csv file",
Flags: []cli.Flag{
cli.StringFlag{
Name: "shape, s",
Usage: "get the fields from shapefile `FILE.shp`",
Destination: &shapeFile,
},
cli.StringFlag{
Name: "rules, r",
Usage: "get the headers from csv file `FILE.csv`",
Destination: &rulesFile,
},
cli.StringFlag{
Name: "out, o",
Value: "generatedConf.json",
Usage: "output file name",
Destination: &confOut,
},
cli.StringFlag{
Name: "sep",
Value: ";",
Usage: "separator used in csv",
Destination: &csvSep,
},
},
Action: createStub,
},
{
Name: "execute",
Aliases: []string{"e"},
Usage: "Modify the shapefiles according to the JSON configuration file from",
Flags: []cli.Flag{
cli.StringFlag{
Name: "conf, c",
Value: "./conf.json",
Usage: "path to the configuration file `FILE.json`",
Destination: &confPath,
},
cli.StringFlag{
Name: "dir, d",
Usage: "path to subdirectories containing shapefiles",
Destination: &baseDirPath,
},
cli.StringFlag{
Name: "log, l",
Usage: "path for the generated logfiles",
Value: ".",
Destination: &logPath,
},
},
Action: execute,
},
}
app.Name = "shapeRuler"
app.Version = "0.72"
app.Usage = "Tool to add rules in shapefiles, for running a simplu3D simulation"
app.Run(os.Args)
}