title: PostGIS 矢量瓦片
date: 2023-08-07
author: ac
tags:
- vector tile
categories: - Database
Martin - 基于PostGIS的矢量瓦片服務(wù)器
1. 簡介
目前流行的矢量瓦片的切圖方案:
-
mapbox gl
+ tippecanoe :v2收費,tippecanoe是mapbox官方推薦的矢量瓦片靜態(tài)生成工具 ,適用于大數(shù)據(jù)量場景,且不頻繁更新的空間數(shù)據(jù); -
openlayers
+geoserver
:開源,使用geoserver的矢量瓦片擴展,增加矢量瓦片的輸出格式; -
maplibre
+Martin
+postgis :開源,martin是矢量切片服務(wù)器,通過postgis的函數(shù)動態(tài)生成矢量瓦片。 -
maptiler
:收費
mapbox v2 必須使用 access token 才能初始化 Map 對象。進行token計算,每月50000免費次數(shù)。
2. Martin
Martin
是一個開源的PostGIS矢量切片服務(wù)器,可以從任何PostGIS表或視圖中創(chuàng)建MVT
矢量切片,也可以從 PMTile 和MBTile文件中動態(tài)生成矢量瓦片,是使用 Rust
編寫,針對切片速度和大流量進行了優(yōu)化,是極快且輕量級的切片服務(wù)器。
2.1 安裝
如果將Martin和PostgreSQL一起使用,PostGIS版本必須為v3.0+
Martin支持Linux、macOS、windows平臺和docker環(huán)境,本例采用windows環(huán)境。先從github上下載martin-Windows-x86_64 。解壓后會發(fā)現(xiàn)這兩個exe:
─martin-Windows-x86_64
├─martin.exe
└─mbtiles.exe
windows平臺可以直接使用martin.exe
來啟動切片服務(wù)器,可以先在命令行查看一下參數(shù):
D:\tools\vectorTileTool\martin-Windows-x86_64>martin.exe --help
Blazing fast and lightweight tile server with PostGIS, MBTiles, and PMTiles support
Usage: martin.exe [OPTIONS] [CONNECTION]...
Arguments:
[CONNECTION]... Connection strings, e.g. postgres://... or /path/to/files
Options:
-c, --config <CONFIG>
Path to config file. If set, no tile source-related parameters are allowed
--save-config <SAVE_CONFIG>
Save resulting config to a file or use "-" to print to stdout. By default, only print if sources are auto-detected
-s, --sprite <SPRITE>
Export a directory with SVG files as a sprite source. Can be specified multiple times
-k, --keep-alive <KEEP_ALIVE>
Connection keep alive timeout. [DEFAULT: 75]
-l, --listen-addresses <LISTEN_ADDRESSES>
The socket address to bind. [DEFAULT: 0.0.0.0:3000]
-W, --workers <WORKERS>
Number of web server workers
-b, --disable-bounds
Disable the automatic generation of bounds for spatial PG tables
--ca-root-file <CA_ROOT_FILE>
Loads trusted root certificates from a file. The file should contain a sequence of PEM-formatted CA certificates
-d, --default-srid <DEFAULT_SRID>
If a spatial PG table has SRID 0, then this default SRID will be used as a fallback
-p, --pool-size <POOL_SIZE>
Maximum connections pool size [DEFAULT: 20]
-m, --max-feature-count <MAX_FEATURE_COUNT>
Limit the number of features in a tile from a PG table source
-h, --help
Print help
-V, --version
Print version
2.2 使用
本例pg安裝postgis是v3.3.3.1
martin
連接pg可以用connection_string
(命令行參數(shù))和配置文件兩種形式。
PostgreSQL連接字符串的形式:
# martin.exe postgresql://user:password@host/db
D:\tools\vectorTileTool\martin-Windows-x86_64>martin.exe postgresql://postgres:123@127.0.0.1:5433/postgres
[2023-08-08T03:50:11Z INFO martin] Starting Martin v0.8.7
[2023-08-08T03:50:11Z INFO martin] Config file is not specified, auto-detecting sources
[2023-08-08T03:50:11Z INFO martin::pg::pool] Connecting to postgresql://postgres:123@127.0.0.1:5433/postgres
[2023-08-08T03:50:11Z INFO martin::pg::configurator] Discovered source zhujiang_river from table public.zhujiang_river with geom column (MULTIPOLYGON, SRID=4326)
[2023-08-08T03:50:11Z INFO martin] Use --save-config to save or print Martin configuration.
[2023-08-08T03:50:11Z INFO martin] Martin has been started on 0.0.0.0:3000.
[2023-08-08T03:50:11Z INFO martin] Use http://0.0.0.0:3000/catalog to get the list of available sources.
配置文件的形式 config.yaml:
# Connection keep alive timeout [default: 75]
keep_alive: 75
# The socket address to bind [default: 0.0.0.0:3000]
listen_addresses: '127.0.0.1:3000'
# Number of web server workers
worker_processes: 8
# Database configuration. This can also be a list of PG configs.
postgres:
# Database connection string. You can use env vars too, for example:
# $DATABASE_URL
# ${DATABASE_URL:-postgresql://postgres@localhost/db}
connection_string: 'postgresql://postgres@localhost:5433/postgres?sslmode=disable&user=postgres&password=123'
# Same as PGSSLCERT for psql
#ssl_cert: './postgresql.crt'
# Same as PGSSLKEY for psql
#ssl_key: './postgresql.key'
# Same as PGSSLROOTCERT for psql
#ssl_root_cert: './root.crt'
# If a spatial table has SRID 0, then this SRID will be used as a fallback
default_srid: 4326
# Maximum connections pool size [default: 20]
pool_size: 20
# Limit the number of table geo features included in a tile. Unlimited by default.
max_feature_count: 1000
# Control the automatic generation of bounds for spatial tables [default: false]
# If enabled, it will spend some time on startup to compute geometry bounds.
disable_bounds: false
D:\tools\vectorTileTool\martin-Windows-x86_64>martin.exe -c config.yaml
[2023-08-08T03:54:43Z INFO martin] Starting Martin v0.8.7
[2023-08-08T03:54:43Z INFO martin] Using config.yaml
[2023-08-08T03:54:43Z INFO martin::pg::pool] Connecting to postgresql://postgres@localhost:5433/postgres?sslmode=disable&user=postgres&password=123
[2023-08-08T03:54:43Z INFO martin::pg::configurator] Discovered source zhujiang_river from table public.zhujiang_river with geom column (MULTIPOLYGON, SRID=4326)
[2023-08-08T03:54:44Z INFO martin] Use --save-config to save or print Martin configuration.
[2023-08-08T03:54:44Z INFO martin] Martin has been started on 127.0.0.1:3000.
[2023-08-08T03:54:44Z INFO martin] Use http://127.0.0.1:3000/catalog to get the list of available sources.
本例采用的是配置文件的形式啟動服務(wù)器,服務(wù)地址是http://127.0.0.1:3000
。
從啟動的日志可以看到Martin
將所連接的pg庫中的zhujiang_river
表發(fā)布為數(shù)據(jù)源。
Martin 會將所有表發(fā)布為數(shù)據(jù)源(如果它們至少有一個幾何列)。如果 SRID 為 0,則必須設(shè)置默認(rèn) SRID,否則該地理列/表將被忽略。所有非幾何表列都將發(fā)布為矢量切片要素標(biāo)簽(屬性)。
打開目錄可以看到已發(fā)布的數(shù)據(jù)源:
數(shù)據(jù)表的元數(shù)據(jù)是符合TileJSON 3.0.0
標(biāo)準(zhǔn)的JSON文件。
TileJSON是表示地圖元數(shù)據(jù)的開放標(biāo)準(zhǔn)
2.3 mapbox
發(fā)布完數(shù)據(jù)源后,使用mapbox gl 來調(diào)用一下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://unpkg.com/mapbox-gl@1.9.1/dist/mapbox-gl.js"></script>
<link href="https://unpkg.com/mapbox-gl@1.9.1/dist/mapbox-gl.css"/>
<style>
#map{
height: 100vh;
width: 100%;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
const TIANDITU_URL = 'http://t0.tianditu.gov.cn';
const tdtKey = "your_token";
const tdtConfig = {
code: 'tdt_img',
name: '天地圖影像',
source: {
type: 'raster',
tiles: [
`${TIANDITU_URL}/DataServer?T=img_w&x={x}&y={y}&l={z}&tk=${tdtKey}`
],
tileSize: 256,
minzoom: 0,
maxzoom: 22
},
layer: {
id: 'tdt-img-tiles',
type: 'raster',
minzoom: 0,
maxzoom: 22,
layout: { visibility: 'visible' }
}
}
let map = new mapboxgl.Map({
container:'map',
style:{
"version":8,
"sources":{},
"layers":[],
glyphs: "mapbox://fonts/mapbox/{fontstack}/{range}.pbf",
},
center: [112.39747, 22.908823], // 廣東
zoom: 8, // starting zoom 地圖初始的拉伸比例
pitch: 0, // 地圖的角度,不寫默認(rèn)是0,取值是0-85度,一般在3D中使用
bearing: 0, // 地圖的初始方向,值是北的逆時針度數(shù),默認(rèn)是0,即是正北
antialias: true, // 抗鋸齒,通過false關(guān)閉提升性能
minZoom: 3,
maxZoom: 17.36
});
map.on('load', () => {
// 天地圖底圖加載
addLayerConfig(tdtConfig);
addMvt();
});
function addLayerConfig(layerConfig){
const {code, source,layer} = layerConfig;
map.addSource(code, source);
const layerParams = {
...layer,
source: code
};
// 添加圖層
map.addLayer(layerParams);
}
function addMvt(){
map.addLayer({
id: 'lines',
type: 'fill',
source: {
type: 'vector',
url: 'http://localhost:3000/zhujiang_river'
},
'source-layer': 'zhujiang_river',
paint: {
'fill-color': 'red'
}
});
}
</script>
</body>
</html>
加載效果:
3.遇到的問題
局限性:只能連接一個pg庫。
對于有分庫的系統(tǒng),可以使用Nginx來轉(zhuǎn)發(fā)。
參考文章
[1] Martin https://martin.maplibre.org/文章來源:http://www.zghlxwxcb.cn/news/detail-643216.html
[2] Martin Tile Server Documentation https://maplibre.org/martin/installation.html文章來源地址http://www.zghlxwxcb.cn/news/detail-643216.html
到了這里,關(guān)于PostGIS 矢量瓦片的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!