Skip to content

Commit

Permalink
Pass appConfig through geospatialdata handler and avoid passing globa…
Browse files Browse the repository at this point in the history
…l variables
  • Loading branch information
ShaneMPutnam committed Jan 11, 2021
1 parent dd2c5b0 commit 99e9c67
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 17 deletions.
8 changes: 4 additions & 4 deletions handlers/geospatialdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package handlers
import (
"net/http"

"github.com/USACE/filestore"
"github.com/USACE/mcat-ras/config"
ras "github.com/USACE/mcat-ras/tools"

"github.com/labstack/echo/v4"
Expand All @@ -19,17 +19,17 @@ import (
// @Success 200 {object} interface{}
// @Failure 500 {object} SimpleResponse
// @Router /geospatialdata [get]
func GeospatialData(fs *filestore.FileStore, destinationCRS int) echo.HandlerFunc {
func GeospatialData(ac *config.APIConfig) echo.HandlerFunc {
return func(c echo.Context) error {

definitionFile := c.QueryParam("definition_file")

rm, err := ras.NewRasModel(definitionFile, *fs)
rm, err := ras.NewRasModel(definitionFile, *ac.FileStore)
if err != nil {
return c.JSON(http.StatusInternalServerError, SimpleResponse{http.StatusInternalServerError, err.Error()})
}

data, err := rm.GeospatialData(destinationCRS)
data, err := rm.GeospatialData(ac.DestinationCRS)
if err != nil {
return c.JSON(http.StatusInternalServerError, SimpleResponse{http.StatusInternalServerError, err.Error()})
}
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func main() {
e.GET("/isgeospatial", handlers.IsGeospatial(appConfig.FileStore))
e.GET("/modeltype", handlers.ModelType(appConfig.FileStore))
e.GET("/modelversion", handlers.ModelVersion(appConfig.FileStore))
e.GET("/geospatialdata", handlers.GeospatialData(appConfig.FileStore, appConfig.DestinationCRS))
e.GET("/geospatialdata", handlers.GeospatialData(appConfig))

e.Logger.Fatal(e.Start(appConfig.Address()))
}
4 changes: 2 additions & 2 deletions tools/geospatial.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ var unitConsistencyMap map[string]string = map[string]string{
"SI Units": "metre"}

// checkUnitConsistency checks that the unit system used by the model and its coordinate reference system are the same
func checkUnitConsistency(modelUnits string, sourceCRS string, ucMap map[string]string) error {
func checkUnitConsistency(modelUnits string, sourceCRS string) error {
sourceSpRef := gdal.CreateSpatialReference(sourceCRS)

if crsUnits, ok := sourceSpRef.AttrValue("UNIT", 0); ok {
if ucMap[modelUnits] == crsUnits {
if unitConsistencyMap[modelUnits] == crsUnits {
return nil
}
return errors.New("The unit system of the model and coordinate reference system are inconsistent")
Expand Down
2 changes: 1 addition & 1 deletion tools/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func (rm *RasModel) GeospatialData(destinationCRS int) (GeoData, error) {
return gd, errors.New("Cannot extract geospatial data, no valid coordinate reference system")
}

if err := checkUnitConsistency(modelUnits, sourceCRS, unitConsistencyMap); err != nil {
if err := checkUnitConsistency(modelUnits, sourceCRS); err != nil {
return gd, err
}

Expand Down
18 changes: 9 additions & 9 deletions tools/structures.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func stringtoFloat(s string) (float64, error) {
return 0, nil
}

func getConduits(line string, single bool, shapeMap map[int]string) (conduits, error) {
func getConduits(line string, single bool) (conduits, error) {
lineData := strings.Split(rightofEquals(line), ",")
conduit := conduits{}

Expand All @@ -235,7 +235,7 @@ func getConduits(line string, single bool, shapeMap map[int]string) (conduits, e
if err != nil {
return conduit, err
}
conduit.Shape = shapeMap[shapeID]
conduit.Shape = conduitShapes[shapeID]

rise, err := stringtoFloat(lineData[1])
if err != nil {
Expand Down Expand Up @@ -264,7 +264,7 @@ func getConduits(line string, single bool, shapeMap map[int]string) (conduits, e
return conduit, nil
}

func getCulvertData(hsSc *bufio.Scanner, i int, lineData []string, shapeMap map[int]string) (culverts, int, error) {
func getCulvertData(hsSc *bufio.Scanner, i int, lineData []string) (culverts, int, error) {
culvert := culverts{}

station, err := strconv.ParseFloat(strings.TrimSpace(lineData[1]), 64)
Expand Down Expand Up @@ -315,15 +315,15 @@ func getCulvertData(hsSc *bufio.Scanner, i int, lineData []string, shapeMap map[
culvert.DownLowChord = downHighLowPair[1]

case strings.HasPrefix(line, "Culvert="):
conduit, err := getConduits(line, true, shapeMap)
conduit, err := getConduits(line, true)
if err != nil {
return culvert, i, err
}
culvert.Conduits = append(culvert.Conduits, conduit)
culvert.NumConduits++

case strings.HasPrefix(line, "Multiple Barrel Culv="):
conduit, err := getConduits(line, false, shapeMap)
conduit, err := getConduits(line, false)
if err != nil {
return culvert, i, err
}
Expand Down Expand Up @@ -437,7 +437,7 @@ func getGates(nextLine string) (gates, error) {
return gate, nil
}

func getWeirData(rm *RasModel, fn string, i int, shapeMap map[int]string) (weirs, error) {
func getWeirData(rm *RasModel, fn string, i int) (weirs, error) {
weir := weirs{}

f, err := rm.FileStore.GetObject(fn)
Expand Down Expand Up @@ -503,7 +503,7 @@ func getWeirData(rm *RasModel, fn string, i int, shapeMap map[int]string) (weirs
weir.NumGates++

case strings.HasPrefix(line, "IW Culv="):
conduit, err := getConduits(line, false, shapeMap)
conduit, err := getConduits(line, false)
if err != nil {
return weir, err
}
Expand Down Expand Up @@ -556,7 +556,7 @@ func getHydraulicStructureData(rm *RasModel, fn string, idx int) (hydraulicStruc

case 2:
var culvert culverts
culvert, i, err = getCulvertData(hsSc, i, data, conduitShapes)
culvert, i, err = getCulvertData(hsSc, i, data)
if err != nil {
return structures, err
}
Expand All @@ -573,7 +573,7 @@ func getHydraulicStructureData(rm *RasModel, fn string, idx int) (hydraulicStruc
bData.NumBridges++

case 5:
weir, err := getWeirData(rm, fn, i, conduitShapes)
weir, err := getWeirData(rm, fn, i)
if err != nil {
return structures, err
}
Expand Down

0 comments on commit 99e9c67

Please sign in to comment.