added python subtree (requires bjam to build)

This commit is contained in:
Artem Pavlenko 2005-02-21 11:57:16 +00:00
parent bd99fe8b4a
commit fb1084c69d
9 changed files with 562 additions and 0 deletions

18
python/Jamfile Normal file
View file

@ -0,0 +1,18 @@
project-root ;
import python ;
extension mapnik
: # source
mapnik_parameters.cc
mapnik_color.cc
mapnik_layer.cc
mapnik_envelope.cc
mapnik_map.cc
mapnik_python.cc
# requirements and dependencies for Boost.Python extensions
<template>@boost/libs/python/build/extension
: # path to mapnik include dir e.g.
<include>$(MAPNIK_ROOT)/include
# path to libmapnik.so e.g.
<library-file>$(MAPNIK_ROOT)/lib/libmapnik.so
;

5
python/Jamrules Normal file
View file

@ -0,0 +1,5 @@
path-global BOOST_ROOT : /opt/boost ;
PYTHON_VERSION = 2.4 ;
PYTHON_ROOT = /opt/python ;
project boost : $(BOOST_ROOT) ;
path-global MAPNIK_ROOT : /opt/mapnik ;

1
python/boost-build.jam Normal file
View file

@ -0,0 +1 @@
boost-build /opt/boost/tools/build/v1 ;

50
python/mapnik_color.cc Normal file
View file

