I am having trouble with the lua config file to add certain values to specific rows by ID on import.

I want to add relation info to nodes like is possible with ways, and I want to only compute length via way:transform(srid):length() for ways that belong to the relations I am interested in (subway, tram, and bus route relations) to speed up computation/import speeds.

In order to add relation info to nodes, I attempted to add the following:

local n2r = {}

function osm2pgsql.process_node(object)
    --local geom = object:as_point()
    if not (object.tags.amenity or object.tags.shop or object.tags.public_transport) then
        return
    end
    local row = {
        tags = object.tags,
        geom = object:as_point()
    }
    local g = n2r[object.id]
    if g then
        local refs = {}
        local ids = {}
        for rel_id, rel_ref in pairs(g) do
            refs[#refs + 1] = rel_ref
            ids[#ids + 1] = rel_id
        end
        table.sort(refs)
        table.sort(ids)
        row.rel_refs = table.concat(refs, ',')
        row.rel_ids = '{' .. table.concat(ids, ',') .. '}'
    end
    tables.nodes:insert({
        tags = object.tags,
        geom = object:as_point()
    })
end

function osm2pgsql.process_relation(object)
    local relation_type = object:grab_tag('type')
    if clean_tags(object.tags) then
        return
    end
    if relation_type == 'route' and (object.tags.route == 'subway' or object.tags.route == 'tram' or object.tags.route == 'bus') then
        tables.routes:insert({
            tags = object.tags,
        })
        for _, member in ipairs(object.members) do
            if member.type == 'w' then
                if not w2r[member.ref] then
                    w2r[member.ref] = {}
                end
                w2r[member.ref][object.id] = object.tags.ref
            --elseif member.type == 'n' then
            --    if not n2r[member.ref] then
            --        n2r[member.ref] = {}
            --    end
            --    n2r[member.ref][object.id] = object.tags.ref
            end
        end
    end
end

But due to the fact that lua doesnt have the built in function way_member_ids for node members, I don't know exactly how to make it work if its even possible. Maybe I just have to add the node ids to a json value as an array or something.

Additionally, I'd like to increase the processing speed from the current 2hrs by only calculating the length of a way if it matches the reprocess flag from a relation I am interested in. To do this I attempted to add the row value upon reprocessing by:

function osm2pgsql.process_way(object)
    if not (object.tags.railway or object.tags.highway) then
        return
    end
    if clean_tags(object.tags) then
        return
    end
    local geom = object:as_linestring()
    local row = {
        tags = object.tags,
        geom = geom,
    }
    local d = w2r[object.id]
    if d then
        local refs = {}
        local ids = {}
        for rel_id, rel_ref in pairs(d) do
            refs[#refs + 1] = rel_ref
            ids[#ids + 1] = rel_id
        end
        table.sort(refs)
        table.sort(ids)
        row.rel_refs = table.concat(refs, ',')
        row.rel_ids = '{' .. table.concat(ids, ',') .. '}'
        row.dist = row.geom:transfrom(srid):length()
    end
    tables.ways:insert(row)
end
1

There are 1 best solutions below

0
On

For part 1 tagging nodes as members of relations: by looking at documentation again, seems impossible as docs quote

"(It is currently not possible to mark nodes or relations. This might or might not be added in future versions of osm2pgsql.)"

that was missed initially.

For part 2 only getting geom length for relation members: you can change the row.dist aspect to use table.concat like the row.rel_refs and row.rel_ids and this works