diff --git a/src/json/extract_bounding_boxes_x3.cpp b/src/json/extract_bounding_boxes_x3.cpp new file mode 100644 index 000000000..e69de29bb diff --git a/utils/mapnik-index/extract_bounding_boxes_x3.cpp b/utils/mapnik-index/extract_bounding_boxes_x3.cpp deleted file mode 100644 index da0827881..000000000 --- a/utils/mapnik-index/extract_bounding_boxes_x3.cpp +++ /dev/null @@ -1,233 +0,0 @@ -/***************************************************************************** - * - * This file is part of Mapnik (c++ mapping toolkit) - * - * Copyright (C) 2016 Artem Pavlenko - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - * - *****************************************************************************/ - -#include -#include -#include -#include -#include -#include - -namespace mapnik { namespace json { - -template -struct calculate_bounding_box -{ - calculate_bounding_box(Box & box) - : box_(box) {} - - void operator()(mapnik::json::point const& pt) const - { - box_.init(pt.x, pt.y); - } - - void operator()(mapnik::json::ring const& ring) const - { - for (auto const& pt : ring) - { - if (!box_.valid()) box_.init(pt.x, pt.y); - else box_.expand_to_include(pt.x, pt.y); - } - } - - void operator()(mapnik::json::rings const& rings) const - { - for (auto const& ring : rings) - { - operator()(ring); - break; // consider first ring only - } - } - - void operator()(mapnik::json::rings_array const& rings_array) const - { - for (auto const& rings : rings_array) - { - operator()(rings); - } - } - - template - void operator() (T const& ) const {} - - Box & box_; -}; - -namespace grammar { - -namespace x3 = boost::spirit::x3; - -using base_iterator_type = char const*; -using x3::lit; -using x3::omit; -using x3::raw; -using x3::char_; -using x3::eps; - -struct feature_callback_tag; - -auto on_feature_callback = [] (auto const& ctx) -{ - // call our callback - x3::get(ctx)(_attr(ctx)); -}; - -namespace { auto const& geojson_value = geojson_grammar();} -// import unicode string rule -//namespace { auto const& geojson_string = unicode_string_grammar(); } -// import positions rule -//namespace { auto const& positions_rule = positions_grammar(); } - -// extract bounding box from GeoJSON Feature - -struct bracket_tag ; - -auto check_brackets = [](auto const& ctx) -{ - _pass(ctx) = (x3::get(ctx) > 0); -}; - -auto open_bracket = [](auto const& ctx) -{ - ++x3::get(ctx); -}; - -auto close_bracket = [](auto const& ctx) -{ - --x3::get(ctx); -}; - -auto assign_range = [](auto const& ctx) -{ - std::get<0>(_val(ctx)) = std::move(_attr(ctx)); -}; - -auto assign_bbox = [](auto const& ctx) -{ - std::get<1>(_val(ctx)) = std::move(_attr(ctx)); -}; - -auto extract_bounding_box = [](auto const& ctx) -{ - mapnik::box2d bbox; - calculate_bounding_box> visitor(bbox); - mapnik::util::apply_visitor(visitor, _attr(ctx)); - _val(ctx) = std::move(bbox); -}; - -auto const coordinates_rule = x3::rule > {} - = lit("\"coordinates\"") >> lit(':') >> positions_rule[extract_bounding_box]; - -auto const bounding_box = x3::rule,mapnik::box2d>> {} - = raw[lit('{')[open_bracket] >> *(eps[check_brackets] >> - (lit("\"FeatureCollection\"") > eps(false) - | - lit('{')[open_bracket] - | - lit('}')[close_bracket] - | - coordinates_rule[assign_bbox] - | - omit[geojson_string] - | - omit[char_]))][assign_range]; - - -auto const feature = bounding_box[on_feature_callback]; - -auto const key_value_ = omit[geojson_string] > lit(':') > omit[geojson_value] ; - -auto const features = lit("\"features\"") - > lit(':') > lit('[') > -(omit[feature] % lit(',')) > lit(']'); - -auto const type = lit("\"type\"") > lit(':') > lit("\"FeatureCollection\""); - -auto const feature_collection = x3::rule {} - = lit('{') > (( type | features | key_value_) % lit(',')) > lit('}'); - - -} - -namespace { -struct collect_features -{ - collect_features(std::vector & values) - : values_(values) {} - void operator() (mapnik::json::geojson_value && val) const - { - values_.push_back(std::move(val)); - } - std::vector & values_; -}; - -template -struct extract_positions -{ - extract_positions(Iterator start, Boxes & boxes) - : start_(start), - boxes_(boxes) {} - - template - void operator() (T const& val) const - { - auto const& r = std::get<0>(val); - mapnik::box2d const& bbox = std::get<1>(val); - auto offset = std::distance(start_, r.begin()); - auto size = std::distance(r.begin(), r.end()); - boxes_.emplace_back(std::make_pair(bbox,std::make_pair(offset, size))); - //boxes_.emplace_back(std::make_tuple(bbox,offset, size)); - } - Iterator start_; - Boxes & boxes_; -}; - -} - -template -void extract_bounding_boxes(Iterator start, Iterator end, Boxes & boxes) -{ - using namespace boost::spirit; - using space_type = mapnik::json::grammar::space_type; - using iterator_type = Iterator; - using boxes_type = Boxes; - - extract_positions callback(start, boxes); - auto keys = mapnik::json::get_keys(); - std::size_t bracket_counter = 0; - auto feature_collection_impl = x3::with(std::ref(bracket_counter)) - [x3::with(std::ref(keys)) - [x3::with(std::ref(callback)) - [mapnik::json::grammar::feature_collection] - ]]; - - if (!x3::phrase_parse(start, end, feature_collection_impl, space_type())) - { - throw std::runtime_error("Can't extract bounding boxes"); - } - -} - -using box_type = mapnik::box2d; -using boxes_type = std::vector>>; -using base_iterator_type = char const*; -template void extract_bounding_boxes(base_iterator_type, base_iterator_type, boxes_type&); -}}