@ -0,0 +1,50 @@
/* This file is part of python_mapnik (c++/python mapping toolkit)
* Copyright (C) 2005 Artem Pavlenko
*
* Mapnik is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
//$Id$
#include <mapnik.hh>
#include <boost/python.hpp>
using mapnik::Color;
struct color_pickle_suite : boost::python::pickle_suite
{
static boost::python::tuple
getinitargs(const Color& c)
{
using namespace boost::python;
return make_tuple(c.red(),c.green(),c.blue());
}
};
void export_color ()
{
using namespace boost::python;
class_<Color>("color",init<>())
.def(init<int,int,int>())
.def("red",&Color::red)
.def("green",&Color::green)
.def("blue",&Color::blue)
.def_pickle(color_pickle_suite())
;
}

50
python/mapnik_envelope.cc Normal file
View file

@ -0,0 +1,50 @@
/* This file is part of python_mapnik (c++/python mapping toolkit)
* Copyright (C) 2005 Artem Pavlenko
*
* Mapnik is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
//$Id$
#include <mapnik.hh>
#include <boost/python.hpp>
using mapnik::coord;
using mapnik::Envelope;
struct envelope_pickle_suite : boost::python::pickle_suite
{
static boost::python::tuple
getinitargs(const Envelope<double>& e)
{
using namespace boost::python;
return make_tuple(e.minx(),e.miny(),e.maxx(),e.maxy());
}
};
void export_envelope()
{
using namespace boost::python;
class_<Envelope<double> >("envelope",init<double,double,double,double>())
.def(init<>())
.def(init<const coord<double,2>&, const coord<double,2>&>())
.add_property("minx",&Envelope<double>::minx)
.add_property("miny",&Envelope<double>::miny)
.add_property("maxx",&Envelope<double>::maxx)
.add_property("maxy",&Envelope<double>::maxy)
.def("center",&Envelope<double>::center)
.def_pickle(envelope_pickle_suite())
;
}

99
python/mapnik_layer.cc Normal file
View file

@ -0,0 +1,99 @@
/* This file is part of python_mapnik (c++/python mapping toolkit)
* Copyright (C) 2005 Artem Pavlenko
*
* Mapnik is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
//$Id$
#include <mapnik.hh>
#include <boost/python.hpp>
#include <boost/python/detail/api_placeholder.hpp>
using mapnik::Layer;
using mapnik::Parameters;
struct layer_pickle_suite : boost::python::pickle_suite
{
static boost::python::tuple
getinitargs(const Layer& l)
{
using namespace boost::python;
return make_tuple(l.params());
}
static boost::python::tuple
getstate(const Layer& l)
{
using namespace boost::python;
return make_tuple(l.getMinZoom(),
l.getMaxZoom(),
l.getStyle());
}
static void
setstate (Layer& l, boost::python::tuple state)
{
using namespace boost::python;
if (len(state) != 3)
{
PyErr_SetObject(PyExc_ValueError,
("expected 3-item tuple in call to __setstate__; got %s"
% state).ptr()
);
throw_error_already_set();
}
l.setMinZoom(extract<double>(state[0]));
l.setMaxZoom(extract<double>(state[1]));
l.setStyle(extract<std::string>(state[2]));
}
};
namespace
{
//user-friendly wrapper that uses Python dictionary
using namespace boost::python;
Layer create_layer(const dict& d)
{
Parameters params;
list keys=d.keys();
for (int i=0;i<len(keys);++i)
{
std::string key=extract<std::string>(keys[i]);
std::string value=extract<std::string>(d[key]);
std::cout<<key<<":"<<value<<std::endl;
params.add(key,value);
}
return Layer(params);
}
}
void export_layer()
{
using namespace boost::python;
class_<Layer>("layer",init<const Parameters&>("Layer constructor"))
//class_<Layer>("layer",no_init)
.def("name",&Layer::name,return_value_policy<copy_const_reference>())
.def("params",&Layer::params,return_value_policy<reference_existing_object>())
.def("envelope",&Layer::envelope,return_value_policy<reference_existing_object>())
.def("minzoom",&Layer::setMinZoom)
.def("maxzoom",&Layer::setMaxZoom)
.def("style",&Layer::setStyle)
.def_pickle(layer_pickle_suite())
;
def("create_layer",&create_layer);
}

92
python/mapnik_map.cc Normal file
View file

@ -0,0 +1,92 @@
/* This file is part of python_mapnik (c++/python mapping toolkit)
* Copyright (C) 2005 Artem Pavlenko
*
* Mapnik is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
//$Id$
#include <mapnik.hh>
#include <boost/python.hpp>
#include <boost/python/detail/api_placeholder.hpp>
using mapnik::Color;
using mapnik::coord;
using mapnik::Envelope;
using mapnik::Layer;
using mapnik::Map;
struct map_pickle_suite : boost::python::pickle_suite
{
static boost::python::tuple
getinitargs(const Map& m)
{
using namespace boost::python;
return make_tuple(m.getWidth(),m.getHeight(),m.srid());
}
static boost::python::tuple
getstate(const Map& m)
{
using namespace boost::python;
list l;
for (unsigned i=0;i<m.layerCount();++i)
{
l.append(m.getLayer(i));
}
return make_tuple(m.getCurrentExtent(),m.getBackground(),l);
}
static void
setstate (Map& m, boost::python::tuple state)
{
using namespace boost::python;
if (len(state) != 3)
{
PyErr_SetObject(PyExc_ValueError,
("expected 3-item tuple in call to __setstate__; got %s"
% state).ptr()
);
throw_error_already_set();
}
Envelope<double> ext = extract<Envelope<double> >(state[0]);
Color bg = extract<Color>(state[1]);
m.zoomToBox(ext);
m.setBackground(bg);
list l=extract<list>(state[2]);
for (int i=0;i<len(l);++i)
{
m.addLayer(extract<Layer>(l[i]));
}
}
};
void export_map()
{
using namespace boost::python;
class_<Map>("map",init<int,int,optional<int> >())
.add_property("width",&Map::getWidth)
.add_property("height",&Map::getHeight)
.def("background",&Map::setBackground)
.def("scale", &Map::scale)
.def("add",&Map::addLayer)
.def("zoom_to_box",&Map::zoomToBox)
.def("pan",&Map::pan)
.def("zoom",&Map::zoom)
.def("pan_and_zoom",&Map::pan_and_zoom)
.def_pickle(map_pickle_suite())
;
}

View file

@ -0,0 +1,94 @@
/* This file is part of python_mapnik (c++/python mapping toolkit)
* Copyright (C) 2005 Artem Pavlenko
*
* Mapnik is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
//$Id$
#include <mapnik.hh>
#include <boost/python.hpp>
#include <boost/python/detail/api_placeholder.hpp>
using mapnik::Parameter;
using mapnik::Parameters;
void (Parameters::*add1)(const Parameter& param)=&Parameters::add;
void (Parameters::*add2)(const std::string&,const std::string&)=&Parameters::add;
struct parameter_pickle_suite : boost::python::pickle_suite
{
static boost::python::tuple
getinitargs(const Parameter& p)
{
using namespace boost::python;
return make_tuple(p.first,p.second);
}
};
struct parameters_pickle_suite : boost::python::pickle_suite
{
static boost::python::tuple
getstate(const Parameters& p)
{
using namespace boost::python;
dict d;
Parameters::const_iterator pos=p.begin();
while(pos!=p.end())
{
d[pos->first]=pos->second;
++pos;
}
return make_tuple(d);
}
static void setstate(Parameters& p, boost::python::tuple state)
{
using namespace boost::python;
if (len(state) != 1)
{
PyErr_SetObject(PyExc_ValueError,
("expected 1-item tuple in call to __setstate__; got %s"
% state).ptr()
);
throw_error_already_set();
}
dict d = extract<dict>(state[0]);
list keys=d.keys();
for (int i=0;i<len(keys);++i)
{
std::string key=extract<std::string>(keys[i]);
std::string value=extract<std::string>(d[key]);
p.add(key,value);
}
}
};
void export_parameters()
{
using namespace boost::python;
class_<Parameter>("parameter",init<std::string,std::string>())
.def_pickle(parameter_pickle_suite())
;
class_<Parameters>("parameters",init<>())
.def("add",add1)
.def("add",add2)
.def("get",&Parameters::get)
.def_pickle(parameters_pickle_suite())
;
}

153
python/mapnik_python.cc Normal file
View file

@ -0,0 +1,153 @@
/* This file is part of python_mapnik (c++/python mapping toolkit)
* Copyright (C) 2005 Artem Pavlenko
*
* Mapnik is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
//$Id$
#include <boost/get_pointer.hpp>
#include <boost/python.hpp>
#include <boost/python/detail/api_placeholder.hpp>
#include "mapnik.hh"
#include "polygon_symbolizer.hh"
#include "line_symbolizer.hh"
#include "image_symbolizer.hh"
void export_color();
void export_layer();
void export_parameters();
void export_envelope();
void export_map();
using namespace mapnik;
namespace boost
{
namespace python
{
template <typename T,
template <typename T> class DeallocPolicy>
T* get_pointer(ref_ptr< T , DeallocPolicy> const& ptr)
{
return ( T* )ptr.get();
}
template <typename T>
struct pointee<ref_ptr<T> >
{
typedef T type;
};
template <> struct pointee<ref_ptr<datasource,datasource_delete> >
{
typedef datasource type;
};
}
}
void render_to_file(const Map& map,const std::string& file,const std::string& format)
{
Image32 image(map.getWidth(),map.getHeight());
Renderer<Image32>::render(map,image);
image.saveToFile(file,format);
}
//BEGIN quick hack
ref_ptr<Symbolizer> create_point_symbolizer(const std::string& file,unsigned w,unsigned h)
{
return ref_ptr<Symbolizer>(new ImageSymbolizer(file,"png",w,h));
}
ref_ptr<Symbolizer> create_line_symbolizer(const Color& stroke,double minScale,double maxScale)
{
return ref_ptr<Symbolizer>(new LineSymbolizer(stroke,minScale,maxScale));
}
ref_ptr<Symbolizer> create_polygon_symbolizer(const Color& stroke,const Color& fill,double minScale,double maxScale)
{
return ref_ptr<Symbolizer>(new PolygonSymbolizer(fill,minScale,maxScale));
}
//END
BOOST_PYTHON_MODULE(mapnik)
{
using namespace boost::python;
class_<datasource,ref_ptr<datasource,datasource_delete>,
boost::noncopyable>("datasource",no_init)
.def("envelope",&datasource::envelope,
return_value_policy<reference_existing_object>())
;
export_parameters();
export_color();
export_envelope();
class_<Style>("style",init<>("Style default constructor"))
.def(init<ref_ptr<Symbolizer> >())
.def("add",&Style::add)
;
class_<Symbolizer,boost::noncopyable> ("symbolizer",no_init)
;
export_layer();
class_<singleton<datasource_cache,CreateStatic>,boost::noncopyable>("singleton",no_init)
.def("instance",&singleton<datasource_cache,CreateStatic>::instance,
return_value_policy<reference_existing_object>())
.staticmethod("instance")
;
class_<datasource_cache,bases<singleton<datasource_cache,CreateStatic> >,
boost::noncopyable>("datasource_cache",no_init)
.def("create",&datasource_cache::create)
.staticmethod("create")
;
class_<singleton<style_cache,CreateStatic>,boost::noncopyable>("singleton",no_init)
.def("instance",&singleton<style_cache,CreateStatic>::instance,
return_value_policy<reference_existing_object>())
.staticmethod("instance")
;
class_<style_cache,bases<singleton<style_cache,CreateStatic> >,
boost::noncopyable>("style_cache",no_init)
.def("insert",&style_cache::insert)
.staticmethod("insert")
.def("remove",&style_cache::remove)
.staticmethod("remove")
;
class_<coord<double,2> >("coord",init<double,double>())
.def_readwrite("x", &coord<double,2>::x)
.def_readwrite("y", &coord<double,2>::y)
;
export_map();
def("render_to_file",&render_to_file);
def("create_point_symbolizer",&create_point_symbolizer);
def("create_line_symbolizer",&create_line_symbolizer);
def("create_polygon_symbolizer",&create_polygon_symbolizer);
register_ptr_to_python<ref_ptr<Symbolizer> >();
}