OpenStreetMap 及 Osmosis 工具

OpenStreetMap 及 Osmosis 工具

1. 简介

2. Osmosis 工具

2.1 介绍

  • OSMwiki, github

    • 使用例子:site
  • 下载:Github releases

    • 需要安装 JDK,以及设置 JDK 的路径

      • "JAVA_HOME=C:\Program Files\Java\jdk-17"
    • 注意设置 osmosis 的环境变量

    • 推荐使用 osmosis-0.48.3 版本,osmosis-0.49.2 实测有 bug

  • 部分功能需要安装 Bzip2,官网

2.2 功能

2.3.1 根据边界截取

A. 按矩形框提取

.osm.bz2 压缩文件中提取,需要安装 Bzip2

bzcat malaysia-singapore-brunei-latest.osm.bz2 | osmosis ^
  --read-xml enableDateParsing=yes file=- ^
  --bounding-box top=1.4988 left=103.5303 bottom=1.1082 right=104.1463 ^
  --write-xml file=- ^
| bzip2 > singapore.osm.bz2
  • enableDateParsing 是否解析日期

    • enableDateParsing=yes 解析日期

或直接从 .osm 文件中截取

osmosis ^
  --read-xml file="malaysia-singapore-brunei-latest.osm" ^
    -bounding-box top=1.4988 left=103.5303 bottom=1.1082 right=104.1463 completeWays=yes completeRelations=yes ^ 
  --write-xml file="singapore.osm"

B. 按边界

按边界(Polygon)截取

bzcat "malaysia-singapore-brunei-latest.osm.bz2" | osmosis ^
  --read-xml file=- ^ 
  --bounding-polygon file="singapore.poly" completeWays=yes completeRelations=yes ^
  --write-xml file=- ^
| bzip2 > "singapore.osm.bz2"

osmosis ^
  --read-pbf file="malaysia-singapore-brunei-latest.osm.pbf" ^
  --bounding-polygon file="singapore.poly" completeWays=yes completeRelations=yes ^
  --write-pbf file="singapore.osm.pbf"
  • completeWays, completeRelations 保存(yes)还是截取(no)跨边界的元素

    • 如果需要将解析的到的路网经过 OSMnx 库进行进一步处理,注意需要设置 completeWays=yes

其中:Polygon 文件(.poly)的格式 说明。Polygon 的边界可以根据 relation 的 id,从这个网址中获取

实用工具: Polygon 的 shapefile 文件(.shp)转 poly 文件 (.poly

点击查看代码
def shppolygon_to_poly(shp_path, poly_path):
    '''
    Convert the polygon (or multi-polygon) shapefile to the poly file

    Parameters
    ----------
    shp_path (str): the file path of shapefile
    poly_path (str): the save path of poly

    Returns
    -------
    None.

    '''
    import geopandas as gpd
	
	# Read shapefile
    data = gpd.read_file(shp_path)
    crs = str(data.crs)
	
	# Convert CRS
	data = data.to_csv('EPSG:4326')
    
    # multi-polygon to polygon
    data = data.explode(ignore_index=True, index_parts=False)
    
    # write a file
    with open(poly_path, 'w') as f:
        f.write('singapore-with-sea-polygon\n')     # poly file name
        # write each area coordinates
        for i, t in enumerate(data['geometry']):
            # write a area boundary
            f.write('area_{0}\n'.format(i+1))       # area name
            for point in t.exterior.coords:
                lng, lat = point[0], point[1]
                f.write('\t{0}\t{1}\n'.format(lng, lat))
            f.write('END\n')
        f.write('END\n')
    return None
# --------------------------------------------------------------------------
# Example 
shp_path = 'MP2019-boundary-with-sea.shp'
poly_path = 'MP2019-boundary-with-sea.poly'
shppolygon_to_poly(shp_path, poly_path)

2.3.2 文件格式转换

实例:将 .osm 格式转换成 .pbf 格式

osmosis --read-xml city.osm --write-pbf city.osm.pbf

参数

  • --read-xml--write-xml 用于指定读写的 .osm 格式文件的路径

  • --read-pbf--write-pbf 用于指定读写的 .pbf 格式文件的路径

2.3.3 合并文件

2.3.4 提取或过滤标签

实例: 提取路网:site

posted @ 2022-03-12 15:34  veager  阅读(1810)  评论(0)    收藏  举报