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的官方文档以获取更详细的信息和更多用法示例。

目录
相关文章
|
22小时前
|
关系型数据库 MySQL Linux
Linux命令systemctl详解
`systemctl`是Linux系统用于管理systemd服务的核心命令,它与systemd守护进程交互,实现启动、停止、重启服务及查看服务状态等功能。主要参数包括`start`、`stop`、`restart`、`status`、`enable`和`disable`等。例如,启动Apache服务使用`systemctl start httpd.service`,查看服务状态用`systemctl status &lt;service&gt;`。使用时需注意权限,服务名通常以`.service`结尾,但命令中可省略。最佳实践包括利用tab键补全、定期查看服务状态和合理配置服务自启。
|
22小时前
|
存储 安全 Linux
Linux命令sync详解
`sync`命令在Linux中用于将内存缓冲区的数据强制写入磁盘,保证数据持久性和一致性。它在关机、重启或重要文件操作前后使用,以防数据丢失。工作原理是强制将内存中的数据同步到磁盘,特点是阻塞式执行且通常无需参数。常见用法包括安全关机、数据备份和配置文件修改后确保更改生效。应注意,过度使用可能影响性能,应适时使用`fsck`检查文件系统一致性。
|
23小时前
|
存储 算法 安全
Linux命令sum详解
`sum`命令在Linux中用于计算文件的校验和与磁盘块数,确保文件传输或存储时的完整性。它使用加法运算生成校验和,可与文件内容比较验证变化。支持不同算法(如CRC),能处理多个文件。基本用法包括:`sum file.txt`来计算校验和,`sum -c checksum.txt`来验证文件完整性。但要注意,更强的校验算法如MD5或SHA家族可能更适合安全性需求。结合`find`和`xargs`可用于目录的递归校验。定期校验和记录校验和是最佳实践。
|
23小时前
|
安全 数据管理 Shell
Linux命令su详解
`su`命令在Linux中用于切换用户身份,常用于权限管理。它允许用户无须注销当前会话就切换到另一个用户,尤其是root。`su`有多种选项,如`-`或`--login`加载目标用户环境,`-c`执行指定命令后返回。使用时需注意权限安全,建议用`sudo`以减少风险。通过限制`/etc/pam.d/su`可加强访问控制。`su`在系统维护和数据管理中扮演角色,但不直接处理数据。
|
23小时前
|
Unix Linux 数据处理
Linux命令stty详解
`stty`是Linux命令,用于设置和查看终端参数,如波特率、字符处理和控制字符。它直接与终端驱动交互,支持多种选项以适应不同的配置需求。例如,`stty -a`显示当前设置,`stty -echo`关闭回显,`stty 115200 cs8`调整波特率和字符大小。注意修改设置可能影响终端行为,建议先备份(`stty -g`)并谨慎操作。查阅手册页以获取详细信息。
|
23小时前
|
安全 Linux 数据处理
Linux命令strip详解
`strip`命令在Linux中用于移除可执行文件和库的符号表及调试信息,减小文件大小,提升运行效率。它的工作原理是删除文件中包含的函数名、变量名等信息。主要参数包括`-s`(移除所有符号)、`-g`(仅移除调试信息)等。在应用时要注意文件备份,因为该操作不可逆。最佳实践是在发布版本中使用,并结合构建流程自动化。
|
23小时前
|
安全 Linux 数据安全/隐私保护
Linux命令strings详解
`strings`是Linux工具,用于从二进制文件中提取可打印字符串,常用于文件分析、安全审计和逆向工程。它可以识别至少4个连续可打印字符的序列,并支持多种参数,如`-n`调整最小长度,`-f`显示文件名。示例用法包括`strings /bin/ls`和`strings -n 6 /usr/bin/uptime | grep GLIBC`。注意敏感信息泄露,结合其他命令可增强分析能力。
|
23小时前
|
存储 监控 Linux
stdbuf命令在Linux中的深度解析
`stdbuf`是Linux工具,用于控制命令的stdin、stdout和stderr的缓冲模式。它可以设置为无缓冲、行缓冲或块缓冲,以优化数据处理和实时性。例如,`stdbuf -o0 cmd`禁用cmd的输出缓冲,`-oL`则按行缓冲。在需要实时监控或高效处理大量数据时,选择合适的缓冲模式至关重要。注意,过度使用无缓冲可能影响性能,并非所有系统都支持`stdbuf`。
|
23小时前
|
存储 运维 安全
Linux命令stat:深入了解文件与文件系统状态
`stat`命令在Linux中用于显示文件和文件系统的详细状态,包括权限、大小、时间戳等。它通过读取inode获取信息,特点是显示全面、易用且支持多种参数,如`-c`自定义格式,`-f`查看文件系统状态,`-L`处理符号链接。例如,`stat example.txt`显示文件详情,`stat -c &quot;%n 的大小是 %s 字节&quot; example.txt`输出文件大小。理解`stat`有助于系统管理和故障排查。
|
23小时前
|
监控 安全 Linux
Linux命令ssltap的深入解析与应用实践
`ssltap`是一个假想的Linux命令,用于模拟SSL/TLS流量分析。它捕获、解密(如果有密钥)并分析加密流量,提供实时监控、协议解析和安全审计。特点包括实时性、灵活性、可扩展性和安全性。示例用法包括捕获特定端口流量和实时监控会话状态。在实际操作中应注意私钥安全、性能影响及合规性,建议定期审计和自动化监控。