1. Pydriosm简介
Pydriosm是一个开源的OSM地图文件下载和解析工具。通过OSM官方API为研究人员/从业者提供OSM地图文件在线下载和本地OSM地图文件读取解析功能,以及便捷的PostgreSQL数据库接口。
2. 安装
pip install pydriosm
3. 使用方法
3.1 下载数据
(1)查看支持的文件下载格式
>>>from pydriosm import GeofabrikDownloader
>>>geofabrik_downloader = GeofabrikDownloader()
>>>geofabrik_download_catalogue = geofabrik_downloader.get_download_catalogue()
>>>print(geofabrik_download_catalogue.columns.tolist())
['Subregion', 'SubregionURL', '.osm.pbf', '.osm.pbf.Size', '.shp.zip', '.osm.bz2']
(2)查看下载链接列表
>>>print(geofabrik_download_catalogue.head())
Subregion ... .osm.bz2
0 Algeria ... http://download.geofabrik.de/africa/algeria-la...
1 Angola ... http://download.geofabrik.de/africa/angola-lat...
2 Benin ... http://download.geofabrik.de/africa/benin-late...
3 Botswana ... http://download.geofabrik.de/africa/botswana-l...
4 Burkina Faso ... http://download.geofabrik.de/africa/burkina-fa...
[5 rows x 6 columns]
(3)下载地图文件
>>>subregion_name = 'London' # case-insensitive
>>>osm_file_format = ".pbf" # or ".osm.pbf"
>>>download_dir = "tests"
# Download the OSM PBF data of London from Geofabrik
>>>geofabrik_downloader.download_osm_data(subregion_name, osm_file_format,
download_dir, verbose=True)
Confirmed to download .osm.pbf data of the following geographic region(s):
Greater London
? [No]|Yes: yes
Downloading "greater-london-latest.osm.pbf" to "\tests" ...
Done.
(4)下载多个文件
>>>subregion_names = ['Rutland', 'West Yorkshire', 'West Midlands']
>>>paths_to_pbf = geofabrik_downloader.download_osm_data(
subregion_names, osm_file_format, download_dir, ret_download_path=True)
verbose=True)
Confirmed to download .osm.pbf data of the following geographic region(s):
Rutland
West Yorkshire
West Midlands
? [No]|Yes: yes
Downloading "rutland-latest.osm.pbf" to "\tests" ...
Done.
Downloading "west-yorkshire-latest.osm.pbf" to "\tests" ...
Done.
Downloading "west-midlands-latest.osm.pbf" to "\tests" ...
Done.
>>>print(type(path_to_pbf))
<class 'list'>
>>>for path_to_pbf in paths_to_pbf:
>>> print(f"'{os.path.relpath(path_to_pbf)}'")
'tests\rutland-latest.osm.pbf'
'tests\west-yorkshire-latest.osm.pbf'
'tests\west-midlands-latest.osm.pbf'
3.2 解析文件
(1)解析文件
>>>from pydriosm import GeofabrikReader
>>>geofabrik_reader = GeofabrikReader()
>>>subregion_name = 'Rutland'
>>>data_dir = download_dir # "tests"
>>>rutland_pbf_raw = geofabrik_reader.read_osm_pbf(subregion_name, data_dir)
>>>print(type(rutland_pbf_raw))
<class 'dict'>
(2)检查point层
>>>rutland_pbf_points = rutland_pbf_raw['points']
>>>print(rutland_points.head())
points
0 {"type": "Feature", "geometry": {"type": "Poin...
1 {"type": "Feature", "geometry": {"type": "Poin...
2 {"type": "Feature", "geometry": {"type": "Poin...
3 {"type": "Feature", "geometry": {"type": "Poin...
4 {"type": "Feature", "geometry": {"type": "Poin...
(3)转换为json格式
rutland_pbf_points 中每一行都是GeoJSON格式的嵌套字典,因此可以转换为json格式:
>>>import json
>
>>>rutland_pbf_points_0 = rutland_pbf_points['points'][0]
>>>print(type(rutland_pbf_points_0))
<class 'str'>
>>>rutland_pbf_points_0_ = json.loads(rutland_pbf_points_0)
>>>print(type(rutland_pbf_points_0_))
<class 'dict'>
>>>print(list(rutland_pbf_points_0_.keys()))
['type', 'geometry', 'properties', 'id']
3.3 不同geometry 类型及其数据结构
对osm文件解析后可以得到文件中不同的geometry 类型(Point, Line,Multiline,MultiPolygon,Geometry Collection):
4. 总结
pydriosm主要特色可总结为:
- 在线文件下载:可输入区域名称下载文件,但受所选择OSM官方API限制,对国内支持稍差,某些地区可能无法匹配;
- 本地文件解析:可对’.pbf/.osm.pbf’,’.shp.zip/.shp’解析,按point,line,multiline,multipolygon,relation等分类存储,便于后续数据进一步过滤,用户可根据个人需求进行数据提取;
- PostgreSQL数据库API:习惯使用数据库的用户,可以直接将解析数据存入数据库。
更多功能请访问原文档解锁。
参考
- https://pydriosm.readthedocs.io/en/latest/index.html
- https://pypi.org/project/pydriosm/