2013-05-14 12:04:11 +04:00
|
|
|
package mapping
|
2013-05-13 15:58:44 +04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2013-05-29 10:47:26 +04:00
|
|
|
"errors"
|
2013-08-29 17:44:15 +04:00
|
|
|
"imposm3/element"
|
2013-05-13 15:58:44 +04:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Field struct {
|
2013-05-21 16:39:24 +04:00
|
|
|
Name string `json:"name"`
|
|
|
|
Key string `json:"key"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Args map[string]interface{} `json:"args"`
|
2013-05-13 15:58:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Table struct {
|
2013-05-27 13:24:22 +04:00
|
|
|
Name string
|
|
|
|
Type string `json:"type"`
|
|
|
|
Mapping map[string][]string `json:"mapping"`
|
|
|
|
Mappings map[string]SubMapping `json:"mappings"`
|
|
|
|
Fields []*Field `json:"fields"`
|
|
|
|
Filters *Filters `json:"filters"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type SubMapping struct {
|
|
|
|
Mapping map[string][]string
|
2013-05-17 17:07:03 +04:00
|
|
|
}
|
|
|
|
|
2013-05-22 13:48:34 +04:00
|
|
|
type GeneralizedTable struct {
|
|
|
|
Name string
|
|
|
|
SourceTableName string `json:"source"`
|
|
|
|
Tolerance float64 `json:"tolerance"`
|
|
|
|
SqlFilter string `json:"sql_filter"`
|
|
|
|
}
|
|
|
|
|
2013-05-17 17:07:03 +04:00
|
|
|
type Filters struct {
|
2013-05-27 13:24:22 +04:00
|
|
|
ExcludeTags *[][2]string `json:"exclude_tags"`
|
2013-05-13 15:58:44 +04:00
|
|
|
}
|
|
|
|
|
2013-05-14 18:15:35 +04:00
|
|
|
type Tables map[string]*Table
|
2013-05-13 15:58:44 +04:00
|
|
|
|
2013-05-22 13:48:34 +04:00
|
|
|
type GeneralizedTables map[string]*GeneralizedTable
|
|
|
|
|
2013-05-13 15:58:44 +04:00
|
|
|
type Mapping struct {
|
2013-05-22 13:48:34 +04:00
|
|
|
Tables Tables `json:"tables"`
|
|
|
|
GeneralizedTables GeneralizedTables `json:"generalized_tables"`
|
2013-05-13 15:58:44 +04:00
|
|
|
}
|
|
|
|
|
2013-05-23 15:09:47 +04:00
|
|
|
type ElementFilter func(tags *element.Tags) bool
|
2013-05-17 17:44:50 +04:00
|
|
|
|
2013-05-27 13:24:22 +04:00
|
|
|
type TagTables map[string]map[string][]DestTable
|
|
|
|
|
|
|
|
type DestTable struct {
|
|
|
|
Name string
|
|
|
|
SubMapping string
|
|
|
|
}
|
2013-05-17 17:44:50 +04:00
|
|
|
|
|
|
|
func NewMapping(filename string) (*Mapping, error) {
|
|
|
|
f, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
decoder := json.NewDecoder(f)
|
|
|
|
|
|
|
|
mapping := Mapping{}
|
|
|
|
err = decoder.Decode(&mapping)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2013-05-29 10:47:26 +04:00
|
|
|
err = mapping.prepare()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-05-17 17:44:50 +04:00
|
|
|
return &mapping, nil
|
2013-05-13 15:58:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Table) ExtraTags() map[string]bool {
|
|
|
|
tags := make(map[string]bool)
|
|
|
|
for _, field := range t.Fields {
|
2013-05-15 10:15:33 +04:00
|
|
|
if field.Key != "" {
|
|
|
|
tags[field.Key] = true
|
|
|
|
}
|
2013-05-13 15:58:44 +04:00
|
|
|
}
|
|
|
|
return tags
|
|
|
|
}
|
|
|
|
|
2013-05-29 10:47:26 +04:00
|
|
|
func (m *Mapping) prepare() error {
|
2013-05-14 18:15:35 +04:00
|
|
|
for name, t := range m.Tables {
|
|
|
|
t.Name = name
|
2013-05-13 15:58:44 +04:00
|
|
|
}
|
2013-05-29 10:47:26 +04:00
|
|
|
for name, t := range m.Tables {
|
|
|
|
switch t.Type {
|
|
|
|
case "":
|
|
|
|
return errors.New("missing table type for table " + name)
|
|
|
|
case "point":
|
|
|
|
case "linestring":
|
|
|
|
case "polygon":
|
|
|
|
default:
|
|
|
|
return errors.New("unknown type " + t.Type + " for table " + name)
|
|
|
|
}
|
|
|
|
}
|
2013-05-22 13:48:34 +04:00
|
|
|
for name, t := range m.GeneralizedTables {
|
|
|
|
t.Name = name
|
|
|
|
}
|
2013-05-29 10:47:26 +04:00
|
|
|
return nil
|
2013-05-13 15:58:44 +04:00
|
|
|
}
|
|
|
|
|
2013-05-27 13:24:22 +04:00
|
|
|
func (tt TagTables) addFromMapping(mapping map[string][]string, table DestTable) {
|
|
|
|
for key, vals := range mapping {
|
|
|
|
for _, v := range vals {
|
|
|
|
vals, ok := tt[key]
|
|
|
|
if ok {
|
|
|
|
vals[v] = append(vals[v], table)
|
|
|
|
} else {
|
|
|
|
tt[key] = make(map[string][]DestTable)
|
|
|
|
tt[key][v] = append(tt[key][v], table)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-17 17:44:50 +04:00
|
|
|
func (m *Mapping) mappings(tableType string, mappings TagTables) {
|
2013-05-13 15:58:44 +04:00
|
|
|
for name, t := range m.Tables {
|
|
|
|
if t.Type != tableType {
|
|
|
|
continue
|
|
|
|
}
|
2013-05-27 13:24:22 +04:00
|
|
|
mappings.addFromMapping(t.Mapping, DestTable{name, ""})
|
|
|
|
|
|
|
|
for subMappingName, subMapping := range t.Mappings {
|
|
|
|
mappings.addFromMapping(subMapping.Mapping, DestTable{name, subMappingName})
|
2013-05-13 15:58:44 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-14 18:15:35 +04:00
|
|
|
func (m *Mapping) tables(tableType string) map[string]*TableFields {
|
|
|
|
result := make(map[string]*TableFields)
|
|
|
|
for name, t := range m.Tables {
|
|
|
|
if t.Type == tableType {
|
|
|
|
result[name] = t.TableFields()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2013-05-13 15:58:44 +04:00
|
|
|
func (m *Mapping) extraTags(tableType string, tags map[string]bool) {
|
|
|
|
for _, t := range m.Tables {
|
|
|
|
if t.Type != tableType {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for key, _ := range t.ExtraTags() {
|
|
|
|
tags[key] = true
|
|
|
|
}
|
2013-05-17 17:07:03 +04:00
|
|
|
if t.Filters != nil && t.Filters.ExcludeTags != nil {
|
2013-05-27 13:24:22 +04:00
|
|
|
for _, keyVal := range *t.Filters.ExcludeTags {
|
|
|
|
tags[keyVal[0]] = true
|
2013-05-17 17:07:03 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-17 17:44:50 +04:00
|
|
|
func (m *Mapping) ElementFilters() map[string][]ElementFilter {
|
|
|
|
result := make(map[string][]ElementFilter)
|
2013-05-17 17:07:03 +04:00
|
|
|
for name, t := range m.Tables {
|
|
|
|
if t.Filters == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if t.Filters.ExcludeTags != nil {
|
2013-05-27 13:24:22 +04:00
|
|
|
for _, filterKeyVal := range *t.Filters.ExcludeTags {
|
2013-05-23 15:09:47 +04:00
|
|
|
f := func(tags *element.Tags) bool {
|
2013-05-27 13:24:22 +04:00
|
|
|
if v, ok := (*tags)[filterKeyVal[0]]; ok {
|
|
|
|
if filterKeyVal[1] == "__any__" || v == filterKeyVal[1] {
|
2013-05-17 17:07:03 +04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
result[name] = append(result[name], f)
|
|
|
|
}
|
|
|
|
}
|
2013-05-13 15:58:44 +04:00
|
|
|
}
|
2013-05-17 17:07:03 +04:00
|
|
|
return result
|
2013-05-13 15:58:44 +04:00
|
|
|
}
|