forked from maddevsio/ariadna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
400 lines (361 loc) · 10.6 KB
/
main.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
package main
import (
"encoding/json"
"fmt"
"github.com/codegangsta/cli"
"github.com/maddevsio/ariadna/common"
"github.com/maddevsio/ariadna/importer"
"github.com/maddevsio/ariadna/updater"
"github.com/maddevsio/ariadna/web"
"github.com/qedus/osmpbf"
"gopkg.in/olivere/elastic.v3"
"io/ioutil"
"os"
"runtime"
"strconv"
"strings"
"time"
)
var (
CitiesAndTowns, Roads []importer.JsonWay
Version string = "dev"
configPath string
indexSettingsPath string
customDataPath string
ElasticSearchIndexName string
PGConnString string
ElasticSearchHost string
IndexType string
FileName string
DownloadUrl string
DontImportIntersections bool
)
func getDecoder(file *os.File) *osmpbf.Decoder {
decoder := osmpbf.NewDecoder(file)
err := decoder.Start(runtime.GOMAXPROCS(-1))
if err != nil {
importer.Logger.Fatal(err.Error())
}
return decoder
}
func getCurrentIndexName(client *elastic.Client) (string, error) {
res, err := client.Aliases().Index("_all").Do()
if err != nil {
return "", err
}
for _, index := range res.IndicesByAlias(common.AC.IndexName) {
if strings.HasPrefix(index, common.AC.IndexName) {
return index, nil
}
}
return "", nil
}
func main() {
app := cli.NewApp()
app.Name = "Ariadna"
app.Usage = "OSM Geocoder"
app.Version = Version
app.Commands = []cli.Command{
{
Name: "import",
Aliases: []string{"i"},
Usage: "Import OSM file to ElasticSearch",
Action: actionImport,
ArgsUsage: "<filename>",
},
{
Name: "update",
Aliases: []string{"u"},
Usage: "Download OSM file and update index",
Action: actionUpdate,
},
{
Name: "http",
Aliases: []string{"h"},
Usage: "Run http server",
Action: actionHttp,
},
{
Name: "custom",
Aliases: []string{"c"},
Usage: "Import custom data",
Action: actionCustom,
},
{
Name: "intersections",
Aliases: []string{"i"},
Usage: "Process intersections only",
Action: actionIntersection,
},
{
Name: "test",
Aliases: []string{"t"},
Usage: "For testing",
Action: actionTest,
},
}
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "config",
Usage: "Config file path",
Destination: &configPath,
},
cli.StringFlag{
Name: "index_settings",
Usage: "ElasticSearch Index settings",
Destination: &indexSettingsPath,
},
cli.StringFlag{
Name: "custom_data",
Usage: "Custom data file path",
Destination: &customDataPath,
},
cli.StringFlag{
Name: "es_index_name",
Usage: "Specify custom elasticsearch index name",
Value: "addresses",
EnvVar: "ARIADNA_ES_INDEX_NAME",
Destination: &ElasticSearchIndexName,
},
cli.StringFlag{
Name: "es_pg_conn_url",
Usage: "Specify custom PG connection URL",
Destination: &PGConnString,
Value: "host=localhost user=geo password=geo dbname=geo sslmode=disable",
EnvVar: "ARIADNA_PG_CONN_URL",
},
cli.StringFlag{
Name: "es_url",
Usage: "Custom url for elasticsearch e.g http://192.168.0.1:9200",
Destination: &ElasticSearchHost,
Value: "http://localhost:9200/",
EnvVar: "ARIADNA_ES_HOST",
},
cli.StringFlag{
Name: "es_index_type",
Usage: "ElasticSearch index type",
Destination: &IndexType,
Value: "address",
EnvVar: "ARIADNA_INDEX_TYPE",
},
cli.StringFlag{
Name: "filename",
Usage: "filename for storing osm.pbf file",
Destination: &FileName,
Value: "xxx",
EnvVar: "ARIADNA_FILE_NAME",
},
cli.StringFlag{
Name: "download_url",
Usage: "Geofabrik url to download file",
Destination: &DownloadUrl,
Value: "xxx",
EnvVar: "ARIADNA_DOWNLOAD_URL",
},
cli.BoolFlag{
Name: "dont_import_intersections",
Usage: "if checked, then ariadna won't import intersections",
Destination: &DontImportIntersections,
EnvVar: "ARIADNA_DONT_IMPORT_INTERSECTIONS",
},
}
app.Before = func(context *cli.Context) error {
if configPath == "" {
configPath = "config.json"
}
if indexSettingsPath == "" {
indexSettingsPath = "index.json"
}
if customDataPath == "" {
customDataPath = "custom.json"
}
common.AC = common.AppConfig{
IndexType: IndexType,
PGConnString: PGConnString,
ElasticSearchHost: ElasticSearchHost,
IndexName: ElasticSearchIndexName,
FileName: FileName,
DownloadUrl: DownloadUrl,
DontImportIntersections: DontImportIntersections,
}
return nil
}
if err := app.Run(os.Args); err != nil {
importer.Logger.Fatal("error on run app, %v", err)
}
}
func actionImport(ctx *cli.Context) error {
indexSettings, err := ioutil.ReadFile(indexSettingsPath)
if err != nil {
importer.Logger.Fatal(err.Error())
}
db := importer.OpenLevelDB("db")
defer db.Close()
file := importer.OpenFile(common.AC.FileName)
defer file.Close()
decoder := getDecoder(file)
client, err := elastic.NewClient(
elastic.SetURL(common.AC.ElasticSearchHost),
)
if err != nil {
importer.Logger.Fatal("Failed to create Elastic search client with error %s", err)
}
indexVersion := fmt.Sprintf("%s_%d", common.AC.IndexName, time.Now().Unix())
importer.Logger.Info("Creating index with name %s", indexVersion)
_, err = client.CreateIndex(indexVersion).BodyString(string(indexSettings)).Do()
common.AC.ElasticSearchIndexUrl = indexVersion
if err != nil {
importer.Logger.Error(err.Error())
}
importer.Logger.Info("Searching cities, villages, towns and districts")
tags := importer.BuildTags("place~city,place~village,place~suburb,place~town,place~neighbourhood")
CitiesAndTowns, _ = importer.Run(decoder, db, tags)
importer.Logger.Info("Cities, villages, towns and districts found")
file = importer.OpenFile(common.AC.FileName)
defer file.Close()
decoder = getDecoder(file)
importer.Logger.Info("Searching addresses")
tags = importer.BuildTags("addr:street+addr:housenumber,amenity+name,building+name,addr:housenumber,shop+name,office+name,public_transport+name,cuisine+name,railway+name,sport+name,natural+name,tourism+name,leisure+name,historic+name,man_made+name,landuse+name,waterway+name,aerialway+name,aeroway+name,craft+name,military+name")
AddressWays, AddressNodes := importer.Run(decoder, db, tags)
importer.Logger.Info("Addresses found")
importer.JsonWaysToES(AddressWays, CitiesAndTowns, client)
importer.JsonNodesToEs(AddressNodes, CitiesAndTowns, client)
file = importer.OpenFile(common.AC.FileName)
defer file.Close()
decoder = getDecoder(file)
if !common.AC.DontImportIntersections {
tags = importer.BuildTags("highway+name")
Roads, _ = importer.Run(decoder, db, tags)
importer.RoadsToPg(Roads)
importer.Logger.Info("Searching all roads intersecitons")
Intersections := importer.GetRoadIntersectionsFromPG()
importer.JsonNodesToEs(Intersections, CitiesAndTowns, client)
}
importer.Logger.Info("Removing indices from alias")
_, err = client.Alias().Add(indexVersion, common.AC.IndexName).Do()
if err != nil {
return err
}
res, err := client.Aliases().Index("_all").Do()
if err != nil {
return err
}
for _, index := range res.IndicesByAlias(common.AC.IndexName) {
if strings.HasPrefix(index, common.AC.IndexName) && index != indexVersion {
_, err = client.Alias().Remove(index, common.AC.IndexName).Do()
if err != nil {
importer.Logger.Error("Failed to delete index alias: %s", err.Error())
}
_, err = client.DeleteIndex(index).Do()
if err != nil {
importer.Logger.Error("Failed to delete index: %s", err.Error())
}
}
}
return nil
}
func actionUpdate(ctx *cli.Context) error {
err := updater.DownloadOSMFile(common.AC.DownloadUrl, common.AC.FileName)
if err != nil {
importer.Logger.Fatal(err.Error())
}
actionImport(ctx)
return nil
}
func actionTest(ctx *cli.Context) error {
client, err := elastic.NewClient(
elastic.SetURL(common.AC.ElasticSearchHost),
)
if err != nil {
return err
}
currentIndex, err := getCurrentIndexName(client)
if err != nil {
return err
}
fmt.Println(currentIndex)
return nil
}
func actionHttp(ctx *cli.Context) error {
err := web.StartServer()
if err != nil {
return err
}
return nil
}
type CustomData struct {
ID int64 `json:"id"`
Name string `json:"name"`
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
}
type Custom []CustomData
func actionCustom(ctx *cli.Context) error {
var custom Custom
data, err := ioutil.ReadFile(customDataPath)
if err != nil {
importer.Logger.Fatal(err.Error())
}
err = json.Unmarshal(data, &custom)
if err != nil {
importer.Logger.Fatal(err.Error())
}
client, err := elastic.NewClient(
elastic.SetURL(common.AC.ElasticSearchHost),
)
bulkClient := client.Bulk()
indexVersion, err := getCurrentIndexName(client)
if err != nil {
return err
}
for _, item := range custom {
centroid := make(map[string]float64)
centroid["lat"] = item.Lat
centroid["lon"] = item.Lon
marshall := importer.JsonEsIndex{
Name: item.Name,
Centroid: centroid,
Custom: true,
}
index := elastic.NewBulkIndexRequest().
Index(indexVersion).
Type(common.AC.IndexType).
Id(strconv.FormatInt(item.ID, 10)).
Doc(marshall)
bulkClient = bulkClient.Add(index)
}
_, err = bulkClient.Do()
if err != nil {
importer.Logger.Error(err.Error())
}
return nil
}
func actionIntersection(ctx *cli.Context) error {
db := importer.OpenLevelDB("db")
defer db.Close()
file := importer.OpenFile(common.AC.FileName)
defer file.Close()
decoder := getDecoder(file)
client, err := elastic.NewClient(
elastic.SetURL(common.AC.ElasticSearchHost),
)
if err != nil {
importer.Logger.Fatal("Failed to create Elastic search client with error %s", err)
}
indexVersion, err := getCurrentIndexName(client)
common.AC.ElasticSearchIndexUrl = indexVersion
importer.Logger.Info("Creating index with name %s", common.AC.ElasticSearchIndexUrl)
importer.Logger.Info("Searching cities, villages, towns and districts")
tags := importer.BuildTags("place~city,place~village,place~suburb,place~town,place~neighbourhood")
CitiesAndTowns, _ = importer.Run(decoder, db, tags)
importer.Logger.Info("Cities, villages, towns and districts found")
file = importer.OpenFile(common.AC.FileName)
defer file.Close()
decoder = getDecoder(file)
tags = importer.BuildTags("highway+name")
Roads, _ = importer.Run(decoder, db, tags)
importer.BuildIndex(Roads)
Intersections := importer.SearchIntersections(Roads)
importer.JsonNodesToEs(Intersections, CitiesAndTowns, client)
return nil
}