From 599b021175c650819a9056486d76f106db7ba817 Mon Sep 17 00:00:00 2001 From: artemp Date: Mon, 14 Dec 2015 11:38:22 +0000 Subject: [PATCH 1/3] support arbitrary (nested) attributes in JSON Geometry --- include/mapnik/json/geometry_grammar.hpp | 2 + include/mapnik/json/geometry_grammar_impl.hpp | 39 +++++++++++++++---- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/include/mapnik/json/geometry_grammar.hpp b/include/mapnik/json/geometry_grammar.hpp index 7d768a785..800a48bf0 100644 --- a/include/mapnik/json/geometry_grammar.hpp +++ b/include/mapnik/json/geometry_grammar.hpp @@ -49,6 +49,8 @@ struct geometry_grammar : qi::symbols geometry_type_dispatch; positions_grammar coordinates; boost::phoenix::function create_geometry; + // generic JSON + generic_json json_; // error handler ErrorHandler error_handler; }; diff --git a/include/mapnik/json/geometry_grammar_impl.hpp b/include/mapnik/json/geometry_grammar_impl.hpp index 30fad882c..90f09bfe7 100644 --- a/include/mapnik/json/geometry_grammar_impl.hpp +++ b/include/mapnik/json/geometry_grammar_impl.hpp @@ -30,8 +30,6 @@ #include #include #include -#include // for clog, endl, etc -#include // for string namespace mapnik { namespace json { @@ -51,18 +49,45 @@ geometry_grammar::geometry_grammar() qi::_a_type _a; qi::_b_type _b; qi::eps_type eps; + qi::omit_type omit; using qi::fail; using qi::on_error; using phoenix::push_back; start = geometry.alias() | lit("null"); + // generic json types + json_.value = json_.object | json_.array | json_.string_ | json_.number + ; + + json_.pairs = json_.key_value % lit(',') + ; + + json_.key_value = (json_.string_ > lit(':') > json_.value) + ; + + json_.object = lit('{') + > *json_.pairs + > lit('}') + ; + json_.array = lit('[') + > json_.value > *(lit(',') > json_.value) + > lit(']') + ; + json_.number = json_.strict_double + | json_.int__ + | lit("true") + | lit ("false") + | lit("null") + ; geometry = lit('{')[_a = 0] - > (-lit(',') >> (lit("\"type\"") > lit(':') > geometry_type_dispatch[_a = _1]) - ^ - (-lit(',') >> (lit("\"coordinates\"") > lit(':') > coordinates[_b = _1])) - ^ - (-lit(',') >> (lit("\"geometries\"") > lit(':') > lit('[') > geometry_collection[_val = _1] > lit(']'))))[create_geometry(_val,_a,_b)] + > (((lit("\"type\"") > lit(':') > geometry_type_dispatch[_a = _1]) + | + (lit("\"coordinates\"") > lit(':') > coordinates[_b = _1]) + | + (lit("\"geometries\"") > lit(':') > lit('[') > geometry_collection[_val = _1] > lit(']')) + | + omit[json_.key_value]) % lit(',')) [create_geometry(_val,_a,_b)] > lit('}') ; From 5e2f3ce61d25e90f92d0962a91b0a849ce7304f7 Mon Sep 17 00:00:00 2001 From: artemp Date: Mon, 14 Dec 2015 11:39:42 +0000 Subject: [PATCH 2/3] geojson/geometry - update unit test --- test/data | 2 +- test/unit/geometry/geometry.cpp | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/test/data b/test/data index 94fa33e08..116c65b3f 160000 --- a/test/data +++ b/test/data @@ -1 +1 @@ -Subproject commit 94fa33e08b81686d85d712afee0b12d4e78f7a0a +Subproject commit 116c65b3f6c12feb7709031ccc92b0f604d44f53 diff --git a/test/unit/geometry/geometry.cpp b/test/unit/geometry/geometry.cpp index d42f0abb9..556f6cacf 100644 --- a/test/unit/geometry/geometry.cpp +++ b/test/unit/geometry/geometry.cpp @@ -35,4 +35,17 @@ SECTION("json point reversed") { REQUIRE( point.y == 10 ); } +SECTION("json point reversed + extra attributes") { + mapnik::util::file input("./test/data/json/point3.json"); + REQUIRE( input.open() ); + mapnik::geometry::geometry geom; + REQUIRE( input.data() ); + std::string json_string(input.data().get(), input.size()); + REQUIRE( mapnik::json::from_geojson(json_string,geom) ); + REQUIRE( geom.is >() ); + auto const& point = mapnik::util::get >(geom); + REQUIRE( point.x == 30 ); + REQUIRE( point.y == 10 ); +} + } From 6ca0e34a80694d6739ec1fb827792df3ec4061ed Mon Sep 17 00:00:00 2001 From: artemp Date: Mon, 14 Dec 2015 11:36:21 +0000 Subject: [PATCH 3/3] mapnik-index - output failed feature JSON when both `--validate-features` and `--verbose` options present. --- utils/mapnik-index/mapnik-index.cpp | 2 +- utils/mapnik-index/process_geojson_file.cpp | 5 +++-- utils/mapnik-index/process_geojson_file.hpp | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/utils/mapnik-index/mapnik-index.cpp b/utils/mapnik-index/mapnik-index.cpp index 7361c05de..f6f72be1d 100644 --- a/utils/mapnik-index/mapnik-index.cpp +++ b/utils/mapnik-index/mapnik-index.cpp @@ -186,7 +186,7 @@ int main (int argc, char** argv) else if (mapnik::detail::is_geojson(filename)) { std::clog << "processing '" << filename << "' as GeoJSON\n"; - auto result = mapnik::detail::process_geojson_file(boxes, filename, validate_features); + auto result = mapnik::detail::process_geojson_file(boxes, filename, validate_features, verbose); if (!result.first) { std::clog << "Error: failed to process " << filename << std::endl; diff --git a/utils/mapnik-index/process_geojson_file.cpp b/utils/mapnik-index/process_geojson_file.cpp index af9656bee..ac0fa92a4 100644 --- a/utils/mapnik-index/process_geojson_file.cpp +++ b/utils/mapnik-index/process_geojson_file.cpp @@ -66,7 +66,7 @@ const mapnik::json::feature_grammar_callback -std::pair> process_geojson_file(T & boxes, std::string const& filename, bool validate_features) +std::pair> process_geojson_file(T & boxes, std::string const& filename, bool validate_features, bool verbose) { mapnik::box2d extent; #if defined(MAPNIK_MEMORY_MAPPED_FILE) @@ -131,6 +131,7 @@ std::pair> process_geojson_file(T & boxes, std::string const& space); if (!result || feat_itr != feat_end) { + if (verbose) std::clog << std::string(start + item.second.first, feat_end ) << std::endl; return std::make_pair(false, extent); } } @@ -142,6 +143,6 @@ std::pair> process_geojson_file(T & boxes, std::string const& using box_type = mapnik::box2d; using item_type = std::pair>; using boxes_type = std::vector; -template std::pair> process_geojson_file(boxes_type&, std::string const&, bool); +template std::pair> process_geojson_file(boxes_type&, std::string const&, bool, bool); }} diff --git a/utils/mapnik-index/process_geojson_file.hpp b/utils/mapnik-index/process_geojson_file.hpp index a4d468ad2..aba49114a 100644 --- a/utils/mapnik-index/process_geojson_file.hpp +++ b/utils/mapnik-index/process_geojson_file.hpp @@ -29,7 +29,7 @@ namespace mapnik { namespace detail { template -std::pair> process_geojson_file(T & boxes, std::string const& filename, bool validate_features); +std::pair> process_geojson_file(T & boxes, std::string const& filename, bool validate_features, bool verbose); }}