Add some helper functions.

This commit is contained in:
Hermann Kraus 2010-08-02 00:37:15 +00:00
parent 0ae110015b
commit 75dcf67c92
2 changed files with 24 additions and 9 deletions

View file

@ -48,9 +48,13 @@ public:
virtual void start(metawriter_property_map const& properties); virtual void start(metawriter_property_map const& properties);
virtual void stop(); virtual void stop();
void set_stream(std::ostream *f) { f_ = f; } void set_stream(std::ostream *f) { f_ = f; }
std::ostream *get_stream() const { return f_; }
void set_only_nonempty(bool only_nonempty) { only_nonempty_ = only_nonempty; }
bool get_only_nonempty() { return only_nonempty_; }
protected: protected:
int count; int count_;
bool only_nonempty_;
private: private:
std::ostream *f_; std::ostream *f_;
}; };
@ -63,7 +67,8 @@ public:
virtual void start(metawriter_property_map const& properties); virtual void start(metawriter_property_map const& properties);
virtual void stop(); virtual void stop();
void set_filename(path_expression_ptr fn);
path_expression_ptr get_filename() const;
private: private:
path_expression_ptr fn_; path_expression_ptr fn_;
std::fstream f_; std::fstream f_;

View file

@ -55,20 +55,20 @@ void metawriter_json_stream::start(metawriter_property_map const& properties)
{ {
assert(f_); assert(f_);
*f_ << "{ \"type\": \"FeatureCollection\", \"features\": [\n"; *f_ << "{ \"type\": \"FeatureCollection\", \"features\": [\n";
count = 0; count_ = 0;
} }
void metawriter_json_stream::stop() void metawriter_json_stream::stop()
{ {
if (count >= 0 && f_) { if (count_ >= 0 && f_) {
*f_ << " ] }\n"; *f_ << " ] }\n";
} }
count = -1; count_ = -1;
} }
metawriter_json_stream::~metawriter_json_stream() metawriter_json_stream::~metawriter_json_stream()
{ {
if (count >= 0) { if (count_ >= 0) {
#ifdef MAPNIK_DEBUG #ifdef MAPNIK_DEBUG
std::cerr << "WARNING: GeoJSON metawriter not stopped before destroying it."; std::cerr << "WARNING: GeoJSON metawriter not stopped before destroying it.";
#endif #endif
@ -77,14 +77,14 @@ metawriter_json_stream::~metawriter_json_stream()
} }
metawriter_json_stream::metawriter_json_stream(metawriter_properties dflt_properties) metawriter_json_stream::metawriter_json_stream(metawriter_properties dflt_properties)
: metawriter(dflt_properties), count(-1), f_(0) {} : metawriter(dflt_properties), count_(-1), f_(0) {}
void metawriter_json_stream::add_box(box2d<double> box, Feature const &feature, void metawriter_json_stream::add_box(box2d<double> box, Feature const &feature,
proj_transform const& prj_trans, CoordTransform const &t, proj_transform const& prj_trans, CoordTransform const &t,
const metawriter_properties& properties) const metawriter_properties& properties)
{ {
#ifdef MAPNIK_DEBUG #ifdef MAPNIK_DEBUG
if (count < 0) if (count_ < 0)
{ {
std::cerr << "WARNING: Metawriter not started before using it.\n"; std::cerr << "WARNING: Metawriter not started before using it.\n";
} }
@ -117,7 +117,7 @@ void metawriter_json_stream::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 << "], [" <<
@ -176,4 +176,14 @@ void metawriter_json::stop()
#endif #endif
} }
void metawriter_json::set_filename(path_expression_ptr fn)
{
fn_ = fn;
}
path_expression_ptr metawriter_json::get_filename() const
{
return fn_;
}
}; };