/
pbf_counter.go
54 lines (47 loc) · 1.16 KB
/
pbf_counter.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
package osmpbfparser
import (
"github.com/thomersch/gosmparse"
"os"
"sync"
)
// PBFCounter count the number of each type data element and return the number.
type PBFCounter struct {
PBFFile string
NodeCount int
WayCount int
RelationCount int
nodeMux sync.Mutex
wayMux sync.Mutex
relationMux sync.Mutex
}
// Run exec func for PBFCounter.
func (p *PBFCounter) Run() (int, int, int, error) {
reader, err := os.Open(p.PBFFile)
if err != nil {
return 0, 0, 0, err
}
defer reader.Close()
decoder := gosmparse.NewDecoder(reader)
if err := decoder.Parse(p); err != nil {
return 0, 0, 0, err
}
return p.NodeCount, p.WayCount, p.RelationCount, nil
}
// ReadNode for gosmparse OSMReader interface.
func (p *PBFCounter) ReadNode(node gosmparse.Node) {
p.nodeMux.Lock()
p.NodeCount++
p.nodeMux.Unlock()
}
// ReadWay for gosmparse OSMReader interface.
func (p *PBFCounter) ReadWay(way gosmparse.Way) {
p.wayMux.Lock()
p.WayCount++
p.wayMux.Unlock()
}
// ReadRelation for gosmparse OSMReader interface.
func (p *PBFCounter) ReadRelation(relation gosmparse.Relation) {
p.relationMux.Lock()
p.RelationCount++
p.relationMux.Unlock()
}