fixed parsing of diff command options
parent
4c7fefb52b
commit
04714e266c
|
@ -19,12 +19,12 @@ type Config struct {
|
||||||
const defaultSrid = 3857
|
const defaultSrid = 3857
|
||||||
const defaultCacheDir = "/tmp/goposm"
|
const defaultCacheDir = "/tmp/goposm"
|
||||||
|
|
||||||
var ImportFlags = flag.NewFlagSet("import", flag.ContinueOnError)
|
var ImportFlags = flag.NewFlagSet("import", flag.ExitOnError)
|
||||||
var DiffImportFlags = flag.NewFlagSet("diff", flag.ContinueOnError)
|
var DiffImportFlags = flag.NewFlagSet("diff", flag.ExitOnError)
|
||||||
|
|
||||||
type ImportBaseOptions struct {
|
type ImportBaseOptions struct {
|
||||||
Connection string
|
Connection string
|
||||||
Cachedir string
|
CacheDir string
|
||||||
MappingFile string
|
MappingFile string
|
||||||
Srid int
|
Srid int
|
||||||
LimitTo string
|
LimitTo string
|
||||||
|
@ -57,7 +57,7 @@ var DiffImportOptions = _DiffImportOptions{}
|
||||||
|
|
||||||
func addBaseFlags(flags *flag.FlagSet, baseOptions *ImportBaseOptions) {
|
func addBaseFlags(flags *flag.FlagSet, baseOptions *ImportBaseOptions) {
|
||||||
flags.StringVar(&baseOptions.Connection, "connection", "", "connection parameters")
|
flags.StringVar(&baseOptions.Connection, "connection", "", "connection parameters")
|
||||||
flags.StringVar(&baseOptions.Cachedir, "cachedir", defaultCacheDir, "cache directory")
|
flags.StringVar(&baseOptions.CacheDir, "cachedir", defaultCacheDir, "cache directory")
|
||||||
flags.StringVar(&baseOptions.MappingFile, "mapping", "", "mapping file")
|
flags.StringVar(&baseOptions.MappingFile, "mapping", "", "mapping file")
|
||||||
flags.IntVar(&baseOptions.Srid, "srid", defaultSrid, "srs id")
|
flags.IntVar(&baseOptions.Srid, "srid", defaultSrid, "srs id")
|
||||||
flags.StringVar(&baseOptions.LimitTo, "limitto", "", "limit to geometries")
|
flags.StringVar(&baseOptions.LimitTo, "limitto", "", "limit to geometries")
|
||||||
|
@ -100,60 +100,85 @@ func init() {
|
||||||
// configFile = flag.String("config", "", "config (json)")
|
// configFile = flag.String("config", "", "config (json)")
|
||||||
// )
|
// )
|
||||||
|
|
||||||
func Parse(args []string) (*Config, []error) {
|
func ParseImport(args []string) []error {
|
||||||
err := ImportFlags.Parse(args)
|
err := ImportFlags.Parse(args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
errs := updateBaseOpts(&ImportOptions.Base)
|
||||||
|
if errs != nil {
|
||||||
|
return errs
|
||||||
|
}
|
||||||
|
|
||||||
config := &Config{
|
errs = checkOptions(&ImportOptions.Base)
|
||||||
|
return errs
|
||||||
|
}
|
||||||
|
|
||||||
|
func updateBaseOpts(opts *ImportBaseOptions) []error {
|
||||||
|
|
||||||
|
conf := &Config{
|
||||||
CacheDir: defaultCacheDir,
|
CacheDir: defaultCacheDir,
|
||||||
Srid: defaultSrid,
|
Srid: defaultSrid,
|
||||||
}
|
}
|
||||||
if ImportOptions.Base.ConfigFile != "" {
|
|
||||||
f, err := os.Open(ImportOptions.Base.ConfigFile)
|
if opts.ConfigFile != "" {
|
||||||
|
f, err := os.Open(opts.ConfigFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, []error{err}
|
return []error{err}
|
||||||
}
|
}
|
||||||
decoder := json.NewDecoder(f)
|
decoder := json.NewDecoder(f)
|
||||||
|
|
||||||
err = decoder.Decode(&config)
|
err = decoder.Decode(&conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, []error{err}
|
return []error{err}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ImportOptions.Base.Connection != "" {
|
if opts.Connection == "" {
|
||||||
config.Connection = ImportOptions.Base.Connection
|
opts.Connection = conf.Connection
|
||||||
}
|
}
|
||||||
if config.Srid == 0 {
|
if conf.Srid == 0 {
|
||||||
config.Srid = defaultSrid
|
conf.Srid = defaultSrid
|
||||||
}
|
}
|
||||||
if ImportOptions.Base.Srid != defaultSrid {
|
if opts.Srid != defaultSrid {
|
||||||
config.Srid = ImportOptions.Base.Srid
|
opts.Srid = conf.Srid
|
||||||
}
|
}
|
||||||
if ImportOptions.Base.MappingFile != "" {
|
if opts.MappingFile == "" {
|
||||||
config.MappingFile = ImportOptions.Base.MappingFile
|
opts.MappingFile = conf.MappingFile
|
||||||
}
|
}
|
||||||
if ImportOptions.Base.LimitTo != "" {
|
if opts.LimitTo == "" {
|
||||||
config.LimitTo = ImportOptions.Base.LimitTo
|
opts.LimitTo = conf.LimitTo
|
||||||
}
|
}
|
||||||
if ImportOptions.Base.Cachedir != defaultCacheDir {
|
if opts.CacheDir == defaultCacheDir {
|
||||||
config.CacheDir = ImportOptions.Base.Cachedir
|
opts.CacheDir = conf.CacheDir
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
errs := checkConfig(config)
|
|
||||||
return config, errs
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkConfig(config *Config) []error {
|
func ParseDiffImport(args []string) []error {
|
||||||
|
err := DiffImportFlags.Parse(args)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
errs := updateBaseOpts(&DiffImportOptions.Base)
|
||||||
|
if errs != nil {
|
||||||
|
return errs
|
||||||
|
}
|
||||||
|
|
||||||
|
errs = checkOptions(&DiffImportOptions.Base)
|
||||||
|
|
||||||
|
return errs
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkOptions(opts *ImportBaseOptions) []error {
|
||||||
errs := []error{}
|
errs := []error{}
|
||||||
if config.Srid != 3857 {
|
if opts.Srid != 3857 {
|
||||||
errs = append(errs, errors.New("srid!=3857 not implemented"))
|
errs = append(errs, errors.New("srid!=3857 not implemented"))
|
||||||
}
|
}
|
||||||
if config.MappingFile == "" {
|
if opts.MappingFile == "" {
|
||||||
errs = append(errs, errors.New("missing mapping"))
|
errs = append(errs, errors.New("missing mapping"))
|
||||||
}
|
}
|
||||||
if config.Connection == "" {
|
if opts.Connection == "" {
|
||||||
errs = append(errs, errors.New("missing connection"))
|
errs = append(errs, errors.New("missing connection"))
|
||||||
}
|
}
|
||||||
return errs
|
return errs
|
||||||
|
|
|
@ -21,17 +21,17 @@ import (
|
||||||
|
|
||||||
var log = logging.NewLogger("")
|
var log = logging.NewLogger("")
|
||||||
|
|
||||||
func Update(oscFile string, conf *config.Config, force bool) {
|
func Update(oscFile string, force bool) {
|
||||||
state, err := diffstate.ParseFromOsc(oscFile)
|
state, err := diffstate.ParseFromOsc(oscFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
lastState, err := diffstate.ParseLastState(conf.CacheDir)
|
lastState, err := diffstate.ParseLastState(config.DiffImportOptions.Base.CacheDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if lastState != nil && lastState.Sequence != 0 && state.Sequence <= lastState.Sequence {
|
if lastState != nil && lastState.Sequence != 0 && state != nil && state.Sequence <= lastState.Sequence {
|
||||||
if !force {
|
if !force {
|
||||||
log.Warn(state, " already imported")
|
log.Warn(state, " already imported")
|
||||||
return
|
return
|
||||||
|
@ -42,28 +42,28 @@ func Update(oscFile string, conf *config.Config, force bool) {
|
||||||
|
|
||||||
elems, errc := parser.Parse(oscFile)
|
elems, errc := parser.Parse(oscFile)
|
||||||
|
|
||||||
osmCache := cache.NewOSMCache(conf.CacheDir)
|
osmCache := cache.NewOSMCache(config.DiffImportOptions.Base.CacheDir)
|
||||||
err = osmCache.Open()
|
err = osmCache.Open()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("osm cache: ", err)
|
log.Fatal("osm cache: ", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
diffCache := cache.NewDiffCache(conf.CacheDir)
|
diffCache := cache.NewDiffCache(config.DiffImportOptions.Base.CacheDir)
|
||||||
err = diffCache.Open()
|
err = diffCache.Open()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("diff cache: ", err)
|
log.Fatal("diff cache: ", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
tagmapping, err := mapping.NewMapping(conf.MappingFile)
|
tagmapping, err := mapping.NewMapping(config.DiffImportOptions.Base.MappingFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
connType := database.ConnectionType(conf.Connection)
|
connType := database.ConnectionType(config.DiffImportOptions.Base.Connection)
|
||||||
dbConf := database.Config{
|
dbConf := database.Config{
|
||||||
Type: connType,
|
Type: connType,
|
||||||
ConnectionParams: conf.Connection,
|
ConnectionParams: config.DiffImportOptions.Base.Connection,
|
||||||
Srid: conf.Srid,
|
Srid: config.DiffImportOptions.Base.Srid,
|
||||||
}
|
}
|
||||||
db, err := database.Open(dbConf, tagmapping)
|
db, err := database.Open(dbConf, tagmapping)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -107,19 +107,19 @@ func Update(oscFile string, conf *config.Config, force bool) {
|
||||||
nodes := make(chan *element.Node)
|
nodes := make(chan *element.Node)
|
||||||
|
|
||||||
relWriter := writer.NewRelationWriter(osmCache, diffCache, relations,
|
relWriter := writer.NewRelationWriter(osmCache, diffCache, relations,
|
||||||
db, polygonsTagMatcher, progress, conf.Srid)
|
db, polygonsTagMatcher, progress, config.DiffImportOptions.Base.Srid)
|
||||||
relWriter.SetClipper(geometryClipper)
|
relWriter.SetClipper(geometryClipper)
|
||||||
relWriter.SetExpireTiles(expiredTiles)
|
relWriter.SetExpireTiles(expiredTiles)
|
||||||
relWriter.Start()
|
relWriter.Start()
|
||||||
|
|
||||||
wayWriter := writer.NewWayWriter(osmCache, diffCache, ways, db,
|
wayWriter := writer.NewWayWriter(osmCache, diffCache, ways, db,
|
||||||
lineStringsTagMatcher, polygonsTagMatcher, progress, conf.Srid)
|
lineStringsTagMatcher, polygonsTagMatcher, progress, config.DiffImportOptions.Base.Srid)
|
||||||
wayWriter.SetClipper(geometryClipper)
|
wayWriter.SetClipper(geometryClipper)
|
||||||
wayWriter.SetExpireTiles(expiredTiles)
|
wayWriter.SetExpireTiles(expiredTiles)
|
||||||
wayWriter.Start()
|
wayWriter.Start()
|
||||||
|
|
||||||
nodeWriter := writer.NewNodeWriter(osmCache, nodes, db,
|
nodeWriter := writer.NewNodeWriter(osmCache, nodes, db,
|
||||||
pointsTagMatcher, progress, conf.Srid)
|
pointsTagMatcher, progress, config.DiffImportOptions.Base.Srid)
|
||||||
nodeWriter.SetClipper(geometryClipper)
|
nodeWriter.SetClipper(geometryClipper)
|
||||||
nodeWriter.Start()
|
nodeWriter.Start()
|
||||||
|
|
||||||
|
@ -277,7 +277,7 @@ For:
|
||||||
log.StopStep(step)
|
log.StopStep(step)
|
||||||
progress.Stop()
|
progress.Stop()
|
||||||
|
|
||||||
err = diffstate.WriteLastState(conf.CacheDir, state)
|
err = diffstate.WriteLastState(config.DiffImportOptions.Base.CacheDir, state)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warn(err) // warn only
|
log.Warn(err) // warn only
|
||||||
}
|
}
|
||||||
|
|
61
goposm.go
61
goposm.go
|
@ -33,6 +33,15 @@ func dief(msg string, args ...interface{}) {
|
||||||
log.Fatalf(msg, args...)
|
log.Fatalf(msg, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func reportErrors(errs []error) {
|
||||||
|
log.Warn("errors in config/options:")
|
||||||
|
for _, err := range errs {
|
||||||
|
log.Warnf("\t%s", err)
|
||||||
|
}
|
||||||
|
logging.Shutdown()
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
golog.SetFlags(golog.LstdFlags | golog.Lshortfile)
|
golog.SetFlags(golog.LstdFlags | golog.Lshortfile)
|
||||||
|
@ -44,22 +53,22 @@ func main() {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
conf, errs := config.Parse(os.Args[2:])
|
|
||||||
if len(errs) > 0 {
|
|
||||||
log.Warn("errors in config/options:")
|
|
||||||
for _, err := range errs {
|
|
||||||
log.Warnf("\t%s", err)
|
|
||||||
}
|
|
||||||
logging.Shutdown()
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
switch os.Args[1] {
|
switch os.Args[1] {
|
||||||
case "import":
|
case "import":
|
||||||
mainimport(conf)
|
errs := config.ParseImport(os.Args[2:])
|
||||||
|
if len(errs) > 0 {
|
||||||
|
reportErrors(errs)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
mainimport()
|
||||||
case "diff":
|
case "diff":
|
||||||
|
errs := config.ParseDiffImport(os.Args[2:])
|
||||||
|
if len(errs) > 0 {
|
||||||
|
reportErrors(errs)
|
||||||
|
break
|
||||||
|
}
|
||||||
for _, oscFile := range config.DiffImportFlags.Args() {
|
for _, oscFile := range config.DiffImportFlags.Args() {
|
||||||
cmd.Update(oscFile, conf, false)
|
cmd.Update(oscFile, false)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
log.Fatal("invalid command")
|
log.Fatal("invalid command")
|
||||||
|
@ -69,7 +78,7 @@ func main() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func mainimport(conf *config.Config) {
|
func mainimport() {
|
||||||
if config.ImportOptions.Cpuprofile != "" {
|
if config.ImportOptions.Cpuprofile != "" {
|
||||||
f, err := os.Create(config.ImportOptions.Cpuprofile)
|
f, err := os.Create(config.ImportOptions.Cpuprofile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -113,21 +122,21 @@ func mainimport(conf *config.Config) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var geometryClipper *clipper.Clipper
|
var geometryClipper *clipper.Clipper
|
||||||
if config.ImportOptions.Write && conf.LimitTo != "" {
|
if config.ImportOptions.Write && config.ImportOptions.Base.LimitTo != "" {
|
||||||
var err error
|
var err error
|
||||||
step := log.StartStep("Reading limitto geometries")
|
step := log.StartStep("Reading limitto geometries")
|
||||||
geometryClipper, err = clipper.NewFromOgrSource(conf.LimitTo)
|
geometryClipper, err = clipper.NewFromOgrSource(config.ImportOptions.Base.LimitTo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
die(err)
|
die(err)
|
||||||
}
|
}
|
||||||
log.StopStep(step)
|
log.StopStep(step)
|
||||||
}
|
}
|
||||||
|
|
||||||
osmCache := cache.NewOSMCache(conf.CacheDir)
|
osmCache := cache.NewOSMCache(config.ImportOptions.Base.CacheDir)
|
||||||
|
|
||||||
if config.ImportOptions.Read != "" && osmCache.Exists() {
|
if config.ImportOptions.Read != "" && osmCache.Exists() {
|
||||||
if config.ImportOptions.Overwritecache {
|
if config.ImportOptions.Overwritecache {
|
||||||
log.Printf("removing existing cache %s", conf.CacheDir)
|
log.Printf("removing existing cache %s", config.ImportOptions.Base.CacheDir)
|
||||||
err := osmCache.Remove()
|
err := osmCache.Remove()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
die("unable to remove cache:", err)
|
die("unable to remove cache:", err)
|
||||||
|
@ -139,7 +148,7 @@ func mainimport(conf *config.Config) {
|
||||||
|
|
||||||
progress := stats.StatsReporter()
|
progress := stats.StatsReporter()
|
||||||
|
|
||||||
tagmapping, err := mapping.NewMapping(conf.MappingFile)
|
tagmapping, err := mapping.NewMapping(config.ImportOptions.Base.MappingFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
die("mapping file: ", err)
|
die("mapping file: ", err)
|
||||||
}
|
}
|
||||||
|
@ -147,11 +156,11 @@ func mainimport(conf *config.Config) {
|
||||||
var db database.DB
|
var db database.DB
|
||||||
|
|
||||||
if config.ImportOptions.Write || config.ImportOptions.DeployProduction || config.ImportOptions.RevertDeploy || config.ImportOptions.RemoveBackup || config.ImportOptions.Optimize {
|
if config.ImportOptions.Write || config.ImportOptions.DeployProduction || config.ImportOptions.RevertDeploy || config.ImportOptions.RemoveBackup || config.ImportOptions.Optimize {
|
||||||
connType := database.ConnectionType(conf.Connection)
|
connType := database.ConnectionType(config.ImportOptions.Base.Connection)
|
||||||
conf := database.Config{
|
conf := database.Config{
|
||||||
Type: connType,
|
Type: connType,
|
||||||
ConnectionParams: conf.Connection,
|
ConnectionParams: config.ImportOptions.Base.Connection,
|
||||||
Srid: conf.Srid,
|
Srid: config.ImportOptions.Base.Srid,
|
||||||
}
|
}
|
||||||
db, err = database.Open(conf, tagmapping)
|
db, err = database.Open(conf, tagmapping)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -183,7 +192,7 @@ func mainimport(conf *config.Config) {
|
||||||
if config.ImportOptions.Diff {
|
if config.ImportOptions.Diff {
|
||||||
diffstate := state.FromPbf(pbfFile)
|
diffstate := state.FromPbf(pbfFile)
|
||||||
if diffstate != nil {
|
if diffstate != nil {
|
||||||
diffstate.WriteToFile(path.Join(conf.CacheDir, "last.state.txt"))
|
diffstate.WriteToFile(path.Join(config.ImportOptions.Base.CacheDir, "last.state.txt"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -209,7 +218,7 @@ func mainimport(conf *config.Config) {
|
||||||
|
|
||||||
var diffCache *cache.DiffCache
|
var diffCache *cache.DiffCache
|
||||||
if config.ImportOptions.Diff {
|
if config.ImportOptions.Diff {
|
||||||
diffCache = cache.NewDiffCache(conf.CacheDir)
|
diffCache = cache.NewDiffCache(config.ImportOptions.Base.CacheDir)
|
||||||
if err = diffCache.Remove(); err != nil {
|
if err = diffCache.Remove(); err != nil {
|
||||||
die(err)
|
die(err)
|
||||||
}
|
}
|
||||||
|
@ -229,7 +238,7 @@ func mainimport(conf *config.Config) {
|
||||||
|
|
||||||
relations := osmCache.Relations.Iter()
|
relations := osmCache.Relations.Iter()
|
||||||
relWriter := writer.NewRelationWriter(osmCache, diffCache, relations,
|
relWriter := writer.NewRelationWriter(osmCache, diffCache, relations,
|
||||||
db, polygonsTagMatcher, progress, conf.Srid)
|
db, polygonsTagMatcher, progress, config.ImportOptions.Base.Srid)
|
||||||
relWriter.SetClipper(geometryClipper)
|
relWriter.SetClipper(geometryClipper)
|
||||||
relWriter.Start()
|
relWriter.Start()
|
||||||
|
|
||||||
|
@ -239,7 +248,7 @@ func mainimport(conf *config.Config) {
|
||||||
|
|
||||||
ways := osmCache.Ways.Iter()
|
ways := osmCache.Ways.Iter()
|
||||||
wayWriter := writer.NewWayWriter(osmCache, diffCache, ways, db,
|
wayWriter := writer.NewWayWriter(osmCache, diffCache, ways, db,
|
||||||
lineStringsTagMatcher, polygonsTagMatcher, progress, conf.Srid)
|
lineStringsTagMatcher, polygonsTagMatcher, progress, config.ImportOptions.Base.Srid)
|
||||||
wayWriter.SetClipper(geometryClipper)
|
wayWriter.SetClipper(geometryClipper)
|
||||||
wayWriter.Start()
|
wayWriter.Start()
|
||||||
|
|
||||||
|
@ -249,7 +258,7 @@ func mainimport(conf *config.Config) {
|
||||||
|
|
||||||
nodes := osmCache.Nodes.Iter()
|
nodes := osmCache.Nodes.Iter()
|
||||||
nodeWriter := writer.NewNodeWriter(osmCache, nodes, db,
|
nodeWriter := writer.NewNodeWriter(osmCache, nodes, db,
|
||||||
pointsTagMatcher, progress, conf.Srid)
|
pointsTagMatcher, progress, config.ImportOptions.Base.Srid)
|
||||||
nodeWriter.SetClipper(geometryClipper)
|
nodeWriter.SetClipper(geometryClipper)
|
||||||
nodeWriter.Start()
|
nodeWriter.Start()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue