/
compute.go
111 lines (106 loc) · 3.31 KB
/
compute.go
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
package compute
import (
"errors"
"strings"
"github.com/HydrologicEngineeringCenter/go-fema-consequences/config"
"github.com/HydrologicEngineeringCenter/go-fema-consequences/outputwriter"
"github.com/HydrologicEngineeringCenter/go-tc-consequences/nhc"
consequences_compute "github.com/USACE/go-consequences/compute"
"github.com/USACE/go-consequences/consequences"
"github.com/USACE/go-consequences/hazardproviders"
"github.com/USACE/go-consequences/structureprovider"
)
type Compute struct {
Hp hazardproviders.HazardProvider
Sp consequences.StreamProvider
Ow consequences.ResultsWriter
TempFileOutput string
OutputFolderPath string
}
func Init(c config.Config) (Compute, error) {
var err error
var sp consequences.StreamProvider
var hp hazardproviders.HazardProvider //not sure what it will be yet, but we can declare it!
hazardProviderVerticalUnitsIsFeet := true
var ow consequences.ResultsWriter //need a file path to write anything...
if c.Sfp != "" {
switch c.Ss {
case "gpkg":
sp = structureprovider.InitGPK(c.Sfp, "nsi")
case "shp":
sp = structureprovider.InitSHP(c.Sfp)
case "nsi":
sp = structureprovider.InitNSISP() //default to NSI API structure provider.
default:
sp = structureprovider.InitNSISP()
}
} else {
sp = structureprovider.InitNSISP()
}
if c.HpUnits != "" {
switch c.HpUnits {
case "feet":
hazardProviderVerticalUnitsIsFeet = true
case "meters":
hazardProviderVerticalUnitsIsFeet = false
}
} else {
err = errors.New("cannot compute without hazard provider path")
}
if c.Hfp != "" {
switch c.HpSource {
case "nhc":
hp = nhc.Init(c.Hfp)
case "depths":
if hazardProviderVerticalUnitsIsFeet {
hp = hazardproviders.Init(c.Hfp)
} else {
hp = hazardproviders.Init_Meters(c.Hfp)
}
default: //assume depth
if hazardProviderVerticalUnitsIsFeet {
hp = hazardproviders.Init(c.Hfp)
} else {
hp = hazardproviders.Init_Meters(c.Hfp)
}
}
} else {
err = errors.New("cannot compute without hazard provider path")
}
ofp := c.Hfp
// pull the .tif off the end?
ofp = ofp[:len(ofp)-4] //good enough for government work?
// pull vsis3 off the front!
//write to temp directory and copy then paste!
ofp = strings.Replace(ofp, "/vsis3/", "/app/", 1)
if ofp != "" {
switch c.Ot {
case "gpkg":
ofp += "_consequences.gpkg"
ow = consequences.InitGpkResultsWriter(ofp, "results")
case "shp":
ofp += "_consequences.shp"
ow = consequences.InitShpResultsWriter(ofp, "results")
case "geojson":
ofp += "_consequences.json"
ow = consequences.InitGeoJsonResultsWriterFromFile(ofp)
case "summaryDollars":
ofp += "_summaryDollars.csv"
ow = consequences.InitSummaryResultsWriterFromFile(ofp)
case "summaryDepths":
ofp += "_summaryDepths.csv"
ow = outputwriter.InitSummaryByDepth(ofp)
default:
ofp += "_consequences.gpkg"
ow = consequences.InitGpkResultsWriter(ofp, "results")
}
} else {
err = errors.New("we need an input hazard file path use")
}
return Compute{Hp: hp, Sp: sp, Ow: ow, OutputFolderPath: c.Ofp, TempFileOutput: ofp}, err
}
func (c Compute) Compute() {
defer c.Hp.Close()
defer c.Ow.Close()
consequences_compute.StreamAbstract(c.Hp, c.Sp, c.Ow)
}