forked from pelias/pbf2json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpbf2json.go
405 lines (312 loc) · 9.16 KB
/
pbf2json.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
401
402
403
404
405
package main
import "encoding/json"
import "fmt"
import "flag"
import "bytes"
import "os"
import "log"
import "io"
import "time"
import "runtime"
import "strings"
import "strconv"
import "github.com/qedus/osmpbf"
import "github.com/syndtr/goleveldb/leveldb"
import "github.com/paulmach/go.geo"
import "math"
type Settings struct {
PbfPath string
LevedbPath string
Tags map[string][]string
BatchSize int
}
func getSettings() Settings {
// command line flags
leveldbPath := flag.String("leveldb", "/tmp", "path to leveldb directory")
tagList := flag.String("tags", "", "comma-separated list of valid tags, group AND conditions with a +")
batchSize := flag.Int("batch", 50000, "batch leveldb writes in batches of this size")
flag.Parse()
args := flag.Args();
if len( args ) < 1 {
log.Fatal("invalid args, you must specify a PBF file")
}
// invalid tags
if( len(*tagList) < 1 ){
log.Fatal("Nothing to do, you must specify tags to match against")
}
// parse tag conditions
conditions := make(map[string][]string)
for _, group := range strings.Split(*tagList,",") {
conditions[group] = strings.Split(group,"+")
}
// fmt.Print(conditions, len(conditions))
// os.Exit(1)
return Settings{ args[0], *leveldbPath, conditions, *batchSize }
}
func main() {
// configuration
config := getSettings()
// open pbf file
file := openFile(config.PbfPath)
defer file.Close()
decoder := osmpbf.NewDecoder(file)
err := decoder.Start(runtime.GOMAXPROCS(-1)) // use several goroutines for faster decoding
if err != nil {
log.Fatal(err)
}
db := openLevelDB(config.LevedbPath)
defer db.Close()
run(decoder, db, config)
}
func run(d *osmpbf.Decoder, db *leveldb.DB, config Settings){
batch := new(leveldb.Batch)
var nc, wc, rc uint64
for {
if v, err := d.Decode(); err == io.EOF {
break
} else if err != nil {
log.Fatal(err)
} else {
switch v := v.(type) {
case *osmpbf.Node:
// inc count
nc++
// ----------------
// write to leveldb
// ----------------
// write immediately
// cacheStore(db, v)
// write in batches
cacheQueue(batch, v)
if batch.Len() > config.BatchSize {
cacheFlush(db, batch)
}
// ----------------
// handle tags
// ----------------
if !hasTags(v.Tags) { break }
v.Tags = trimTags(v.Tags)
if containsValidTags( v.Tags, config.Tags ) {
onNode(v)
}
case *osmpbf.Way:
// ----------------
// write to leveldb
// ----------------
// flush outstanding batches
if batch.Len() > 1 {
cacheFlush(db, batch)
}
// inc count
wc++
if !hasTags(v.Tags) { break }
v.Tags = trimTags(v.Tags)
if containsValidTags( v.Tags, config.Tags ) {
// lookup from leveldb
latlons, err := cacheLookup(db, v)
// skip ways which fail to denormalize
if err != nil { break }
// compute centroid
var centroid = computeCentroid(latlons);
onWay(v,latlons,centroid)
}
case *osmpbf.Relation:
// inc count
rc++
onRelation(v)
default:
log.Fatalf("unknown type %T\n", v)
}
}
}
// fmt.Printf("Nodes: %d, Ways: %d, Relations: %d\n", nc, wc, rc)
}
type JsonNode struct {
ID int64 `json:"id"`
Type string `json:"type"`
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
Tags map[string]string `json:"tags"`
Timestamp time.Time `json:"timestamp"`
}
func onNode(node *osmpbf.Node){
marshall := JsonNode{ node.ID, "node", node.Lat, node.Lon, node.Tags, node.Timestamp }
json, _ := json.Marshal(marshall)
fmt.Println(string(json))
}
type JsonWay struct {
ID int64 `json:"id"`
Type string `json:"type"`
Tags map[string]string `json:"tags"`
// NodeIDs []int64 `json:"refs"`
Centroid map[string]string `json:"centroid"`
Nodes []map[string]string `json:"nodes"`
Timestamp time.Time `json:"timestamp"`
}
func onWay(way *osmpbf.Way, latlons []map[string]string, centroid map[string]string){
marshall := JsonWay{ way.ID, "way", way.Tags/*, way.NodeIDs*/, centroid, latlons, way.Timestamp }
json, _ := json.Marshal(marshall)
fmt.Println(string(json))
}
func onRelation(relation *osmpbf.Relation){
// do nothing (yet)
}
// write to leveldb immediately
func cacheStore(db *leveldb.DB, node *osmpbf.Node){
id, val := formatLevelDB(node)
err := db.Put([]byte(id), []byte(val), nil)
if err != nil {
log.Fatal(err)
}
}
// queue a leveldb write in a batch
func cacheQueue(batch *leveldb.Batch, node *osmpbf.Node){
id, val := formatLevelDB(node)
batch.Put([]byte(id), []byte(val))
}
// flush a leveldb batch to database and reset batch to 0
func cacheFlush(db *leveldb.DB, batch *leveldb.Batch){
err := db.Write(batch, nil)
if err != nil {
log.Fatal(err)
}
batch.Reset()
}
func cacheLookup(db *leveldb.DB, way *osmpbf.Way) ([]map[string]string, error) {
var container []map[string]string
for _, each := range way.NodeIDs {
stringid := strconv.FormatInt(each,10)
data, err := db.Get([]byte(stringid), nil)
if err != nil {
log.Println("denormalize failed for way:", way.ID, "node not found:", stringid)
return container, err
}
s := string(data)
spl := strings.Split(s, ":");
latlon := make(map[string]string)
lat, lon := spl[0], spl[1]
latlon["lat"] = lat
latlon["lon"] = lon
container = append(container, latlon)
}
return container, nil
// fmt.Println(way.NodeIDs)
// fmt.Println(container)
// os.Exit(1)
}
func formatLevelDB(node *osmpbf.Node) (id string, val []byte){
stringid := strconv.FormatInt(node.ID,10)
var bufval bytes.Buffer
bufval.WriteString(strconv.FormatFloat(node.Lat,'f',6,64))
bufval.WriteString(":")
bufval.WriteString(strconv.FormatFloat(node.Lon,'f',6,64))
byteval := []byte(bufval.String())
return stringid, byteval
}
func openFile(filename string) *os.File {
// no file specified
if len(filename) < 1 {
log.Fatal("invalid file: you must specify a pbf path as arg[1]")
}
// try to open the file
file, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
return file
}
func openLevelDB(path string) *leveldb.DB {
// try to open the db
db, err := leveldb.OpenFile(path, nil)
if err != nil {
log.Fatal(err)
}
return db
}
// extract all keys to array
// keys := []string{}
// for k := range v.Tags {
// keys = append(keys, k)
// }
// check tags contain features from a whitelist
func matchTagsAgainstCompulsoryTagList(tags map[string]string, tagList []string) bool {
for _, name := range tagList {
feature := strings.Split(name,"~")
foundVal, foundKey := tags[feature[0]]
// key check
if !foundKey {
return false
}
// value check
if len( feature ) > 1 {
if foundVal != feature[1] {
return false
}
}
}
return true
}
// check tags contain features from a groups of whitelists
func containsValidTags(tags map[string]string, group map[string][]string) bool {
for _, list := range group {
if matchTagsAgainstCompulsoryTagList( tags, list ){
return true
}
}
return false
}
// trim leading/trailing spaces from keys and values
func trimTags(tags map[string]string) map[string]string {
trimmed := make(map[string]string)
for k, v := range tags {
trimmed[strings.TrimSpace(k)] = strings.TrimSpace(v);
}
return trimmed
}
// check if a tag list is empty or not
func hasTags(tags map[string]string) bool {
n := len(tags)
if n == 0 {
return false
}
return true
}
// compute the centroid of a way
func computeCentroid(latlons []map[string]string) map[string]string {
points := geo.PointSet{}
for _, each := range latlons {
var lon, _ = strconv.ParseFloat( each["lon"], 64 );
var lat, _ = strconv.ParseFloat( each["lat"], 64 );
points.Push( geo.NewPoint( lon, lat ))
}
var compute = getCentroid(points);
var centroid = make(map[string]string)
centroid["lat"] = strconv.FormatFloat(compute.Lat(),'f',6,64)
centroid["lon"] = strconv.FormatFloat(compute.Lng(),'f',6,64)
return centroid
}
// compute the centroid of a polygon set
// using a spherical co-ordinate system
func getCentroid(ps geo.PointSet) *geo.Point {
X := 0.0
Y := 0.0
Z := 0.0
var toRad = math.Pi / 180
var fromRad = 180 / math.Pi
for _, point := range ps {
var lon = point[0] * toRad
var lat = point[1] * toRad
X += math.Cos(lat) * math.Cos(lon)
Y += math.Cos(lat) * math.Sin(lon)
Z += math.Sin(lat)
}
numPoints := float64(len(ps))
X = X / numPoints
Y = Y / numPoints
Z = Z / numPoints
var lon = math.Atan2(Y, X)
var hyp = math.Sqrt(X * X + Y * Y)
var lat = math.Atan2(Z, hyp)
var centroid = geo.NewPoint(lon * fromRad, lat * fromRad)
return centroid;
}