go-vector-tiler 是一个高性能的矢量瓦片生成库,用于将地理空间数据转换为矢量瓦片(Mapbox Vector Tiles)格式。
- 支持多种矢量数据格式输入
- 高效的瓦片生成算法
- 支持多线程并发处理
- 可配置的几何简化
- 支持Web墨卡托和WGS84坐标系
- 提供GeoJSON和MVT两种输出格式
- 完善的错误处理和进度监控
go get github.com/flywave/go-vector-tilerpackage main
import (
"github.com/flywave/go-vector-tiler/tile"
)
func main() {
// 创建配置
config := &tile.Config{
Provider: &MyDataProvider{}, // 实现Provider接口
MinZoom: 0,
MaxZoom: 14,
Concurrency: 4,
OutputDir: "./tiles",
Exporter: tile.NewMVTExporter(),
}
// 创建Tiler实例
tiler := tile.NewTiler(config)
// 生成瓦片
if err := tiler.Tiler(); err != nil {
panic(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 // 边界范围
SRS string // 空间参考系统
Exporter Exporter // 导出器
OutputDir string // 输出目录
}type Exporter interface {
SaveTile(res []*Layer, tile *Tile, path string) error
Extension() string
RelativeTilePath(zoom, x, y int) string
}- GeoJSONExporter - 导出为GeoJSON格式
- MVTExporter - 导出为MVT(Mapbox Vector Tiles)格式
type MVTOptions struct {
FileMode os.FileMode // 文件权限(默认0644)
DirMode os.FileMode // 目录权限(默认0755)
Proto mvt.ProtoType // 协议版本(默认PROTO_MAPBOX)
UseEmptyTile bool // 使用空瓦片(默认true)
BufferSize int // 缓冲区大小(默认16KB)
}// 创建自定义MVT导出器
options := tile.MVTOptions{
FileMode: 0644,
DirMode: 0755,
Proto: mvt.PROTO_MAPBOX,
UseEmptyTile: true,
BufferSize: 1024 * 32, // 32KB
}
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) Log(msg string) {
fmt.Println(msg)
}
func (p *MyProgress) Warn(msg string, args ...interface{}) {
fmt.Printf("警告: "+msg+"\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 文件