mapnik/src/metawriter_factory.cpp

97 lines
3.2 KiB
C++
Raw Normal View History

/*****************************************************************************
2012-02-02 02:53:35 +01:00
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2011 Artem Pavlenko
*
* 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
*
*****************************************************************************/
// mapnik
#include <mapnik/metawriter_factory.hpp>
#include <mapnik/metawriter_json.hpp>
#include <mapnik/metawriter_inmem.hpp>
2012-03-11 23:24:28 +01:00
#include <mapnik/xml_node.hpp>
2012-03-07 15:26:13 +01:00
#include <mapnik/ptree_helpers.hpp>
2012-03-08 18:51:23 +01:00
#include <mapnik/config_error.hpp>
// boost
2012-03-10 01:20:50 +01:00
#include <boost/make_shared.hpp>
#include <boost/optional.hpp>
using boost::optional;
using std::string;
namespace mapnik
{
metawriter_ptr
2012-03-07 15:26:13 +01:00
metawriter_create(xml_node const& pt)
{
2012-02-02 02:53:35 +01:00
metawriter_ptr writer;
2012-03-07 15:26:13 +01:00
string type = pt.get_attr<string>("type");
2012-03-07 15:26:13 +01:00
optional<string> properties = pt.get_opt_attr<string>("default-output");
2012-02-02 02:53:35 +01:00
if (type == "json") {
2012-03-07 15:26:13 +01:00
string file = pt.get_attr<string>("file");
2012-03-10 01:20:50 +01:00
metawriter_json_ptr json = boost::make_shared<metawriter_json>(properties, parse_path(file));
2012-03-07 15:26:13 +01:00
optional<boolean> output_empty = pt.get_opt_attr<boolean>("output-empty");
2012-02-02 02:53:35 +01:00
if (output_empty) {
json->set_output_empty(*output_empty);
}
2012-03-07 15:26:13 +01:00
optional<boolean> pixel_coordinates = pt.get_opt_attr<boolean>("pixel-coordinates");
2012-02-02 02:53:35 +01:00
if (pixel_coordinates) {
json->set_pixel_coordinates(*pixel_coordinates);
}
writer = json;
} else if (type == "inmem") {
2012-03-10 01:20:50 +01:00
metawriter_inmem_ptr inmem = boost::make_shared<metawriter_inmem>(properties);
2012-02-02 02:53:35 +01:00
writer = inmem;
} else {
2012-03-12 01:09:26 +01:00
throw config_error(string("Unknown type '") + type + "'", pt);
}
2012-02-02 02:53:35 +01:00
return writer;
}
2012-02-02 02:53:35 +01:00
void
2012-03-07 15:26:13 +01:00
metawriter_save(const metawriter_ptr &metawriter,
boost::property_tree::ptree &metawriter_node, bool explicit_defaults)
{
2012-02-02 02:53:35 +01:00
metawriter_json *json = dynamic_cast<metawriter_json *>(metawriter.get());
if (json) {
set_attr(metawriter_node, "type", "json");
std::string const& filename = path_processor_type::to_string(*(json->get_filename()));
if (!filename.empty() || explicit_defaults) {
set_attr(metawriter_node, "file", filename);
}
}
metawriter_inmem *inmem = dynamic_cast<metawriter_inmem *>(metawriter.get());
if (inmem) {
set_attr(metawriter_node, "type", "inmem");
}
if (!metawriter->get_default_properties().empty() || explicit_defaults) {
set_attr(metawriter_node, "default-output", metawriter->get_default_properties().to_string());
}
}
} // namespace mapnik