osm2mongo/main.go

61 lines
1.7 KiB
Go
Raw Permalink 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")
osmfile := flag.String("osmfile", "./RU.osm.pbf", "Path to OSM file (PBF format only)")
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")
concurrency := flag.Int("concurrency", 32, "Workers count")
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 {
log.Println("Creating indexes...")
if err := createIndexes(db); err != nil {
log.Fatal(err)
}
log.Println("Done!")
2019-05-28 17:15:22 +03:00
}
log.Printf("Started import file %s to db %s (%d workers)", *osmfile, *dbname, *concurrency)
insertCh := make(chan Object, 1)
2019-05-30 14:05:08 +03:00
for i := 0; i < *concurrency; i++ {
worker := i
r.Go(func(ctx context.Context) error {
return write(ctx, db, insertCh, *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, insertCh, *concurrency, layers)
2019-05-30 14:05:08 +03:00
})
if err := r.Wait(); err != nil {
log.Fatal(err)
2019-05-28 01:34:40 +03:00
}
}