From 8bb6df1a072d0ccb57ee7dbaa8c5e8c8b9a39f6e Mon Sep 17 00:00:00 2001 From: Hermann Kraus Date: Sat, 16 Jul 2011 12:24:52 +0000 Subject: [PATCH] Optionally output JSON data with pixel coordinates. --- include/mapnik/metawriter.hpp | 5 ++--- include/mapnik/metawriter_inmem.hpp | 2 -- include/mapnik/metawriter_json.hpp | 13 +++++++++---- src/metawriter.cpp | 23 +++++++++++++++-------- src/metawriter_factory.cpp | 4 +--- src/metawriter_inmem.cpp | 11 ----------- 6 files changed, 27 insertions(+), 31 deletions(-) diff --git a/include/mapnik/metawriter.hpp b/include/mapnik/metawriter.hpp index c0377b45a..3549e4f8e 100644 --- a/include/mapnik/metawriter.hpp +++ b/include/mapnik/metawriter.hpp @@ -81,7 +81,7 @@ public: * \param box Area (in pixel coordinates) * \param feature The feature being processed * \param prj_trans Projection transformation - * \param t Cooridnate transformation + * \param t Coordinate transformation * \param properties List of properties to output */ virtual void add_box(box2d const& box, Feature const& feature, @@ -107,7 +107,6 @@ public: * \param properties metawriter_property_map object with userdefined values. * Useful for setting filename etc. */ - virtual void start(metawriter_property_map const& properties) { boost::ignore_unused_variable_warning(properties); @@ -122,7 +121,7 @@ public: */ void set_size(int width, int height) { width_ = width; height_ = height; } /** Set Map object's srs. */ - virtual void set_map_srs(projection const& proj) = 0; + virtual void set_map_srs(projection const& proj) { /* Not required when working with image coordinates. */ } /** Return the list of default properties. */ metawriter_properties const& get_default_properties() const { return dflt_properties_;} protected: diff --git a/include/mapnik/metawriter_inmem.hpp b/include/mapnik/metawriter_inmem.hpp index 4c27ea23a..38bfc32b1 100644 --- a/include/mapnik/metawriter_inmem.hpp +++ b/include/mapnik/metawriter_inmem.hpp @@ -80,8 +80,6 @@ public: metawriter_properties const& properties); virtual void start(metawriter_property_map const& properties); - virtual void stop(); - virtual void set_map_srs(projection const& proj); /** * An instance of a rendered feature. The box represents the image diff --git a/include/mapnik/metawriter_json.hpp b/include/mapnik/metawriter_json.hpp index cf8936168..1b84b07bb 100644 --- a/include/mapnik/metawriter_json.hpp +++ b/include/mapnik/metawriter_json.hpp @@ -70,6 +70,8 @@ public: void set_output_empty(bool output_empty) { output_empty_ = output_empty; } /** See set_output_empty(). */ bool get_output_empty() { return output_empty_; } + void set_pixel_coordinates(bool on) { pixel_coordinates_ = on; } + bool get_pixel_coordinates() { return pixel_coordinates_; } virtual void set_map_srs(projection const& proj); protected: enum { @@ -80,6 +82,7 @@ protected: /** Features written. */ int count_; bool output_empty_; + bool pixel_coordinates_; /** Transformation from map srs to output srs. */ proj_transform *trans_; projection output_srs_; @@ -94,15 +97,17 @@ protected: if (count_ == HEADER_NOT_WRITTEN) write_header(); if (count_++) *f_ << ",\n"; - *f_ << "{ \"type\": \"Feature\",\n \"geometry\": { \"type\": \""<forward(x, y, z); - *f_ << "["<forward(x, y, z); + } + *f_ << "[" << x << "," << y << "]"; if (!last) { *f_ << ","; } diff --git a/src/metawriter.cpp b/src/metawriter.cpp index b207cd6eb..c128ef834 100644 --- a/src/metawriter.cpp +++ b/src/metawriter.cpp @@ -104,7 +104,7 @@ void metawriter_json_stream::write_properties(Feature const& feature, metawriter { *f_ << "}," << //Close coordinates object "\n \"properties\": {"; - std::map fprops = feature.props(); + std::map const &fprops = feature.props(); int i = 0; BOOST_FOREACH(std::string p, properties) { @@ -143,14 +143,21 @@ void metawriter_json_stream::add_box(box2d const &box, Feature const& fe { /* Check if feature is in bounds. */ if (box.maxx() < 0 || box.maxy() < 0 || box.minx() > width_ || box.miny() > height_) return; + double minx, miny, maxx, maxy; + if (pixel_coordinates_) { + minx = box.minx(); + miny = box.miny(); + maxx = box.maxx(); + maxy = box.maxy(); + } else { + //t_ coord transform has transform for box2d combined with proj_transform + box2d transformed = t.backward(box, *trans_); - //t_ coord transform has transform for box2d combined with proj_transform - box2d transformed = t.backward(box, *trans_); - - double minx = transformed.minx(); - double miny = transformed.miny(); - double maxx = transformed.maxx(); - double maxy = transformed.maxy(); + minx = transformed.minx(); + miny = transformed.miny(); + maxx = transformed.maxx(); + maxy = transformed.maxy(); + } write_feature_header("Polygon"); diff --git a/src/metawriter_factory.cpp b/src/metawriter_factory.cpp index 6d6d3e7f4..06e8894e1 100644 --- a/src/metawriter_factory.cpp +++ b/src/metawriter_factory.cpp @@ -41,9 +41,9 @@ metawriter_create(const boost::property_tree::ptree &pt) { metawriter_ptr writer; string type = get_attr(pt, "type"); + optional properties = get_opt_attr(pt, "default-output"); if (type == "json") { string file = get_attr(pt, "file"); - optional properties = get_opt_attr(pt, "default-output"); metawriter_json_ptr json = metawriter_json_ptr(new metawriter_json(properties, parse_path(file))); optional output_empty = get_opt_attr(pt, "output-empty"); if (output_empty) { @@ -52,10 +52,8 @@ metawriter_create(const boost::property_tree::ptree &pt) { writer = json; } else if (type == "inmem") { - optional properties = get_opt_attr(pt, "default-output"); metawriter_inmem_ptr inmem = metawriter_inmem_ptr(new metawriter_inmem(properties)); writer = inmem; - } else { throw config_error(string("Unknown type '") + type + "'"); } diff --git a/src/metawriter_inmem.cpp b/src/metawriter_inmem.cpp index 926212fa9..b0ca16d1f 100644 --- a/src/metawriter_inmem.cpp +++ b/src/metawriter_inmem.cpp @@ -57,7 +57,6 @@ namespace mapnik { metawriter_inmem::metawriter_inmem(metawriter_properties dflt_properties) : metawriter(dflt_properties) { - // ??? } metawriter_inmem::~metawriter_inmem() { @@ -130,16 +129,6 @@ metawriter_inmem::start(metawriter_property_map const& /*properties*/) { instances_.clear(); } -void -metawriter_inmem::stop() { -} - -void -metawriter_inmem::set_map_srs(projection const& /*proj*/) { - // currently unused, since the inmem metawriter keeps everything in - // image coordinates. -} - const std::list & metawriter_inmem::instances() const { return instances_;