/
mapnik.go
198 lines (168 loc) · 4.24 KB
/
mapnik.go
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package main
import (
"fmt"
"image"
"image/draw"
"log"
"os"
"sync"
"time"
"github.com/natefinch/pie"
"github.com/omniscale/go-mapnik/v2"
"github.com/omniscale/magnacarto/render"
)
func renderReq(m *mapnik.Map, mapReq render.Request) ([]byte, error) {
if useProj4 {
m.SetSRS(fmt.Sprintf("+init=epsg:%d", mapReq.EPSGCode))
} else {
m.SetSRS(fmt.Sprintf("epsg:%d", mapReq.EPSGCode))
}
m.Resize(mapReq.Width, mapReq.Height)
m.ZoomTo(mapReq.BBOX[0], mapReq.BBOX[1], mapReq.BBOX[2], mapReq.BBOX[3])
renderOpts := mapnik.RenderOpts{}
renderOpts.Format = mapReq.Format
renderOpts.ScaleFactor = mapReq.ScaleFactor
if mapReq.BGColor == nil {
b, err := m.Render(renderOpts)
if err != nil {
return nil, err
}
return b, nil
}
// draw image on requested bgcolor
img, err := m.RenderImage(renderOpts)
if err != nil {
return nil, err
}
result := image.NewNRGBA(img.Bounds())
bg := image.NewUniform(mapReq.BGColor)
draw.Draw(result, img.Bounds(), bg, image.ZP, draw.Src)
draw.Draw(result, img.Bounds(), img, image.ZP, draw.Over)
b, err := mapnik.Encode(result, mapReq.Format)
if err != nil {
return nil, err
}
return b, nil
}
type API struct {
// cacheEnabled enables caching of a single mapfile.
cacheEnabled bool
cacheWaitTimeout time.Duration
mapfile string
mu sync.Mutex
cache *mapnik.Map
lastMTime time.Time
}
type Args struct {
Mapfile string
Req render.Request
}
func (a *API) Render(args *Args, response *[]byte) error {
if a.cacheEnabled {
m, err := a.getMap(args.Mapfile)
if err != nil {
return err
}
if m != nil {
tmp, err := renderReq(m, args.Req)
*response = tmp
a.mu.Unlock()
return err
}
}
// cache not enabled or cacheWaitTimeout
m, err := a.loadMap(args.Mapfile)
if err != nil {
return err
}
tmp, err := renderReq(m, args.Req)
if err != nil {
return err
}
*response = tmp
return nil
}
// getMap returns a cached mapnik.Map. Loads a new map if the mapfile changed
// (filename and/or timestamp of mapfile). Returns nil if the lock for the
// cached map could not be obtained within cacheWaitTimeout.
// Caller needs to mu.Unlock if returned map is not nil.
func (a *API) getMap(mapfile string) (*mapnik.Map, error) {
fi, err := os.Stat(mapfile)
if err != nil {
return nil, fmt.Errorf("checking time of mapfile (%s): %w", mapfile, err)
}
deadline := time.Now().Add(a.cacheWaitTimeout)
for !a.mu.TryLock() {
if time.Now().After(deadline) {
return nil, nil
}
time.Sleep(10 * time.Millisecond)
}
if a.cache != nil && (mapfile != a.mapfile || fi.ModTime() != a.lastMTime) {
// different mapfile or mtime, release cache
a.cache.Free()
a.cache = nil
}
// return map from cache
if a.cache != nil {
return a.cache, nil
}
m, err := a.loadMap(mapfile)
if err != nil {
a.mu.Unlock()
return nil, err
}
a.cache = m
a.mapfile = mapfile
a.lastMTime = fi.ModTime()
return m, nil
}
func (a *API) loadMap(mapfile string) (*mapnik.Map, error) {
start := time.Now()
m := mapnik.New()
err := m.Load(mapfile)
if err != nil {
return nil, err
}
fmt.Fprintf(os.Stderr, "loaded map %s in %s\n", mapfile, time.Since(start))
return m, nil
}
// CacheLoadedMap enables caching of a single loaded map object. Render will
// wait for the cached map for up to cacheWaitTimeout, will load a new map
// after timeout.
func (a *API) CacheLoadedMap(cacheWaitTimeout time.Duration, _ *interface{}) error {
a.cacheEnabled = true
a.cacheWaitTimeout = cacheWaitTimeout
return nil
}
func (a *API) Is3(args struct{}, response *bool) error {
if mapnik.Version.Major == 3 {
*response = true
} else {
*response = false
}
return nil
}
func (a *API) RegisterFonts(fontDir string, _ *interface{}) error {
return mapnik.RegisterFonts(fontDir)
}
var useProj4 bool // useProj4 determines whether we use old +init syntax or not
func checkProjVersion() {
m := mapnik.NewSized(1, 1)
defer m.Free()
m.SetSRS("epsg:4326") // this should fail with Proj4
m.ZoomTo(-1, -1, 0, 0)
_, err := m.Render(mapnik.RenderOpts{})
if err != nil {
useProj4 = true
}
}
func main() {
checkProjVersion()
api := API{}
p := pie.NewProvider()
if err := p.RegisterName("Mapnik", &api); err != nil {
log.Fatalf("failed to register Plugin: %s", err)
}
p.Serve()
}