diff --git a/config/config.go b/config/config.go index 92508f1..bc9ebf5 100644 --- a/config/config.go +++ b/config/config.go @@ -220,6 +220,23 @@ func NewMapping(filename string) (*Mapping, error) { return &mapping, nil } +func Bool(val string, tags map[string]string, elem interface{}) interface{} { + if val == "" || val == "0" || val == "false" || val == "no" { + return false + } + return true +} + +func Direction(val string, tags map[string]string, elem interface{}) interface{} { + if val == "1" || val == "yes" || val == "true" { + return 1 + } else if val == "-1" { + return -1 + } else { + return 0 + } +} + func main() { // data := ` // { diff --git a/db/postgis.go b/db/postgis.go index 92c3a5c..850f45a 100644 --- a/db/postgis.go +++ b/db/postgis.go @@ -22,8 +22,9 @@ type DB interface { } type ColumnSpec struct { - Name string - Type string + Name string + Type string + Value func(string, map[string]string, interface{}) interface{} } type TableSpec struct { Name string @@ -65,7 +66,11 @@ func (spec *TableSpec) WayValues(way element.Way) []interface{} { if !ok { values = append(values, nil) } else { - values = append(values, v) + if col.Value != nil { + values = append(values, col.Value(v, way.Tags, way)) + } else { + values = append(values, v) + } } } return values @@ -216,7 +221,7 @@ func (pg *PostGIS) InsertWays(ways []element.Way, spec TableSpec) error { for _, w := range ways { _, err := stmt.Exec(spec.WayValues(w)...) if err != nil { - return &SQLInsertError{SQLError{sql, err}, w} + return &SQLInsertError{SQLError{sql, err}, spec.WayValues(w)} } } diff --git a/goposm.go b/goposm.go index 9196cc6..1b6ad28 100644 --- a/goposm.go +++ b/goposm.go @@ -248,26 +248,27 @@ func main() { waitFill := sync.WaitGroup{} wayChan := make(chan []element.Way) waitDb := &sync.WaitGroup{} - config := db.Config{ + conf := db.Config{ Type: "postgres", ConnectionParams: *connection, Srid: 3857, Schema: "public", } - pg, err := db.Open(config) + pg, err := db.Open(conf) if err != nil { log.Fatal(err) } specs := []db.TableSpec{ { "goposm_test", - config.Schema, + conf.Schema, []db.ColumnSpec{ - {"name", "VARCHAR"}, - {"highway", "VARCHAR"}, + {"name", "VARCHAR", nil}, + {"highway", "VARCHAR", nil}, + {"oneway", "SMALLINT", config.Direction}, }, "GEOMETRY", - config.Srid, + conf.Srid, }, } err = pg.Init(specs)