本文主要是介绍【GIS开发】批量地图瓦片转mbtiles文件(Python),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- 1、简介
- 2、下载和编译
- 3、命令行方式运行
- 3.1 瓦片图片转mbtiles文件
- 3.2 mbtiles文件转瓦片图片
- 4、脚本方式运行
- 4.1 安装nose库
- 4.2 编写脚本
- 4.3 运行脚本
- 5 加载mbtiles文件
- 5.1 osgEarth加载mbtiles文件
- 6、导出mbtiles中的图片
- 后记
1、简介
MBTiles文件,是指由MapBox制定的一种将瓦片地图数据存储到SQLite数据库中并可快速使用,管理和分享的规范。MBTiles文件内容数据是按照OSGeo的TMS规范来生成和组织的地图瓦片,且支持的图片存储类型为PNG和JPG。MBTiles实质上,是由一个SQLite包装起256*256大小的瓦片地图图片。透过数据库索引的方式提高瓦片索引的效率。据说,这种方式比文件夹方式的瓦片组织要高效得多。
MBTiles的标准在Github或者OpenStreetWiki或者Mapbox主页上可以找到,具体格式不再这里赘述。
https://github.com/mapbox/mbtiles-spec
https://wiki.openstreetmap.org/wiki/MBTiles
https://docs.mapbox.com/help/getting-started/
MBUtil程序库,是用于导入和导出 MBTiles格式的实用程序,通常使用 Mapbox [TileMill](http:// mapbox.com/tilemill/)。
2、下载和编译
https://github.com/mapbox/mbutil
https://pypi.org/project/mbutil/
将代码解压如下:
在命令行(cmd)运行如下代码,进行mbutil库的下载和安装操作:
git clone git://github.com/mapbox/mbutil.git
cd mbutil
python setup.py install
在命令行(cmd)运行如下代码,执行如下语句查询mbutil的使用帮助信息。
python mb-util -h
3、命令行方式运行
3.1 瓦片图片转mbtiles文件
MBUtil 将元数据导入和导出为 JSON,在 tile 目录的根目录中,保存名为 metadata.json
的文件。
{"name": "World Light","description": "A Test Metadata","version": "3"
}
或者
{"name": "ZYX Tiles","type": "baselayer","description": "","version": "1","format": "png"
}
下载网上的瓦片数据,可以通过本人另外一篇文章:【GIS开发】批量下载和拼接地图瓦片(python)
将下载的瓦片图片数据准备好,如下图所示:
在命令行运行如下代码:
# 打开 mbutil 所在文件夹
cd E:\Python\mbutil
# TMS瓦片转换为 .mbutiles文件
python mb-util d:\maps\ d:\test.mbtiles
3.2 mbtiles文件转瓦片图片
python mb-util C:\Users\tomcat\Desktop\test\test.mbtiles C:\Users\tomcat\Desktop\test2
第一个参数是python的路径(python.exe)
第二个路径是MBUtil工具的路径(mb-util)
第三个参数是压缩的地图切片路径(.mbtiles)
第四个参数是解压后切片存储的路径(必须是不存在的文件夹)
4、脚本方式运行
4.1 安装nose库
测试框架nose库(nosetests)。
pip install nose
4.2 编写脚本
test.py:
import os, shutil
import sys
import json
from nose import with_setup
from mbutil import mbtiles_to_disk, disk_to_mbtilesdef setup_test():print('setup_test 将用于 with_setup')clear_data()def teardown_test():print('teardown_test 也将用于 with_setup')def clear_data():try: shutil.rmtree('D:/test/output')except Exception: pass@with_setup(setup_test, teardown_test)
def test_mbtiles_to_disk():mbtiles_to_disk('D:/test/test1.mbtiles', 'D:/test/output')assert os.path.exists('D:/test/output/0/0/0.png')assert os.path.exists('D:/test/output/metadata.json')@with_setup(setup_test, teardown_test)
def test_mbtiles_to_disk_and_back():# 分解文件mbtiles_to_disk('D:/test/test1.mbtiles', 'D:/test/output')assert os.path.exists('D:/test/output/0/0/0.png')assert os.path.exists('D:/test/output/metadata.json')# 合并文件disk_to_mbtiles('D:/test/output', 'D:/test/output/test2.mbtiles')assert os.path.exists('D:/test/output/test2.mbtiles')# 再分解文件mbtiles_to_disk('D:/test/output/test2.mbtiles', 'D:/test/output/test2')assert os.path.exists('D:/test/output/test2/0/0/0.png')assert os.path.exists('D:/test/output/test2/metadata.json')
4.3 运行脚本
nosetests -v D:\test\test.py
5 加载mbtiles文件
5.1 osgEarth加载mbtiles文件
将上文生成的mbtiles文件放在osgEarth的指定数据文件夹里,如下图所示:
新建一个.earth文件,用来加载mbtiles文件,如下图所示:
编写批处理命令文件,方便osgEarthViewer.exe运行时参数传入,如下图所示:
osgEarthViewer.exe运行后,结果如下所示:
6、导出mbtiles中的图片
由于mbtiles文件实际就是一个sqlite3数据库文件,所以可以按照数据库的方式导出里面的资源。
import sqlite3def writeTofile(data, filename):# Convert binary data to proper format and write it on Hard Diskwith open(filename, 'wb') as file:file.write(data)print("Stored blob data into: ", filename, "\n")def readBlobData(empId):try:sqliteConnection = sqlite3.connect('d:\\out\\MapWorld.mbtiles')cursor = sqliteConnection.cursor()print("Connected to SQLite")sql_fetch_blob_query = """SELECT * from tiles where tile_row = ?"""cursor.execute(sql_fetch_blob_query, (empId,))record = cursor.fetchall()for row in record:print("Id = ", row[0], "Name = ", row[1])tile_column = row[1]tile_row = row[2]tile_data = row[3]print("Storing employee image and resume on disk \n")photoPath = "d:\\out\\earth" + str(tile_column) + "_"+ str(tile_row) + ".tif"writeTofile(tile_data, photoPath)cursor.close()except sqlite3.Error as error:print("Failed to read blob data from sqlite table", error)finally:if sqliteConnection:sqliteConnection.close()print("sqlite connection is closed")readBlobData(1)
readBlobData(2)
后记
如果你觉得该方法或代码有一点点用处,可以给作者点个赞、赏杯咖啡;╮( ̄▽ ̄)╭
如果你感觉方法或代码不咋地//(ㄒoㄒ)//,就在评论处留言,作者继续改进。o_O???
谢谢各位童鞋们啦( ´ ▽´ )ノ ( ´ ▽´)っ!!!
这篇关于【GIS开发】批量地图瓦片转mbtiles文件(Python)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!