forked from remizovm/geonames
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhierarchy.go
58 lines (47 loc) · 1.24 KB
/
hierarchy.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
package geonames
import (
"log"
"strconv"
"github.com/remizovm/geonames/models"
)
const hierarchyURL = `hierarchy.zip`
// Hierarchy returns all available pairs of linked parents and children
// For example, a city is linked to it's country as a child:
// Country->City1,City2 etc
func (c *Client) Hierarchy() (map[int][]*models.HierarchyNode, error) {
var err error
result := make(map[int][]*models.HierarchyNode)
zipped, err := httpGet(geonamesURL + hierarchyURL)
if err != nil {
return nil, err
}
f, err := unzip(zipped)
if err != nil {
return nil, err
}
data, err := getZipData(f, "hierarchy.txt")
if err != nil {
return nil, err
}
parse(data, 0, func(raw [][]byte) bool {
if len(raw) != 3 {
return true
}
parentID, err := strconv.Atoi(string(raw[0]))
if err != nil {
log.Printf("while parsing hierarchy parent id %s: %s", string(raw[0]), err.Error())
return true
}
childID, err := strconv.Atoi(string(raw[1]))
if err != nil {
log.Printf("while parsing hierarchy child id %s: %s", string(raw[1]), err.Error())
return true
}
result[parentID] = append(result[parentID], &models.HierarchyNode{
ParentID: parentID,
ChildID: childID,
Type: string(raw[2])})
return true
})
return result, nil
}