Linux【工具 02】OpenStreetMap数据处理工具OSMCTools下载安装使用举例(osmconvert\osmfilter命令说明)如何获取区域边界说明

简介: Linux【工具 02】OpenStreetMap数据处理工具OSMCTools下载安装使用举例(osmconvert\osmfilter命令说明)如何获取区域边界说明

OpenStreetMap的下载地址:Geofabrik Download Server

OSMCTools的GitHub地址:https://github.com/ramunasd/osmctools

Windows操作系统,可以从 http://m.m.i24.cc/osmconvert_latest.zip 下载osmconvert最新版本的zip文件;也可以从官方网站 http://wiki.openstreetmap.org/wiki/osmconvert 下载一个适用于Windows的安装程序进行安装。这两个地址都无法访问,原因不明。本次安装的是 Linux 系统的。

1.Tools

  • osmassignpoly
  • osmchange - updates an .osm file using one or more .osc files
  • osmconvert - reads and converts OSM data to the selected output file format
  • osmfilter - filters OSM data
  • osmgeobase
  • osmposition
  • osmrelpoly
  • osmupdate - cares about updating an .osm, .o5m or .pbf file
  • pbftoosm - converts .pbf file into .osm XML format

2.官网安装步骤

git clone https://github.com/ramunasd/osmctools.git
  cd osmctools
  autoreconf --install
  ./configure
  make install

3.实际安装步骤

3.1 环境

[root@tcloud ~]# cat /etc/centos-release
CentOS Linux release 7.9.2009 (Core)

3.2 步骤

# 1.安装autoconf
yum install -y autoconf
# 否则会报错
-bash: autoreconf: command not found
# 2.安装 automake libtool
yum install -y automake libtool
# 否则报错
Can't exec "aclocal": No such file or directory at /usr/share/autoconf/Autom4te/FileUtils.pm line 326.
autoreconf: failed to run aclocal: No such file or directory
  1. 从GIT上下载源码
# 1.从GIT上下载源码
git clone https://github.com/ramunasd/osmctools.git

2. 生成 configure 脚本等文件

# 2.生成 configure 脚本等文件
[root@tcloud osmctools-master]# autoreconf --install                                  
configure.ac:3: installing './install-sh'
configure.ac:3: installing './missing'
src/Makefile.am: installing './depcomp'

  1. 构建环境并生成 Makefile 以及设置编译选项的脚本
# 3.构建环境并生成 Makefile 以及设置编译选项的脚本
[root@tcloud osmctools-master]# ./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /usr/bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking for style of include used by make... GNU
checking dependency style of gcc... gcc3
checking for gzopen in -lz... yes
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating Makefile
config.status: creating src/Makefile
config.status: creating include/config.h
config.status: executing depfiles commands
# 4.安装
make install
[root@tcloud osmctools-master]# make install
Making install in src
make[1]: Entering directory `/opt/osmctools-master/src'
gcc -DHAVE_CONFIG_H -I. -I../include     -march=native -O3 -MT osmconvert.o -MD -MP -M                                                                                                      F .deps/osmconvert.Tpo -c -o osmconvert.o osmconvert.c
mv -f .deps/osmconvert.Tpo .deps/osmconvert.Po
gcc  -march=native -O3   -o osmconvert osmconvert.o -lz
gcc -DHAVE_CONFIG_H -I. -I../include     -march=native -O3 -MT osmfilter.o -MD -MP -MF                                                                                                       .deps/osmfilter.Tpo -c -o osmfilter.o osmfilter.c
mv -f .deps/osmfilter.Tpo .deps/osmfilter.Po
gcc  -march=native -O3   -o osmfilter osmfilter.o
gcc -DHAVE_CONFIG_H -I. -I../include     -march=native -O3 -MT osmupdate.o -MD -MP -MF .deps/osmupdate.Tpo -c -o osmupdate.o osmupdate.c
mv -f .deps/osmupdate.Tpo .deps/osmupdate.Po
gcc  -march=native -O3   -o osmupdate osmupdate.o
gcc -DHAVE_CONFIG_H -I. -I../include     -march=native -O3 -MT osmchange.o -MD -MP -MF .deps/osmchange.Tpo -c -o osmchange.o osmchange.c
mv -f .deps/osmchange.Tpo .deps/osmchange.Po
gcc  -march=native -O3   -o osmchange osmchange.o
gcc -DHAVE_CONFIG_H -I. -I../include     -march=native -O3 -MT pbftoosm.o -MD -MP -MF .deps/pbftoosm.Tpo -c -o pbftoosm.o pbftoosm.c
mv -f .deps/pbftoosm.Tpo .deps/pbftoosm.Po
gcc  -march=native -O3   -o pbftoosm pbftoosm.o -lz
gcc -DHAVE_CONFIG_H -I. -I../include     -march=native -O3 -MT osmrelpoly.o -MD -MP -MF .deps/osmrelpoly.Tpo -c -o osmrelpoly.o osmrelpoly.c
mv -f .deps/osmrelpoly.Tpo .deps/osmrelpoly.Po
gcc  -march=native -O3   -o osmrelpoly osmrelpoly.o
gcc -DHAVE_CONFIG_H -I. -I../include     -march=native -O3 -MT osmassignpoly.o -MD -MP -MF .deps/osmassignpoly.Tpo -c -o osmassignpoly.o osmassignpoly.c
mv -f .deps/osmassignpoly.Tpo .deps/osmassignpoly.Po
gcc  -march=native -O3   -o osmassignpoly osmassignpoly.o
gcc -DHAVE_CONFIG_H -I. -I../include     -march=native -O3 -MT osmgeobase.o -MD -MP -MF .deps/osmgeobase.Tpo -c -o osmgeobase.o osmgeobase.c
mv -f .deps/osmgeobase.Tpo .deps/osmgeobase.Po
gcc  -march=native -O3   -o osmgeobase osmgeobase.o
gcc -DHAVE_CONFIG_H -I. -I../include     -march=native -O3 -MT osmposition.o -MD -MP -MF .deps/osmposition.Tpo -c -o osmposition.o osmposition.c
mv -f .deps/osmposition.Tpo .deps/osmposition.Po
gcc  -march=native -O3   -o osmposition osmposition.o
make[2]: Entering directory `/opt/osmctools-master/src'
 /usr/bin/mkdir -p '/usr/local/bin'
  /usr/bin/install -c osmconvert osmfilter osmupdate osmchange pbftoosm osmrelpoly osmassignpoly osmgeobase osmposition '/usr/local/bin'
