-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtile_encoder.go
More file actions
166 lines (139 loc) · 3.88 KB
/
tile_encoder.go
File metadata and controls
166 lines (139 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//go:build opencv
// +build opencv
package maptiles
import (
"bytes"
"fmt"
"image"
"image/jpeg"
"image/png"
"math"
"gocv.io/x/gocv"
)
// TileFormat identifies tile encoding format.
type TileFormat string
const (
FormatPNG TileFormat = "png"
FormatJPEG TileFormat = "jpeg"
FormatWebP TileFormat = "webp"
)
// TileEncoderConfig stores map tile encoding preferences.
type TileEncoderConfig struct {
Format TileFormat
TileSize int
Quality int
Compression int
}
// TileCoordinate identifies tile in slippy map scheme (zoom/x/y).
type TileCoordinate struct {
Zoom int
X int
Y int
}
// LatLng represents geographic coordinates.
type LatLng struct {
Lat float64
Lng float64
}
// TileEncoder generates map tiles from imagery and SLAM data.
type TileEncoder struct {
config TileEncoderConfig
}
// NewTileEncoder creates tile encoder with specified config.
func NewTileEncoder(cfg TileEncoderConfig) (*TileEncoder, error) {
if cfg.TileSize == 0 {
cfg.TileSize = 256
}
if cfg.Quality == 0 {
cfg.Quality = 80
}
if cfg.Compression == 0 {
cfg.Compression = 6
}
if cfg.Format == "" {
cfg.Format = FormatPNG
}
return &TileEncoder{config: cfg}, nil
}
// Encode converts image to tile bytes in configured format.
func (e *TileEncoder) Encode(img image.Image) ([]byte, error) {
buf := &bytes.Buffer{}
switch e.config.Format {
case FormatPNG:
encoder := png.Encoder{CompressionLevel: png.CompressionLevel(e.config.Compression)}
if err := encoder.Encode(buf, img); err != nil {
return nil, fmt.Errorf("encode png: %w", err)
}
case FormatJPEG:
if err := jpeg.Encode(buf, img, &jpeg.Options{Quality: e.config.Quality}); err != nil {
return nil, fmt.Errorf("encode jpeg: %w", err)
}
case FormatWebP:
mat, err := gocv.ImageToMatRGB(img)
if err != nil {
return nil, fmt.Errorf("image to mat for webp: %w", err)
}
defer mat.Close()
encoded, err := gocv.IMEncode(".webp", mat)
if err != nil {
return nil, fmt.Errorf("encode webp: %w", err)
}
defer encoded.Close()
return encoded.GetBytes(), nil
default:
return nil, fmt.Errorf("unsupported format: %s", e.config.Format)
}
return buf.Bytes(), nil
}
// EncodeFromMat converts OpenCV Mat to tile bytes.
func (e *TileEncoder) EncodeFromMat(mat gocv.Mat) ([]byte, error) {
if mat.Empty() {
return nil, fmt.Errorf("empty mat")
}
// Resize to tile dimensions
resized := gocv.NewMat()
defer resized.Close()
gocv.Resize(mat, &resized, image.Pt(e.config.TileSize, e.config.TileSize), 0, 0, gocv.InterpolationLinear)
img, err := resized.ToImage()
if err != nil {
return nil, fmt.Errorf("mat to image: %w", err)
}
return e.Encode(img)
}
// LatLngToTile converts geographic coordinates to tile coordinate at zoom level.
func LatLngToTile(lat, lng float64, zoom int) TileCoordinate {
n := math.Pow(2.0, float64(zoom))
x := int((lng + 180.0) / 360.0 * n)
latRad := lat * math.Pi / 180.0
y := int((1.0 - math.Log(math.Tan(latRad)+(1.0/math.Cos(latRad)))/math.Pi) / 2.0 * n)
return TileCoordinate{Zoom: zoom, X: x, Y: y}
}
// TileToLatLng converts tile coordinate to northwest corner geographic coordinates.
func TileToLatLng(tile TileCoordinate) LatLng {
n := math.Pow(2.0, float64(tile.Zoom))
lng := float64(tile.X)/n*360.0 - 180.0
latRad := math.Atan(math.Sinh(math.Pi * (1.0 - 2.0*float64(tile.Y)/n)))
lat := latRad * 180.0 / math.Pi
return LatLng{Lat: lat, Lng: lng}
}
// TilePath generates filesystem path for tile storage.
func TilePath(tile TileCoordinate, format TileFormat) string {
ext := "png"
switch format {
case FormatJPEG:
ext = "jpg"
case FormatWebP:
ext = "webp"
}
return fmt.Sprintf("%d/%d/%d.%s", tile.Zoom, tile.X, tile.Y, ext)
}
// TileBounds returns geographic bounds of a tile.
func TileBounds(tile TileCoordinate) (nw, se LatLng) {
nw = TileToLatLng(tile)
se = TileToLatLng(TileCoordinate{
Zoom: tile.Zoom,
X: tile.X + 1,
Y: tile.Y + 1,
})
return nw, se
}