Skip to content

Commit 50cc443

Browse files
committed
Add module level methods 'has_xxx()' + upgrade more unit tests
1 parent ce63656 commit 50cc443

3 files changed

Lines changed: 140 additions & 127 deletions

File tree

src/mapnik_python.cpp

Lines changed: 96 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,94 @@
2121
*****************************************************************************/
2222

2323
#include <mapnik/config.hpp>
24-
24+
#include <mapnik/version.hpp>
2525
#include <pybind11/pybind11.h>
2626

2727
namespace py = pybind11;
2828

29+
unsigned mapnik_version()
30+
{
31+
return MAPNIK_VERSION;
32+
}
33+
34+
std::string mapnik_version_string()
35+
{
36+
return MAPNIK_VERSION_STRING;
37+
}
38+
39+
bool has_proj()
40+
{
41+
#if defined(MAPNIK_USE_PROJ)
42+
return true;
43+
#else
44+
return false;
45+
#endif
46+
}
47+
48+
bool has_svg_renderer()
49+
{
50+
#if defined(SVG_RENDERER)
51+
return true;
52+
#else
53+
return false;
54+
#endif
55+
}
56+
57+
bool has_grid_renderer()
58+
{
59+
#if defined(GRID_RENDERER)
60+
return true;
61+
#else
62+
return false;
63+
#endif
64+
}
65+
66+
bool has_jpeg()
67+
{
68+
#if defined(HAVE_JPEG)
69+
return true;
70+
#else
71+
return false;
72+
#endif
73+
}
74+
75+
bool has_png()
76+
{
77+
#if defined(HAVE_PNG)
78+
return true;
79+
#else
80+
return false;
81+
#endif
82+
}
83+
84+
bool has_tiff()
85+
{
86+
#if defined(HAVE_TIFF)
87+
return true;
88+
#else
89+
return false;
90+
#endif
91+
}
92+
93+
bool has_webp()
94+
{
95+
#if defined(HAVE_WEBP)
96+
return true;
97+
#else
98+
return false;
99+
#endif
100+
}
101+
102+
// indicator for cairo rendering support inside libmapnik
103+
bool has_cairo()
104+
{
105+
#if defined(HAVE_CAIRO)
106+
return true;
107+
#else
108+
return false;
109+
#endif
110+
}
111+
29112
void export_color(py::module const&);
30113
void export_composite_modes(py::module const&);
31114
void export_coord(py::module const&);
@@ -54,6 +137,18 @@ PYBIND11_MODULE(_mapnik, m) {
54137
export_image(m);
55138
export_projection(m);
56139
export_proj_transform(m);
140+
141+
m.def("mapnik_version", &mapnik_version,"Get the Mapnik version number");
142+
m.def("mapnik_version_string", &mapnik_version_string,"Get the Mapnik version string");
143+
m.def("has_proj", &has_proj, "Get proj status");
144+
m.def("has_jpeg", &has_jpeg, "Get jpeg read/write support status");
145+
m.def("has_png", &has_png, "Get png read/write support status");
146+
m.def("has_tiff", &has_tiff, "Get tiff read/write support status");
147+
m.def("has_webp", &has_webp, "Get webp read/write support status");
148+
m.def("has_svg_renderer", &has_svg_renderer, "Get svg_renderer status");
149+
m.def("has_grid_renderer", &has_grid_renderer, "Get grid_renderer status");
150+
m.def("has_cairo", &has_cairo, "Get cairo library status");
151+
// m.def("has_pycairo", &has_pycairo, "Get pycairo module status");
57152
}
58153

