osm2pgsql Lua配置问题:节点关联标记与道路长度按需计算
osm2pgsql Lua配置优化:节点关联关系与导入速度提升
一、给节点添加关联关系信息
你尝试给节点关联路线关系信息时,注释掉了process_relation中处理节点成员的代码,其实不需要依赖特殊内置函数,直接放开这段逻辑即可。另外注意process_node最后插入数据库时要使用构造好的row变量,否则关联信息不会被写入。
修正后的代码如下:
local n2r = {} local w2r = {} -- 确保w2r已定义 function osm2pgsql.process_node(object) 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 -- 插入构造好的row,而非重新创建table tables.nodes:insert(row) 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
关键说明:
- 直接遍历关系成员,判断类型为
n(节点)时,将关系ID和ref存入n2r字典,逻辑和处理道路完全一致 process_node必须插入构造好的row,否则关联字段不会被写入数据库- 若需要用JSON数组存储
rel_ids,可将row.rel_ids改为json.encode(ids),前提是数据库表字段为JSON类型且Lua环境已引入json库
二、仅对目标路线关系的道路计算长度以提升导入速度
你的思路正确:只给属于地铁、有轨电车、公交路线的道路计算长度,避免无意义的几何运算。需要修正代码中的拼写错误(transfrom→transform),并确保srid变量提前定义。
修正后的process_way代码:
-- 提前定义目标SRID,比如4326(WGS84)或3857(Web墨卡托) local srid = 3857 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, ',') .. '}' -- 修正拼写错误:transfrom → transform row.dist = geom:transform(srid):length() end tables.ways:insert(row) end
关键说明:
- 仅当道路属于目标路线关系时(
d不为空),才执行几何变换和长度计算,大幅减少计算量 - 确保
srid与数据库空间参考系一致,避免投影错误 - 若不需要存储
rel_refs和rel_ids,可只保留长度计算逻辑,进一步简化
内容的提问来源于stack exchange,提问作者evan
相关产品推荐
相关产品推荐

