-
Notifications
You must be signed in to change notification settings - Fork 10
/
utils.go
141 lines (135 loc) · 3 KB
/
utils.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
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
package gismanager
import (
"database/sql"
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"github.com/mholt/archiver"
//postgres Driver
_ "github.com/lib/pq"
yaml "gopkg.in/yaml.v2"
)
//FromConfig load GIS Manager config from yaml file
func FromConfig(configFile string) (config *ManagerConfig, err error) {
gpkgConfig := ManagerConfig{}
gpkgConfig.logger = GetLogger()
path, _ := filepath.Abs(configFile)
yamlFile, err := ioutil.ReadFile(path)
if err != nil {
gpkgConfig.logger.Errorf("yamlFile.Get err %v ", err)
return
}
err = yaml.Unmarshal(yamlFile, &gpkgConfig)
if err != nil {
gpkgConfig.logger.Errorf("Unmarshal: %v", err)
return
}
config = &gpkgConfig
return
}
func isSupported(ext string) bool {
for _, a := range supportedEXT {
if a == ext {
return true
}
}
return false
}
//GetGISFiles retrun List of All GIS Files in this path
func GetGISFiles(root string) ([]string, error) {
root, _ = filepath.Abs(root)
var files []string
fileInfo, statErr := os.Stat(root)
if statErr != nil {
return files, statErr
}
if !fileInfo.IsDir() {
extension := strings.ToLower(filepath.Ext(fileInfo.Name()))
if isSupported(extension) {
finalPath, preProcessErr := preprocessFile(root, "")
if preProcessErr != nil {
return files, preProcessErr
}
files = append(files, finalPath)
return files, nil
}
return files, nil
}
dirInfo, err := ioutil.ReadDir(root)
if err != nil {
return files, err
}
for _, file := range dirInfo {
subFiles, subErr := GetGISFiles(path.Join(root, file.Name()))
if subErr == nil {
files = append(files, subFiles...)
}
}
return files, nil
}
//DBIsAlive check if database alive
func DBIsAlive(dbType string, connectionStr string) (err error) {
db, dbErr := sql.Open(dbType, connectionStr)
if dbErr != nil {
err = dbErr
return
}
if pingErr := db.Ping(); pingErr != nil {
db.Close()
err = pingErr
return
}
return
}
func zippedShapeFile(zippedPath string, destPath string) (err error) {
fileInfo, statErr := os.Stat(zippedPath)
if statErr != nil || os.IsNotExist(statErr) {
err = statErr
return
}
if fileInfo.IsDir() {
err = errors.New("zippedPath must be file not a directory")
return
}
err = archiver.Zip.Open(zippedPath, destPath)
return
}
func preprocessFile(filePath string, tempPath string) (finalPath string, err error) {
logger := GetLogger()
ext := strings.ToLower(filepath.Ext(filePath))
switch ext {
case ".zip":
newDir, tempDirErr := ioutil.TempDir(tempPath, "zipped_shapeFile")
fmt.Println(newDir)
if tempDirErr != nil {
logger.Error(tempDirErr)
err = tempDirErr
break
}
unzipErr := zippedShapeFile(filePath, newDir)
if unzipErr != nil {
logger.Error(unzipErr)
err = unzipErr
break
}
files, filesErr := GetGISFiles(newDir)
if filesErr != nil {
logger.Error(filesErr)
err = filesErr
break
}
if len(files) == 0 {
err = errors.New("cannot find gis files")
break
}
finalPath = files[0]
break
default:
finalPath = filePath
}
return
}