59154
// #pragma GCC diagnostic push
@@ -623,88 +718,6 @@ PYBIND11_MODULE(_mapnik, m) {
623718
// PyErr_SetString(PyExc_RuntimeError, ex.what());
624719
// }
625720

626-
// unsigned mapnik_version()
627-
// {
628-
// return MAPNIK_VERSION;
629-
// }
630-
631-
// std::string mapnik_version_string()
632-
// {
633-
// return MAPNIK_VERSION_STRING;
634-
// }
635-
636-
// bool has_proj()
637-
// {
638-
// #if defined(MAPNIK_USE_PROJ)
639-
// return true;
640-
// #else
641-
// return false;
642-
// #endif
643-
// }
644-
645-
// bool has_svg_renderer()
646-
// {
647-
// #if defined(SVG_RENDERER)
648-
// return true;
649-
// #else
650-
// return false;
651-
// #endif
652-
// }
653-
654-
// bool has_grid_renderer()
655-
// {
656-
// #if defined(GRID_RENDERER)
657-
// return true;
658-
// #else
659-
// return false;
660-
// #endif
661-
// }
662-
663-
// bool has_jpeg()
664-
// {
665-
// #if defined(HAVE_JPEG)
666-
// return true;
667-
// #else
668-
// return false;
669-
// #endif
670-
// }
671-
672-
// bool has_png()
673-
// {
674-
// #if defined(HAVE_PNG)
675-
// return true;
676-
// #else
677-
// return false;
678-
// #endif
679-
// }
680-
681-
// bool has_tiff()
682-
// {
683-
// #if defined(HAVE_TIFF)
684-
// return true;
685-
// #else
686-
// return false;
687-
// #endif
688-
// }
689-
690-
// bool has_webp()
691-
// {
692-
// #if defined(HAVE_WEBP)
693-
// return true;
694-
// #else
695-
// return false;
696-
// #endif
697-
// }
698-
699-
// // indicator for cairo rendering support inside libmapnik
700-
// bool has_cairo()
701-
// {
702-
// #if defined(HAVE_CAIRO)
703-
// return true;
704-
// #else
705-
// return false;
706-
// #endif
707-
// }
708721

709722
// // indicator for pycairo support in the python bindings
710723
// bool has_pycairo()

test/python_tests/png_encoding_test.py

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def test_expected_encodings(setup):
5353
im.save(expected, opt)
5454
else:
5555
im.save(actual, opt)
56-
assert mapnik.Image.open(actual).tostring('png32') == mapnik.Image.open(expected).tostring('png32'), '%s (actual) not == to %s (expected)' % (actual, expected)
56+
assert mapnik.Image.open(actual).to_string('png32') == mapnik.Image.open(expected).to_string('png32'), '%s (actual) not == to %s (expected)' % (actual, expected)
5757

5858
# solid image
5959
im.fill(mapnik.Color('green'))
@@ -65,7 +65,7 @@ def test_expected_encodings(setup):
6565
im.save(expected, opt)
6666
else:
6767
im.save(actual, opt)
68-
assert mapnik.Image.open(actual).tostring('png32') == mapnik.Image.open(expected).tostring('png32'), '%s (actual) not == to %s (expected)' % (actual, expected)
68+
assert mapnik.Image.open(actual).to_string('png32') == mapnik.Image.open(expected).to_string('png32'), '%s (actual) not == to %s (expected)' % (actual, expected)
6969

7070
# aerial
7171
im = mapnik.Image.open('./images/support/transparency/aerial_rgba.png')
@@ -77,7 +77,7 @@ def test_expected_encodings(setup):
7777
im.save(expected, opt)
7878
else:
7979
im.save(actual, opt)
80-
assert mapnik.Image.open(actual).tostring('png32') == mapnik.Image.open(expected).tostring('png32'), '%s (actual) not == to %s (expected)' % (actual, expected)
80+
assert mapnik.Image.open(actual).to_string('png32') == mapnik.Image.open(expected).to_string('png32'), '%s (actual) not == to %s (expected)' % (actual, expected)
8181

8282
def test_transparency_levels():
8383
# create partial transparency image
@@ -100,96 +100,96 @@ def test_transparency_levels():
100100
format = 'png8:m=o:t=0'
101101
im.save(t0, format)
102102
im_in = mapnik.Image.open(t0)
103-
t0_len = len(im_in.tostring(format))
104-
assert t0_len == len(mapnik.Image.open('images/support/transparency/white0.png').tostring(format))
103+
t0_len = len(im_in.to_string(format))
104+
assert t0_len == len(mapnik.Image.open('images/support/transparency/white0.png').to_string(format))
105105
format = 'png8:m=o:t=1'
106106
im.save(t1, format)
107107
im_in = mapnik.Image.open(t1)
108-
t1_len = len(im_in.tostring(format))
109-
assert len(im.tostring(format)) == len(mapnik.Image.open('images/support/transparency/white1.png').tostring(format))
108+
t1_len = len(im_in.to_string(format))
109+
assert len(im.to_string(format)) == len(mapnik.Image.open('images/support/transparency/white1.png').to_string(format))
110110
format = 'png8:m=o:t=2'
111111
im.save(t2, format)
112112
im_in = mapnik.Image.open(t2)
113-
t2_len = len(im_in.tostring(format))
114-
assert len(im.tostring(format)) == len(mapnik.Image.open('images/support/transparency/white2.png').tostring(format))
113+
t2_len = len(im_in.to_string(format))
114+
assert len(im.to_string(format)) == len(mapnik.Image.open('images/support/transparency/white2.png').to_string(format))
115115
assert t0_len < t1_len < t2_len
116116

117117
# hextree
118118
format = 'png8:m=h:t=0'
119119
im.save(t0, format)
120120
im_in = mapnik.Image.open(t0)
121-
t0_len = len(im_in.tostring(format))
122-
assert t0_len == len(mapnik.Image.open('images/support/transparency/white0.png').tostring(format))
121+
t0_len = len(im_in.to_string(format))
122+
assert t0_len == len(mapnik.Image.open('images/support/transparency/white0.png').to_string(format))
123123
format = 'png8:m=h:t=1'
124124
im.save(t1, format)
125125
im_in = mapnik.Image.open(t1)
126-
t1_len = len(im_in.tostring(format))
127-
assert len(im.tostring(format)) == len(mapnik.Image.open('images/support/transparency/white1.png').tostring(format))
126+
t1_len = len(im_in.to_string(format))
127+
assert len(im.to_string(format)) == len(mapnik.Image.open('images/support/transparency/white1.png').to_string(format))
128128
format = 'png8:m=h:t=2'
129129
im.save(t2, format)
130130
im_in = mapnik.Image.open(t2)
131-
t2_len = len(im_in.tostring(format))
132-
assert len(im.tostring(format)) == len(mapnik.Image.open('images/support/transparency/white2.png').tostring(format))
131+
t2_len = len(im_in.to_string(format))
132+
assert len(im.to_string(format)) == len(mapnik.Image.open('images/support/transparency/white2.png').to_string(format))
133133
assert t0_len < t1_len < t2_len
134134

135135
def test_transparency_levels_aerial():
136136
im = mapnik.Image.open('../data/images/12_654_1580.png')
137137
im_in = mapnik.Image.open(
138138
'./images/support/transparency/aerial_rgba.png')
139-
assert len(im.tostring('png8')) == len(im_in.tostring('png8'))
140-
assert len(im.tostring('png32')) == len(im_in.tostring('png32'))
139+
assert len(im.to_string('png8')) == len(im_in.to_string('png8'))
140+
assert len(im.to_string('png32')) == len(im_in.to_string('png32'))
141141

142142
im_in = mapnik.Image.open(
143143
'./images/support/transparency/aerial_rgb.png')
144-
assert len(im.tostring('png32')) == len(im_in.tostring('png32'))
145-
assert len(im.tostring('png32:t=0')) == len(im_in.tostring('png32:t=0'))
146-
assert not len(im.tostring('png32:t=0')) == len(im_in.tostring('png32'))
147-
assert len(im.tostring('png8')) == len(im_in.tostring('png8'))
148-
assert len(im.tostring('png8:t=0')) == len(im_in.tostring('png8:t=0'))
144+
assert len(im.to_string('png32')) == len(im_in.to_string('png32'))
145+
assert len(im.to_string('png32:t=0')) == len(im_in.to_string('png32:t=0'))
146+
assert not len(im.to_string('png32:t=0')) == len(im_in.to_string('png32'))
147+
assert len(im.to_string('png8')) == len(im_in.to_string('png8'))
148+
assert len(im.to_string('png8:t=0')) == len(im_in.to_string('png8:t=0'))
149149
# unlike png32 paletted images without alpha will look the same even if
150150
# no alpha is forced
151-
assert len(im.tostring('png8:t=0')) == len(im_in.tostring('png8'))
152-
assert len(im.tostring('png8:t=0:m=o')) == len(im_in.tostring('png8:m=o'))
151+
assert len(im.to_string('png8:t=0')) == len(im_in.to_string('png8'))
152+
assert len(im.to_string('png8:t=0:m=o')) == len(im_in.to_string('png8:m=o'))
153153

154154
def test_9_colors_hextree():
155155
expected = './images/support/encoding-opts/png8-9cols.png'
156156
im = mapnik.Image.open(expected)
157157
t0 = tmp_dir + 'png-encoding-9-colors.result-hextree.png'
158158
im.save(t0, 'png8:m=h')
159-
assert mapnik.Image.open(t0).tostring() == mapnik.Image.open(expected).tostring(), '%s (actual) not == to %s (expected)' % (t0, expected)
159+
assert mapnik.Image.open(t0).to_string() == mapnik.Image.open(expected).to_string(), '%s (actual) not == to %s (expected)' % (t0, expected)
160160

161161
def test_9_colors_octree():
162162
expected = './images/support/encoding-opts/png8-9cols.png'
163163
im = mapnik.Image.open(expected)
164164
t0 = tmp_dir + 'png-encoding-9-colors.result-octree.png'
165165
im.save(t0, 'png8:m=o')
166-
assert mapnik.Image.open(t0).tostring() == mapnik.Image.open(expected).tostring(), '%s (actual) not == to %s (expected)' % (t0, expected)
166+
assert mapnik.Image.open(t0).to_string() == mapnik.Image.open(expected).to_string(), '%s (actual) not == to %s (expected)' % (t0, expected)
167167

168168
def test_17_colors_hextree():
169169
expected = './images/support/encoding-opts/png8-17cols.png'
170170
im = mapnik.Image.open(expected)
171171
t0 = tmp_dir + 'png-encoding-17-colors.result-hextree.png'
172172
im.save(t0, 'png8:m=h')
173-
assert mapnik.Image.open(t0).tostring() == mapnik.Image.open(expected).tostring(), '%s (actual) not == to %s (expected)' % (t0, expected)
173+
assert mapnik.Image.open(t0).to_string() == mapnik.Image.open(expected).to_string(), '%s (actual) not == to %s (expected)' % (t0, expected)
174174

175175
def test_17_colors_octree():
176176
expected = './images/support/encoding-opts/png8-17cols.png'
177177
im = mapnik.Image.open(expected)
178178
t0 = tmp_dir + 'png-encoding-17-colors.result-octree.png'
179179
im.save(t0, 'png8:m=o')
180-
assert mapnik.Image.open(t0).tostring() == mapnik.Image.open(expected).tostring(), '%s (actual) not == to %s (expected)' % (t0, expected)
180+
assert mapnik.Image.open(t0).to_string() == mapnik.Image.open(expected).to_string(), '%s (actual) not == to %s (expected)' % (t0, expected)
181181

182182
def test_2px_regression_hextree():
183183
im = mapnik.Image.open('./images/support/encoding-opts/png8-2px.A.png')
184184
expected = './images/support/encoding-opts/png8-2px.png'
185185

186186
t0 = tmp_dir + 'png-encoding-2px.result-hextree.png'
187187
im.save(t0, 'png8:m=h')
188-
assert mapnik.Image.open(t0).tostring() == mapnik.Image.open(expected).tostring(), '%s (actual) not == to %s (expected)' % (t0, expected)
188+
assert mapnik.Image.open(t0).to_string() == mapnik.Image.open(expected).to_string(), '%s (actual) not == to %s (expected)' % (t0, expected)
189189

190190
def test_2px_regression_octree():
191191
im = mapnik.Image.open('./images/support/encoding-opts/png8-2px.A.png')
192192
expected = './images/support/encoding-opts/png8-2px.png'
193193
t0 = tmp_dir + 'png-encoding-2px.result-octree.png'
194194
im.save(t0, 'png8:m=o')
195-
assert mapnik.Image.open(t0).tostring() == mapnik.Image.open(expected).tostring(), '%s (actual) not == to %s (expected)' % (t0, expected)
195+
assert mapnik.Image.open(t0).to_string() == mapnik.Image.open(expected).to_string(), '%s (actual) not == to %s (expected)' % (t0, expected)

0 commit comments

Comments
 (0)