forked from remizovm/geonames
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathall_countries.go
156 lines (133 loc) · 3.34 KB
/
all_countries.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package geonames
import (
"archive/zip"
"bufio"
"errors"
"fmt"
"log"
"os"
"strconv"
"strings"
"time"
"github.com/remizovm/geonames/models"
)
const allCountriesURI = `allCountries.zip`
// AllCountries returns a big pack of all features of all countries
func (c *Client) AllCountries() (map[int]*models.Feature, error) {
result := make(map[int]*models.Feature)
err := CallAllCountries(func(f *models.Feature) {
result[f.GeonameID] = f
})
return result, err
}
// CallAllCountries calls the passed handler on each extracted feature.
func CallAllCountries(handler func(*models.Feature)) error {
url := fmt.Sprintf("%s%s", geonamesURL, allCountriesURI)
dat, err := httpGetNew(url)
if err != nil {
return err
}
tempPath := getTempPath(allCountriesURI)
f, err := writeToFile(tempPath, dat)
if err != nil {
return err
}
dat.Close()
defer f.Close()
defer os.Remove(tempPath)
r, err := zip.OpenReader(f.Name())
if err != nil {
return err
}
defer r.Close()
txtName := strings.Replace(allCountriesURI, "zip", "txt", -1)
var s *bufio.Scanner
for i := range r.File {
if r.File[i].Name == txtName {
rc, e := r.File[i].Open()
if e != nil {
return e
}
defer rc.Close()
s = bufio.NewScanner(rc)
break
}
}
if s == nil {
return errors.New("unknown error")
}
sParse(s, 0, func(raw []string) bool {
if len(raw) != 19 {
return true
}
geonameID, err := strconv.Atoi(raw[0])
if err != nil {
log.Printf("while parsing feature geoname id %s: %s", raw[0], err.Error())
return true
}
alternateNames := strings.Split(raw[3], ",")
for i := range alternateNames {
alternateNames[i] = strings.TrimSpace(alternateNames[i])
if alternateNames[i] == "" {
alternateNames = append(alternateNames[:i], alternateNames[i+1:]...)
}
}
latitude, err := strconv.ParseFloat(raw[4], 64)
if err != nil {
log.Printf("while parsing feature latitude %s: %s", raw[4], err.Error())
return true
}
longitude, err := strconv.ParseFloat(raw[5], 64)
if err != nil {
log.Printf("while parsing feature longitude %s: %s", raw[5], err.Error())
return true
}
var population *int
if (raw[14]) != "" {
populationInt, e := strconv.Atoi(raw[14])
if e == nil {
population = &populationInt
}
}
var elevation *int
if (raw[15]) != "" {
elevationInt, e := strconv.Atoi(raw[15])
if e == nil {
elevation = &elevationInt
}
}
dem, err := strconv.Atoi(raw[16])
if err != nil {
log.Printf("while parsing feature dem %s: %s", raw[16], err.Error())
return true
}
modificationDate, err := time.Parse("2006-01-02", raw[18])
if err != nil {
log.Printf("while parsing feature modification date %s: %s", raw[18], err.Error())
return true
}
handler(&models.Feature{
GeonameID: geonameID,
Name: raw[1],
ASCIIName: raw[2],
AlternateNames: alternateNames,
Latitude: latitude,
Longitude: longitude,
Class: raw[6],
Code: raw[7],
CountryCode: raw[8],
Cc2: raw[9],
Admin1Code: raw[10],
Admin2Code: raw[11],
Admin3Code: raw[12],
Admin4Code: raw[13],
Population: population,
Elevation: elevation,
Dem: dem,
TimeZone: raw[17],
ModificationDate: modificationDate,
})
return true
})
return nil
}