go-vector-tiler 是一个高性能的矢量瓦片生成库,用于将地理空间数据转换为矢量瓦片(Mapbox Vector Tiles)格式。支持多层级、大范围的 MVT/PBF 生产,输出符合 Mapbox Vector Tile Specification v2.x。
- 支持多层级、大范围瓦片生产(全球 z0-z22)
- 输出符合 MVT spec v2.x:按瓦片裁剪+buffer、量化、退化几何清理、多边形环方向强制(外环正面积/内环负面积)、layer version=2、keys/values 字典去重
- 支持多线程并发处理,单瓦片失败不中断(可配置 FailFast)
- 可配置的几何简化(Douglas-Peucker,按 zoom 自适应容差)
- 支持 Web 墨卡托和 WGS84 坐标系输入
- 提供 MVT(可选 gzip 压缩 / .pbf 扩展名)、GeoJSON、SVG 三种输出格式
- 支持 recipe.json 与标准 tilejson.json 元数据生成
- 完善的错误处理(重试、错误收集)和进度监控
go get github.com/flywave/go-vector-tilerpackage main
import (
tile "github.com/flywave/go-vector-tiler"
)
func main() {
// 创建配置
config := &tile.Config{
Provider: &MyDataProvider{}, // 实现 Provider 接口
MinZoom: 0,
MaxZoom: 14,
Concurrency: 4,
OutputDir: "./tiles",
Exporter: tile.NewMVTExporter(),
}
// 创建Tiler实例
tiler, err := tile.NewTiler(config)
if err != nil {
panic(err)
}
// 生成瓦片
if err := tiler.Tiler(); err != nil {
panic(err)
}
// 非 FailFast 模式下,单瓦片错误不会中断任务,可在此检查
for _, err := range tiler.GetErrors() {
// 记录/上报错误
_ = err
}
}type Config struct {
Provider Provider // 数据提供者
Progress Progress // 进度监控
TileExtent uint64 // 瓦片范围(默认32768)
TileBuffer uint64 // 瓦片缓冲区(默认64)
SimplifyGeometries bool // 是否简化几何(默认true)
SimplificationMaxZoom uint // 最大简化级别(默认10)
Concurrency int // 并发数(默认4)
MinZoom int // 最小级别(默认0)
MaxZoom int // 最大级别(默认14)
SpecificZooms []int // 指定级别
Bound *[4]float64 // 边界范围(config.SRS 坐标)
SRS string // 空间参考系统
Exporter Exporter // 导出器
OutputDir string // 输出目录
TileRange *TileRange // 瓦片范围限制
Retry *RetryConfig // 重试配置
FailFast bool // true=首个错误即中断(默认false,继续生产)
}type Exporter interface {
SaveTile(res []*Layer, tile *Tile, path string) error
Extension() string
RelativeTilePath(zoom, x, y int) string
}- MVTExporter - 导出为 MVT(Mapbox Vector Tiles)格式,spec v2.x 合规
- GeoJSONExporter - 导出为 GeoJSON 格式(调试用途)
- SVGExporter - 导出为 SVG 格式(可视化调试)
type MVTOptions struct {
FileMode os.FileMode // 文件权限(默认0644)
DirMode os.FileMode // 目录权限(默认0755)
Proto mvt.ProtoType // 协议版本(默认PROTO_MAPBOX)
UseEmptyTile bool // 空瓦片写入占位文件(默认true)
SkipEmptyTiles bool // 跳过空瓦片不写盘(默认false)
BufferSize int // IO缓冲区大小(默认16KB)
Extent int32 // MVT坐标网格(默认4096, spec惯例)
GzipCompress bool // 写盘时gzip压缩(默认false)
Ext string // 文件扩展名(默认"mvt", 可配"pbf")
GenerateMetadata bool // 生成recipe.json元数据
MetadataPath string // recipe.json路径
SourceURL string // 数据源URL(写入元数据)
GenerateTileJSON bool // 生成标准tilejson.json
TileJSONPath string // tilejson.json路径(默认"tilejson.json")
TilesURL string // 瓦片URL模板(写入tilejson)
}// 创建自定义MVT导出器
options := tile.DefaultMVTOptions
options.SkipEmptyTiles = true
options.GzipCompress = true
options.Ext = "pbf"
options.GenerateTileJSON = true
exporter := tile.NewMVTExporterWithOptions(options)
// 创建配置
config := &tile.Config{
Provider: &MyDataProvider{},
MinZoom: 0,
MaxZoom: 14,
Concurrency: 8,
Exporter: exporter,
OutputDir: "./mvt_tiles",
}type MyProgress struct{}
func (p *MyProgress) Init(total int) {
fmt.Printf("开始处理,总任务数: %d\n", total)
}
func (p *MyProgress) Update(current, total int) {
fmt.Printf("进度: %d/%d\n", current, total)
}
func (p *MyProgress) Complete() {
fmt.Println("处理完成")
}
func (p *MyProgress) Log(format string, args ...interface{}) {
fmt.Printf(format+"\n", args...)
}
func (p *MyProgress) Warn(format string, args ...interface{}) {
fmt.Printf("警告: "+format+"\n", args...)
}
// 使用自定义进度监控
config := &tile.Config{
Progress: &MyProgress{},
// 其他配置...
}欢迎贡献代码!请遵循以下步骤:
- Fork仓库
- 创建特性分支 (
git checkout -b feature/your-feature) - 提交更改 (
git commit -am 'Add some feature') - 推送到分支 (
git push origin feature/your-feature) - 创建Pull Request
MIT License - 详见 LICENSE 文件