Add cairo support to the python bindings - needs Pycairo installed.

This commit is contained in:
Tom Hughes 2008-03-12 00:37:53 +00:00
parent 0dafa5eae3
commit 06006d2f24
4 changed files with 90 additions and 6 deletions

View file

@ -24,6 +24,8 @@ import re
import os
Import('env')
env.ParseConfig('pkg-config --cflags pycairo')
prefix = env['PYTHON_PREFIX'] + '/' + env['LIBDIR_SCHEMA'] + '/python' + env['PYTHON_VERSION'] + '/site-packages/'
install_prefix = env['DESTDIR'] + '/' + prefix

View file

@ -26,6 +26,7 @@
#include <boost/python/detail/api_placeholder.hpp>
#include <boost/python/exception_translator.hpp>
void register_cairo();
void export_color();
void export_coord();
void export_layer();
@ -57,12 +58,14 @@ void export_projection();
#include <mapnik/map.hpp>
#include <mapnik/agg_renderer.hpp>
#include <mapnik/cairo_renderer.hpp>
#include <mapnik/graphics.hpp>
#include <mapnik/image_util.hpp>
#include <mapnik/load_map.hpp>
#include <mapnik/config_error.hpp>
#include <mapnik/save_map.hpp>
#include <pycairo.h>
void render(const mapnik::Map& map,mapnik::Image32& image, unsigned offset_x = 0, unsigned offset_y = 0)
{
@ -76,6 +79,20 @@ void render2(const mapnik::Map& map,mapnik::Image32& image)
ren.apply();
}
void render3(const mapnik::Map& map,PycairoSurface* surface, unsigned offset_x = 0, unsigned offset_y = 0)
{
Cairo::RefPtr<Cairo::Surface> s(new Cairo::Surface(surface->surface));
mapnik::cairo_renderer<Cairo::Surface> ren(map,s,offset_x, offset_y);
ren.apply();
}
void render4(const mapnik::Map& map,PycairoSurface* surface)
{
Cairo::RefPtr<Cairo::Surface> s(new Cairo::Surface(surface->surface));
mapnik::cairo_renderer<Cairo::Surface> ren(map,s);
ren.apply();
}
void render_tile_to_file(const mapnik::Map& map,
unsigned offset_x, unsigned offset_y,
unsigned width, unsigned height,
@ -125,6 +142,7 @@ BOOST_PYTHON_MODULE(_mapnik)
using mapnik::save_map;
register_exception_translator<mapnik::config_error>(translator);
register_cairo();
export_query();
export_feature();
export_featureset();
@ -152,12 +170,14 @@ BOOST_PYTHON_MODULE(_mapnik)
export_projection();
export_coord();
export_map();
def("render_to_file",&render_to_file1);
def("render_to_file",&render_to_file2);
def("render_tile_to_file",&render_tile_to_file);
def("render",&render);
def("render",&render2);
def("render",&render3);
def("render",&render4);
def("scale_denominator", &scale_denominator);
def("load_map", & load_map, load_map_overloads());

View file

@ -0,0 +1,48 @@
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2008 Tom Hughes
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*****************************************************************************/
//$Id$
#include <boost/python/type_id.hpp>
#include <boost/python/converter/registry.hpp>
#include <pycairo.h>
static Pycairo_CAPI_t *Pycairo_CAPI;
static void *extract_surface(PyObject* op)
{
if (PyObject_TypeCheck(op, const_cast<PyTypeObject*>(Pycairo_CAPI->Surface_Type)))
{
return op;
}
else
{
return 0;
}
}
void register_cairo()
{
Pycairo_IMPORT;
boost::python::converter::registry::insert(&extract_surface, boost::python::type_id<PycairoSurface>());
}

View file

@ -308,13 +308,27 @@ im = Image(m.width,m.height)
render(m, im)
# Save image to files
images = []
im.save('demo.png', 'png') # true-colour RGBA
images.append('demo.png')
im.save('demo256.png', 'png256') # save to palette based (max 256 colours) png
images.append('demo256.png')
im.save('demo.jpg', 'jpeg')
images.append('demo.jpg')
print """\n\nThree maps have been rendered in the current directory:
- demo.jpg
- demo.png
- demo256.png
# Render cairo examples
try:
import cairo
surface = cairo.SVGSurface('demo.svg', m.width,m.height)
render(m, surface)
images.append('demo.svg')
surface = cairo.PDFSurface('demo.pdf', m.width,m.height)
render(m, surface)
images.append('demo.pdf')
except:
print '\n\nSkipping cairo examples as Pycairo not available'
Have a look!\n\n"""
print "\n\n", len(images), "maps have been rendered in the current directory:"
for image in images:
print "-", image
print "\n\nHave a look!\n\n"