diff options
author | Alexander NeonXP Kiryukhin <a.kiryukhin@mail.ru> | 2019-05-30 14:05:08 +0300 |
---|---|---|
committer | Alexander NeonXP Kiryukhin <a.kiryukhin@mail.ru> | 2019-05-30 14:05:08 +0300 |
commit | 2f4b6e90597784a8a7c01027e0ff5c6b69634a96 (patch) | |
tree | 5d94daf9fc394425b03baf3915ffd03a0011e442 /reader.go | |
parent | 97fd40df4d85d91f3414dc88aa96222c1d827b49 (diff) |
Parallel write
Speedup
Diffstat (limited to 'reader.go')
-rw-r--r-- | reader.go | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/reader.go b/reader.go new file mode 100644 index 0000000..5ab603c --- /dev/null +++ b/reader.go @@ -0,0 +1,114 @@ +package main + +import ( + "context" + "log" + "os" + + "github.com/paulmach/osm" + "github.com/paulmach/osm/osmpbf" +) + +func read(ctx context.Context, file string, nodesCh chan Node, waysCh chan Way, relationsCh chan Relation, concurrency int, layers []string) error { + f, err := os.Open(file) + if err != nil { + return err + } + defer f.Close() + scanner := osmpbf.New(context.Background(), f, concurrency) + defer scanner.Close() + + layersToImport := map[string]bool{ + "ways": false, + "nodes": false, + "relations": false, + } + + for _, l := range layers { + layersToImport[l] = true + } + + for scanner.Scan() { + if ctx.Err() != nil { + return ctx.Err() + } + o := scanner.Object() + switch o := o.(type) { + case *osm.Way: + if !layersToImport["ways"] { + continue + } + nodes := make([]int64, 0, len(o.Nodes)) + for _, v := range o.Nodes { + nodes = append(nodes, int64(v.ID)) + } + + w := Way{ + OsmID: int64(o.ID), + Tags: convertTags(o.Tags), + Nodes: nodes, + Timestamp: o.Timestamp, + Version: o.Version, + Visible: o.Visible, + } + waysCh <- w + case *osm.Node: + if !layersToImport["nodes"] { + continue + } + n := Node{ + OsmID: int64(o.ID), + Location: Coords{ + Type: "Point", + Coordinates: []float64{ + o.Lon, + o.Lat, + }}, + Tags: convertTags(o.Tags), + Version: o.Version, + Timestamp: o.Timestamp, + Visible: o.Visible, + } + nodesCh <- n + case *osm.Relation: + if !layersToImport["relations"] { + continue + } + members := make([]Member, 0, len(o.Members)) + for _, v := range o.Members { + var location *Coords + if v.Lat != 0.0 && v.Lon != 0.0 { + location = &Coords{ + Type: "Point", + Coordinates: []float64{ + v.Lon, + v.Lat, + }} + } + members = append(members, Member{ + Type: v.Type, + Version: v.Version, + Orientation: v.Orientation, + Ref: v.Ref, + Role: v.Role, + Location: location, + }) + } + r := Relation{ + OsmID: int64(o.ID), + Tags: convertTags(o.Tags), + Version: o.Version, + Timestamp: o.Timestamp, + Visible: o.Visible, + Members: members, + } + relationsCh <- r + } + } + log.Println("Read done") + scanErr := scanner.Err() + if scanErr != nil { + return scanErr + } + return nil +} |