-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeo.go
184 lines (162 loc) · 3.04 KB
/
geo.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main
import (
"database/sql"
"fmt"
"log"
_ "github.com/lib/pq"
)
type tmp_geonameid struct {
geonameid int
name string
asciiname string
latitude string
longitude string
featureClass string
featureCode string
countryCode string
admin1Code string
admin2Code string
admin3Code string
admin4Code string
population string
elevation string
dem string
timezone string
modificationDate string
}
type GeoNameLang map[string]string
func getProgress(db *sql.DB) int {
rows, err := db.Query(`
SELECT
COUNT(*)
FROM "tmp_geonameid"
WHERE "featureClass" = 'A' OR "featureClass" = 'P'
`)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
var num int = 0
for rows.Next() {
err2 := rows.Scan(&num)
if err2 != nil {
log.Fatal(err2)
}
}
return num
}
func buildGeoData(db *sql.DB) {
_, err := db.Exec(`TRUNCATE "geo" CASCADE;`)
if err != nil {
log.Fatal(err)
}
totalNumbers := getProgress(db)
rows, err := db.Query(`
SELECT
"geonameid",
"name",
"asciiname",
"latitude",
"longitude",
"featureClass",
"featureCode",
"countryCode",
"admin1Code",
"admin2Code",
"admin3Code",
"admin4Code",
"population",
"elevation",
"dem",
"timezone",
"modificationDate"
FROM "tmp_geonameid"
WHERE "featureClass" = 'A' OR "featureClass" = 'P'
`)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
i := 0
for rows.Next() {
i++
var r tmp_geonameid
err2 := rows.Scan(
&r.geonameid,
&r.name,
&r.asciiname,
&r.latitude,
&r.longitude,
&r.featureClass,
&r.featureCode,
&r.countryCode,
&r.admin1Code,
&r.admin2Code,
&r.admin3Code,
&r.admin4Code,
&r.population,
&r.elevation,
&r.dem,
&r.timezone,
&r.modificationDate,
)
if (i % 1000) == 0 {
fmt.Printf("Current %d/%d\n", i, totalNumbers)
}
if err2 == nil {
countryGeoId, isCountryCodeExist := isoToGeonameID[r.countryCode]
if !isCountryCodeExist {
continue
}
adminCode1Name := r.countryCode + "." + r.admin1Code
adminCode1ID, ok := adminCodeMapList[adminCode1Name]
if !ok {
adminCode1ID = 0
}
population := NewNullInt64(r.population)
elevation := NewNullInt64(r.elevation)
dem := NewNullInt64(r.dem)
processGeoFields(db, &r, adminCode1ID, countryGeoId, elevation, population, dem)
}
}
}
func processGeoFields(
db *sql.DB,
tmpRow *tmp_geonameid,
adminCode1ID int,
countryGeoId int,
elevation sql.NullInt64,
population sql.NullInt64,
dem sql.NullInt64,
) {
_, err := db.Exec(`
INSERT INTO "geo" (
"geonameid",
"location",
"name",
"country",
"adminCode",
"population",
"timezone"
) VALUES(
$1,
ST_GeomFromText($2),
$3,
$4,
$5,
$6,
$7
)
`,
tmpRow.geonameid,
fmt.Sprintf("Point(%s %s)", tmpRow.longitude, tmpRow.latitude),
fixName(tmpRow.asciiname),
countryGeoId,
NewNullInt64FromInt(adminCode1ID),
population,
tmpRow.timezone,
)
if err != nil {
log.Fatal(err)
}
}