首先奉上官网地址http://mapbox.com/developers/mbtiles/#storing_tiles
由于英文水平有限,看资料很费眼睛,特将它翻译成中文
存储瓦片
地图制作者面对一个数以百万计的地图瓦片残酷的现实:大多数文件系统和传输协议对处理数以百万计的图像不是很有效,在磁盘为FAT32格式中,一个文件夹中最多含有65536个文件,HFS最多能列出32,767个文件,EXT3超过20000个文件时会变的很慢。不论是你通过USB还是网络来复制数以百万计的瓦片数据是低效并且缓慢的。MBTiles利用SQLite数据库来存储,并提供一种规范,使得数以百万的瓦片数据存储在一个文件中,而且SQLite数据库支持多种平台,所以使用MBTiles在移动设备上浏览瓦片数据是比较理想的方式。
简单介绍下SQLITE
如果你之前使用过SQL数据库,比如MySQL或PostgreSQL),那么使用SQLite数据库会觉得很熟悉,您可以运行熟悉的SQL SELECT、INSERT、UPDATE语句,并创建表、索引、视图。SQLite和其他数据库之间的区别是:每个SQLite数据库只包含在一个文件,没有外部权限系统,数据库后台进程,或配置。每个.sqlite文件是一个独立的数据库,你可以从电脑复制一个.sqlite文件到移动设备中,它的行、表和索引都可完全使用。
SQLite是很小的并且是无处不在的:iTunes使用它来存储元数据,firfox使用它来存储缓存信息,还有一些其他产品(虽然过时了,但仍记忆犹新)
总之,SQLite非常适合作为一个便携式,单个文件解决方案和用于存储和网络地图服务。
在SQL中使用瓦片坐标
在WEB地图介绍中我们看到,瓦片是参照了他们的z / x / y 形式坐标,在磁盘存储上,他们通常存储在以z、x为名字上的目录中,这样就有一个瓦片文件路径是0/0/0.png,MBTiles提供了这样一个功能:瓦片表
sqlite> SELECT * FROM tiles; zoom_level | tile_column | tile_row | tile_data
5 | 13 | 23 | [PNG data]
5 | 13 | 24 | [PNG data]
5 | 14 | 23 | [PNG data]
5 | 14 | 24 | [PNG data]
5 | 15 | 25 | [PNG data]
这张表很容易查询并回答一个特定的瓦片或问题,例如“在这张地图中级别为8时有多少张瓦片?”
sqlite> SELECT tile_data FROM tiles WHERE zoom_level = 8 AND tile_column = 116 AND tile_row = 192; [PNG data] sqlite> SELECT COUNT(*) FROM tiles WHERE zoom_level = 8; 130
使用视图引用冗余的图像
地图覆盖大面积的纯蓝色像海洋或空的土地,造成成千上万的重复、冗余的瓦片数据,例如,4/2/8的瓦片在太平洋中间,可能看起来就是一张蓝色图片
虽然它可能是一些处于第3级,但在16级可能存在数以百万计的蓝色图片,他们都完全一样。
MBTiles通过视图使用这些冗余瓦片数据可以减少占用的空间,而不是一个单一的、文字表,MBTiles实现者经常把瓦片表分成两种:一个用来存储原始图像和一个存储瓷砖坐标对应那些图片:
CREATE TABLE images (tile_data BLOB, tile_id TEXT);
CREATE TABLE map (zoom_level INTEGER, tile_column INTEGER, tile_row INTEGER, tile_id TEXT);
瓦片的表是这两个表的视图,允许成千上万的瓷砖坐标参考相同的图像大字段:
CREATE VIEW tiles AS SELECT
map.zoom_level AS zoom_level,
map.tile_column AS tile_column,
map.tile_row AS tile_row,
images.tile_data AS tile_data
FROM map JOIN images ON images.tile_id = map.tile_id;
使用这种技术,MBTiles可以比普通文件系统存储更有效率 —有时提高60%或更多
MBTiles 在使用上
MBTiles是一种存储格式,他常被TileMill来导出或上传自定义地图。你可以通过MapBox ios SDK 来使用移动设备上MBTiles离线文件
原文如下:
PermalinkStoring tiles
Makers of web maps with millions of tiles are faced with a harsh reality: most filesystems and transfer protocols aren’t designed to handle millions of images efficiently. For files in a single directory FAT32 maxes out at 65,536, HFS cannot list files after 32,767, and EXT3 begins to slow down around 20,000. And whether transferring to a USB device or over the network, copying millions of individual tiles can be inefficient and painfully slow. The MBTiles spec provides a way of storing millions of tiles in a single SQLite database making it possible to store and transfer web maps in a single file. And because SQLite is available on so many platforms, MBTiles is an ideal format for reading tiles directly for serving on the web or displaying on mobile devices.
View the spec mbtiles-spec is an open specification on GitHub. Fork the repository and create issues or pull requests to improve the next version. .
Permalink
A short introduction to SQLite
If you’ve worked with SQL databases like MySQL or PostgreSQL before, using a SQLite database will feel very familiar. You can run familiar SQL SELECT, INSERT, UPDATE statements, and create tables, indexes, and views. The difference between SQLite and other databases is that each SQLite database is contained in a single file on disk. With no external permission systems, database daemons, or configuration, each .sqlite file is a self-contained database. You can copy a .sqlite file from desktop to mobile phone and have all its rows, tables, and indexes ready to be used.
SQLite is small and ubiquitous – it is used by iTunes to store metadata, by Firefox for caches, and many more products (a dated yet impressive list can be found here).
In short, SQLite is a great fit as a portable, single-file solution for storing and serving web maps.
Permalink
Using tile coordinates in SQL
In the introduction to web maps we saw how tiles are referenced by their z/x/y coordinates. On disk, they are often stored literally in z and x subdirectories such that they have a filesystem path like 0/0/0.png. MBTiles offers a functional equivalent to this – the tiles table:
sqlite> SELECT * FROM tiles;
zoom_level | tile_column | tile_row | tile_data
5 | 13 | 23 | [PNG data]
5 | 13 | 24 | [PNG data]
5 | 14 | 23 | [PNG data]
5 | 14 | 24 | [PNG data]
5 | 15 | 25 | [PNG data]
This table makes it easy to retrieve the image for a particular tile or answer questions like “How many tiles does this map have on zoom level 8?”
sqlite> SELECT tile_data FROM tiles WHERE zoom_level = 8 AND tile_column = 116 AND tile_row = 192;
[PNG data]
sqlite> SELECT COUNT(*) FROM tiles WHERE zoom_level = 8;
130Permalink
Using views to reference redundant images
Maps that cover large areas of solid color like ocean or empty land can contain thousands of duplicate, redundant tiles. For example, the tile 4/2/8 in the middle of the pacific ocean might look like this empty patch of blue:
While it may be a few tiles at z4, the same area covered at z16 might be millions of solid blue tiles, all exactly the same.
MBTiles can reduce the amount of space used by these redundant tiles drastically by implementing the tiles table as a view. Instead of a single, literal table, MBTiles implementers often split the tiles table into two: one to store the raw images and one to store the tile coordinates for those images:
CREATE TABLE images (tile_data BLOB, tile_id TEXT);
CREATE TABLE map (zoom_level INTEGER, tile_column INTEGER, tile_row INTEGER, tile_id TEXT);
The tiles table is defined as a view that joins the two together, allowing thousands of tile coordinates to reference the same image blob.
CREATE VIEW tiles AS SELECT
map.zoom_level AS zoom_level,
map.tile_column AS tile_column,
map.tile_row AS tile_row,
images.tile_data AS tile_data
FROM map JOIN images ON images.tile_id = map.tile_id;
Using this technique MBTiles can store tiles with lower disk usage than filesystem equivalents – sometimes 60% or more depending on the map.
Permalink
MBTiles in action
MBTiles is the storage format used to export and upload custom maps from TileMill to your MapBox account. You can also use MBTiles files offline on mobile devices with the MapBox iOS SDK.
引文连接: