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>
|
#include <mapnik/metawriter.hpp>
|
||||||
|
|
||||||
// STL
|
// STL
|
||||||
#include <fstream>
|
#include <ostream>
|
||||||
|
|
||||||
namespace mapnik {
|
namespace mapnik {
|
||||||
/** JSON writer. */
|
/** JSON writer. */
|
||||||
|
@ -36,6 +36,7 @@ class metawriter_json : public metawriter, private boost::noncopyable
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
metawriter_json(metawriter_properties dflt_properties, std::string fn);
|
metawriter_json(metawriter_properties dflt_properties, std::string fn);
|
||||||
|
metawriter_json(metawriter_properties dflt_properties, std::ostream);
|
||||||
~metawriter_json();
|
~metawriter_json();
|
||||||
virtual void add_box(box2d<double> box, Feature const &feature,
|
virtual void add_box(box2d<double> box, Feature const &feature,
|
||||||
proj_transform const& prj_trans,
|
proj_transform const& prj_trans,
|
||||||
|
@ -44,7 +45,7 @@ class metawriter_json : public metawriter, private boost::noncopyable
|
||||||
virtual void start();
|
virtual void start();
|
||||||
virtual void stop();
|
virtual void stop();
|
||||||
private:
|
private:
|
||||||
std::fstream f;
|
std::ostream *f;
|
||||||
std::string fn_;
|
std::string fn_;
|
||||||
int count;
|
int count;
|
||||||
};
|
};
|
||||||
|
|
|
@ -32,6 +32,7 @@
|
||||||
// STL
|
// STL
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
namespace mapnik {
|
namespace mapnik {
|
||||||
|
|
||||||
|
@ -48,21 +49,27 @@ metawriter_json::~metawriter_json()
|
||||||
|
|
||||||
void metawriter_json::start()
|
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);
|
assert(f);
|
||||||
if (f.fail()) perror((std::string("Failed to open file ") + fn_).c_str());
|
*f << "{ \"type\": \"FeatureCollection\", \"features\": [\n";
|
||||||
f << "{ \"type\": \"FeatureCollection\", \"features\": [\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void metawriter_json::stop()
|
void metawriter_json::stop()
|
||||||
{
|
{
|
||||||
if (f.is_open())
|
if (f) *f << " ] }\n";
|
||||||
|
if (f && !fn_.empty())
|
||||||
{
|
{
|
||||||
f << " ] }\n";
|
dynamic_cast<std::fstream *>(f)->close();
|
||||||
f.close();
|
f = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -105,8 +112,8 @@ void metawriter_json::add_box(box2d<double> box, Feature const &feature,
|
||||||
double maxx = box.maxx();
|
double maxx = box.maxx();
|
||||||
double maxy = box.maxy();
|
double maxy = box.maxy();
|
||||||
|
|
||||||
if (count++) f << ",\n";
|
if (count++) *f << ",\n";
|
||||||
f << std::fixed << std::setprecision(8) << "{ \"type\": \"Feature\",\n \"geometry\": { \"type\": \"Polygon\",\n \"coordinates\": [ [ [" <<
|
*f << std::fixed << std::setprecision(8) << "{ \"type\": \"Feature\",\n \"geometry\": { \"type\": \"Polygon\",\n \"coordinates\": [ [ [" <<
|
||||||
minx << ", " << miny << "], [" <<
|
minx << ", " << miny << "], [" <<
|
||||||
maxx << ", " << miny << "], [" <<
|
maxx << ", " << miny << "], [" <<
|
||||||
maxx << ", " << maxy << "], [" <<
|
maxx << ", " << maxy << "], [" <<
|
||||||
|
@ -123,11 +130,11 @@ void metawriter_json::add_box(box2d<double> box, Feature const &feature,
|
||||||
//Property found
|
//Property found
|
||||||
text = boost::replace_all_copy(boost::replace_all_copy(itr->second.to_string(), "\\", "\\\\"), "\"", "\\\"");
|
text = boost::replace_all_copy(boost::replace_all_copy(itr->second.to_string(), "\\", "\\\\"), "\"", "\\\"");
|
||||||
}
|
}
|
||||||
if (i++) f << ",";
|
if (i++) *f << ",";
|
||||||
f << "\n \"" << p << "\":\"" << text << "\"";
|
*f << "\n \"" << p << "\":\"" << text << "\"";
|
||||||
}
|
}
|
||||||
|
|
||||||
f << "\n} }";
|
*f << "\n} }";
|
||||||
}
|
}
|
||||||
|
|
||||||
metawriter_properties::metawriter_properties(boost::optional<std::string> str)
|
metawriter_properties::metawriter_properties(boost::optional<std::string> str)
|
||||||
|
|
Loading…
Add table
Reference in a new issue