Skip to content

Latest commit

 

History

46 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-vector-tiler

Go Report Card License

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-tiler

快速开始

package 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
	}
}

API文档

Config

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,继续生产)
}

Exporter接口

type Exporter interface {
	SaveTile(res []*Layer, tile *Tile, path string) error
	Extension() string
	RelativeTilePath(zoom, x, y int) string
}

内置导出器

  1. MVTExporter - 导出为 MVT(Mapbox Vector Tiles)格式,spec v2.x 合规
  2. GeoJSONExporter - 导出为 GeoJSON 格式(调试用途)
  3. SVGExporter - 导出为 SVG 格式(可视化调试)

MVTExporter配置

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{},
	// 其他配置...
}

贡献指南

欢迎贡献代码!请遵循以下步骤:

  1. Fork仓库
  2. 创建特性分支 (git checkout -b feature/your-feature)
  3. 提交更改 (git commit -am 'Add some feature')
  4. 推送到分支 (git push origin feature/your-feature)
  5. 创建Pull Request

许可证

MIT License - 详见 LICENSE 文件

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages