Enable metawriter_json to operate on any stream object.
This commit is contained in:
parent
42891914ab
commit
2c10bb68ef
2 changed files with 23 additions and 15 deletions
|
@ -28,7 +28,7 @@
|
|||
#include <mapnik/metawriter.hpp>
|
||||
|
||||
// STL
|
||||
#include <fstream>
|
||||
#include <ostream>
|
||||
|
||||
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<double> 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;
|
||||
};
|
||||
|
|
|
@ -32,6 +32,7 @@
|
|||
// STL
|
||||
#include <iomanip>
|
||||
#include <cstdio>
|
||||
#include <fstream>
|
||||
|
||||
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<std::fstream *>(f)->close();
|
||||
f = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -105,8 +112,8 @@ void metawriter_json::add_box(box2d<double> 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<double> 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<std::string> str)
|
||||
|
|
Loading…
Reference in a new issue