如何将 mbtiles 转换为. osm. pbf 。
原标题:How to convert mbtiles to .osm.pbf
我正在撰写一个 Qt 应用程序, 它应该在它的一个视图中显示一个特定地理区域的地理图, 在其中我需要能够绘制其他图形元素。 要求是所有地图- 平面图都必须先下载下来, 以便不在网上使用, 因为应用程序使用期间没有互联网连接 。 在长时间搜索一个适合的库, 以便我可以链接到我的 Qt 项目, 从而支持我的需要( 关闭地盘装入、 绘制和绘制地图, 以 Qt 框架, 非 QML 仅 C++ 整合 ) 。 我认为 libOsmScout 可以完成这项工作。 然而, 我设法从 OpenMapTiles 下载我的区域.mbtiles 文件, 以便意识到 libOsmScout 无法与. mbtiles 一起进行本地工作。 图书馆只能用. osm. pffff 文件“ 直接” 来工作 。 (http://liboscout. forgourge. net/ test/ transport/) Iveal ress
最佳回答
Easiest way is to use ogr2ogr to convert to pbf format.
ogr2ogr -f MVT output_dir_name input.mbtiles -dsco MAXZOOM=2
This will create a directory structure with each zoom level having a directory and pbf file named according to the tile position.
Or
using this tool called MBUtil.
There are some steps which you can follow from the github repo. After that, you can then use the following command to convert to directory structure with each tile in pbf format.
mb-util input.mbtiles output_dir_name image_format=pbf
where tiles is the directory name you want
问题回答
In the past, I ve used the mb-utils approach. However, working cross-platform and with multiple versions of Python, it has become really time consuming. I recommend using ogr2ogr (as Aman Bagrecha posted). Here s a script that will convert every .mbtiles file in a directory. (NOTE: It deletes the .mbtile files so modify if you don t want that).
import os
import sqlite3
def get_zoom_levels(filename):
# Connect to the SQLite database file
conn = sqlite3.connect(filename)
cursor = conn.cursor()
# Fetch minzoom and maxzoom from metadata
cursor.execute("SELECT value FROM metadata WHERE name= minzoom ;")
minzoom = cursor.fetchone()[0]
cursor.execute("SELECT value FROM metadata WHERE name= maxzoom ;")
maxzoom = cursor.fetchone()[0]
# Close the connection
conn.close()
return minzoom, maxzoom
new_line = "
"
print("Requires Python 3! Use python3 from the command line if needed.")
srcDir = os.getcwd()
mbtilesFileCount = 0
# Counting mbtiles files
for file in os.listdir(srcDir):
if file.endswith(".mbtiles"):
mbtilesFileCount += 1
print(f"{mbtilesFileCount} mbtiles file(s) found{new_line}")
# Processing each mbtiles file
for file in os.listdir(srcDir):
if file.endswith(".mbtiles"):
newDirName = os.path.basename(file).split( . )[0]
# Check if the directory already exists
if os.path.exists(newDirName):
print(f"Error: Directory {newDirName} already exists. Skipping command.")
continue # Skip to the next iteration of the loop
# This gets the actual zoom levels needed to correctly output the sub-directories
minzoom, maxzoom = get_zoom_levels(file)
command = f ogr2ogr -f MVT {newDirName} {file} -dsco MINZOOM={minzoom} -dsco MAXZOOM={maxzoom}
print(f Running {command} )
result = os.system(command)
# If command was successful, delete the file
if result == 0:
print(f"Deleting {file} after successful processing.")
os.remove(file)
else:
print(f"Failed to process {file}. File not deleted.")
相关问题
Do types in QT applications for different platforms have similar size?
I created an application for Windows in C++ using QT. If I want to port it to Linux or Mac OS, will sizeof(int) or sizeof(long) change? In other words, do types in QT applications for different ...
Qt: Do events get processed in order?
If I had a class A, where one of its functions does:
void A::func()
{
emit first_signal();
emit second_signal();
}
Assuming that a class B has 2 slots, one connected to first_signal, and the ...
How to determine how much free space on a drive in Qt?
I m using Qt and want a platform-independent way of getting the available free disk space.
I know in Linux I can use statfs and in Windows I can use GetDiskFreeSpaceEx(). I know boost has a way, ...
Getting only one dimension of indexes from the getSelectedIndexes function in QT?
I m working on a small project in QT (well, pyQT4 actually, but it shouldn t matter too much) and I ve run into the following problem. I have a QTableView with several rows and columns. I have set the ...
Drag & drop with .ui files
I m having big troubles with drag & drop. I ve created a new Qt Designer Form Class in which I have one QListWidget and one QWidget. Now I want to enable dragging & dropping between these two ...
Link errors on Snow Leopard
I creating a small desktop application using Qt and Poco on Mac OS X Snow Leopard.
Qt works fine, but once I started linking with Poco I get the following warning:
ld: warning: in /Developer/SDKs/...
Showing two windows in Qt4
My friend and I have each created parts of a GUI using Qt 4. They both work independently and I am trying to integrate his form with the my main window. As of now this is the code I am using to try ...
Qt equivalent of .NET data binding?
Is there an equivalent of .NET s data binding in Qt?
I want to populate some combo boxes and other widgets with QStrings that refer to specific entities in my database. However, it would be cleaner ...