From 00405b232bcd323aa6f62360870626525ea21b85 Mon Sep 17 00:00:00 2001 From: Oliver Tonnhofer Date: Mon, 13 May 2013 15:19:39 +0200 Subject: [PATCH] build linestring and/or polygons depending on the mapping --- config/config.go | 24 ++++++++++++++++++++++++ db/postgis.go | 11 ++++++++++- element/element.go | 4 ++++ goposm.go | 42 +++++++++++++++++++++++++++++++++--------- 4 files changed, 71 insertions(+), 10 deletions(-) diff --git a/config/config.go b/config/config.go index e7c875c..92508f1 100644 --- a/config/config.go +++ b/config/config.go @@ -108,6 +108,30 @@ func (m *Mapping) RelationTagFilter() *TagFilter { return &TagFilter{mappings, tags} } +func (m *Mapping) PointTables() *TagFilter { + mappings := make(map[string]map[string][]string) + m.mappings("point", mappings) + tags := make(map[string]bool) + m.extraTags("point", tags) + return &TagFilter{mappings, tags} +} + +func (m *Mapping) LineStringTables() *TagFilter { + mappings := make(map[string]map[string][]string) + m.mappings("linestring", mappings) + tags := make(map[string]bool) + m.extraTags("linestring", tags) + return &TagFilter{mappings, tags} +} + +func (m *Mapping) PolygonTables() *TagFilter { + mappings := make(map[string]map[string][]string) + m.mappings("polygon", mappings) + tags := make(map[string]bool) + m.extraTags("polygon", tags) + return &TagFilter{mappings, tags} +} + type TagFilter struct { mappings map[string]map[string][]string extraTags map[string]bool diff --git a/db/postgis.go b/db/postgis.go index 3b8a2e5..92c3a5c 100644 --- a/db/postgis.go +++ b/db/postgis.go @@ -101,6 +101,15 @@ func (e *SQLError) Error() string { return fmt.Sprintf("SQL Error: %s in query %s", e.originalError.Error(), e.query) } +type SQLInsertError struct { + SQLError + data interface{} +} + +func (e *SQLInsertError) Error() string { + return fmt.Sprintf("SQL Error: %s in query %s (%+v)", e.originalError.Error(), e.query, e.data) +} + func (pg *PostGIS) createTable(spec TableSpec) error { var sql string var err error @@ -207,7 +216,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 &SQLError{sql, err} + return &SQLInsertError{SQLError{sql, err}, w} } } diff --git a/element/element.go b/element/element.go index 9281631..f87df50 100644 --- a/element/element.go +++ b/element/element.go @@ -20,6 +20,10 @@ type Way struct { Wkb []byte } +func (w *Way) IsClosed() bool { + return len(w.Refs) >= 4 && w.Refs[0] == w.Refs[len(w.Refs)-1] +} + type MemberType int const ( diff --git a/goposm.go b/goposm.go index 77a04ea..1b6a838 100644 --- a/goposm.go +++ b/goposm.go @@ -265,7 +265,7 @@ func main() { {"name", "VARCHAR"}, {"highway", "VARCHAR"}, }, - "LINESTRING", + "GEOMETRY", config.Srid, }, } @@ -289,7 +289,8 @@ func main() { for i := 0; i < runtime.NumCPU(); i++ { waitFill.Add(1) go func() { - // m := mapping.WayTagFilter() + lineStringTables := mapping.LineStringTables() + polygonTables := mapping.PolygonTables() var err error geos := geos.NewGEOS() defer geos.Finish() @@ -302,18 +303,41 @@ func main() { continue } proj.NodesToMerc(w.Nodes) - w.Wkb, err = geom.LineStringWKB(geos, w.Nodes) - if err != nil { - if err, ok := err.(ErrorLevel); ok { - if err.Level() <= 0 { + if tables := lineStringTables.Tables(w.Tags); len(tables) > 0 { + way := element.Way{} + way.Id = w.Id + way.Tags = w.Tags + way.Wkb, err = geom.LineStringWKB(geos, w.Nodes) + if err != nil { + if err, ok := err.(ErrorLevel); ok { + if err.Level() <= 0 { + continue + } + } + log.Println(err) + continue + } + batch = append(batch, way) + } + if w.IsClosed() { + if tables := polygonTables.Tables(w.Tags); len(tables) > 0 { + way := element.Way{} + way.Id = w.Id + way.Tags = w.Tags + way.Wkb, err = geom.PolygonWKB(geos, w.Nodes) + if err != nil { + if err, ok := err.(ErrorLevel); ok { + if err.Level() <= 0 { + continue + } + } + log.Println(err) continue } + batch = append(batch, way) } - log.Println(err) - continue } // log.Println(w.Id, w.Tags, m.Tables(w.Tags)) - batch = append(batch, *w) if len(batch) >= int(dbImportBatchSize) { wayChan <- batch