器→工具, 开源项目

开源地理数据OpenStreetMap的使用

钱魏Way · · 3,991 次浏览
!文章内容如有错误或排版问题,请提交反馈,非常感谢!

OpenStreetMap数据简介

OpenStreetMap,简称OSM,是一个开源的世界地图,可依据开放许可协议自由使用,并且可以由人们自由的进行编辑,随着开源意识的普及,以及电子地图应用的普及,osm数据的质量和体量不断增加,在一些领域的精确度已经不逊于Google地图,甚至在一些方面可以说是超越了google地图的。

OSM数据特点:

  • 免费开放,用户可以对原始数据编辑
  • 轨迹数据来自全世界用户的采集(质量参差不齐)
  • 按照数据的类别,用不同的标签标示数据类别
  • 没有Javascript Api,提供数据下载,数据格式.osm
  • 缩放级别[2,18],坐标系:WGS84

警告:OpenStreetMap同GADM一样提供的中国国界数据不符合中国的领土主张,切勿在公开使用。在中国大陆地区参与OpenStreetMap绘制很可能是非法行为。请参阅法律警示页面:WikiProject_China

数据下载地址:http://download.geofabrik.de/

OSM提供的数据格式:

  • .osm.pbf:PBF文件是OpenStreetMap数据的二进制紧凑格式,特点就是相同的数据量占用存储空间小。相比bz2等格式,pbf读写效率更高。
  • .shp.zip:ESRI Shapefile(shp),如何使用见搭建自己的反地理查询系统
  • .osm.bz2:XML格式的压缩

除了可以下载全部数据外,OSM还可以按照国家等进行下载。

OpenStreetMap的使用

想要将OSM数据导入到PostgreSQL,可以使用的工具为osm2pgsql。具体怎么使用网上有很多的教程。这里使用的是Windows版本:https://lonvia.dev.openstreetmap.org/osm2pgsql-winbuild/releases/

使用方法:

PS D:\Program Files\osm2pgsql-bin> .\osm2pgsql.exe -h
osm2pgsql version 1.2.1 (64bit id space)

Usage:
D:\Program Files\osm2pgsql-bin\osm2pgsql.exe [options] planet.osm
D:\Program Files\osm2pgsql-bin\osm2pgsql.exe [options] planet.osm.{pbf,gz,bz2}
D:\Program Files\osm2pgsql-bin\osm2pgsql.exe [options] file1.osm file2.osm file3.osm

This will import the data from the OSM file(s) into a PostgreSQL database
suitable for use by the Mapnik renderer.

Common options:
-a|--append Add the OSM file into the database without removing
existing data.
-c|--create Remove existing data from the database. This is the
default if --append is not specified.
-l|--latlong Store data in degrees of latitude & longitude.
-m|--merc Store data in proper spherical mercator (default).
-E|--projnum Use projection EPSG:num.
-s|--slim Store temporary data in the database. This greatly
reduces the RAM usage but is much slower. This switch is
required if you want to update with --append later.
-S|--style Location of the style file. Defaults to
default.style.
-C|--cache Use up to this many MB for caching nodes (default: 800)
-F|--flat-nodes Specifies the flat file to use to persistently store node
information in slim mode instead of in PostgreSQL.
This file is a single >40Gb large file. Only recommended
for full planet imports. Default is disabled.

Database options:
-d|--database The name of the PostgreSQL database to connect to.
-U|--username PostgreSQL user name (specify passsword in PGPASSWORD
environment variable or use -W).
-W|--password Force password prompt.
-H|--host Database server hostname or socket location.
-P|--port Database server port.

A typical command to import a full planet is
D:\Program Files\osm2pgsql-bin\osm2pgsql.exe -c -d gis --slim -C <cache size> -k \
--flat-nodes <flat nodes> planet-latest.osm.pbf
where
<cache size> should be equivalent to the size of the
pbf file to be imported if there is enough RAM
or about 75% of memory in MB on machines with less
<flat nodes> is a location where a 50+GB file can be saved.

A typical command to update a database imported with the above command is
osmosis --rri workingDirectory=<osmosis dir> --simc --wxc -\
|D:\Program Files\osm2pgsql-bin\osm2pgsql.exe -a -d gis --slim -k --flat-nodes <flat nodes> -r xml -
where
<flat nodes> is the same location as above.
<osmosis dir> is the location osmosis replication was initialized to.

Run D:\Program Files\osm2pgsql-bin\osm2pgsql.exe --help --verbose (-h -v) for a full list of options.
PS D:\Program Files\osm2pgsql-bin>

这里我们选择中国数据做演示:

PS D:\Program Files\osm2pgsql-bin> .\osm2pgsql.exe -U postgres -W -d osm -P 5432 -C 16000 -S .\default.style -H localhost D:\china-latest.osm.pbf
osm2pgsql version 1.2.1 (64 bit id space)

Password:
Allocating memory for sparse node cache
Node-cache: cache=16000MB, maxblocks=256000*65536, allocationmethod=1
Using built-in tag processing pipeline
Using projection SRS 3857 (Spherical Mercator)
Setting up table: planet_osm_point
Setting up table: planet_osm_line
Setting up table: planet_osm_polygon
Setting up table: planet_osm_roads

Reading in file: D:\china-latest.osm.pbf
Using PBF parser.
Processing: Node(80470k 5747.9k/s) Way(2689k 54.88k/s) Relation(0 0.00/s)
PS D:\Program Files\osm2pgsql-bin> .\osm2pgsql.exe -U postgres -W -d osm -P 5432 -C 16000 -S .\default.style -H localhost D:\china-latest.osm.pbf
osm2pgsql version 1.2.1 (64 bit id space)

Password:
Allocating memory for sparse node cache
Node-cache: cache=16000MB, maxblocks=256000*65536, allocationmethod=1
Using built-in tag processing pipeline
Using projection SRS 3857 (Spherical Mercator)
Setting up table: planet_osm_point
Setting up table: planet_osm_line
Setting up table: planet_osm_polygon
Setting up table: planet_osm_roads

Reading in file: D:\china-latest.osm.pbf
Using PBF parser.
Processing: Node(80470k 5747.9k/s) Way(2689k 54.88k/s) Relation(0 0.00/s)

-C 16000 是为了处理 Node cache size is too small to fit all nodes. Please increase cache size 错误。

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注