This repository has been archived by the owner on Jan 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
osmpbf.go
executable file
·83 lines (72 loc) · 1.95 KB
/
osmpbf.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
package main
import "encoding/json"
import "fmt"
import "os"
import "log"
import "io"
import "runtime"
import "github.com/qedus/osmpbf"
// import "time"
func main() {
f, err := os.Open(os.Args[1])
if err != nil {
log.Fatal(err)
}
defer f.Close()
d := osmpbf.NewDecoder(f)
err = d.Start(runtime.GOMAXPROCS(-1)) // use several goroutines for faster decoding
if err != nil {
log.Fatal(err)
}
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:
onNode(v)
nc++
case *osmpbf.Way:
onWay(v)
wc++
case *osmpbf.Relation:
onRelation(v)
rc++
default:
log.Fatalf("unknown type %T\n", v)
}
}
}
// fmt.Printf("Nodes: %d, Ways: %d, Relations: %d\n", nc, wc, rc)
}
type JsonNode struct {
Type string `json:"type"`
ID int64 `json:"id"`
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", node.ID, node.Lat, node.Lon, node.Tags/*, node.Timestamp*/ }
json, _ := json.Marshal(marshall)
fmt.Println(string(json))
}
type JsonWay struct {
Type string `json:"type"`
ID int64 `json:"id"`
Tags map[string]string `json:"tags"`
NodeIDs []int64 `json:"refs"`
// Timestamp time.Time `json:"timestamp"`
}
func onWay(way *osmpbf.Way){
marshall := JsonWay{ "way", way.ID, way.Tags, way.NodeIDs/*, way.Timestamp*/ }
json, _ := json.Marshal(marshall)
fmt.Println(string(json))
}
func onRelation(relation *osmpbf.Relation){
// do nothing (yet)
}