【发布时间】:2023-02-26 19:58:04
【问题描述】:
我的 osm2pgsql 脚本需要很长时间才能将数据加载到我的数据库中,我不知道为什么。
执行类似计算的参考 lua 脚本花费相同时间的 1/10。我知道我的个人计算机不会快速处理数据,但它需要数小时才能处理使用普通脚本需要 10-15 分钟的内容。
作为参考,我运行的是英特尔 i7-8000 系列笔记本电脑、8GB 内存和速度相当快的固态硬盘。
我正在运行的脚本是:
osm2pgsql -c -O flex --style=lua_config/custom.lua --slim --drop -C 2000 --database=test /osm_data/berlin-latest.osm.pbf
berlin-latest.pbf 文件为 74.7mb,因此 -C 值甚至高于 osm2pgsql 文档建议的值。
Lua 配置文件基于 github 页面 (route-relations lua file link) 中的 route-relations.lua 示例,并且是:
-- Set Standard Data info vals
local schema_name = 'berlin'
local srid = 25832 -- european srid
local tables = {}
------------------------------------------------------------------------------------
-- Create Tables for Data
tables.nodes = osm2pgsql.define_node_table('nodes', {
{ column = 'tags', type = 'jsonb' },
{ column = 'geom', type = 'point', projection = srid, not_null = true },
}, { schema = schema_name })
tables.ways = osm2pgsql.define_way_table('ways', {
{ column = 'tags', type = 'jsonb' },
{ column = 'rel_refs', type = 'text' },
{ column = 'rel_ids', sql_type = 'int8[]' },
{ column = 'geom', type = 'linestring', projection = srid, not_null = true },
{ column = 'dist', type = 'real'},
{ column = 'source', type = 'int' , create_only = true },
{ column = 'target', type = 'int', create_only = true },
}, { schema = schema_name })
tables.routes = osm2pgsql.define_relation_table('routes', {
{ column = 'name', type = 'text' },
{ column = 'tags', type = 'jsonb' },
{ column = 'nodes', sql_type = 'int8[]' },
}, { schema = schema_name })
---------------------------------------------------------------------------------
-- Create sorting functions
function clean_tags(tags)
tags.odbl = nil
tags.created_by = nil
tags.source = nil
tags['source:ref'] = nil
return next(tags) == nil
end
-- connect relations to member ways
local w2r = {}
function osm2pgsql.process_node(object)
if ((object.tags.amenity == 'bar' or object.tags.amenity == 'cafe') or object.tags.shop or object.tags.public_transport == 'stop') then
tables.nodes:insert({
tags = object.tags,
geom = object:as_point()
})
end
end
function osm2pgsql.process_way(object)
if not ((object.tags.railway == 'subway' or object.tags.railway == 'tram') 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,
dist = geom:transform(srid):length(),
}
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, ',') .. '}'
end
tables.ways:insert(row)
end
function osm2pgsql.select_relation_members(relation)
-- Only interested in relations with type=route, route=road and a ref
if relation.tags.type == 'route' and (relation.tags.route == 'subway' or relation.tags.route == 'tram' or relation.tags.route == 'bus') then
return { ways = osm2pgsql.way_member_ids(relation) }
end
end
function osm2pgsql.process_relation(object)
local relation_type = object:grab_tag('type')
local relation_name = object:grab_tag('name')
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({
name = relation_name,
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
end
end
end
end
此文件或没有重新处理节点以添加关系信息的早期版本至少需要 2 小时才能加载,而路由关系需要 16 秒。我知道我还有很多节点/方式/关系要处理,但速度上的差异似乎有点额外。
例如,脚本耗时 12 小时。
【问题讨论】: