make it possible to disable compilation of grid_renderer - closes #1962

This commit is contained in:
Dane Springmeyer 2013-07-23 19:37:25 -04:00
parent 5ad2ebb30e
commit ef4dfeb747
14 changed files with 507 additions and 395 deletions

View file

@ -8,6 +8,10 @@ For a complete change history, see the git log.
## Future ## Future
- Added to python bindings: `has_tiff`, `has_png`, `has_webp`, `has_proj4`, `has_svg_renderer`, and `has_grid_renderer`
- Made it possible to disable compilation of `grid_renderer` with `./configure GRID_RENDERER=False` (#1962)
- Added `webp` image encoding and decoding support (#1955) - Added `webp` image encoding and decoding support (#1955)
- Added `premultiplied` property on mapnik::image_32 / mapnik.Image to enable knowledge of premultiplied status of image buffer. - Added `premultiplied` property on mapnik::image_32 / mapnik.Image to enable knowledge of premultiplied status of image buffer.

View file

@ -336,6 +336,7 @@ opts.AddVariables(
# Variables affecting rendering back-ends # Variables affecting rendering back-ends
BoolVariable('GRID_RENDERER', 'build support for native grid renderer', 'True'),
BoolVariable('SVG_RENDERER', 'build support for native svg renderer', 'False'), BoolVariable('SVG_RENDERER', 'build support for native svg renderer', 'False'),
BoolVariable('CPP_TESTS', 'Compile the C++ tests', 'True'), BoolVariable('CPP_TESTS', 'Compile the C++ tests', 'True'),
BoolVariable('BENCHMARK', 'Compile the C++ benchmark scripts', 'False'), BoolVariable('BENCHMARK', 'Compile the C++ benchmark scripts', 'False'),
@ -447,6 +448,7 @@ pickle_store = [# Scons internal variables
'CAIRO_LIBPATHS', 'CAIRO_LIBPATHS',
'CAIRO_ALL_LIBS', 'CAIRO_ALL_LIBS',
'CAIRO_CPPPATHS', 'CAIRO_CPPPATHS',
'GRID_RENDERER',
'SVG_RENDERER', 'SVG_RENDERER',
'SQLITE_LINKFLAGS', 'SQLITE_LINKFLAGS',
'BOOST_LIB_VERSION_FROM_HEADER', 'BOOST_LIB_VERSION_FROM_HEADER',

View file

@ -20,6 +20,8 @@
* *
*****************************************************************************/ *****************************************************************************/
#if defined(GRID_RENDERER)
// boost // boost
#include <boost/python.hpp> #include <boost/python.hpp>
#include <boost/python/module.hpp> #include <boost/python/module.hpp>
@ -80,3 +82,5 @@ void export_grid()
; ;
} }
#endif

View file

@ -20,6 +20,8 @@
* *
*****************************************************************************/ *****************************************************************************/
#if defined(GRID_RENDERER)
// boost // boost
#include <boost/python.hpp> #include <boost/python.hpp>
#include <boost/python/module.hpp> #include <boost/python/module.hpp>
@ -49,3 +51,5 @@ void export_grid_view()
) )
; ;
} }
#endif

View file

@ -41,8 +41,10 @@ void export_image();
void export_image_view(); void export_image_view();
void export_gamma_method(); void export_gamma_method();
void export_scaling_method(); void export_scaling_method();
#if defined(GRID_RENDERER)
void export_grid(); void export_grid();
void export_grid_view(); void export_grid_view();
#endif
void export_map(); void export_map();
void export_python(); void export_python();
void export_expression(); void export_expression();
@ -92,7 +94,9 @@ void export_logger();
#include <mapnik/value_error.hpp> #include <mapnik/value_error.hpp>
#include <mapnik/save_map.hpp> #include <mapnik/save_map.hpp>
#include <mapnik/scale_denominator.hpp> #include <mapnik/scale_denominator.hpp>
#if defined(GRID_RENDERER)
#include "python_grid_utils.hpp" #include "python_grid_utils.hpp"
#endif
#include "mapnik_value_converter.hpp" #include "mapnik_value_converter.hpp"
#include "mapnik_threads.hpp" #include "mapnik_threads.hpp"
#include "python_optional.hpp" #include "python_optional.hpp"
@ -366,7 +370,33 @@ std::string mapnik_version_string()
return MAPNIK_VERSION_STRING; return MAPNIK_VERSION_STRING;
} }
// indicator for jpeg read/write support within libmapnik bool has_proj4()
{
#if defined(MAPNIK_USE_PROJ4)
return true;
#else
return false;
#endif
}
bool has_svg_renderer()
{
#if defined(SVG_RENDERER)
return true;
#else
return false;
#endif
}
bool has_grid_renderer()
{
#if defined(GRID_RENDERER)
return true;
#else
return false;
#endif
}
bool has_jpeg() bool has_jpeg()
{ {
#if defined(HAVE_JPEG) #if defined(HAVE_JPEG)
@ -376,6 +406,33 @@ bool has_jpeg()
#endif #endif
} }
bool has_png()
{
#if defined(HAVE_PNG)
return true;
#else
return false;
#endif
}
bool has_tiff()
{
#if defined(HAVE_TIFF)
return true;
#else
return false;
#endif
}
bool has_webp()
{
#if defined(HAVE_WEBP)
return true;
#else
return false;
#endif
}
// indicator for cairo rendering support inside libmapnik // indicator for cairo rendering support inside libmapnik
bool has_cairo() bool has_cairo()
{ {
@ -427,7 +484,6 @@ BOOST_PYTHON_MODULE(_mapnik)
using mapnik::load_map_string; using mapnik::load_map_string;
using mapnik::save_map; using mapnik::save_map;
using mapnik::save_map_to_string; using mapnik::save_map_to_string;
using mapnik::render_grid;
register_exception_translator<std::exception>(&standard_error_translator); register_exception_translator<std::exception>(&standard_error_translator);
register_exception_translator<std::out_of_range>(&out_of_range_error_translator); register_exception_translator<std::out_of_range>(&out_of_range_error_translator);
@ -448,8 +504,10 @@ BOOST_PYTHON_MODULE(_mapnik)
export_image_view(); export_image_view();
export_gamma_method(); export_gamma_method();
export_scaling_method(); export_scaling_method();
#if defined(GRID_RENDERER)
export_grid(); export_grid();
export_grid_view(); export_grid_view();
#endif
export_expression(); export_expression();
export_rule(); export_rule();
export_style(); export_style();
@ -487,7 +545,8 @@ BOOST_PYTHON_MODULE(_mapnik)
">>> clear_cache()\n" ">>> clear_cache()\n"
); );
def("render_grid",&render_grid, #if defined(GRID_RENDERER)
def("render_grid",&mapnik::render_grid,
( arg("map"), ( arg("map"),
arg("layer"), arg("layer"),
args("key")="__id__", args("key")="__id__",
@ -495,6 +554,7 @@ BOOST_PYTHON_MODULE(_mapnik)
arg("fields")=boost::python::list() arg("fields")=boost::python::list()
) )
); );
#endif
def("render_to_file",&render_to_file1, def("render_to_file",&render_to_file1,
"\n" "\n"
@ -579,9 +639,11 @@ BOOST_PYTHON_MODULE(_mapnik)
(arg("map"),arg("image"),args("layer")) (arg("map"),arg("image"),args("layer"))
); );
#if defined(GRID_RENDERER)
def("render_layer", &mapnik::render_layer_for_grid, def("render_layer", &mapnik::render_layer_for_grid,
(arg("map"),arg("grid"),args("layer"),arg("fields")=boost::python::list()) (arg("map"),arg("grid"),args("layer"),arg("fields")=boost::python::list())
); );
#endif
#if defined(HAVE_CAIRO) && defined(HAVE_PYCAIRO) #if defined(HAVE_CAIRO) && defined(HAVE_PYCAIRO)
def("render",&render3, def("render",&render3,
@ -739,7 +801,12 @@ BOOST_PYTHON_MODULE(_mapnik)
def("save_map_to_string", &save_map_to_string, save_map_to_string_overloads()); def("save_map_to_string", &save_map_to_string, save_map_to_string_overloads());
def("mapnik_version", &mapnik_version,"Get the Mapnik version number"); def("mapnik_version", &mapnik_version,"Get the Mapnik version number");
def("mapnik_version_string", &mapnik_version_string,"Get the Mapnik version string"); def("mapnik_version_string", &mapnik_version_string,"Get the Mapnik version string");
def("has_proj4", &has_proj4, "Get proj4 status");
def("has_jpeg", &has_jpeg, "Get jpeg read/write support status"); def("has_jpeg", &has_jpeg, "Get jpeg read/write support status");
def("has_png", &has_png, "Get png read/write support status");
def("has_tiff", &has_jpeg, "Get tiff read/write support status");
def("has_webp", &has_jpeg, "Get webp read/write support status");
def("has_grid_renderer", &has_grid_renderer, "Get grid_renderer status");
def("has_cairo", &has_cairo, "Get cairo library status"); def("has_cairo", &has_cairo, "Get cairo library status");
def("has_pycairo", &has_pycairo, "Get pycairo module status"); def("has_pycairo", &has_pycairo, "Get pycairo module status");

View file

@ -20,6 +20,8 @@
* *
*****************************************************************************/ *****************************************************************************/
#if defined(GRID_RENDERER)
// boost // boost
#include <boost/python.hpp> #include <boost/python.hpp>
#include <boost/scoped_array.hpp> #include <boost/scoped_array.hpp>
@ -472,3 +474,5 @@ boost::python::dict render_grid(mapnik::Map const& map,
} }
} }
#endif

