osm2mongo/main.go

62 lines
1.7 KiB
Go
Raw Normal View History

2019-05-26 02:17:25 +03:00
package main
import (
"context"
"flag"
"log"
2019-05-30 14:05:08 +03:00
"strings"
2019-05-26 02:17:25 +03:00
"time"
2019-05-30 14:05:08 +03:00
"github.com/neonxp/rutina"
_ "go.mongodb.org/mongo-driver/bson"
2019-05-26 02:17:25 +03:00
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func main() {
dbconnection := flag.String("dbconnection", "mongodb://localhost:27017", "Mongo database name")
2019-05-28 01:34:40 +03:00
dbname := flag.String("dbname", "map", "Mongo database name")
2019-05-26 02:17:25 +03:00
osmfile := flag.String("osmfile", "", "OSM file")
2019-05-28 01:34:40 +03:00
initial := flag.Bool("initial", false, "Is initial import")
2019-05-30 14:05:08 +03:00
indexes := flag.Bool("indexes", false, "Create indexes")
layersString := flag.String("layers", "nodes,ways,relations", "Layers to import")
2019-05-28 01:34:40 +03:00
blockSize := flag.Int("block", 1000, "Block size to bulk write")
2019-05-30 14:05:08 +03:00
concurrency := flag.Int("concurrency", 32, "Concurrency read and write")
2019-05-26 02:17:25 +03:00
flag.Parse()
2019-05-30 14:05:08 +03:00
layers := strings.Split(*layersString, ",")
r := rutina.New()
2019-05-26 02:17:25 +03:00
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
client, err := mongo.Connect(ctx, options.Client().ApplyURI(*dbconnection))
if err != nil {
log.Fatal(err)
}
defer client.Disconnect(context.Background())
db := client.Database(*dbname)
2019-05-28 17:15:22 +03:00
if *indexes {
if err := createIndexes(db); err != nil {
log.Fatal(err)
}
log.Println("Indexes created")
2019-05-28 17:15:22 +03:00
}
log.Printf("Started import file %s to db %s", *osmfile, *dbname)
2019-05-30 14:05:08 +03:00
nodesCh := make(chan Node, 1)
waysCh := make(chan Way, 1)
relationsCh := make(chan Relation, 1)
for i := 0; i < *concurrency; i++ {
worker := i
r.Go(func(ctx context.Context) error {
return write(ctx, db, nodesCh, waysCh, relationsCh, *initial, *blockSize, worker)
})
2019-05-26 02:17:25 +03:00
}
2019-05-28 01:34:40 +03:00
2019-05-30 14:05:08 +03:00
r.Go(func(ctx context.Context) error {
return read(ctx, *osmfile, nodesCh, waysCh, relationsCh, *concurrency, layers)
})
if err := r.Wait(); err != nil {
log.Fatal(err)
2019-05-28 01:34:40 +03:00
}
}