-
Notifications
You must be signed in to change notification settings - Fork 2
/
metadata.go
77 lines (66 loc) · 1.56 KB
/
metadata.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
package main
import (
"encoding/csv"
"log"
"os"
"regexp"
"strings"
)
type metadataProvider struct {
data map[string]metaData
}
type location struct {
lat float64
long float64
}
type metaData struct {
location location
country string
population uint64
}
func normalizeName(name string) string {
space := regexp.MustCompile(`[^A-Za-z]+`)
result := strings.ToUpper(space.ReplaceAllString(name, ""))
return result
}
func newMetadataProvider() *metadataProvider {
return newMetadataProviderWithFilename("metadata.csv")
}
func newMetadataProviderWithFilename(filename string) *metadataProvider {
csvFile, err := os.Open(filename)
if err != nil {
log.Print(err)
return nil
}
r := csv.NewReader(csvFile)
records, err := r.ReadAll()
if err != nil {
log.Print(err)
return nil
}
data := make(map[string]metaData, len(records))
for _, row := range records {
data[normalizeName(row[0])] = metaData{location{atof(row[2]), atof(row[3])}, row[0], atoi(row[1])}
}
return &metadataProvider{data: data}
}
func (l *metadataProvider) getMetadata(location string) *metaData {
if l, ok := l.data[normalizeName(location)]; ok {
return &l
}
return nil
}
//getLocation returns lat/long for a location name
func (l *metadataProvider) getLocation(location string) *location {
if l, ok := l.data[normalizeName(location)]; ok {
return &l.location
}
return nil
}
//getPopulation for a given location by name
func (l *metadataProvider) getPopulation(location string) uint64 {
if l, ok := l.data[normalizeName(location)]; ok {
return l.population
}
return 0
}