View file

@ -9,6 +9,9 @@ subdirs = ['','svg','wkt','grid','json','util','text_placements','formatting']
if env['SVG_RENDERER']: if env['SVG_RENDERER']:
subdirs.append('svg/output') subdirs.append('svg/output')
if env['GRID_RENDERER']:
subdirs.append('grid')
if 'install' in COMMAND_LINE_TARGETS: if 'install' in COMMAND_LINE_TARGETS:
for subdir in subdirs: for subdir in subdirs:
pathdir = os.path.join(base,subdir,'*.hpp') pathdir = os.path.join(base,subdir,'*.hpp')

View file

@ -27,7 +27,11 @@
#include <mapnik/svg/svg_path_attributes.hpp> #include <mapnik/svg/svg_path_attributes.hpp>
#include <mapnik/gradient.hpp> #include <mapnik/gradient.hpp>
#include <mapnik/box2d.hpp> #include <mapnik/box2d.hpp>
#if defined(GRID_RENDERER)
#include <mapnik/grid/grid_pixel.hpp> #include <mapnik/grid/grid_pixel.hpp>
#endif
#include <mapnik/noncopyable.hpp> #include <mapnik/noncopyable.hpp>
// boost // boost
@ -334,6 +338,7 @@ public:
} }
} }
#if defined(GRID_RENDERER)
template <typename Rasterizer, typename Scanline, typename Renderer> template <typename Rasterizer, typename Scanline, typename Renderer>
void render_id(Rasterizer& ras, void render_id(Rasterizer& ras,
Scanline& sl, Scanline& sl,
@ -416,6 +421,7 @@ public:
} }
} }
} }
#endif
private: private:

View file

@ -292,21 +292,24 @@ if env['RUNTIME_LINK'] == "static":
source += glob.glob('../deps/agg/src/' + '*.cpp') source += glob.glob('../deps/agg/src/' + '*.cpp')
# grid backend # grid backend
source += Split( if env['GRID_RENDERER']: # svg backend
""" source += Split(
grid/grid.cpp """
grid/grid_renderer.cpp grid/grid.cpp
grid/process_building_symbolizer.cpp grid/grid_renderer.cpp
grid/process_line_pattern_symbolizer.cpp grid/process_building_symbolizer.cpp
grid/process_line_symbolizer.cpp grid/process_line_pattern_symbolizer.cpp
grid/process_markers_symbolizer.cpp grid/process_line_symbolizer.cpp
grid/process_point_symbolizer.cpp grid/process_markers_symbolizer.cpp
grid/process_polygon_pattern_symbolizer.cpp grid/process_point_symbolizer.cpp
grid/process_polygon_symbolizer.cpp grid/process_polygon_pattern_symbolizer.cpp
grid/process_raster_symbolizer.cpp grid/process_polygon_symbolizer.cpp
grid/process_shield_symbolizer.cpp grid/process_raster_symbolizer.cpp
grid/process_text_symbolizer.cpp grid/process_shield_symbolizer.cpp
""") grid/process_text_symbolizer.cpp
""")
lib_env.Append(CPPDEFINES = '-DGRID_RENDERER')
libmapnik_defines.append('-DGRID_RENDERER')
# https://github.com/mapnik/mapnik/issues/1438 # https://github.com/mapnik/mapnik/issues/1438
if env['SVG_RENDERER']: # svg backend if env['SVG_RENDERER']: # svg backend

View file

