shp转osm

1,653 阅读4分钟

使用JOSM

该软件可在win平台上使用安装。

安装包下载:josm.openstreetmap.de/

安装好后打开软件,进入影像--影像首选项 image.png

然后插件栏中搜索opend插件,点击更新插件即可安装

image.png

安装完毕后即可在文件栏中打开shp文件(只能是wgs84坐标数据)并且另存为osm文件。

注意事项

osm文件本质上是xml格式的地理信息文件,如我将拥有以下字段的link.shp(多段线数据)转化为osm数据后:

转换前的shp文件字段:

image.png

转换后的osm数据中分为两部分:node节点数据和way节点数据:

node节点数据记录的是线节点(多段线的端点和中间折点)的坐标信息,id是JOSM软件自动生成的。

image.png

一个way节点对应一个多段线即一个link,在way节点下会有nd节点和tag节点,nd节点记录的是该多段线拓扑方向的节点序列,下图表示id为'-103231'的link拓扑正方向的节点序列是节点'-137717'、节点'-137718'、节点'-137719',tag节点中的k对应shp文件中的字段名称,v对应字段值,原shp文件共有8个字段,所以一个way节点下有8个tag节点。

image.png

但是shp文件中的字段完全由用户自定义,所以为了得到合法的osm文件,我们需要规范shp文件的字段名称。我们先从osm网站上下载一个osm文件,打开即可看到其内容组织规范:

node节点:

image.png

way节点:

image.png

image.png

看到了tag节点下的k值有bicycle、footway、highway、lanes、name、name:en、name:zh,但是每个way节点下的tag标签k值不一样。

使用org2osm

ubuntu下可以使用org2osm来进行转化

org2osm官网地址:wiki.openstreetmap.org/wiki/Ogr2os…

org2osm简介

官网简介:ogr2osm is a Python script which converts any OGR supported vector format into .osm or .pbf format, with an emphasis on converting polygon boundaries into relations.

是一个python脚本,可将任何图层矢量数据格式转化为.osm格式或者.pbf格式。

其支持的OGR supported vector format在以下页面中列举了出来: gdal.org/drivers/vec…

org2osm安装

  1. 先安装python3

  2. GDAL相关的依赖安装

sudo apt-get update
sudo apt-get install -y software-properties-common
apt-get install -y gdal-bin
sudo apt-get install -y libgdal-dev
export CPLUS_INCLUDE_PATH=/usr/include/gdal
export C_INCLUDE_PATH=/usr/include/gdal
pip install -i http://pypi.douban.com/simple --trusted-host pypi.douban.com GDAL==$(gdal-config --version)
  1. 安装org2osm
apt install git # 确保安装了git
git clone https://github.com/roelderickx/ogr2osm.git
cd ogr2osm
git submodule update --init
pip3 install .

org2osm使用

命令行键入ogr2osm --help即可查看使用说明

root@f969b07be118:/tk/test# ogr2osm --help
usage: ogr2osm [-h] [--version] [-t TRANSLATION] [--encoding ENCODING] [--sql SQLQUERY] [--no-memory-copy] [-e EPSG_CODE] [-p PROJ4_STRING] [--gis-order] [--rounding-digits ROUNDINGDIGITS] [--significant-digits SIGNIFICANTDIGITS]
               [--split-ways MAXNODESPERWAY] [--id ID] [--idfile IDFILE] [--saveid SAVEID] [-o OUTPUT] [-f] [--pbf] [--no-upload-false] [--never-download] [--never-upload] [--locked] [--add-bounds] [--suppress-empty-tags]
               DATASOURCE

positional arguments:
  DATASOURCE            DATASOURCE can be a file path or a org PostgreSQL connection string such as: "PG:dbname=pdx_bldgs user=emma host=localhost" (including the quotes)

optional arguments:
  -h, --help            show this help message and exit
  --version             show program's version number and exit
  -t TRANSLATION, --translation TRANSLATION
                        Select the attribute-tags translation method. See the translations/ directory for valid values.
  --encoding ENCODING   Encoding of the source file. If specified, overrides the default of utf-8
  --sql SQLQUERY        SQL query to execute on a PostgreSQL source
  --no-memory-copy      Do not make an in-memory working copy
  -e EPSG_CODE, --epsg EPSG_CODE
                        EPSG code of source file. Do not include the 'EPSG:' prefix. If specified, overrides projection from source metadata if it exists.
  -p PROJ4_STRING, --proj4 PROJ4_STRING
                        PROJ.4 string. If specified, overrides projection from source metadata if it exists.
  --gis-order           Consider the source coordinates to be in traditional GIS order
  --rounding-digits ROUNDINGDIGITS
                        Number of decimal places for rounding when snapping nodes together (default: 7)
  --significant-digits SIGNIFICANTDIGITS
                        Number of decimal places for coordinates to output (default: 9)
  --split-ways MAXNODESPERWAY
                        Split ways with more than the specified number of nodes. Defaults to 1800. Any value below 2 - do not split.
  --id ID               ID to start counting from for the output file. Defaults to 0.
  --idfile IDFILE       Read ID to start counting from from a file.
  --saveid SAVEID       Save last ID after execution to a file.
  -o OUTPUT, --output OUTPUT
                        Set destination .osm file name and location.
  -f, --force           Force overwrite of output file.
  --pbf                 Write the output as a PBF file in stead of an OSM file
  --no-upload-false     Omit upload=false from the completed file to suppress JOSM warnings when uploading.
  --never-download      Prevent JOSM from downloading more data to this file.
  --never-upload        Completely disables all upload commands for this file in JOSM, rather than merely showing a warning before uploading.
  --locked              Prevent any changes to this file in JOSM, such as editing or downloading, and also prevents uploads. Implies upload="never" and download="never".
  --add-bounds          Add boundaries to output file
  --suppress-empty-tags
                        Suppress empty tags

简单使用:将当前目录下的link.shp文件转化为link.osm

root@f969b07be118:/tk/test# ls
LINK.DBF  link.osm  link.prj  link.shp  link.shx

root@f969b07be118:/tk/test# ogr2osm link.shp
Using default translations
Preparing to convert 'link.shp' to '/tk/test/link.osm'.
Detected projection metadata:
GEOGCS["WGS 84",
    DATUM["WGS_1984",
        SPHEROID["WGS 84",6378137,298.257223563,
            AUTHORITY["EPSG","7030"]],
        AUTHORITY["EPSG","6326"]],
    PRIMEM["Greenwich",0,
        AUTHORITY["EPSG","8901"]],
    UNIT["degree",0.0174532925199433,
        AUTHORITY["EPSG","9122"]],
    AXIS["Latitude",NORTH],
    AXIS["Longitude",EAST],
    AUTHORITY["EPSG","4326"]]
Splitting long ways
Writing file header
Writing nodes
Writing ways
Writing relations
Writing file footer

root@f969b07be118:/tk/test# ls
LINK.DBF  link.osm  link.prj  link.shp  link.shx