1
2local tables = {}
3
4tables.highways = osm2pgsql.define_table{
5    name = 'osm2pgsql_test_highways',
6    ids = { type = 'way', id_column = 'way_id' },
7    columns = {
8        { column = 'tags',  type = 'hstore' },
9        { column = 'refs',  type = 'text' },
10        { column = 'geom',  type = 'linestring', projection = 4326 },
11    }
12}
13
14tables.routes = osm2pgsql.define_table{
15    name = 'osm2pgsql_test_routes',
16    ids = { type = 'relation', id_column = 'rel_id' },
17    columns = {
18        { column = 'tags',    type = 'hstore' },
19        { column = 'members', type = 'text' },
20        { column = 'geom',    type = 'multilinestring', projection = 4326 },
21    }
22}
23
24local w2r = {}
25
26function osm2pgsql.process_way(object)
27    local row = {
28        tags = object.tags,
29        geom = { create = 'line' }
30    }
31
32    local d = w2r[object.id]
33    if d then
34        local refs = {}
35        for rel_id, rel_ref in pairs(d) do
36            refs[#refs + 1] = rel_ref
37        end
38        table.sort(refs)
39
40        row.refs = table.concat(refs, ',')
41    end
42
43    tables.highways:add_row(row)
44end
45
46function osm2pgsql.select_relation_members(relation)
47    if relation.tags.type == 'route' then
48        return { ways = osm2pgsql.way_member_ids(relation) }
49    end
50end
51
52function osm2pgsql.process_relation(object)
53    if object.tags.type ~= 'route' then
54        return
55    end
56
57    local mlist = {}
58    for _, member in ipairs(object.members) do
59        if member.type == 'w' then
60            if not w2r[member.ref] then
61                w2r[member.ref] = {}
62            end
63            w2r[member.ref][object.id] = object.tags.ref
64            mlist[#mlist + 1] = member.ref
65        end
66    end
67
68    tables.routes:add_row({
69        tags = object.tags,
70        members = table.concat(mlist, ','),
71        geom = { create = 'line' }
72    })
73end
74
75