-
Notifications
You must be signed in to change notification settings - Fork 0
/
chpopstat.go
192 lines (164 loc) · 4.52 KB
/
chpopstat.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
185
186
187
188
189
190
191
192
// SPDX-FileCopyrightText: 2019 Sascha Brawer <sascha@brawer.ch>
// SPDX-License-Identifier: MIT
package main
import (
"archive/zip"
"compress/gzip"
"errors"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"os/exec"
"path"
"regexp"
"strconv"
"time"
)
const chStatPopHost = "https://www.bfs.admin.ch"
const chStatPopListUrl = "/bfs/de/home/dienstleistungen/geostat/geodaten-bundesstatistik/gebaeude-wohnungen-haushalte-personen/bevoelkerung-haushalte-ab-2010/_jcr_content/par/tabs/items/geodaten_statpop/tabpar/ws_parametrized_list.dynamiclist.html"
type SwissPopulationStatistics struct {
client *http.Client
}
func (s SwissPopulationStatistics) FindUpstreamVersion() (*time.Time, error) {
_, version, err := findLatestCHStatPop(s.client)
return version, err
}
func findLatestCHStatPop(client *http.Client) (string, *time.Time, error) {
errNotFound := errors.New("could not find latest STATPOP dataset at bfs.admin.ch")
listUrl, err := url.Parse(chStatPopHost + chStatPopListUrl)
if err != nil {
return "", nil, err
}
resp, err := client.Get(listUrl.String())
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", nil, err
}
re := regexp.MustCompile("href=\"(/bfs/de/home/dienstleistungen/geostat/geodaten-bundesstatistik/[^\"]+)\"")
match := re.FindStringSubmatch(string(body))
if match == nil {
return "", nil, errNotFound
}
relativeDatasetUrl, err := url.Parse(match[1])
if err != nil {
return "", nil, err
}
datasetUrl := listUrl.ResolveReference(relativeDatasetUrl)
resp2, err := client.Get(datasetUrl.String())
defer resp.Body.Close()
body2, err := ioutil.ReadAll(resp2.Body)
if err != nil {
return "", nil, err
}
reUrl := regexp.MustCompile(`<a href="([^"]+)"[^>]*?>Download`)
matchUrl := reUrl.FindStringSubmatch(string(body2))
if matchUrl == nil {
return "", nil, errNotFound
}
downloadUrl, err := url.Parse(matchUrl[1])
if err != nil {
return "", nil, errNotFound
}
downloadUrl = datasetUrl.ResolveReference(downloadUrl)
rePubDate := regexp.MustCompile("<th>Veröffentlicht am</th>\\s*<td>([0-9]{1,2})\\.([0-9]{1,2})\\.(20[0-9]{2})")
matchPubDate := rePubDate.FindStringSubmatch(string(body2))
if matchPubDate == nil {
return "", nil, errNotFound
}
year, _ := strconv.Atoi(matchPubDate[3])
month, _ := strconv.Atoi(matchPubDate[2])
day, _ := strconv.Atoi(matchPubDate[1])
pubDate := time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC)
return downloadUrl.String(), &pubDate, nil
}
func (s SwissPopulationStatistics) Process(s2Level int, outpath string) error {
// For the actual conversion, we currently call a C++ tool.
// Its source code is in "tools"; see Dockerfile for installation.
toolPath, err := exec.LookPath("chpopstat_convert")
if err != nil {
return err
}
url, _, err := findLatestCHStatPop(s.client)
if err != nil {
return err
}
tempDir, err := ioutil.TempDir("", "geosmell-chstatpop")
if err != nil {
return err
}
fetchedPath := path.Join(tempDir, "fetched.zip")
extractedPath := path.Join(tempDir, "extracted.csv")
convertedPath := path.Join(tempDir, "converted.csv")
if err != nil {
return err
}
resp, err := s.client.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
fetchedFile, err := os.Create(fetchedPath)
if err != nil {
return err
}
defer fetchedFile.Close()
_, err = io.Copy(fetchedFile, resp.Body)
if err != nil {
return err
}
extractedFile, err := os.Create(extractedPath)
if err != nil {
return err
}
defer extractedFile.Close()
zipFile, err := zip.OpenReader(fetchedFile.Name())
if err != nil {
return err
}
found := false
re := regexp.MustCompile(`STATPOP20[0-9]{2}G\.csv$`)
for _, file := range zipFile.File {
if re.FindString(file.Name) != "" {
statFile, err := file.Open()
if err != nil {
return err
}
defer statFile.Close()
_, err = io.Copy(extractedFile, statFile)
if err != nil {
return err
}
found = true
break
}
}
if !found {
return errors.New("ZIP file does not contain file STATPOP20??G.csv")
}
cmd := exec.Command(toolPath, extractedPath, convertedPath, strconv.Itoa(s2Level))
if err := cmd.Start(); err != nil {
return err
}
if err := cmd.Wait(); err != nil {
return err
}
convertedStream, err := os.Open(convertedPath)
if err != nil {
return err
}
defer convertedStream.Close()
resultFile, err := os.Create(outpath)
if err != nil {
return err
}
gzstream := gzip.NewWriter(resultFile)
defer gzstream.Close()
_, err = io.Copy(gzstream, convertedStream)
if err != nil {
return err
}
return nil
}