@ -24,8 +24,11 @@
#include <mapnik/feature_style_processor_impl.hpp> #include <mapnik/feature_style_processor_impl.hpp>
#include <mapnik/agg_renderer.hpp> #include <mapnik/agg_renderer.hpp>
#include <mapnik/graphics.hpp> #include <mapnik/graphics.hpp>
#if defined(GRID_RENDERER)
#include <mapnik/grid/grid_renderer.hpp> #include <mapnik/grid/grid_renderer.hpp>
#include <mapnik/grid/grid.hpp> #include <mapnik/grid/grid.hpp>
#endif
#if defined(HAVE_CAIRO) #if defined(HAVE_CAIRO)
#include <cairo.h> #include <cairo.h>
@ -48,7 +51,10 @@ template class feature_style_processor<cairo_renderer<cairo_surface_ptr> >;
template class feature_style_processor<svg_renderer<std::ostream_iterator<char> > >; template class feature_style_processor<svg_renderer<std::ostream_iterator<char> > >;
#endif #endif
#if defined(GRID_RENDERER)
template class feature_style_processor<grid_renderer<grid> >; template class feature_style_processor<grid_renderer<grid> >;
#endif
template class feature_style_processor<agg_renderer<image_32> >; template class feature_style_processor<agg_renderer<image_32> >;
} }

View file

@ -25,7 +25,11 @@
#include <mapnik/font_engine_freetype.hpp> #include <mapnik/font_engine_freetype.hpp>
#include <mapnik/text_properties.hpp> #include <mapnik/text_properties.hpp>
#include <mapnik/graphics.hpp> #include <mapnik/graphics.hpp>
#if defined(GRID_RENDERER)
#include <mapnik/grid/grid.hpp> #include <mapnik/grid/grid.hpp>
#endif
#include <mapnik/text_path.hpp> #include <mapnik/text_path.hpp>
#include <mapnik/pixel_position.hpp> #include <mapnik/pixel_position.hpp>
#include <mapnik/font_util.hpp> #include <mapnik/font_util.hpp>
@ -686,6 +690,7 @@ void text_renderer<T>::render(pixel_position const& pos)
} }
} }
#if defined(GRID_RENDERER)
template <typename T> template <typename T>
void text_renderer<T>::render_id(mapnik::value_integer feature_id, void text_renderer<T>::render_id(mapnik::value_integer feature_id,
pixel_position const& pos) pixel_position const& pos)
@ -715,6 +720,7 @@ void text_renderer<T>::render_id(mapnik::value_integer feature_id,
} }
} }
} }
#endif
#ifdef MAPNIK_THREADSAFE #ifdef MAPNIK_THREADSAFE
boost::mutex freetype_engine::mutex_; boost::mutex freetype_engine::mutex_;
@ -729,6 +735,7 @@ template text_renderer<image_32>::text_renderer(image_32&,
double); double);
template box2d<double>text_renderer<image_32>::prepare_glyphs(text_path const&); template box2d<double>text_renderer<image_32>::prepare_glyphs(text_path const&);
template void text_renderer<image_32>::render(pixel_position const&); template void text_renderer<image_32>::render(pixel_position const&);
#if defined(GRID_RENDERER)
template void text_renderer<grid>::render_id(mapnik::value_integer, template void text_renderer<grid>::render_id(mapnik::value_integer,
pixel_position const&); pixel_position const&);
template text_renderer<grid>::text_renderer(grid&, template text_renderer<grid>::text_renderer(grid&,
@ -736,4 +743,5 @@ template text_renderer<grid>::text_renderer(grid&,
halo_rasterizer_e, halo_rasterizer_e,
composite_mode_e, double); composite_mode_e, double);
template box2d<double>text_renderer<grid>::prepare_glyphs(text_path const& ); template box2d<double>text_renderer<grid>::prepare_glyphs(text_path const& );
#endif
} }

View file

@ -45,18 +45,18 @@ def make_map():
m.zoom_all() m.zoom_all()
return m return m
def test_clearing_grid_data(): if mapnik.has_grid_renderer():
g = mapnik.Grid(256,256) def test_clearing_grid_data():
utf = g.encode() g = mapnik.Grid(256,256)
# make sure it equals itself utf = g.encode()
eq_(g.encode(),utf) # make sure it equals itself
m = make_map() eq_(g.encode(),utf)
mapnik.render_layer(m,g,layer=0,fields=['__id__','Name']) m = make_map()
eq_(g.encode()!=utf,True) mapnik.render_layer(m,g,layer=0,fields=['__id__','Name'])
# clear grid, should now match original eq_(g.encode()!=utf,True)
g.clear() # clear grid, should now match original
eq_(g.encode(),utf) g.clear()
eq_(g.encode(),utf)
if __name__ == "__main__": if __name__ == "__main__":
setup() setup()

View file

