From 2c10bb68eff377ee3335ceb586476aaf4f71004f Mon Sep 17 00:00:00 2001 From: Hermann Kraus Date: Sun, 18 Jul 2010 23:49:11 +0000 Subject: [PATCH] Enable metawriter_json to operate on any stream object. --- include/mapnik/metawriter_json.hpp | 5 +++-- src/metawriter.cpp | 33 ++++++++++++++++++------------ 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/include/mapnik/metawriter_json.hpp b/include/mapnik/metawriter_json.hpp index 4d29cc552..a8cb87e16 100644 --- a/include/mapnik/metawriter_json.hpp +++ b/include/mapnik/metawriter_json.hpp @@ -28,7 +28,7 @@ #include // STL -#include +#include namespace mapnik { /** JSON writer. */ @@ -36,6 +36,7 @@ class metawriter_json : public metawriter, private boost::noncopyable { public: metawriter_json(metawriter_properties dflt_properties, std::string fn); + metawriter_json(metawriter_properties dflt_properties, std::ostream); ~metawriter_json(); virtual void add_box(box2d box, Feature const &feature, proj_transform const& prj_trans, @@ -44,7 +45,7 @@ class metawriter_json : public metawriter, private boost::noncopyable virtual void start(); virtual void stop(); private: - std::fstream f; + std::ostream *f; std::string fn_; int count; }; diff --git a/src/metawriter.cpp b/src/metawriter.cpp index 2ee9b1048..562489886 100644 --- a/src/metawriter.cpp +++ b/src/metawriter.cpp @@ -32,6 +32,7 @@ // STL #include #include +#include namespace mapnik { @@ -48,21 +49,27 @@ metawriter_json::~metawriter_json() void metawriter_json::start() { - if (f.is_open()) + if (!fn_.empty()) { - std::cerr << "ERROR: GeoJSON metawriter is already active!\n"; + if (f) + { + std::cerr << "ERROR: GeoJSON metawriter is already active!\n"; + return; + } + f = new std::fstream(fn_.c_str(), std::fstream::out | std::fstream::trunc); + if (f->fail()) perror((std::string("Failed to open file ") + fn_).c_str()); } - f.open(fn_.c_str(), std::fstream::out | std::fstream::trunc); - if (f.fail()) perror((std::string("Failed to open file ") + fn_).c_str()); - f << "{ \"type\": \"FeatureCollection\", \"features\": [\n"; + assert(f); + *f << "{ \"type\": \"FeatureCollection\", \"features\": [\n"; } void metawriter_json::stop() { - if (f.is_open()) + if (f) *f << " ] }\n"; + if (f && !fn_.empty()) { - f << " ] }\n"; - f.close(); + dynamic_cast(f)->close(); + f = 0; } } @@ -105,8 +112,8 @@ void metawriter_json::add_box(box2d box, Feature const &feature, double maxx = box.maxx(); double maxy = box.maxy(); - if (count++) f << ",\n"; - f << std::fixed << std::setprecision(8) << "{ \"type\": \"Feature\",\n \"geometry\": { \"type\": \"Polygon\",\n \"coordinates\": [ [ [" << + if (count++) *f << ",\n"; + *f << std::fixed << std::setprecision(8) << "{ \"type\": \"Feature\",\n \"geometry\": { \"type\": \"Polygon\",\n \"coordinates\": [ [ [" << minx << ", " << miny << "], [" << maxx << ", " << miny << "], [" << maxx << ", " << maxy << "], [" << @@ -123,11 +130,11 @@ void metawriter_json::add_box(box2d box, Feature const &feature, //Property found text = boost::replace_all_copy(boost::replace_all_copy(itr->second.to_string(), "\\", "\\\\"), "\"", "\\\""); } - if (i++) f << ","; - f << "\n \"" << p << "\":\"" << text << "\""; + if (i++) *f << ","; + *f << "\n \"" << p << "\":\"" << text << "\""; } - f << "\n} }"; + *f << "\n} }"; } metawriter_properties::metawriter_properties(boost::optional str)