From 6f9528c2e3c6e81b53983671d5f1e562286c1ab2 Mon Sep 17 00:00:00 2001 From: Artem Pavlenko Date: Wed, 14 Dec 2005 17:01:09 +0000 Subject: [PATCH] 1.added serialization support 2.some class names changes --- SConstruct | 8 +-- boost/SConscript | 11 +++- datasources/postgis/postgis.cpp | 2 +- datasources/postgis/postgis.hpp | 2 +- datasources/raster/raster_datasource.cpp | 2 +- datasources/raster/raster_datasource.hpp | 2 +- datasources/shape/shape.cpp | 2 +- datasources/shape/shape.hpp | 2 +- include/datasource.hpp | 4 +- include/datasource_cache.hpp | 2 +- include/layer.hpp | 35 +++++++++-- include/map.hpp | 22 ++++++- include/mapnik.hpp | 5 ++ include/params.hpp | 77 +++++++++++++++++++----- python/mapnik_layer.cpp | 6 +- python/mapnik_parameters.cpp | 29 +++++---- src/SConscript | 3 +- src/datasource_cache.cpp | 4 +- src/layer.cpp | 51 +++++++++------- src/map.cpp | 11 ++-- src/mapnik.cpp | 61 +++++++++++++++++++ src/params.cpp | 35 +---------- src/scanline_aa.cpp | 4 +- 23 files changed, 260 insertions(+), 120 deletions(-) create mode 100644 src/mapnik.cpp diff --git a/SConstruct b/SConstruct index 991b5ca41..d62d28bf0 100644 --- a/SConstruct +++ b/SConstruct @@ -41,11 +41,11 @@ build_prefix = build_dir+'/'+str(platform) env = Environment(ENV=os.environ, options=opts) -cxx_debug='-Wall -ftemplate-depth-100 -O0 -fno-inline -g -pthread -DDEBUG' -cxx_release='-Wall -ftemplate-depth-100 -O3 -finline-functions -Wno-inline -pthread -DNDEBUG' +cxx_debug='-Wall -Wno-non-virtual-dtor -Wno-ctor-dtor-privacy -ftemplate-depth-100 -O0 -fno-inline -g -pthread -DDEBUG' +cxx_release='-Wall -Wno-non-virtual-dtor -Wno-ctor-dtor-privacy -ftemplate-depth-100 -O3 -finline-functions -Wno-inline -pthread -DNDEBUG ' -release_env = env.Copy(CXXFLAGS = cxx_release) -debug_env = env.Copy(CXXFLAGS = cxx_debug) +#release_env = env.Copy(CXXFLAGS = cxx_release) +#debug_env = env.Copy(CXXFLAGS = cxx_debug) if ARGUMENTS.get('debug',0): env.Append(CXXFLAGS = cxx_debug) diff --git a/boost/SConscript b/boost/SConscript index b015e7c2d..17c8b30d8 100644 --- a/boost/SConscript +++ b/boost/SConscript @@ -24,16 +24,23 @@ Import('env') prefix = env['PREFIX'] boost_root = env['BOOST_ROOT'] -# boost filesystem +#boost filesystem filesystem_src_dir = boost_root + '/libs/filesystem/src/' boost_fs_src= glob.glob(filesystem_src_dir + '*.cpp') lib_boost_filesystem = env.SharedLibrary('libboost-filesystem',boost_fs_src,LIBS=[],CPPPATH=boost_root) env.Install(prefix+'/lib',lib_boost_filesystem) + #boost regex regex_src_dir = boost_root + '/libs/regex/src/' boost_regex_src = glob.glob(regex_src_dir+ '*.cpp') lib_boost_regex = env.SharedLibrary('libboost-regex',boost_regex_src,LIBS=[],CPPPATH=boost_root) - env.Install(prefix+'/lib',lib_boost_regex) +#boost serialization +serialization_src_dir = boost_root + '/libs/serialization/src/' +boost_serialization_src = glob.glob(serialization_src_dir+ '*.cpp') +#cxxflags = env['CXXFLAGS'] + " -Wno-non-virtual-dtor -Wno-ctor-dtor-privacy" +#lib_boost_serialization = env.SharedLibrary('libboost-serialization',boost_serialization_src,CXXFLAGS=cxxflags,LIBS=[],CPPPATH=boost_root) +lib_boost_serialization = env.SharedLibrary('libboost-serialization',boost_serialization_src,LIBS=[],CPPPATH=boost_root) +env.Install(prefix+'/lib',lib_boost_serialization) diff --git a/datasources/postgis/postgis.cpp b/datasources/postgis/postgis.cpp index 273f1c9b6..16f2aae4a 100644 --- a/datasources/postgis/postgis.cpp +++ b/datasources/postgis/postgis.cpp @@ -38,7 +38,7 @@ using boost::lexical_cast; using boost::bad_lexical_cast; using boost::shared_ptr; -postgis_datasource::postgis_datasource(const Parameters& params) +postgis_datasource::postgis_datasource(const parameters& params) : table_(params.get("table")), type_(datasource::Vector), desc_(params.get("name")), diff --git a/datasources/postgis/postgis.hpp b/datasources/postgis/postgis.hpp index a2ad1bc60..20bd36606 100644 --- a/datasources/postgis/postgis.hpp +++ b/datasources/postgis/postgis.hpp @@ -54,7 +54,7 @@ public: featureset_ptr features(const query& q) const; mapnik::Envelope const& envelope() const; layer_descriptor const& get_descriptor() const; - postgis_datasource(const Parameters ¶ms); + postgis_datasource(const parameters ¶ms); ~postgis_datasource(); private: static std::string table_from_sql(const std::string& sql); diff --git a/datasources/raster/raster_datasource.cpp b/datasources/raster/raster_datasource.cpp index 17016f9c1..4b8fd57c9 100644 --- a/datasources/raster/raster_datasource.cpp +++ b/datasources/raster/raster_datasource.cpp @@ -31,7 +31,7 @@ using std::endl; using boost::lexical_cast; using boost::bad_lexical_cast; -raster_datasource::raster_datasource(const Parameters& params) +raster_datasource::raster_datasource(const parameters& params) : desc_(params.get("name")) { filename_=params.get("file"); diff --git a/datasources/raster/raster_datasource.hpp b/datasources/raster/raster_datasource.hpp index 37ffa48c2..a3c381345 100644 --- a/datasources/raster/raster_datasource.hpp +++ b/datasources/raster/raster_datasource.hpp @@ -37,7 +37,7 @@ private: layer_descriptor desc_; static std::string name_; public: - raster_datasource(const Parameters& params); + raster_datasource(const parameters& params); virtual ~raster_datasource(); int type() const; static std::string name(); diff --git a/datasources/shape/shape.cpp b/datasources/shape/shape.cpp index 258a3340a..3af2f69d4 100644 --- a/datasources/shape/shape.cpp +++ b/datasources/shape/shape.cpp @@ -26,7 +26,7 @@ DATASOURCE_PLUGIN(shape_datasource); -shape_datasource::shape_datasource(const Parameters ¶ms) +shape_datasource::shape_datasource(const parameters ¶ms) : shape_name_(params.get("file")), type_(datasource::Vector), file_length_(0), diff --git a/datasources/shape/shape.hpp b/datasources/shape/shape.hpp index 1db981cb3..db57f9691 100644 --- a/datasources/shape/shape.hpp +++ b/datasources/shape/shape.hpp @@ -33,7 +33,7 @@ public: static std::string name(); featureset_ptr features(const query& q) const; const Envelope& envelope() const; - shape_datasource(const Parameters ¶ms); + shape_datasource(const parameters ¶ms); layer_descriptor const& get_descriptor() const; virtual ~shape_datasource(); private: diff --git a/include/datasource.hpp b/include/datasource.hpp index 172340123..a926662c9 100644 --- a/include/datasource.hpp +++ b/include/datasource.hpp @@ -72,7 +72,7 @@ namespace mapnik }; typedef std::string datasource_name(); - typedef datasource* create_ds(const Parameters& params); + typedef datasource* create_ds(const parameters& params); typedef void destroy_ds(datasource *ds); @@ -93,7 +93,7 @@ namespace mapnik { \ return classname::name();\ }\ - extern "C" datasource* create(const Parameters ¶ms) \ + extern "C" datasource* create(const parameters ¶ms) \ { \ return new classname(params); \ }\ diff --git a/include/datasource_cache.hpp b/include/datasource_cache.hpp index c71744424..5446fd6af 100644 --- a/include/datasource_cache.hpp +++ b/include/datasource_cache.hpp @@ -43,7 +43,7 @@ namespace mapnik static bool insert(const std::string& name,const lt_dlhandle module); public: static void register_datasources(const std::string& path); - static boost::shared_ptr create(Parameters const& params); + static boost::shared_ptr create(parameters const& params); }; } #endif //DATASOURCE_CACHE_HPP diff --git a/include/layer.hpp b/include/layer.hpp index 88b1c110b..b46cb73f2 100644 --- a/include/layer.hpp +++ b/include/layer.hpp @@ -25,29 +25,45 @@ #include "feature.hpp" #include "datasource.hpp" #include +#include namespace mapnik { class Layer { - private: - Parameters params_; + friend class boost::serialization::access; + template + void serialize(Archive & ar, const unsigned int /*version*/) + { + ar & boost::serialization::make_nvp("name",name_) + & boost::serialization::make_nvp("params",params_) + & boost::serialization::make_nvp("min_zoom",minZoom_) + & boost::serialization::make_nvp("max_zoom",maxZoom_) + & boost::serialization::make_nvp("active",active_) + & boost::serialization::make_nvp("selectable",selectable_) + & boost::serialization::make_nvp("styles",styles_) + ; + } + parameters params_; std::string name_; double minZoom_; double maxZoom_; bool active_; bool selectable_; - datasource_p ds_; + std::vector styles_; std::string selection_style_; + + mutable datasource_p ds_; mutable std::vector > selection_; - + public: - explicit Layer(const Parameters& params); + Layer(); + explicit Layer(const parameters& params); Layer(Layer const& l); Layer& operator=(Layer const& l); bool operator==(Layer const& other) const; - Parameters const& params() const; + parameters const& params() const; const std::string& name() const; void add_style(std::string const& stylename); std::vector const& styles() const; @@ -72,4 +88,11 @@ namespace mapnik void swap(const Layer& other); }; } + +BOOST_CLASS_IMPLEMENTATION(std::vector, boost::serialization::object_serializable) +BOOST_CLASS_TRACKING(std::vector, boost::serialization::track_never) + +BOOST_CLASS_IMPLEMENTATION(mapnik::Layer, boost::serialization::object_serializable) +BOOST_CLASS_TRACKING(mapnik::Layer, boost::serialization::track_never) + #endif //LAYER_HPP diff --git a/include/map.hpp b/include/map.hpp index 2089d368d..6ef59e849 100644 --- a/include/map.hpp +++ b/include/map.hpp @@ -21,13 +21,25 @@ #ifndef MAP_HPP #define MAP_HPP +#include +#include + namespace mapnik { class Layer; class Map { - private: + friend class boost::serialization::access; + template + void serialize(Archive & ar, const unsigned int /*version*/) + { + ar & boost::serialization::make_nvp("width",width_) + & boost::serialization::make_nvp("height",height_) + & boost::serialization::make_nvp("srid",srid_) + & boost::serialization::make_nvp("layers",layers_); + } + static const unsigned MIN_MAPSIZE=16; static const unsigned MAX_MAPSIZE=1024; unsigned width_; @@ -37,6 +49,7 @@ namespace mapnik std::vector layers_; Envelope currentExtent_; public: + Map(); Map(int width,int height,int srid=-1); Map(const Map& rhs); Map& operator=(const Map& rhs); @@ -65,4 +78,11 @@ namespace mapnik void fixAspectRatio(); }; } + +BOOST_CLASS_IMPLEMENTATION(std::vector, boost::serialization::object_serializable) +BOOST_CLASS_TRACKING(std::vector, boost::serialization::track_never) + +BOOST_CLASS_IMPLEMENTATION(mapnik::Map, boost::serialization::object_serializable) +BOOST_CLASS_TRACKING(mapnik::Map, boost::serialization::track_never) + #endif //MAP_HPP diff --git a/include/mapnik.hpp b/include/mapnik.hpp index 8ade5f884..fc3d81bc6 100644 --- a/include/mapnik.hpp +++ b/include/mapnik.hpp @@ -71,6 +71,11 @@ namespace mapnik { + void save_to_xml(Map const& map,const char* filename); + void save_to_text(Map const& map,const char* filename); + + void load_from_xml(Map & map, const char * filename); + void load_from_text(Map & map, const char * filename); } #endif //MAPNIK_HPP diff --git a/include/params.hpp b/include/params.hpp index 484ab5c80..e879fefdf 100644 --- a/include/params.hpp +++ b/include/params.hpp @@ -22,26 +22,75 @@ #define PARAMS_HPP #include +//#include +//#include +//#include +#include +#include +#include +#include +#include +#include +#include +#include +#include namespace mapnik { - typedef std::pair Parameter; + typedef std::pair parameter; + typedef std::map param_map; - class Parameters + class parameters : public param_map { - typedef std::map ParamMap; - private: - ParamMap data_; + friend class boost::serialization::access; + + template + void save(Archive & ar, const unsigned int /*version*/) const + { + const size_t size = param_map::size(); + ar & boost::serialization::make_nvp("count",size); + param_map::const_iterator itr; + for (itr=param_map::begin();itr!=param_map::end();++itr) + { + ar & boost::serialization::make_nvp("name",itr->first); + ar & boost::serialization::make_nvp("value",itr->second); + } + } + + template + void load(Archive & ar, const unsigned int /*version*/) + { + size_t size; + ar & boost::serialization::make_nvp("size",size); + for (size_t i=0;isecond; + } + return std::string(); + } }; } -#endif //PARAMS_HPP + +BOOST_CLASS_IMPLEMENTATION(mapnik::parameter, boost::serialization::object_serializable) +BOOST_CLASS_TRACKING(mapnik::parameter, boost::serialization::track_never) + +BOOST_CLASS_IMPLEMENTATION(mapnik::parameters, boost::serialization::object_serializable) +BOOST_CLASS_TRACKING(mapnik::parameters, boost::serialization::track_never) + +#endif //PARAMS_HPP diff --git a/python/mapnik_layer.cpp b/python/mapnik_layer.cpp index e6a451ef2..09905a744 100644 --- a/python/mapnik_layer.cpp +++ b/python/mapnik_layer.cpp @@ -25,7 +25,7 @@ #include using mapnik::Layer; -using mapnik::Parameters; +using mapnik::parameters; struct layer_pickle_suite : boost::python::pickle_suite { @@ -85,13 +85,13 @@ namespace using namespace boost::python; Layer create_layer(const dict& d) { - Parameters params; + parameters params; boost::python::list keys=d.keys(); for (int i=0;i(keys[i]); std::string value=extract(d[key]); - params.add(key,value); + params[key] = value; } return Layer(params); } diff --git a/python/mapnik_parameters.cpp b/python/mapnik_parameters.cpp index 03422b0a0..00cc78f8e 100644 --- a/python/mapnik_parameters.cpp +++ b/python/mapnik_parameters.cpp @@ -22,17 +22,16 @@ #include #include -using mapnik::Parameter; -using mapnik::Parameters; +using mapnik::parameter; +using mapnik::parameters; - -void (Parameters::*add1)(const Parameter&)=&Parameters::add; -void (Parameters::*add2)(const std::string&,const std::string&)=&Parameters::add; +//void (parameters::*add1)(const parameter&)=¶meters::insert; +//void (parameters::*add2)(std::make_pair(const std::string&,const std::string&))=¶meters::insert; struct parameter_pickle_suite : boost::python::pickle_suite { static boost::python::tuple - getinitargs(const Parameter& p) + getinitargs(const parameter& p) { using namespace boost::python; return boost::python::make_tuple(p.first,p.second); @@ -42,11 +41,11 @@ struct parameter_pickle_suite : boost::python::pickle_suite struct parameters_pickle_suite : boost::python::pickle_suite { static boost::python::tuple - getstate(const Parameters& p) + getstate(const parameters& p) { using namespace boost::python; dict d; - Parameters::const_iterator pos=p.begin(); + parameters::const_iterator pos=p.begin(); while(pos!=p.end()) { d[pos->first]=pos->second; @@ -55,7 +54,7 @@ struct parameters_pickle_suite : boost::python::pickle_suite return boost::python::make_tuple(d); } - static void setstate(Parameters& p, boost::python::tuple state) + static void setstate(parameters& p, boost::python::tuple state) { using namespace boost::python; if (len(state) != 1) @@ -72,7 +71,7 @@ struct parameters_pickle_suite : boost::python::pickle_suite { std::string key=extract(keys[i]); std::string value=extract(d[key]); - p.add(key,value); + p[key] = value; } } }; @@ -81,14 +80,14 @@ struct parameters_pickle_suite : boost::python::pickle_suite void export_parameters() { using namespace boost::python; - class_("parameter",init()) + class_("parameter",init()) .def_pickle(parameter_pickle_suite()) ; - class_("parameters",init<>()) - .def("add",add1) - .def("add",add2) - .def("get",&Parameters::get) + class_("parameters",init<>()) + //.def("add",add1) + //.def("add",add2) + .def("get",¶meters::get) .def_pickle(parameters_pickle_suite()) ; } diff --git a/src/SConscript b/src/SConscript index 96fa49602..16acf8425 100644 --- a/src/SConscript +++ b/src/SConscript @@ -30,12 +30,13 @@ freetype2_root = env['FREETYPE2_ROOT'] headers =[ '#include',boost_root,freetype2_root,agg_headers] -libraries=['rt','z','png','ltdl','jpeg','tiff','agg','boost-filesystem','boost-regex'] +libraries=['rt','z','png','ltdl','jpeg','tiff','agg','boost-filesystem','boost-regex','boost-serialization'] libpaths = [prefix+'/lib','#agg'] linkflags = '-Wl,-rpath-link,. -Wl,-soname,libmapnik.so' source = Split( """ + mapnik.cpp datasource_cache.cpp envelope.cpp graphics.cpp diff --git a/src/datasource_cache.cpp b/src/datasource_cache.cpp index 49f8f467c..63c40be6c 100644 --- a/src/datasource_cache.cpp +++ b/src/datasource_cache.cpp @@ -42,12 +42,12 @@ namespace mapnik std::map > datasource_cache::plugins_; bool datasource_cache::registered_=false; - datasource_p datasource_cache::create(const Parameters& params) + datasource_p datasource_cache::create(const parameters& params) { datasource_p ds; try { - std::string type=params.get("type"); + const std::string type=params.get("type"); map >::iterator itr=plugins_.find(type); if (itr!=plugins_.end()) { diff --git a/src/layer.cpp b/src/layer.cpp index 57d44d664..f1fa2a602 100644 --- a/src/layer.cpp +++ b/src/layer.cpp @@ -18,35 +18,28 @@ //$Id: layer.cpp 17 2005-03-08 23:58:43Z pavlenko $ -#include + #include "style.hpp" #include "datasource.hpp" #include "datasource_cache.hpp" #include "layer.hpp" +#include +#include + namespace mapnik { using namespace std; - - Layer::Layer(const Parameters& params) + Layer::Layer() {} + Layer::Layer(const parameters& params) :params_(params), + name_(params_["name"]), minZoom_(0), maxZoom_(std::numeric_limits::max()), active_(true), selectable_(false), selection_style_("default_selection") - { - - try - { - name_=params_.get("name"); - ds_=datasource_cache::instance()->create(params_); - } - catch (...) - { - throw; - } - } + {} Layer::Layer(const Layer& rhs) :params_(rhs.params_), @@ -55,7 +48,7 @@ namespace mapnik maxZoom_(rhs.maxZoom_), active_(rhs.active_), selectable_(rhs.selectable_), - ds_(rhs.ds_), + //ds_(rhs.ds_), styles_(rhs.styles_), selection_style_(rhs.selection_style_) {} @@ -79,14 +72,14 @@ namespace mapnik maxZoom_=rhs.maxZoom_; active_=rhs.active_; selectable_=rhs.selectable_; - ds_=rhs.ds_; + //ds_=rhs.ds_; styles_=rhs.styles_; selection_style_=rhs.selection_style_; } Layer::~Layer() {} - Parameters const& Layer::params() const + parameters const& Layer::params() const { return params_; } @@ -153,14 +146,28 @@ namespace mapnik const datasource_p& Layer::datasource() const { - return ds_; + if (!ds_) + { + try + { + ds_=datasource_cache::instance()->create(params_); + } + catch (...) + { + std::cerr << "exception caught : can not create datasorce" << std::endl; + } + } + return ds_; } Envelope Layer::envelope() const { - if (ds_) - return ds_->envelope(); - return Envelope(); + datasource_p const& ds = datasource(); + if (ds) + { + return ds->envelope(); + } + return Envelope(); } void Layer::selection_style(const std::string& name) diff --git a/src/map.cpp b/src/map.cpp index 094f22380..5e9e833e6 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -25,12 +25,15 @@ namespace mapnik { - + Map::Map() + : width_(400), + height_(400), + srid_(-1) {} Map::Map(int width,int height,int srid) : width_(width), - height_(height), - srid_(srid), - background_(Color(255,255,255)) {} + height_(height), + srid_(srid), + background_(Color(255,255,255)) {} Map::Map(const Map& rhs) : width_(rhs.width_), diff --git a/src/mapnik.cpp b/src/mapnik.cpp new file mode 100644 index 000000000..ead495613 --- /dev/null +++ b/src/mapnik.cpp @@ -0,0 +1,61 @@ +/* This file is part of Mapnik (c++ 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.hpp" +#include +#include +#include +#include +#include + +namespace mapnik +{ + void save_to_xml(Map const& m,const char* filename) + { + std::ofstream ofs(filename); + assert(ofs.good()); + boost::archive::xml_oarchive oa(ofs); + oa << boost::serialization::make_nvp("map",m); + } + + void save_to_text(Map const& m,const char* filename) + { + std::ofstream ofs(filename); + assert(ofs.good()); + boost::archive::text_oarchive oa(ofs); + oa << m; + } + + void load_from_xml(Map & m,const char* filename) + { + std::ifstream ifs(filename); + assert(ifs.good()); + boost::archive::xml_iarchive ia(ifs); + ia >> boost::serialization::make_nvp("map",m); + } + + void load_from_text(Map & m,const char* filename) + { + std::ifstream ifs(filename); + assert(ifs.good()); + boost::archive::text_iarchive ia(ifs); + ia >> m; + } +} diff --git a/src/params.cpp b/src/params.cpp index c7b2853b0..e5f3e65f4 100644 --- a/src/params.cpp +++ b/src/params.cpp @@ -18,42 +18,9 @@ //$Id: params.cpp 17 2005-03-08 23:58:43Z pavlenko $ -#include -#include #include "params.hpp" namespace mapnik { - - const std::string Parameters::get(const std::string& name) const - { - std::map::const_iterator itr; - itr=data_.find(name); - if (itr!=data_.end()) - return itr->second; - return std::string(); - } - - void Parameters::add(const Parameter& param) - { - data_.insert(param); - } - - void Parameters::add(const std::string& name,const std::string& value) - { - Parameter param(name,value); - data_.insert(param); - } - - std::map::const_iterator Parameters::begin() const - { - return data_.begin(); - } - - std::map::const_iterator Parameters::end() const - { - return data_.end(); - } - - Parameters::~Parameters() {} + } diff --git a/src/scanline_aa.cpp b/src/scanline_aa.cpp index ba549fc85..937888d5b 100644 --- a/src/scanline_aa.cpp +++ b/src/scanline_aa.cpp @@ -603,9 +603,7 @@ namespace mapnik pixbuf_->blendPixel(x,y,rgba,alpha); } } - - - + template inline void ScanlineRasterizerAA::render_hline(int x0,int x1,int y,unsigned rgba) {