如何使用Osmium仅保留指定标签?
用Osmium过滤OSM数据并仅保留指定标签
你可以通过Osmium的tags-transform工具结合tags-filter来实现只保留指定标签的需求,具体操作如下:
操作步骤
- 创建标签过滤规则脚本
新建一个Lua脚本文件(比如命名为keep-specified-tags.lua),内容如下:
-- 定义需要保留的标签列表,必须包含place标签(否则Nominatim无法识别地点类型) local allowed = { "name", "name:en", "name:fr", "place" } function filter_tags(tags) local filtered = {} for key, value in pairs(tags) do for _, tag in ipairs(allowed) do if key == tag then filtered[key] = value break end end end return filtered end
- 执行组合过滤命令
用管道将tags-filter的输出传给tags-transform,一次性完成元素筛选和标签保留:
osmium tags-filter planet-latest.osm.pbf place=city,town,village | osmium tags-transform --overwrite -f pbf -o planet-light.osm.pbf keep-specified-tags.lua -
关键说明
- 第一步
tags-filter负责筛选出所有place属性为city/town/village的元素 - 第二步
tags-transform通过Lua脚本只保留你指定的标签,同时强制保留place标签——这是Nominatim识别地点类型的关键,不能省略 - 若后续需要保留其他Nominatim必需的标签,直接添加到Lua脚本的
allowed列表即可
内容的提问来源于stack exchange,提问作者Nasedo47
相关产品推荐
相关产品推荐

