geojson.input - rely on expectation failure to select optional single Feature/Geometry parser

This commit is contained in:
artemp 2015-11-18 14:43:40 +00:00
parent 998164dc72
commit 938702dc8b

View file

@ -345,22 +345,20 @@ void geojson_datasource::initialise_index(Iterator start, Iterator end)
template <typename Iterator> template <typename Iterator>
void geojson_datasource::parse_geojson(Iterator start, Iterator end) void geojson_datasource::parse_geojson(Iterator start, Iterator end)
{ {
using boost::spirit::qi::expectation_failure;
boost::spirit::standard::space_type space; boost::spirit::standard::space_type space;
mapnik::context_ptr ctx = std::make_shared<mapnik::context_type>(); mapnik::context_ptr ctx = std::make_shared<mapnik::context_type>();
std::size_t start_id = 1; std::size_t start_id = 1;
mapnik::json::default_feature_callback callback(features_); mapnik::json::default_feature_callback callback(features_);
Iterator itr = start; Iterator itr = start;
try
{
bool result = boost::spirit::qi::phrase_parse(itr, end, (geojson_datasource_static_fc_grammar) bool result = boost::spirit::qi::phrase_parse(itr, end, (geojson_datasource_static_fc_grammar)
(boost::phoenix::ref(ctx),boost::phoenix::ref(start_id), boost::phoenix::ref(callback)), (boost::phoenix::ref(ctx),boost::phoenix::ref(start_id), boost::phoenix::ref(callback)),
space); space);
if (!result || itr != end) if (!result || itr != end)
{
if (!inline_string_.empty()) throw mapnik::datasource_exception("geojson_datasource: Failed to parse GeoJSON file from in-memory string");
else throw mapnik::datasource_exception("geojson_datasource: Failed to parse GeoJSON file '" + filename_ + "'");
}
if (features_.size() == 0)
{ {
itr = start; itr = start;
// try parsing as single Feature or single Geometry JSON // try parsing as single Feature or single Geometry JSON
@ -373,6 +371,20 @@ void geojson_datasource::parse_geojson(Iterator start, Iterator end)
else throw mapnik::datasource_exception("geojson_datasource: Failed parse GeoJSON file '" + filename_ + "'"); else throw mapnik::datasource_exception("geojson_datasource: Failed parse GeoJSON file '" + filename_ + "'");
} }
} }
}
catch (expectation_failure<char const*> const& ex)
{
itr = start;
// try parsing as single Feature or single Geometry JSON
bool result = boost::spirit::qi::phrase_parse(itr, end, (geojson_datasource_static_feature_callback_grammar)
(boost::phoenix::ref(ctx),boost::phoenix::ref(start_id), boost::phoenix::ref(callback)),
space);
if (!result || itr != end)
{
if (!inline_string_.empty()) throw mapnik::datasource_exception("geojson_datasource: Failed parse GeoJSON file from in-memory string");
else throw mapnik::datasource_exception("geojson_datasource: Failed parse GeoJSON file '" + filename_ + "'");
}
}
using values_container = std::vector< std::pair<box_type, std::pair<std::size_t, std::size_t>>>; using values_container = std::vector< std::pair<box_type, std::pair<std::size_t, std::size_t>>>;
values_container values; values_container values;