-
Notifications
You must be signed in to change notification settings - Fork 2
/
csv.go
139 lines (129 loc) · 2.89 KB
/
csv.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
package mbt
import (
"encoding/csv"
"github.com/buckhx/diglet/util"
"github.com/deckarep/golang-set"
"io"
"os"
"strconv"
)
type FeatureSource interface {
Publish() (chan *Feature, error)
}
type GeoFields map[string]string
func (g GeoFields) Validate() error {
return nil
}
type CsvSource struct {
path string
headers map[string]int
delimiter string
filter mapset.Set
fields GeoFields
}
func NewCsvSource(path string, filter []string, delimiter string, fields GeoFields) *CsvSource {
var set mapset.Set
if filter == nil || len(filter) == 0 {
set = nil
} else {
set = mapset.NewSet()
for _, k := range filter {
set.Add(k)
}
set.Add(fields["lat"])
set.Add(fields["lon"])
}
return &CsvSource{
path: path,
delimiter: delimiter,
filter: set,
fields: fields,
}
}
func (c *CsvSource) Publish() (features chan *Feature, err error) {
lines, err := c.publishLines()
if err != nil {
return
}
features = make(chan *Feature, 1000)
go func() {
defer close(features)
for line := range lines {
if feature, err := c.featureAdapter(line); err != nil {
util.Warn(err, "feature adapter")
} else {
features <- feature
}
}
}()
return
}
func (c *CsvSource) publishLines() (lines chan []string, err error) {
//TODO optionally trim lines
f, err := os.Open(c.path)
if err != nil {
return
}
reader := csv.NewReader(f)
c.headers = readHeaders(reader, c.filter)
err = c.fields.Validate() //c.headers)
//TODO if err != nil
lines = make(chan []string, 100)
go func() {
defer close(lines)
defer f.Close()
for {
line, err := reader.Read()
if err == io.EOF {
break
}
if err != nil {
panic(err)
util.Warn(err, "line reading")
} else if line[c.headers[c.fields["lat"]]] == "" {
continue
//err = util.Errorf("No coordinates %v", line)
//util.Warn(err, "no lat/lon")
} else {
lines <- line
}
}
}()
return
}
func (c *CsvSource) featureAdapter(line []string) (feature *Feature, err error) {
feature = NewFeature("point")
props := make(map[string]interface{}) // TODO alloc correct amount
for k, i := range c.headers {
// biggest mem allocation
props[k] = line[i]
}
feature.Properties = props
lat, err := strconv.ParseFloat(line[c.headers[c.fields["lat"]]], 64)
if err != nil {
util.Info("%v", line)
return nil, err
}
lon, err := strconv.ParseFloat(line[c.headers[c.fields["lon"]]], 64)
if err != nil {
return nil, err
}
point := NewShape(Coordinate{Lat: lat, Lon: lon})
feature.AddShape(point)
return
}
func readHeaders(reader *csv.Reader, filter mapset.Set) (headers map[string]int) {
line, err := reader.Read()
util.Warn(err, "reading headers")
headers = make(map[string]int, len(line))
for i, k := range line {
//if _, ok := c.fields[k]; !ok {
k = util.Slugged(k, "_")
if filter == nil || filter.Contains(k) {
headers[k] = i
}
//}
}
util.Debug("Headers %v", headers)
return
}