make[2]: Nothing to be done for `install-data-am'.
make[2]: Leaving directory `/opt/osmctools-master/src'
make[1]: Leaving directory `/opt/osmctools-master/src'
make[1]: Entering directory `/opt/osmctools-master'
make[2]: Entering directory `/opt/osmctools-master'
make[2]: Nothing to be done for `install-exec-am'.
make[2]: Nothing to be done for `install-data-am'.
make[2]: Leaving directory `/opt/osmctools-master'
make[1]: Leaving directory `/opt/osmctools-master'

安装成功后增加的bin文件跟介绍里的一样:

4.工具使用实例

4.1 osmconvert

它是一个用于处理OSM(OpenStreetMap)地图数据的命令行工具。可以将OSM地图文件从一种格式转换成另一种格式,比如OSM XML文件转换成PBF(Protocolbuffer Binary Format)、CSV(Comma Separated Values)等格式,并且可以对地图数据进行筛选、剪切、合并等操作。

# 可以直接输入 osmconvert 根据指引进行文件处理
[root@tcloud ~]# osmconvert
osmconvert 0.8.11
Converts .osm, .o5m, .pbf, .osc, .osh files, applies changes
of .osc, .o5c, .osh files and sets limiting borders.
Use command line option -h to get a parameter overview,
or --help to get detailed help.
If you are familiar with the command line, press <Return>.
If you do not know how to operate the command line, please
enter "a" (press key E and hit <Return>).

不同版本的 osmconvert 用法可能不同,常见的用法:

  1. 查看PBF文件的统计信息
osmconvert china-latest.osm.pbf --out-statistics
-----------------------------------------------------------------
timestamp min: 2007-02-27T00:40:13Z
timestamp max: 2023-05-16T19:28:11Z
lon min: 52.8472761
lon max: 135.6776013
lat min: 4.9997288
lat max: 54.0115689
nodes: 152714910
ways: 11459000
relations: 148001
node id min: 274901
node id max: 10903266309
way id min: 4181592
way id max: 1173486456
relation id min: 2664
relation id max: 15863565
keyval pairs max: 605
keyval pairs max object: relation 270056
noderefs max: 2000
noderefs max object: way 28779814
relrefs max: 8899
relrefs max object: relation 911618
-----------------------------------------------------------------
Finished! Calculation time: 31s.
  1. 将OSM XML文件转换成PBF格式:
osmconvert input.osm -o=output.pbf
  1. 将PBF格式的地图文件转换成OSM XML格式:
# pbf格式的文件无法直接查看转换后可直接看数据
osmconvert input.pbf -o=output.osm
  1. 将PBF格式的地图文件转换成CSV格式:
# 转换成csv文件可以直接导入数据库
osmconvert input.pbf --csv="@id @lon @lat name place" -o=output.csv
  1. 筛选地图数据中的节点、路线或关系:
osmconvert input.pbf --all-to-nodes -o=output_nodes.osm
osmconvert input.pbf --all-to-ways -o=output_ways.osm
osmconvert input.pbf --all-to-relations -o=output_relations.osm
  1. 对地图数据进行剪切或合并:
osmconvert input1.osm input2.osm -o=output_combined.osm
# -b=经度最小值,纬度最小值,经度最大值,纬度最大值
osmconvert input.pbf -b=-122.6,45.4,-122.3,45.6 --complete-ways -o=output_bbox.osm

如何获取边界数据呢?这里提供一种方式,使用GIS工具QGIS【其他工具应该也是可以的】查看:

使用要素导出获取画布范围的座标信息:

# 较大的pbf文件使用参数--hash-memory="XXXM"来增加哈希表的大小
# 否则报错 "Hash size had to be reduced."
# 测试剪切郑州市的数据【西南东北】
osmconvert china-latest.osm.pbf -b=112.621514332,34.241174295,114.294626906,34.994721475 --hash-memory="2048M" --complete-ways --out-pbf -o=zhengzhou-latest.osm.pbf

文件大小从980.58MB->5.23MB统计一下剪切后的文件:

[root@node178 /opt/omsctools]$ osmconvert zhengzhou-latest.osm.pbf --out-statistics
timestamp min: 2009-01-04T08:29:32Z
timestamp max: 2023-05-16T18:42:30Z
lon min: 98.4340096
lon max: 121.3549585
lat min: 31.1094074
lat max: 39.6300194
nodes: 846199
ways: 86574
relations: 1638
node id min: 244078483
node id max: 10903000445
way id min: 30896261
way id max: 1173456169
relation id min: 170266
relation id max: 15863565
keyval pairs max: 605
keyval pairs max object: relation 270056
noderefs max: 1987
noderefs max object: way 643962897
relrefs max: 6360
relrefs max object: relation 170266

更多命令和参数可通过 help 查询:

# 使用 help 查看可执行的命令
[root@tcloud ~]# osmconvert --help
Examples
./osmconvert europe.pbf --drop-author >europe.osm
./osmconvert europe.pbf |gzip >europe.osm.gz
bzcat europe.osm.bz2 |./osmconvert --out-pbf >europe.pbf
./osmconvert europe.pbf -B=ch.poly >switzerland.osm
./osmconvert switzerland.osm --out-o5m >switzerland.o5m
./osmconvert june_july.osc --out-o5c >june_july.o5c
./osmconvert june.o5m june_july.o5c.gz --out-o5m >july.o5m
./osmconvert sep.osm sep_oct.osc oct_nov.osc >nov.osm
./osmconvert northamerica.osm southamerica.osm >americas.osm

4.2 osmfilter

osmfilter是一个用于处理OpenStreetMap (OSM)数据文件的命令行工具。它可以帮助您从大型OSM数据文件中提取特定的地理数据,进行过滤和转换操作。

以下是一些常见的用法示例:

  1. 过滤数据:
osmfilter input.osm --keep="highway=motorway" -o=output.osm

上述命令将从input.osm文件中保留所有highway=motorway标签的元素,并将结果保存到output.osm文件中。

  1. 组合多个条件:
osmfilter input.osm --keep="highway=motorway =trunk" -o=output.osm

上述命令将保留所有同时满足highway=motorwayhighway=trunk条件的元素。

  1. 使用地理边界进行过滤:
osmfilter input.osm --bounding-box=left,bottom,right,top -o=output.osm

上述命令将根据指定的地理边界框过滤数据。您需要将leftbottomrighttop替换为实际的经度和纬度坐标。

  1. 过滤指定的节点、路径和区域:
osmfilter input.osm --keep-nodes="amenity=restaurant" --keep-ways="highway=primary" --keep-relations="type=boundary" -o=output.osm

上述命令将保留具有amenity=restaurant标签的节点,highway=primary标签的路径,以及type=boundary标签的区域。

这些只是osmfilter的一些基本用法示例,它还提供了其他功能和选项,例如属性过滤、标签转换、地理位置计算等。您可以查阅osmfilter的官方文档以获取更详细的信息和更多用法示例。

目录
相关文章
|
2月前
|
Linux 应用服务中间件 Shell
二、Linux文本处理与文件操作核心命令
熟悉了Linux的基本“行走”后,就该拿起真正的“工具”干活了。用grep这个“放大镜”在文件里搜索内容,用find这个“探测器”在系统中寻找文件,再用tar把东西打包带走。最关键的是要学会使用管道符|,它像一条流水线,能把这些命令串联起来,让简单工具组合出强大的功能,比如 ps -ef | grep 'nginx' 就能快速找出nginx进程。
414 1
二、Linux文本处理与文件操作核心命令
|
2月前
|
安全 Linux Shell
四、Linux核心工具:Vim, 文件链接与SSH
要想在Linux世界里游刃有余,光会“走路”还不够,还得配上几样“高级装备”。首先是Vim编辑器,它像一把瑞士军刀,让你能在命令行里高效地修改文件。然后要懂“软硬链接”,软链接像个快捷方式,硬链接则是给文件起了个别名。最后,SSH是你的“传送门”,不仅能让你安全地远程登录服务器,还能用scp轻松传输文件,设置好密钥更能实现免-密登录,极大提升效率。
369 3
|
2月前
|
Linux
linux命令—stat
`stat` 是 Linux 系统中用于查看文件或文件系统详细状态信息的命令。相比 `ls -l`,它提供更全面的信息,包括文件大小、权限、所有者、时间戳(最后访问、修改、状态变更时间)、inode 号、设备信息等。其常用选项包括 `-f` 查看文件系统状态、`-t` 以简洁格式输出、`-L` 跟踪符号链接,以及 `-c` 或 `--format` 自定义输出格式。通过这些选项,用户可以灵活获取所需信息,适用于系统调试、权限检查、磁盘管理等场景。
289 137
|
2月前
|
安全 Ubuntu Unix
一、初识 Linux 与基本命令
玩转Linux命令行,就像探索一座新城市。首先要熟悉它的“地图”,也就是/根目录下/etc(放配置)、/home(住家)这些核心区域。然后掌握几个“生存口令”:用ls看周围,cd去别处,mkdir建新房,cp/mv搬东西,再用cat或tail看文件内容。最后,别忘了随时按Tab键,它能帮你自动补全命令和路径,是提高效率的第一神器。
667 57
|
1月前
|
存储 安全 Linux
Linux卡在emergency mode怎么办?xfs_repair 命令轻松解决
Linux虚拟机遇紧急模式?别慌!多因磁盘挂载失败。本文教你通过日志定位问题,用`xfs_repair`等工具修复文件系统,三步快速恢复。掌握查日志、修磁盘、验重启,轻松应对紧急模式,保障系统稳定运行。
380 2
|
2月前
|
缓存 监控 Linux
Linux内存问题排查命令详解
Linux服务器卡顿?可能是内存问题。掌握free、vmstat、sar三大命令,快速排查内存使用情况。free查看实时内存,vmstat诊断系统整体性能瓶颈,sar实现长期监控,三者结合,高效定位并解决内存问题。
264 0
Linux内存问题排查命令详解
|
2月前
|
安全 Linux iOS开发
SonarQube Server 2025 Release 5 (macOS, Linux, Windows) - 代码质量、安全与静态分析工具
SonarQube Server 2025 Release 5 (macOS, Linux, Windows) - 代码质量、安全与静态分析工具
180 0
SonarQube Server 2025 Release 5 (macOS, Linux, Windows) - 代码质量、安全与静态分析工具
|
2月前
|
Unix Linux 程序员
Linux文本搜索工具grep命令使用指南
以上就是对Linux环境下强大工具 `grep` 的基础到进阶功能介绍。它不仅能够执行简单文字查询任务还能够处理复杂文字处理任务,并且支持强大而灵活地正则表达规范来增加查询精度与效率。无论您是程序员、数据分析师还是系统管理员,在日常工作中熟练运用该命令都将极大提升您处理和分析数据效率。
270 16
|
5月前
|
JSON 自然语言处理 Linux
linux命令—tree
tree是一款强大的Linux命令行工具,用于以树状结构递归展示目录和文件,直观呈现层级关系。支持多种功能,如过滤、排序、权限显示及格式化输出等。安装方法因系统而异常用场景包括:基础用法(显示当前或指定目录结构)、核心参数应用(如层级控制-L、隐藏文件显示-a、完整路径输出-f)以及进阶操作(如磁盘空间分析--du、结合grep过滤内容、生成JSON格式列表-J等)。此外,还可生成网站目录结构图并导出为HTML文件。注意事项:使用Tab键补全路径避免错误;超大目录建议限制遍历层数;脚本中推荐禁用统计信息以优化性能。更多详情可查阅手册mantree。
496 143
linux命令—tree