-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils_file.go
63 lines (45 loc) · 1.09 KB
/
utils_file.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
package main
import "net/http"
import "io/ioutil"
import "bufio"
import "strings"
import "strconv"
// Point holds latitude, longitude coordinates in degrees
type Point struct {
lat float64
lng float64
}
// Download a file from a specified path
func downloadFile(path string) ([]byte, error) {
r, err := http.Get(path)
if err != nil {
return nil, err
}
defer r.Body.Close()
data, err := ioutil.ReadAll(r.Body)
return data, err
}
// Parse .poly file line by line from a byte array
func parsePolyData(data []byte) ([]Point, error) {
scanner := bufio.NewScanner(strings.NewReader(string(data)))
scanner.Split(bufio.ScanLines)
var line string
points := make([]Point, 0)
for scanner.Scan() {
line = strings.TrimSpace(scanner.Text())
if line == "none" || line == "END" || line == "1" {
continue
}
arr := strings.Split(line, " ")
lat, err := strconv.ParseFloat(arr[1], 32)
if err != nil {
return nil, err
}
lng, err := strconv.ParseFloat(arr[0], 32)
if err != nil {
return nil, err
}
points = append(points, Point{lat: lat, lng: lng})
}
return points, nil
}