-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocation.go
More file actions
118 lines (109 loc) · 2.6 KB
/
location.go
File metadata and controls
118 lines (109 loc) · 2.6 KB
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
package teryt
import (
"fmt"
"strings"
)
type Location struct {
ID string
Type string
Name string
Parts []string
FullName string
ParentID string
Parent *Location
Children []*Location
}
type Config struct {
Separator string
}
func (l *Location) Build(cfg Config) {
l.FullName = strings.Join(l.Parts, cfg.Separator)
}
type keyType struct {
voivodeship, county, commune int
symbol string
}
func BuildLocations(tercData []SetTERC, simcData []SetSIMC) (*Location, error) {
root := &Location{}
communeByKey := map[keyType]*Location{
keyType{}: root,
}
var (
id string
parentKey, key keyType
)
communeBySymbol := make(map[string]*Location)
for _, v := range tercData {
key = keyType{
voivodeship: v.Woj,
county: v.Pow,
commune: v.Gmi,
}
if v.Pow == 0 { // województwo
parentKey = keyType{}
id = fmt.Sprintf("terc:%02d", v.Woj)
} else if v.Gmi == 0 { // powiat
parentKey = keyType{voivodeship: v.Woj}
id = fmt.Sprintf("terc:%02d%02d", v.Woj, v.Pow)
} else { // gmina
parentKey = keyType{voivodeship: v.Woj, county: v.Pow}
id = fmt.Sprintf("terc:%02d%02d%02d", v.Woj, v.Pow, v.Gmi)
}
parent, ok := communeByKey[parentKey]
if !ok {
return nil, fmt.Errorf("cannot find parent for: %s", id)
}
loc := &Location{
ID: id,
Type: v.Nazdod,
Name: v.Nazwa,
Parts: append(append([]string{}, parent.Parts...), v.Nazwa),
Parent: parent,
ParentID: parent.ID,
Children: nil,
}
parent.Children = append(parent.Children, loc)
communeByKey[key] = loc
communeBySymbol[loc.ID] = loc
}
for _, v := range simcData {
key := keyType{
voivodeship: v.Woj,
county: v.Pow,
commune: v.Gmi,
}
if v.Sym != v.SymPod {
key.symbol = "simc:" + v.SymPod
}
parent, ok := communeByKey[key]
loc := &Location{
ID: "simc:" + v.Sym,
Type: v.Rm.Name(),
Name: v.Nazwa,
Children: nil,
}
if ok {
loc.Parts = append(append([]string{}, parent.Parts...), v.Nazwa)
loc.Parent = parent
loc.ParentID = parent.ID
parent.Children = append(parent.Children, loc)
} else if v.Sym != v.SymPod {
loc.ParentID = key.symbol
}
key.symbol = loc.ID
communeBySymbol[loc.ID] = loc
}
for _, v := range communeByKey {
if v.ParentID == "" || v.Parent != nil {
continue
}
parent, ok := communeBySymbol[v.ParentID]
if !ok {
return nil, fmt.Errorf("cannot find parent: %s", v.ParentID)
}
v.Parts = append(append([]string{}, parent.Parts...), v.Name)
v.Parent = parent
parent.Children = append(parent.Children, v)
}
return root, nil
}