forked from omniscale/go-mapnik
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapnik.go
539 lines (478 loc) · 14.1 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
// Package mapnik renders beautiful maps with Mapnik.
package mapnik
// #include <stdlib.h>
// #include "mapnik_c_api.h"
import "C"
import (
"errors"
"fmt"
"image"
"image/color"
"image/draw"
"io/ioutil"
"os"
"path/filepath"
"strings"
"unsafe"
)
type LogLevel int
var (
None = LogLevel(C.MAPNIK_NONE)
Debug = LogLevel(C.MAPNIK_DEBUG)
Warn = LogLevel(C.MAPNIK_WARN)
Error = LogLevel(C.MAPNIK_ERROR)
)
var (
// You can overwrite defaults at compile time, eg:
// go build -ldflags "-X github.com/omniscale/go-mapnik/v2.fontPath $(mapnik-config -fonts)"
fontPath = "/usr/local/lib/mapnik/fonts"
pluginPath = "/usr/local/lib/mapnik/input"
)
func init() {
// Register default datasources path and fonts path like the Python bindings do.
if err := RegisterDatasources(pluginPath); err != nil {
fmt.Fprintf(os.Stderr, "MAPNIK: %s\n", err)
}
if err := RegisterFonts(fontPath); err != nil {
fmt.Fprintf(os.Stderr, "MAPNIK: %s\n", err)
}
}
// RegisterDatasources registers all input plugins found in the given path.
func RegisterDatasources(path string) error {
fileInfos, err := ioutil.ReadDir(path)
if err != nil {
return err
}
for _, file := range fileInfos {
cs := C.CString(filepath.Join(path, file.Name()))
defer C.free(unsafe.Pointer(cs))
// Register datasources one-by-one to avoid recursive_mutex assertion
// errors in register_datasources, triggered at least on MacOS 10.13
// with Mapnik 3.0.22
if C.mapnik_register_datasource(cs) == 0 {
e := C.GoString(C.mapnik_register_last_error())
if e != "" {
return errors.New("registering datasources: " + e)
}
return errors.New("error while registering datasources")
}
}
return nil
}
// RegisterDatasources registers all fonts found in the given path.
func RegisterFonts(dirpath string) error {
return filepath.Walk(dirpath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
fullPath := path
if !isFontFile(fullPath) {
} else {
cs := C.CString(fullPath)
defer C.free(unsafe.Pointer(cs))
// Register fonts one-by-one. See comment in RegisterDatasources.
if C.mapnik_register_font(cs) == 0 {
e := C.GoString(C.mapnik_register_last_error())
if e != "" {
return errors.New("registering fonts: " + e)
}
return errors.New("error while registering fonts")
}
}
return nil
})
//~ fileInfos, err := ioutil.ReadDir(dirpath)
//~ if err != nil {
//~ return err
//~ }
//~ for _, file := range fileInfos {
//~ fullPath := filepath.Join(dirpath, file.Name())
//~ if !isFontFile(fullPath) {
//~ continue
//~ }
//~ cs := C.CString(fullPath)
//~ defer C.free(unsafe.Pointer(cs))
//~ // Register fonts one-by-one. See comment in RegisterDatasources.
//~ if C.mapnik_register_font(cs) == 0 {
//~ e := C.GoString(C.mapnik_register_last_error())
//~ if e != "" {
//~ return errors.New("registering fonts: " + e)
//~ }
//~ return errors.New("error while registering fonts")
//~ }
//~ }
//~ return nil
}
func isFontFile(path string) bool {
ext := strings.ToLower(filepath.Ext(path))
return (ext == ".ttf" ||
ext == ".otf" ||
ext == ".woff" ||
ext == ".ttc" ||
ext == ".pfa" ||
ext == ".pfb" ||
ext == ".dfont")
}
// LogSeverity sets the global log level for Mapnik. Requires a Mapnik build with logging enabled.
func LogSeverity(level LogLevel) {
C.mapnik_logging_set_severity(C.int(level))
}
type version struct {
Numeric int
Major int
Minor int
Patch int
String string
}
var Version version
func init() {
Version.Numeric = int(C.mapnik_version)
Version.Major = int(C.mapnik_version_major)
Version.Minor = int(C.mapnik_version_minor)
Version.Patch = int(C.mapnik_version_patch)
Version.String = fmt.Sprintf("%d.%d.%d", Version.Major, Version.Minor, Version.Patch)
}
// Map base type
type Map struct {
m *C.struct__mapnik_map_t
width int
height int
layerStatus []bool
}
// New initializes a new Map.
func New() *Map {
return &Map{
m: C.mapnik_map(C.uint(800), C.uint(600)),
width: 800,
height: 600,
}
}
// NewSized initializes a new Map with the given size.
func NewSized(width, height int) *Map {
return &Map{
m: C.mapnik_map(C.uint(width), C.uint(height)),
width: width,
height: height,
}
}
func (m *Map) lastError() error {
return errors.New("mapnik: " + C.GoString(C.mapnik_map_last_error(m.m)))
}
// Load reads in a Mapnik map XML.
//
// Note: Since Mapnik 3 all layers with status="off" are not loaded and cannot
// be activated by a custom LayerSelector. As a workaround, all layers with names
// starting with '__OFF__' are disabled on load and the '__OFF__' prefix is removed
// from the layer name.
func (m *Map) Load(stylesheet string) error {
cs := C.CString(stylesheet)
defer C.free(unsafe.Pointer(cs))
if C.mapnik_map_load(m.m, cs) != 0 {
return m.lastError()
}
C.mapnik_apply_layer_off_hack(m.m)
return nil
}
// Load a Mapnik map XML from string.
//
// Note: Since Mapnik 3 all layers with status="off" are not loaded and cannot
// be activated by a custom LayerSelector. As a workaround, all layers with names
// starting with '__OFF__' are disabled on load and the '__OFF__' prefix is removed
// from the layer name.
func (m *Map) LoadFromString(stylesheet string) error {
cs := C.CString(stylesheet)
defer C.free(unsafe.Pointer(cs))
if C.mapnik_map_load_from_string(m.m, cs) != 0 {
return m.lastError()
}
C.mapnik_apply_layer_off_hack(m.m)
return nil
}
// Resize changes the map size in pixel.
// Sizes larger than 16k pixels are ignored by Mapnik. Use NewSized
// to initialize larger maps.
func (m *Map) Resize(width, height int) {
C.mapnik_map_resize(m.m, C.uint(width), C.uint(height))
m.width = width
m.height = height
}
// Free deallocates the map.
func (m *Map) Free() {
C.mapnik_map_free(m.m)
m.m = nil
}
// SRS returns the projection of the map.
func (m *Map) SRS() string {
return C.GoString(C.mapnik_map_get_srs(m.m))
}
// SetSRS sets the projection of the map as a proj4 string ('+init=epsg:4326', etc).
func (m *Map) SetSRS(srs string) {
cs := C.CString(srs)
defer C.free(unsafe.Pointer(cs))
C.mapnik_map_set_srs(m.m, cs)
}
// ScaleDenominator returns the current scale denominator. Call after Resize and ZoomAll/ZoomTo.
func (m *Map) ScaleDenominator() float64 {
return float64(C.mapnik_map_get_scale_denominator(m.m))
}
// ZoomAll zooms to the maximum extent.
func (m *Map) ZoomAll() error {
if C.mapnik_map_zoom_all(m.m) != 0 {
return m.lastError()
}
return nil
}
// ZoomTo zooms to the given bounding box.
func (m *Map) ZoomTo(minx, miny, maxx, maxy float64) {
bbox := C.mapnik_bbox(C.double(minx), C.double(miny), C.double(maxx), C.double(maxy))
defer C.mapnik_bbox_free(bbox)
C.mapnik_map_zoom_to_box(m.m, bbox)
}
func (m *Map) BackgroundColor() color.NRGBA {
c := color.NRGBA{}
C.mapnik_map_background(m.m, (*C.uint8_t)(&c.R), (*C.uint8_t)(&c.G), (*C.uint8_t)(&c.B), (*C.uint8_t)(&c.A))
return c
}
func (m *Map) SetBackgroundColor(c color.NRGBA) {
C.mapnik_map_set_background(m.m, C.uint8_t(c.R), C.uint8_t(c.G), C.uint8_t(c.B), C.uint8_t(c.A))
}
func (m *Map) printLayerStatus() {
n := C.mapnik_map_layer_count(m.m)
for i := 0; i < int(n); i++ {
fmt.Println(
C.GoString(C.mapnik_map_layer_name(m.m, C.size_t(i))),
C.mapnik_map_layer_is_active(m.m, C.size_t(i)),
)
}
}
func (m *Map) storeLayerStatus() {
if len(m.layerStatus) > 0 {
return // allready stored
}
m.layerStatus = m.currentLayerStatus()
}
func (m *Map) currentLayerStatus() []bool {
n := C.mapnik_map_layer_count(m.m)
active := make([]bool, n)
for i := 0; i < int(n); i++ {
if C.mapnik_map_layer_is_active(m.m, C.size_t(i)) == 1 {
active[i] = true
}
}
return active
}
func (m *Map) resetLayerStatus() {
if len(m.layerStatus) == 0 {
return // not stored
}
n := C.mapnik_map_layer_count(m.m)
if int(n) > len(m.layerStatus) {
// should not happen
return
}
for i := 0; i < int(n); i++ {
if m.layerStatus[i] {
C.mapnik_map_layer_set_active(m.m, C.size_t(i), 1)
} else {
C.mapnik_map_layer_set_active(m.m, C.size_t(i), 0)
}
}
m.layerStatus = nil
}
// Status defines if a layer should be rendered or not.
type Status int
const (
// Exclude layer from rendering.
Exclude Status = -1
// Default keeps layer at the current rendering status.
Default Status = 0
// Include layer for rendering.
Include Status = 1
)
type LayerSelector interface {
Select(layername string) Status
}
type SelectorFunc func(string) Status
func (f SelectorFunc) Select(layername string) Status {
return f(layername)
}
// SelectLayers enables/disables single layers. LayerSelector or SelectorFunc gets called for each layer.
// Returns true if at least one layer was included (or set to default).
func (m *Map) SelectLayers(selector LayerSelector) bool {
m.storeLayerStatus()
selected := false
n := C.mapnik_map_layer_count(m.m)
for i := 0; i < int(n); i++ {
layerName := C.GoString(C.mapnik_map_layer_name(m.m, C.size_t(i)))
switch selector.Select(layerName) {
case Include:
selected = true
C.mapnik_map_layer_set_active(m.m, C.size_t(i), 1)
case Exclude:
C.mapnik_map_layer_set_active(m.m, C.size_t(i), 0)
case Default:
selected = true
}
}
return selected
}
// ResetLayer resets all layers to the initial status.
func (m *Map) ResetLayers() {
m.resetLayerStatus()
}
func (m *Map) SetMaxExtent(minx, miny, maxx, maxy float64) {
C.mapnik_map_set_maximum_extent(m.m, C.double(minx), C.double(miny), C.double(maxx), C.double(maxy))
}
func (m *Map) ResetMaxExtent() {
C.mapnik_map_reset_maximum_extent(m.m)
}
// RenderOpts defines rendering options.
type RenderOpts struct {
// Scale renders the map at a fixed scale denominator.
Scale float64
// ScaleFactor renders the map with larger fonts sizes, line width, etc. For printing or retina/hq iamges.
ScaleFactor float64
// Format for the rendered image ('jpeg80', 'png256', etc. see: https://github.com/mapnik/mapnik/wiki/Image-IO)
Format string
}
// Render returns the map as an encoded image.
func (m *Map) Render(opts RenderOpts) ([]byte, error) {
scaleFactor := opts.ScaleFactor
if scaleFactor == 0.0 {
scaleFactor = 1.0
}
i := C.mapnik_map_render_to_image(m.m, C.double(opts.Scale), C.double(scaleFactor))
if i == nil {
return nil, m.lastError()
}
defer C.mapnik_image_free(i)
if opts.Format == "raw" {
size := 0
raw := C.mapnik_image_to_raw(i, (*C.size_t)(unsafe.Pointer(&size)))
return C.GoBytes(unsafe.Pointer(raw), C.int(size)), nil
}
var format *C.char
if opts.Format != "" {
format = C.CString(opts.Format)
} else {
format = C.CString("png256")
}
b := C.mapnik_image_to_blob(i, format)
if b == nil {
return nil, errors.New("mapnik: " + C.GoString(C.mapnik_image_last_error(i)))
}
C.free(unsafe.Pointer(format))
defer C.mapnik_image_blob_free(b)
return C.GoBytes(unsafe.Pointer(b.ptr), C.int(b.len)), nil
}
// RenderImage returns the map as an unencoded image.Image.
func (m *Map) RenderImage(opts RenderOpts) (*image.NRGBA, error) {
scaleFactor := opts.ScaleFactor
if scaleFactor == 0.0 {
scaleFactor = 1.0
}
i := C.mapnik_map_render_to_image(m.m, C.double(opts.Scale), C.double(scaleFactor))
if i == nil {
return nil, m.lastError()
}
defer C.mapnik_image_free(i)
size := 0
raw := C.mapnik_image_to_raw(i, (*C.size_t)(unsafe.Pointer(&size)))
b := C.GoBytes(unsafe.Pointer(raw), C.int(size))
img := &image.NRGBA{
Pix: b,
Stride: int(m.width * 4),
Rect: image.Rect(0, 0, int(m.width), int(m.height)),
}
return img, nil
}
// RenderToFile writes the map as an encoded image to the file system.
func (m *Map) RenderToFile(opts RenderOpts, path string) error {
scaleFactor := opts.ScaleFactor
if scaleFactor == 0.0 {
scaleFactor = 1.0
}
cs := C.CString(path)
defer C.free(unsafe.Pointer(cs))
var format *C.char
if opts.Format != "" {
format = C.CString(opts.Format)
} else {
format = C.CString("png256")
}
defer C.free(unsafe.Pointer(format))
if C.mapnik_map_render_to_file(m.m, cs, C.double(opts.Scale), C.double(scaleFactor), format) != 0 {
return m.lastError()
}
return nil
}
// SetBufferSize sets the pixel buffer at the map image edges where Mapnik should not render any labels.
func (m *Map) SetBufferSize(s int) {
C.mapnik_map_set_buffer_size(m.m, C.int(s))
}
// Encode image.Image with Mapniks image encoder.
// This is optimized for *image.NRGBA or *image.RGBA.
func Encode(img image.Image, format string) ([]byte, error) {
tmp := toNRGBA(img)
i := C.mapnik_image_from_raw(
(*C.uint8_t)(unsafe.Pointer(&tmp.Pix[0])),
C.int(tmp.Bounds().Dx()),
C.int(tmp.Bounds().Dy()),
)
defer C.mapnik_image_free(i)
cformat := C.CString(format)
b := C.mapnik_image_to_blob(i, cformat)
if b == nil {
return nil, errors.New("mapnik: " + C.GoString(C.mapnik_image_last_error(i)))
}
C.free(unsafe.Pointer(cformat))
defer C.mapnik_image_blob_free(b)
return C.GoBytes(unsafe.Pointer(b.ptr), C.int(b.len)), nil
}
func toNRGBA(src image.Image) *image.NRGBA {
switch src := src.(type) {
case *image.NRGBA:
return src
case *image.RGBA:
result := image.NewNRGBA(src.Bounds())
drawRGBAOver(result, result.Bounds(), src, image.ZP)
return result
default:
result := image.NewNRGBA(src.Bounds())
draw.Draw(result, result.Bounds(), src, image.ZP, draw.Over)
return result
}
}
func drawRGBAOver(dst *image.NRGBA, r image.Rectangle, src *image.RGBA, sp image.Point) {
i0 := (r.Min.X - dst.Rect.Min.X) * 4
i1 := (r.Max.X - dst.Rect.Min.X) * 4
si0 := (sp.X - src.Rect.Min.X) * 4
yMax := r.Max.Y - dst.Rect.Min.Y
y := r.Min.Y - dst.Rect.Min.Y
sy := sp.Y - src.Rect.Min.Y
for ; y != yMax; y, sy = y+1, sy+1 {
dpix := dst.Pix[y*dst.Stride:]
spix := src.Pix[sy*src.Stride:]
for i, si := i0, si0; i < i1; i, si = i+4, si+4 {
sr := spix[si+0]
sg := spix[si+1]
sb := spix[si+2]
sa := spix[si+3]
// reverse pre-multiplication adapted from color.NRGBAModel
if sa == 0xff {
dpix[i+0] = sr
dpix[i+1] = sg
dpix[i+2] = sb
} else if sa == 0x00 {
dpix[i+0] = 0
dpix[i+1] = 0
dpix[i+2] = 0
} else {
dpix[i+0] = uint8(((uint32(sr) * 0xffff) / uint32(sa)) >> 8)
dpix[i+1] = uint8(((uint32(sg) * 0xffff) / uint32(sa)) >> 8)
dpix[i+2] = uint8(((uint32(sb) * 0xffff) / uint32(sa)) >> 8)
}
dpix[i+3] = sa
}
}
}