This repository has been archived by the owner on Jun 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
types.go
89 lines (75 loc) · 2.05 KB
/
types.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
package main
import (
"fmt"
"strconv"
"strings"
)
// LocationRecords is a list of strings
type LocationRecords = []LocationRecord
// SearchResults are, erm, search results
type SearchResults = map[string]LocationRecords
// LocationRecord represents a single row in the CSV files
// Many of the CSV fields are ignored as irrelevant
type LocationRecord struct {
ID string `json:"-"`
Name string `json:"name"`
Normalised string `json:"-"`
GlobalType string `json:"-"`
LocalType string `json:"-"`
GeometryX string `json:"-"`
GeometryY string `json:"-"`
Xmin float64 // 12
Ymin float64 // 13
Xmax float64 // 14
Ymax float64 // 15
MostDetailView string // 10
LeastDetailView string // 11
District string `json:"district"`
}
// NewLocationRecord creates a new Location record from a list of
// strings (read presumably from a CSV file)
func NewLocationRecord(row []string) LocationRecord {
loc := LocationRecord{
ID: row[0],
Name: row[2],
Normalised: normalise(row[2]),
GlobalType: row[6],
LocalType: row[7],
GeometryX: row[8],
GeometryY: row[9],
MostDetailView: row[10],
LeastDetailView: row[11],
District: row[24],
}
val, err := strconv.ParseFloat(row[12], 64)
if err == nil {
loc.Xmin = val
}
val, err = strconv.ParseFloat(row[13], 64)
if err == nil {
loc.Ymin = val
}
val, err = strconv.ParseFloat(row[14], 64)
if err == nil {
loc.Xmax = val
}
val, err = strconv.ParseFloat(row[15], 64)
if err == nil {
loc.Ymax = val
}
return loc
}
func (l LocationRecord) String() string {
return fmt.Sprintf("{Name:%s, %s\nMin: %f,%f\nMax:%f,%f }\n", l.Name, l.District, l.Xmin, l.Ymin, l.Xmax, l.Ymax)
}
// GetTypeName returns the type of the location
// record provided.
func (l LocationRecord) GetTypeName() string {
if l.GlobalType == "populatedPlace" {
return "Place"
}
return l.LocalType
}
func normalise(name string) string {
return strings.ToLower(name)
}