forked from omniscale/go-mapnik
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapnik_c_api.cpp
414 lines (360 loc) · 9.94 KB
/
mapnik_c_api.cpp
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
// Formatted with: astyle --style=google --pad-oper --add-brackets
#include <mapnik/debug.hpp>
#include <mapnik/version.hpp>
#include <mapnik/map.hpp>
#include <mapnik/layer.hpp>
#include <mapnik/color.hpp>
#include <mapnik/image_util.hpp>
#include <mapnik/agg_renderer.hpp>
#include <mapnik/load_map.hpp>
#include <mapnik/datasource_cache.hpp>
#include <mapnik/font_engine_freetype.hpp>
#if MAPNIK_VERSION < 300000
#error go-mapnik requires Mapnik 3
#endif
#include "mapnik_c_api.h"
#include <stdlib.h>
#ifdef __cplusplus
extern "C"
{
#endif
typedef mapnik::image_rgba8 mapnik_rgba_image;
static std::string * register_err;
inline void mapnik_register_reset_last_error() {
if (register_err) {
delete register_err;
register_err = NULL;
}
}
int mapnik_register_datasource(const char* path) {
mapnik_register_reset_last_error();
try {
mapnik::datasource_cache::instance().register_datasource(path);
} catch (std::exception const& ex) {
register_err = new std::string(ex.what());
return 0;
}
return 1;
}
int mapnik_register_font(const char* path) {
mapnik_register_reset_last_error();
try {
return mapnik::freetype_engine::register_font(path);
} catch (std::exception const& ex) {
register_err = new std::string(ex.what());
return 0;
}
}
const char *mapnik_register_last_error() {
if (register_err) {
return register_err->c_str();
}
return NULL;
}
void mapnik_logging_set_severity(int level) {
mapnik::logger::severity_type severity;
switch (level) {
case MAPNIK_DEBUG:
severity = mapnik::logger::debug;
break;
case MAPNIK_WARN:
severity = mapnik::logger::warn;
break;
case MAPNIK_ERROR:
severity = mapnik::logger::error;
break;
default:
severity = mapnik::logger::none;
break;
}
mapnik::logger::instance().set_severity(severity);
}
struct _mapnik_map_t {
mapnik::Map * m;
std::string * err;
};
mapnik_map_t * mapnik_map(unsigned width, unsigned height) {
mapnik_map_t * map = new mapnik_map_t;
map->m = new mapnik::Map(width, height);
map->err = NULL;
return map;
}
void mapnik_map_free(mapnik_map_t * m) {
if (m) {
if (m->m) {
delete m->m;
}
if (m->err) {
delete m->err;
}
delete m;
}
}
inline void mapnik_map_reset_last_error(mapnik_map_t *m) {
if (m && m->err) {
delete m->err;
m->err = NULL;
}
}
const char * mapnik_map_get_srs(mapnik_map_t * m) {
if (m && m->m) {
return m->m->srs().c_str();
}
return NULL;
}
int mapnik_map_set_srs(mapnik_map_t * m, const char* srs) {
if (m) {
m->m->set_srs(srs);
return 0;
}
return -1;
}
double mapnik_map_get_scale_denominator(mapnik_map_t * m) {
if (m && m->m) {
return m->m->scale_denominator();
}
return 0.0;
}
int mapnik_map_load(mapnik_map_t * m, const char* stylesheet) {
mapnik_map_reset_last_error(m);
if (m && m->m) {
try {
mapnik::load_map(*m->m, stylesheet);
} catch (std::exception const& ex) {
m->err = new std::string(ex.what());
return -1;
}
return 0;
}
return -1;
}
void mapnik_apply_layer_off_hack(mapnik_map_t * m) {
// 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.
if (m && m->m) {
for (size_t idx = 0; idx < m->m->layer_count(); idx++) {
mapnik::layer &layer = m->m->get_layer(idx);
if (layer.name().rfind("__OFF__", 0) == 0) {
layer.set_active(false);
layer.set_name(layer.name().substr(7));
}
}
}
}
int mapnik_map_zoom_all(mapnik_map_t * m) {
mapnik_map_reset_last_error(m);
if (m && m->m) {
try {
m->m->zoom_all();
} catch (std::exception const& ex) {
m->err = new std::string(ex.what());
return -1;
}
return 0;
}
return -1;
}
void mapnik_map_resize(mapnik_map_t *m, unsigned int width, unsigned int height) {
if (m && m->m) {
m->m->resize(width, height);
}
}
MAPNIKCAPICALL void mapnik_map_set_buffer_size(mapnik_map_t * m, int buffer_size) {
m->m->set_buffer_size(buffer_size);
}
const char *mapnik_map_last_error(mapnik_map_t *m) {
if (m && m->err) {
return m->err->c_str();
}
return NULL;
}
struct _mapnik_bbox_t {
mapnik::box2d<double> b;
};
mapnik_bbox_t * mapnik_bbox(double minx, double miny, double maxx, double maxy) {
mapnik_bbox_t * b = new mapnik_bbox_t;
b->b = mapnik::box2d<double>(minx, miny, maxx, maxy);
return b;
}
void mapnik_bbox_free(mapnik_bbox_t * b) {
if (b) {
delete b;
}
}
void mapnik_map_zoom_to_box(mapnik_map_t * m, mapnik_bbox_t * b) {
if (m && m->m && b) {
m->m->zoom_to_box(b->b);
}
}
struct _mapnik_image_t {
mapnik_rgba_image *i;
std::string * err;
};
inline void mapnik_image_reset_last_error(mapnik_image_t *i) {
if (i && i->err) {
delete i->err;
i->err = NULL;
}
}
void mapnik_image_free(mapnik_image_t * i) {
if (i) {
if (i->i) {
delete i->i;
}
if (i->err) {
delete i->err;
}
delete i;
}
}
const char *mapnik_image_last_error(mapnik_image_t *i) {
if (i && i->err) {
return i->err->c_str();
}
return NULL;
}
mapnik_image_t * mapnik_map_render_to_image(mapnik_map_t * m, double scale, double scale_factor) {
mapnik_map_reset_last_error(m);
mapnik_rgba_image * im = new mapnik_rgba_image(m->m->width(), m->m->height());
if (m && m->m) {
try {
mapnik::agg_renderer<mapnik_rgba_image> ren(*m->m, *im, scale_factor);
if (scale > 0.0) {
ren.apply(scale);
} else {
ren.apply();
}
} catch (std::exception const& ex) {
delete im;
m->err = new std::string(ex.what());
return NULL;
}
}
mapnik_image_t * i = new mapnik_image_t;
i->i = im;
i->err = NULL;
return i;
}
int mapnik_map_render_to_file(mapnik_map_t * m, const char* filepath, double scale, double scale_factor, const char *format) {
mapnik_map_reset_last_error(m);
if (m && m->m) {
try {
mapnik_rgba_image buf(m->m->width(), m->m->height());
mapnik::agg_renderer<mapnik_rgba_image> ren(*m->m, buf, scale_factor);
if (scale > 0.0) {
ren.apply(scale);
} else {
ren.apply();
}
mapnik::save_to_file(buf, filepath, format);
} catch (std::exception const& ex) {
m->err = new std::string(ex.what());
return -1;
}
return 0;
}
return -1;
}
void mapnik_image_blob_free(mapnik_image_blob_t * b) {
if (b) {
if (b->ptr) {
delete[] b->ptr;
}
delete b;
}
}
mapnik_image_blob_t * mapnik_image_to_blob(mapnik_image_t * i, const char *format) {
mapnik_image_reset_last_error(i);
mapnik_image_blob_t * blob = new mapnik_image_blob_t;
blob->ptr = NULL;
blob->len = 0;
if (i && i->i) {
try {
std::string s = save_to_string(*(i->i), format);
blob->len = s.length();
blob->ptr = new char[blob->len];
memcpy(blob->ptr, s.c_str(), blob->len);
} catch (std::exception const& ex) {
i->err = new std::string(ex.what());
delete blob;
return NULL;
}
}
return blob;
}
const uint8_t * mapnik_image_to_raw(mapnik_image_t * i, size_t * size) {
if (i && i->i) {
*size = i->i->width() * i->i->height() * 4;
return (uint8_t *)i->i->data();
}
return NULL;
}
mapnik_image_t * mapnik_image_from_raw(const uint8_t * raw, int width, int height) {
mapnik_image_t * img = new mapnik_image_t;
img->i = new mapnik_rgba_image(width, height);
memcpy(img->i->data(), raw, width * height * 4);
img->err = NULL;
return img;
}
int mapnik_map_layer_count(mapnik_map_t * m) {
if (m && m->m) {
return m->m->layer_count();
}
return 0;
}
const char * mapnik_map_layer_name(mapnik_map_t * m, size_t idx) {
if (m && m->m) {
mapnik::layer const& layer = m->m->get_layer(idx);
return layer.name().c_str();
}
return NULL;
}
int mapnik_map_layer_is_active(mapnik_map_t * m, size_t idx) {
if (m && m->m) {
mapnik::layer const& layer = m->m->get_layer(idx);
return layer.active();
}
return 0;
}
void mapnik_map_layer_set_active(mapnik_map_t * m, size_t idx, int active) {
if (m && m->m) {
mapnik::layer &layer = m->m->get_layer(idx);
layer.set_active(active);
}
}
int mapnik_map_background(mapnik_map_t * m, uint8_t *r, uint8_t *g, uint8_t *b, uint8_t *a) {
if (m && m->m) {
boost::optional<mapnik::color> const &bg = m->m->background();
if (bg) {
mapnik::color c = bg.get();
*r = c.red();
*g = c.green();
*b = c.blue();
*a = c.alpha();
return 1;
}
}
return 0;
}
void mapnik_map_set_background(mapnik_map_t * m, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
if (m && m->m) {
mapnik::color bg(r, g, b, a);
m->m->set_background(bg);
}
}
void mapnik_map_set_maximum_extent(mapnik_map_t * m, double x0, double y0, double x1, double y1) {
if (m && m->m) {
mapnik::box2d<double> extent(x0, y0, x1, y1);
m->m->set_maximum_extent(extent);
}
}
void mapnik_map_reset_maximum_extent(mapnik_map_t * m) {
if (m && m->m) {
m->m->reset_maximum_extent();
}
}
#ifdef __cplusplus
}
#endif