@ -15,370 +15,371 @@ def setup():
# from another directory we need to chdir() # from another directory we need to chdir()
os.chdir(execution_path('.')) os.chdir(execution_path('.'))
def show_grids(name,g1,g2): if mapnik.has_grid_renderer():
g1_file = '/tmp/mapnik-%s-actual.json' % name def show_grids(name,g1,g2):
open(g1_file,'w').write(json.dumps(g1,sort_keys=True)) g1_file = '/tmp/mapnik-%s-actual.json' % name
g2_file = '/tmp/mapnik-%s-expected.json' % name open(g1_file,'w').write(json.dumps(g1,sort_keys=True))
open(g2_file,'w').write(json.dumps(g2,sort_keys=True)) g2_file = '/tmp/mapnik-%s-expected.json' % name
val = 'JSON does not match ->\n' open(g2_file,'w').write(json.dumps(g2,sort_keys=True))
if g1['grid'] != g2['grid']: val = 'JSON does not match ->\n'
val += ' X grid does not match\n' if g1['grid'] != g2['grid']:
else: val += ' X grid does not match\n'
val += ' ✓ grid matches\n' else:
if g1['data'].keys() != g2['data'].keys(): val += ' ✓ grid matches\n'
val += ' X data does not match\n' if g1['data'].keys() != g2['data'].keys():
else: val += ' X data does not match\n'
val += ' ✓ data matches\n' else:
if g1['keys'] != g2['keys']: val += ' ✓ data matches\n'
val += ' X keys do not\n' if g1['keys'] != g2['keys']:
else: val += ' X keys do not\n'
val += ' ✓ keys match\n' else:
val += '\n\t%s\n\t%s' % (g1_file,g2_file) val += ' ✓ keys match\n'
return val val += '\n\t%s\n\t%s' % (g1_file,g2_file)
return val
def show_grids2(name,g1,g2):
g2_expected = '../data/grids/mapnik-%s-actual.json' % name def show_grids2(name,g1,g2):
if not os.path.exists(g2_expected): g2_expected = '../data/grids/mapnik-%s-actual.json' % name
# create test fixture based on actual results if not os.path.exists(g2_expected):
open(g2_expected,'a+').write(json.dumps(g1,sort_keys=True)) # create test fixture based on actual results
return open(g2_expected,'a+').write(json.dumps(g1,sort_keys=True))
g1_file = '/tmp/mapnik-%s-actual.json' % name return
open(g1_file,'w').write(json.dumps(g1,sort_keys=True)) g1_file = '/tmp/mapnik-%s-actual.json' % name
val = 'JSON does not match ->\n' open(g1_file,'w').write(json.dumps(g1,sort_keys=True))
if g1['grid'] != g2['grid']: val = 'JSON does not match ->\n'
val += ' X grid does not match\n' if g1['grid'] != g2['grid']:
else: val += ' X grid does not match\n'
val += ' ✓ grid matches\n' else:
if g1['data'].keys() != g2['data'].keys(): val += ' ✓ grid matches\n'
val += ' X data does not match\n' if g1['data'].keys() != g2['data'].keys():
else: val += ' X data does not match\n'
val += ' ✓ data matches\n' else:
if g1['keys'] != g2['keys']: val += ' ✓ data matches\n'
val += ' X keys do not\n' if g1['keys'] != g2['keys']:
else: val += ' X keys do not\n'
val += ' ✓ keys match\n' else:
val += '\n\t%s\n\t%s' % (g1_file,g2_expected) val += ' ✓ keys match\n'
return val val += '\n\t%s\n\t%s' % (g1_file,g2_expected)
return val
# first pass impl where resolution is passed as render
# time rather than encoding time, likely will be deprecated soon # first pass impl where resolution is passed as render
grid_correct_old = {"keys": ["", "North West", "North East", "South West", "South East"], "data": {"South East": {"Name": "South East"}, "North East": {"Name": "North East"}, "North West": {"Name": "North West"}, "South West": {"Name": "South West"}}, "grid": [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " !!! ### ", " !!!!! ##### ", " !!!!! ##### ", " !!! ### ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " $$$$ %%%% ", " $$$$$ %%%%% ", " $$$$$ %%%%% ", " $$$ %%% ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "]} # time rather than encoding time, likely will be deprecated soon
grid_correct_old = {"keys": ["", "North West", "North East", "South West", "South East"], "data": {"South East": {"Name": "South East"}, "North East": {"Name": "North East"}, "North West": {"Name": "North West"}, "South West": {"Name": "South West"}}, "grid": [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " !!! ### ", " !!!!! ##### ", " !!!!! ##### ", " !!! ### ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " $$$$ %%%% ", " $$$$$ %%%%% ", " $$$$$ %%%%% ", " $$$ %%% ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "]}
# now using svg rendering
grid_correct_old2 = {"data": {"North East": {"Name": "North East"}, "North West": {"Name": "North West"}, "South East": {"Name": "South East"}, "South West": {"Name": "South West"}}, "grid": [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " !!! ### ", " !!! ### ", " !!! ### ", " !!! ### ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " $$$ %%% ", " $$$ %%% ", " $$$ %%% ", " $$$ %%% ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "], "keys": ["", "North West", "North East", "South West", "South East"]} # now using svg rendering
grid_correct_old2 = {"data": {"North East": {"Name": "North East"}, "North West": {"Name": "North West"}, "South East": {"Name": "South East"}, "South West": {"Name": "South West"}}, "grid": [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " !!! ### ", " !!! ### ", " !!! ### ", " !!! ### ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " $$$ %%% ", " $$$ %%% ", " $$$ %%% ", " $$$ %%% ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "], "keys": ["", "North West", "North East", "South West", "South East"]}
grid_correct_old3 = {"data": {"North East": {"Name": "North East"}, "North West": {"Name": "North West"}, "South East": {"Name": "South East"}, "South West": {"Name": "South West"}}, "grid": [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " !!! ### ", " !!! ### ", " !!! ### ", " !!! ### ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " $$$ %%% ", " $$$ %%% ", " $$$ %%% ", " $$ %% ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "], "keys": ["", "North West", "North East", "South West", "South East"]}
grid_correct_old3 = {"data": {"North East": {"Name": "North East"}, "North West": {"Name": "North West"}, "South East": {"Name": "South East"}, "South West": {"Name": "South West"}}, "grid": [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " !!! ### ", " !!! ### ", " !!! ### ", " !!! ### ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " $$$ %%% ", " $$$ %%% ", " $$$ %%% ", " $$ %% ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "], "keys": ["", "North West", "North East", "South West", "South East"]}
# previous rendering using agg ellipse directly
grid_correct_new = {"data": {"North East": {"Name": "North East"}, "North West": {"Name": "North West"}, "South East": {"Name": "South East"}, "South West": {"Name": "South West"}}, "grid": [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " !! ## ", " !!! ### ", " !! ## ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " $$ %% ", " $$$ %% ", " $$ %% ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "], "keys": ["", "North West", "North East", "South West", "South East"]} # previous rendering using agg ellipse directly
grid_correct_new = {"data": {"North East": {"Name": "North East"}, "North West": {"Name": "North West"}, "South East": {"Name": "South East"}, "South West": {"Name": "South West"}}, "grid": [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " !! ## ", " !!! ### ", " !! ## ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " $$ %% ", " $$$ %% ", " $$ %% ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "], "keys": ["", "North West", "North East", "South West", "South East"]}
# newer rendering using svg
grid_correct_new2 = {"data": {"North East": {"Name": "North East"}, "North West": {"Name": "North West"}, "South East": {"Name": "South East"}, "South West": {"Name": "South West"}}, "grid": [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " !! ## ", " !!! ### ", " !! ## ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " $$ %% ", " $$$ %% ", " $$ %% ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "], "keys": ["", "North West", "North East", "South West", "South East"]} # newer rendering using svg
grid_correct_new2 = {"data": {"North East": {"Name": "North East"}, "North West": {"Name": "North West"}, "South East": {"Name": "South East"}, "South West": {"Name": "South West"}}, "grid": [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " !! ## ", " !!! ### ", " !! ## ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " $$ %% ", " $$$ %% ", " $$ %% ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "], "keys": ["", "North West", "North East", "South West", "South East"]}
grid_correct_new3 = {"data": {"North East": {"Name": "North East"}, "North West": {"Name": "North West"}, "South East": {"Name": "South East"}, "South West": {"Name": "South West"}}, "grid": [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " !! ## ", " !!! ### ", " !! ## ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " $$ %% ", " $$$ %% ", " $ %% ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "], "keys": ["", "North West", "North East", "South West", "South East"]}
grid_correct_new3 = {"data": {"North East": {"Name": "North East"}, "North West": {"Name": "North West"}, "South East": {"Name": "South East"}, "South West": {"Name": "South West"}}, "grid": [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " !! ## ", " !!! ### ", " !! ## ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " $$ %% ", " $$$ %% ", " $ %% ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "], "keys": ["", "North West", "North East", "South West", "South East"]}
def resolve(grid,row,col):
""" Resolve the attributes for a given pixel in a grid. def resolve(grid,row,col):
""" """ Resolve the attributes for a given pixel in a grid.
row = grid['grid'][row] """
utf_val = row[col] row = grid['grid'][row]
#http://docs.python.org/library/functions.html#ord utf_val = row[col]
codepoint = ord(utf_val) #http://docs.python.org/library/functions.html#ord
if (codepoint >= 93): codepoint = ord(utf_val)
codepoint-=1 if (codepoint >= 93):
if (codepoint >= 35): codepoint-=1
codepoint-=1 if (codepoint >= 35):
codepoint -= 32 codepoint-=1
key = grid['keys'][codepoint] codepoint -= 32
return grid['data'].get(key) key = grid['keys'][codepoint]
return grid['data'].get(key)
def create_grid_map(width,height,sym):
ds = mapnik.MemoryDatasource() def create_grid_map(width,height,sym):
context = mapnik.Context() ds = mapnik.MemoryDatasource()
context.push('Name') context = mapnik.Context()
f = mapnik.Feature(context,1) context.push('Name')
f['Name'] = 'South East' f = mapnik.Feature(context,1)
f.add_geometries_from_wkt('POINT (143.10 -38.60)') f['Name'] = 'South East'
ds.add_feature(f) f.add_geometries_from_wkt('POINT (143.10 -38.60)')
ds.add_feature(f)
f = mapnik.Feature(context,2)
f['Name'] = 'South West' f = mapnik.Feature(context,2)
f.add_geometries_from_wkt('POINT (142.48 -38.60)') f['Name'] = 'South West'
ds.add_feature(f) f.add_geometries_from_wkt('POINT (142.48 -38.60)')
ds.add_feature(f)
f = mapnik.Feature(context,3)
f['Name'] = 'North West' f = mapnik.Feature(context,3)
f.add_geometries_from_wkt('POINT (142.48 -38.38)') f['Name'] = 'North West'
ds.add_feature(f) f.add_geometries_from_wkt('POINT (142.48 -38.38)')
ds.add_feature(f)
f = mapnik.Feature(context,4)
f['Name'] = 'North East' f = mapnik.Feature(context,4)
f.add_geometries_from_wkt('POINT (143.10 -38.38)') f['Name'] = 'North East'
ds.add_feature(f) f.add_geometries_from_wkt('POINT (143.10 -38.38)')
s = mapnik.Style() ds.add_feature(f)
r = mapnik.Rule() s = mapnik.Style()
sym.allow_overlap = True r = mapnik.Rule()
r.symbols.append(sym) sym.allow_overlap = True
s.rules.append(r) r.symbols.append(sym)
lyr = mapnik.Layer('Places') s.rules.append(r)
lyr.datasource = ds lyr = mapnik.Layer('Places')
lyr.styles.append('places_labels') lyr.datasource = ds
m = mapnik.Map(width,height) lyr.styles.append('places_labels')
m.append_style('places_labels',s) m = mapnik.Map(width,height)
m.layers.append(lyr) m.append_style('places_labels',s)
return m m.layers.append(lyr)
return m
def test_render_grid_old():
""" test old method """ def test_render_grid_old():
width,height = 256,256 """ test old method """
symb = mapnik.PointSymbolizer(mapnik.PathExpression('../data/images/dummy.png')) width,height = 256,256
sym = mapnik.MarkersSymbolizer() symb = mapnik.PointSymbolizer(mapnik.PathExpression('../data/images/dummy.png'))
sym.width = mapnik.Expression('10') sym = mapnik.MarkersSymbolizer()
sym.height = mapnik.Expression('10') sym.width = mapnik.Expression('10')
m = create_grid_map(width,height,sym) sym.height = mapnik.Expression('10')
#print mapnik.save_map_to_string(m) m = create_grid_map(width,height,sym)
ul_lonlat = mapnik.Coord(142.30,-38.20) #print mapnik.save_map_to_string(m)
lr_lonlat = mapnik.Coord(143.40,-38.80) ul_lonlat = mapnik.Coord(142.30,-38.20)
m.zoom_to_box(mapnik.Box2d(ul_lonlat,lr_lonlat)) lr_lonlat = mapnik.Coord(143.40,-38.80)
grid = mapnik.render_grid(m,0,key='Name',resolution=4,fields=['Name']) m.zoom_to_box(mapnik.Box2d(ul_lonlat,lr_lonlat))
eq_(grid,grid_correct_old3,show_grids('old-markers',grid,grid_correct_old3)) grid = mapnik.render_grid(m,0,key='Name',resolution=4,fields=['Name'])
eq_(resolve(grid,0,0),None) eq_(grid,grid_correct_old3,show_grids('old-markers',grid,grid_correct_old3))
eq_(resolve(grid,0,0),None)
# check every pixel of the nw symbol
expected = {"Name": "North West"} # check every pixel of the nw symbol
expected = {"Name": "North West"}
# top row
eq_(resolve(grid,23,9),expected) # top row
eq_(resolve(grid,23,10),expected) eq_(resolve(grid,23,9),expected)
eq_(resolve(grid,23,11),expected) eq_(resolve(grid,23,10),expected)
eq_(resolve(grid,23,11),expected)
def test_render_grid_new():
""" test old against new""" def test_render_grid_new():
width,height = 256,256 """ test old against new"""
sym = mapnik.MarkersSymbolizer() width,height = 256,256
sym.width = mapnik.Expression('10') sym = mapnik.MarkersSymbolizer()
sym.height = mapnik.Expression('10') sym.width = mapnik.Expression('10')
m = create_grid_map(width,height,sym) sym.height = mapnik.Expression('10')
ul_lonlat = mapnik.Coord(142.30,-38.20) m = create_grid_map(width,height,sym)
lr_lonlat = mapnik.Coord(143.40,-38.80) ul_lonlat = mapnik.Coord(142.30,-38.20)
m.zoom_to_box(mapnik.Box2d(ul_lonlat,lr_lonlat)) lr_lonlat = mapnik.Coord(143.40,-38.80)
m.zoom_to_box(mapnik.Box2d(ul_lonlat,lr_lonlat))
# new method
grid = mapnik.Grid(m.width,m.height,key='Name') # new method
mapnik.render_layer(m,grid,layer=0,fields=['Name']) grid = mapnik.Grid(m.width,m.height,key='Name')
utf1 = grid.encode('utf',resolution=4) mapnik.render_layer(m,grid,layer=0,fields=['Name'])
eq_(utf1,grid_correct_new3,show_grids('new-markers',utf1,grid_correct_new3)) utf1 = grid.encode('utf',resolution=4)
eq_(utf1,grid_correct_new3,show_grids('new-markers',utf1,grid_correct_new3))
# check a full view is the same as a full image
grid_view = grid.view(0,0,width,height) # check a full view is the same as a full image
# for kicks check at full res too grid_view = grid.view(0,0,width,height)
utf3 = grid.encode('utf',resolution=1) # for kicks check at full res too
utf4 = grid_view.encode('utf',resolution=1) utf3 = grid.encode('utf',resolution=1)
eq_(utf3['grid'],utf4['grid']) utf4 = grid_view.encode('utf',resolution=1)
eq_(utf3['keys'],utf4['keys']) eq_(utf3['grid'],utf4['grid'])
eq_(utf3['data'],utf4['data']) eq_(utf3['keys'],utf4['keys'])
eq_(utf3['data'],utf4['data'])
eq_(resolve(utf4,0,0),None)
eq_(resolve(utf4,0,0),None)
# resolve some center points in the
# resampled view # resolve some center points in the
utf5 = grid_view.encode('utf',resolution=4) # resampled view
eq_(resolve(utf5,25,10),{"Name": "North West"}) utf5 = grid_view.encode('utf',resolution=4)
eq_(resolve(utf5,25,46),{"Name": "North East"}) eq_(resolve(utf5,25,10),{"Name": "North West"})
eq_(resolve(utf5,38,10),{"Name": "South West"}) eq_(resolve(utf5,25,46),{"Name": "North East"})
eq_(resolve(utf5,38,46),{"Name": "South East"}) eq_(resolve(utf5,38,10),{"Name": "South West"})
eq_(resolve(utf5,38,46),{"Name": "South East"})
grid_feat_id = {'keys': ['', '3', '4', '2', '1'], 'data': {'1': {'Name': 'South East'}, '3': {'Name': u'North West'}, '2': {'Name': 'South West'}, '4': {'Name': 'North East'}}, 'grid': [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' !! ## ', ' !!! ### ', ' !! ## ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' $$$ %% ', ' $$$ %%% ', ' $$ %% ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']}
grid_feat_id = {'keys': ['', '3', '4', '2', '1'], 'data': {'1': {'Name': 'South East'}, '3': {'Name': u'North West'}, '2': {'Name': 'South West'}, '4': {'Name': 'North East'}}, 'grid': [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' !! ## ', ' !!! ### ', ' !! ## ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' $$$ %% ', ' $$$ %%% ', ' $$ %% ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']}
grid_feat_id2 = {"data": {"1": {"Name": "South East"}, "2": {"Name": "South West"}, "3": {"Name": "North West"}, "4": {"Name": "North East"}}, "grid": [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " !! ## ", " !!! ### ", " !! ## ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " $$ %% ", " $$$ %% ", " $$ %% ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "], "keys": ["", "3", "4", "2", "1"]}
grid_feat_id2 = {"data": {"1": {"Name": "South East"}, "2": {"Name": "South West"}, "3": {"Name": "North West"}, "4": {"Name": "North East"}}, "grid": [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " !! ## ", " !!! ### ", " !! ## ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " $$ %% ", " $$$ %% ", " $$ %% ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "], "keys": ["", "3", "4", "2", "1"]}
grid_feat_id3 = {"data": {"1": {"Name": "South East", "__id__": 1}, "2": {"Name": "South West", "__id__": 2}, "3": {"Name": "North West", "__id__": 3}, "4": {"Name": "North East", "__id__": 4}}, "grid": [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " !! ## ", " !!! ### ", " !! ## ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " $$ %% ", " $$$ %% ", " $ %% ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "], "keys": ["", "3", "4", "2", "1"]}
grid_feat_id3 = {"data": {"1": {"Name": "South East", "__id__": 1}, "2": {"Name": "South West", "__id__": 2}, "3": {"Name": "North West", "__id__": 3}, "4": {"Name": "North East", "__id__": 4}}, "grid": [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " !! ## ", " !!! ### ", " !! ## ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " $$ %% ", " $$$ %% ", " $ %% ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "], "keys": ["", "3", "4", "2", "1"]}
def test_render_grid3():
""" test using feature id""" def test_render_grid3():
width,height = 256,256 """ test using feature id"""
sym = mapnik.MarkersSymbolizer() width,height = 256,256
sym.width = mapnik.Expression('10') sym = mapnik.MarkersSymbolizer()
sym.height = mapnik.Expression('10') sym.width = mapnik.Expression('10')
m = create_grid_map(width,height,sym) sym.height = mapnik.Expression('10')
ul_lonlat = mapnik.Coord(142.30,-38.20) m = create_grid_map(width,height,sym)
lr_lonlat = mapnik.Coord(143.40,-38.80) ul_lonlat = mapnik.Coord(142.30,-38.20)
m.zoom_to_box(mapnik.Box2d(ul_lonlat,lr_lonlat)) lr_lonlat = mapnik.Coord(143.40,-38.80)
m.zoom_to_box(mapnik.Box2d(ul_lonlat,lr_lonlat))
grid = mapnik.Grid(m.width,m.height,key='__id__')
mapnik.render_layer(m,grid,layer=0,fields=['__id__','Name']) grid = mapnik.Grid(m.width,m.height,key='__id__')
utf1 = grid.encode('utf',resolution=4) mapnik.render_layer(m,grid,layer=0,fields=['__id__','Name'])
eq_(utf1,grid_feat_id3,show_grids('id-markers',utf1,grid_feat_id3)) utf1 = grid.encode('utf',resolution=4)
# check a full view is the same as a full image eq_(utf1,grid_feat_id3,show_grids('id-markers',utf1,grid_feat_id3))
grid_view = grid.view(0,0,width,height) # check a full view is the same as a full image
# for kicks check at full res too grid_view = grid.view(0,0,width,height)
utf3 = grid.encode('utf',resolution=1) # for kicks check at full res too
utf4 = grid_view.encode('utf',resolution=1) utf3 = grid.encode('utf',resolution=1)
eq_(utf3['grid'],utf4['grid']) utf4 = grid_view.encode('utf',resolution=1)
eq_(utf3['keys'],utf4['keys']) eq_(utf3['grid'],utf4['grid'])
eq_(utf3['data'],utf4['data']) eq_(utf3['keys'],utf4['keys'])
eq_(utf3['data'],utf4['data'])
eq_(resolve(utf4,0,0),None)
eq_(resolve(utf4,0,0),None)
# resolve some center points in the
# resampled view # resolve some center points in the
utf5 = grid_view.encode('utf',resolution=4) # resampled view
eq_(resolve(utf5,25,10),{"Name": "North West","__id__": 3}) utf5 = grid_view.encode('utf',resolution=4)
eq_(resolve(utf5,25,46),{"Name": "North East","__id__": 4}) eq_(resolve(utf5,25,10),{"Name": "North West","__id__": 3})
eq_(resolve(utf5,38,10),{"Name": "South West","__id__": 2}) eq_(resolve(utf5,25,46),{"Name": "North East","__id__": 4})
eq_(resolve(utf5,38,46),{"Name": "South East","__id__": 1}) eq_(resolve(utf5,38,10),{"Name": "South West","__id__": 2})
eq_(resolve(utf5,38,46),{"Name": "South East","__id__": 1})
def gen_grid_for_id(pixel_key):
ds = mapnik.MemoryDatasource() def gen_grid_for_id(pixel_key):
context = mapnik.Context() ds = mapnik.MemoryDatasource()
context.push('Name') context = mapnik.Context()
f = mapnik.Feature(context,pixel_key) context.push('Name')
f['Name'] = str(pixel_key) f = mapnik.Feature(context,pixel_key)
f.add_geometries_from_wkt('POLYGON ((0 0, 0 256, 256 256, 256 0, 0 0))') f['Name'] = str(pixel_key)
ds.add_feature(f) f.add_geometries_from_wkt('POLYGON ((0 0, 0 256, 256 256, 256 0, 0 0))')
s = mapnik.Style() ds.add_feature(f)
r = mapnik.Rule() s = mapnik.Style()
symb = mapnik.PolygonSymbolizer() r = mapnik.Rule()
r.symbols.append(symb) symb = mapnik.PolygonSymbolizer()
s.rules.append(r) r.symbols.append(symb)
lyr = mapnik.Layer('Places') s.rules.append(r)
lyr.datasource = ds lyr = mapnik.Layer('Places')
lyr.styles.append('places_labels') lyr.datasource = ds
width,height = 256,256 lyr.styles.append('places_labels')
m = mapnik.Map(width,height) width,height = 256,256
m.append_style('places_labels',s) m = mapnik.Map(width,height)
m.layers.append(lyr) m.append_style('places_labels',s)
m.zoom_all() m.layers.append(lyr)
grid = mapnik.Grid(m.width,m.height,key='__id__') m.zoom_all()
mapnik.render_layer(m,grid,layer=0,fields=['__id__','Name']) grid = mapnik.Grid(m.width,m.height,key='__id__')
return grid mapnik.render_layer(m,grid,layer=0,fields=['__id__','Name'])
return grid
def test_negative_id():
grid = gen_grid_for_id(-1) def test_negative_id():
eq_(grid.get_pixel(128,128),-1) grid = gen_grid_for_id(-1)
utf1 = grid.encode('utf',resolution=4) eq_(grid.get_pixel(128,128),-1)
eq_(utf1['keys'],['-1']) utf1 = grid.encode('utf',resolution=4)
eq_(utf1['keys'],['-1'])
def test_32bit_int_id():
int32 = 2147483647 def test_32bit_int_id():
grid = gen_grid_for_id(int32) int32 = 2147483647
eq_(grid.get_pixel(128,128),int32) grid = gen_grid_for_id(int32)
utf1 = grid.encode('utf',resolution=4) eq_(grid.get_pixel(128,128),int32)
eq_(utf1['keys'],[str(int32)]) utf1 = grid.encode('utf',resolution=4)
max_neg = -(int32) eq_(utf1['keys'],[str(int32)])
grid = gen_grid_for_id(max_neg) max_neg = -(int32)
eq_(grid.get_pixel(128,128),max_neg) grid = gen_grid_for_id(max_neg)
utf1 = grid.encode('utf',resolution=4) eq_(grid.get_pixel(128,128),max_neg)
eq_(utf1['keys'],[str(max_neg)]) utf1 = grid.encode('utf',resolution=4)
eq_(utf1['keys'],[str(max_neg)])
def test_64bit_int_id():
int64 = 0x7FFFFFFFFFFFFFFF def test_64bit_int_id():
grid = gen_grid_for_id(int64) int64 = 0x7FFFFFFFFFFFFFFF
eq_(grid.get_pixel(128,128),int64) grid = gen_grid_for_id(int64)
utf1 = grid.encode('utf',resolution=4) eq_(grid.get_pixel(128,128),int64)
eq_(utf1['keys'],[str(int64)]) utf1 = grid.encode('utf',resolution=4)
max_neg = -(int64) eq_(utf1['keys'],[str(int64)])
grid = gen_grid_for_id(max_neg) max_neg = -(int64)
eq_(grid.get_pixel(128,128),max_neg) grid = gen_grid_for_id(max_neg)
utf1 = grid.encode('utf',resolution=4) eq_(grid.get_pixel(128,128),max_neg)
eq_(utf1['keys'],[str(max_neg)]) utf1 = grid.encode('utf',resolution=4)
eq_(utf1['keys'],[str(max_neg)])
def test_id_zero():
grid = gen_grid_for_id(0) def test_id_zero():
eq_(grid.get_pixel(128,128),0) grid = gen_grid_for_id(0)
utf1 = grid.encode('utf',resolution=4) eq_(grid.get_pixel(128,128),0)
eq_(utf1['keys'],['0']) utf1 = grid.encode('utf',resolution=4)
eq_(utf1['keys'],['0'])
line_expected = {"keys": ["", "1"], "data": {"1": {"Name": "1"}}, "grid": [" !", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", "!! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! "]}
line_expected = {"keys": ["", "1"], "data": {"1": {"Name": "1"}}, "grid": [" !", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", " !! ", "!! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! ", " ! "]}
def test_line_rendering():
ds = mapnik.MemoryDatasource() def test_line_rendering():
context = mapnik.Context() ds = mapnik.MemoryDatasource()
context.push('Name') context = mapnik.Context()
pixel_key = 1 context.push('Name')
f = mapnik.Feature(context,pixel_key) pixel_key = 1
f['Name'] = str(pixel_key) f = mapnik.Feature(context,pixel_key)
f.add_geometries_from_wkt('LINESTRING (30 10, 10 30, 40 40)') f['Name'] = str(pixel_key)
ds.add_feature(f) f.add_geometries_from_wkt('LINESTRING (30 10, 10 30, 40 40)')
s = mapnik.Style() ds.add_feature(f)
r = mapnik.Rule() s = mapnik.Style()
symb = mapnik.LineSymbolizer() r = mapnik.Rule()
r.symbols.append(symb) symb = mapnik.LineSymbolizer()
s.rules.append(r) r.symbols.append(symb)
lyr = mapnik.Layer('Places') s.rules.append(r)
lyr.datasource = ds lyr = mapnik.Layer('Places')
lyr.styles.append('places_labels') lyr.datasource = ds
width,height = 256,256 lyr.styles.append('places_labels')
m = mapnik.Map(width,height) width,height = 256,256
m.append_style('places_labels',s) m = mapnik.Map(width,height)
m.layers.append(lyr) m.append_style('places_labels',s)
m.zoom_all() m.layers.append(lyr)
#mapnik.render_to_file(m,'test.png') m.zoom_all()
grid = mapnik.Grid(m.width,m.height,key='__id__') #mapnik.render_to_file(m,'test.png')
mapnik.render_layer(m,grid,layer=0,fields=['Name']) grid = mapnik.Grid(m.width,m.height,key='__id__')
utf1 = grid.encode() mapnik.render_layer(m,grid,layer=0,fields=['Name'])
eq_(utf1,line_expected,show_grids('line',utf1,line_expected)) utf1 = grid.encode()
eq_(utf1,line_expected,show_grids('line',utf1,line_expected))
point_expected = {"data": {"1": {"Name": "South East"}, "2": {"Name": "South West"}, "3": {"Name": "North West"}, "4": {"Name": "North East"}}, "grid": [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " !!!! #### ", " !!!! #### ", " !!!! #### ", " !!!! #### ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " $$$$ %%%% ", " $$$$ %%%% ", " $$$$ %%%% ", " $$$$ %%%% ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "], "keys": ["", "3", "4", "2", "1"]}
point_expected = {"data": {"1": {"Name": "South East"}, "2": {"Name": "South West"}, "3": {"Name": "North West"}, "4": {"Name": "North East"}}, "grid": [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " !!!! #### ", " !!!! #### ", " !!!! #### ", " !!!! #### ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " $$$$ %%%% ", " $$$$ %%%% ", " $$$$ %%%% ", " $$$$ %%%% ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "], "keys": ["", "3", "4", "2", "1"]}
def test_point_symbolizer_grid():
width,height = 256,256 def test_point_symbolizer_grid():
sym = mapnik.PointSymbolizer(mapnik.PathExpression('../data/images/dummy.png')) width,height = 256,256
m = create_grid_map(width,height,sym) sym = mapnik.PointSymbolizer(mapnik.PathExpression('../data/images/dummy.png'))
ul_lonlat = mapnik.Coord(142.30,-38.20) m = create_grid_map(width,height,sym)
lr_lonlat = mapnik.Coord(143.40,-38.80) ul_lonlat = mapnik.Coord(142.30,-38.20)
m.zoom_to_box(mapnik.Box2d(ul_lonlat,lr_lonlat)) lr_lonlat = mapnik.Coord(143.40,-38.80)
grid = mapnik.Grid(m.width,m.height) m.zoom_to_box(mapnik.Box2d(ul_lonlat,lr_lonlat))
mapnik.render_layer(m,grid,layer=0,fields=['Name']) grid = mapnik.Grid(m.width,m.height)
utf1 = grid.encode() mapnik.render_layer(m,grid,layer=0,fields=['Name'])
eq_(utf1,point_expected,show_grids('point-sym',utf1,point_expected)) utf1 = grid.encode()
eq_(utf1,point_expected,show_grids('point-sym',utf1,point_expected))
# should throw because this is a mis-usage
# https://github.com/mapnik/mapnik/issues/1325 # should throw because this is a mis-usage
@raises(RuntimeError) # https://github.com/mapnik/mapnik/issues/1325
def test_render_to_grid_multiple_times(): @raises(RuntimeError)
# create map with two layers def test_render_to_grid_multiple_times():
m = mapnik.Map(256,256) # create map with two layers
s = mapnik.Style() m = mapnik.Map(256,256)
r = mapnik.Rule() s = mapnik.Style()
sym = mapnik.MarkersSymbolizer() r = mapnik.Rule()
sym.allow_overlap = True sym = mapnik.MarkersSymbolizer()
r.symbols.append(sym) sym.allow_overlap = True
s.rules.append(r) r.symbols.append(sym)
m.append_style('points',s) s.rules.append(r)
m.append_style('points',s)
# NOTE: we use a csv datasource here
# because the memorydatasource fails silently for # NOTE: we use a csv datasource here
# queries requesting fields that do not exist in the datasource # because the memorydatasource fails silently for
ds1 = mapnik.Datasource(**{"type":"csv","inline":''' # queries requesting fields that do not exist in the datasource
wkt,Name ds1 = mapnik.Datasource(**{"type":"csv","inline":'''
"POINT (143.10 -38.60)",South East'''}) wkt,Name
lyr1 = mapnik.Layer('One') "POINT (143.10 -38.60)",South East'''})
lyr1.datasource = ds1 lyr1 = mapnik.Layer('One')
lyr1.styles.append('points') lyr1.datasource = ds1
m.layers.append(lyr1) lyr1.styles.append('points')
m.layers.append(lyr1)
ds2 = mapnik.Datasource(**{"type":"csv","inline":'''
wkt,Value ds2 = mapnik.Datasource(**{"type":"csv","inline":'''
"POINT (142.48 -38.60)",South West'''}) wkt,Value
lyr2 = mapnik.Layer('Two') "POINT (142.48 -38.60)",South West'''})
lyr2.datasource = ds2 lyr2 = mapnik.Layer('Two')
lyr2.styles.append('points') lyr2.datasource = ds2
m.layers.append(lyr2) lyr2.styles.append('points')
m.layers.append(lyr2)
ul_lonlat = mapnik.Coord(142.30,-38.20)
lr_lonlat = mapnik.Coord(143.40,-38.80) ul_lonlat = mapnik.Coord(142.30,-38.20)
m.zoom_to_box(mapnik.Box2d(ul_lonlat,lr_lonlat)) lr_lonlat = mapnik.Coord(143.40,-38.80)
grid = mapnik.Grid(m.width,m.height) m.zoom_to_box(mapnik.Box2d(ul_lonlat,lr_lonlat))
mapnik.render_layer(m,grid,layer=0,fields=['Name']) grid = mapnik.Grid(m.width,m.height)
# should throw right here since Name will be a property now on the `grid` object mapnik.render_layer(m,grid,layer=0,fields=['Name'])
# and it is not found on the second layer # should throw right here since Name will be a property now on the `grid` object
mapnik.render_layer(m,grid,layer=1,fields=['Value']) # and it is not found on the second layer
utf1 = grid.encode() mapnik.render_layer(m,grid,layer=1,fields=['Value'])
utf1 = grid.encode()
if __name__ == "__main__": if __name__ == "__main__":
setup() setup()

View file

@ -21,7 +21,7 @@ defaults = {
'scales':[1.0,2.0], 'scales':[1.0,2.0],
'agg': True, 'agg': True,
'cairo': mapnik.has_cairo(), 'cairo': mapnik.has_cairo(),
'grid': True 'grid': mapnik.has_grid_renderer()
} }
cairo_threshold = 10 cairo_threshold = 10