This repository was archived by the owner on Feb 14, 2023. It is now read-only.
forked from omniscale/go-mapnik
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapnik.go
More file actions
398 lines (347 loc) · 9.83 KB
/
mapnik.go
File metadata and controls
398 lines (347 loc) · 9.83 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
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
// Package mapnik renders beautiful maps with Mapnik.
package mapnik
//go:generate bash ./configure.bash
/*
#include "mapnik_c_api.h"
*/
import "C"
import (
"errors"
"fmt"
"image"
"image/color"
"unsafe"
)
type LogLevel int
var (
None = LogLevel(C.MAPNIKLOG_NONE)
Debug = LogLevel(C.MAPNIKLOG_DEBUG)
Warn = LogLevel(C.MAPNIKLOG_WARN)
Error = LogLevel(C.MAPNIKLOG_ERROR)
)
func init() {
// register default datasources path and fonts path like the python bindings do
RegisterDatasources(pluginPath)
RegisterFonts(fontPath)
}
// RegisterDatasources adds path to the Mapnik plugin search path.
func RegisterDatasources(path string) {
cs := C.CString(path)
defer C.free(unsafe.Pointer(cs))
C.mapnik_register_datasources(cs)
}
// RegisterDatasources adds path to the Mapnik fonts search path.
func RegisterFonts(path string) {
cs := C.CString(path)
defer C.free(unsafe.Pointer(cs))
C.mapnik_register_fonts(cs)
}
// 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() {
}
// Map base type
type Map struct {
m *C.struct__mapnik_map_t
width int
height int
layerStatus []bool
}
type MapnikVectorData struct {
data *C.char
mvd *C.struct__mapnik_vector_data_t
}
func NewVectorData(data []byte, x, y, z uint64) *MapnikVectorData {
if len(data) == 0 {
return nil
}
mvd := &MapnikVectorData{data: (*C.char)(unsafe.Pointer(&data[0]))}
mvd.mvd = C.mapnik_vector_data(mvd.data, C.int(len(data)), C.int(x), C.int(y), C.int(z))
return mvd
}
func (mvd *MapnikVectorData) Free() {
if mvd.data != nil {
C.free(unsafe.Pointer(mvd.data))
mvd.data = nil
}
}
// New initializes a new Map.
func New() *Map {
return &Map{
m: C.mapnik_map(C.uint(800), C.uint(600)),
width: 800,
height: 600,
}
}
func (m *Map) SetVectorData(vd *MapnikVectorData) {
C.mapnik_map_set_vector_data(m.m, vd.mvd)
}
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.
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()
}
return nil
}
// Resize changes the map size in pixel.
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.
func (m *Map) SelectLayers(selector LayerSelector) {
m.storeLayerStatus()
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:
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:
}
}
}
// 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.
func Encode(img image.Image, format string) ([]byte, error) {
var i *C.mapnik_image_t
switch img := img.(type) {
// XXX does mapnik expect NRGBA or RGBA? this might stop working
//as expected if we start encoding images with full alpha channel
case *image.NRGBA:
i = C.mapnik_image_from_raw(
(*C.uint8_t)(unsafe.Pointer(&img.Pix[0])),
C.int(img.Bounds().Dx()),
C.int(img.Bounds().Dy()),
)
case *image.RGBA:
i = C.mapnik_image_from_raw(
(*C.uint8_t)(unsafe.Pointer(&img.Pix[0])),
C.int(img.Bounds().Dx()),
C.int(img.Bounds().Dy()),
)
}
if i == nil {
return nil, errors.New("unable to create image from raw")
}
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
}