implement from_geojson in terms of parse_(feature|geometry)

This commit is contained in:
artemp 2016-11-29 10:48:06 +01:00
parent 941a025682
commit 36eb7f61bd
3 changed files with 97 additions and 16 deletions

View file

@ -25,25 +25,12 @@
// mapnik
#include <mapnik/feature.hpp>
#include <mapnik/json/feature_grammar.hpp>
// boost
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
// stl
#include <string>
namespace mapnik { namespace json {
inline bool from_geojson(std::string const& json, mapnik::feature_impl & feature)
{
static const mapnik::transcoder tr("utf8");
using iterator_type = char const*;
static const mapnik::json::feature_grammar<iterator_type,mapnik::feature_impl> g(tr);
using namespace boost::spirit;
standard::space_type space;
iterator_type start = json.c_str();
iterator_type end = start + json.length();
return qi::phrase_parse(start, end, (g)(boost::phoenix::ref(feature)), space);
}
bool from_geojson(std::string const& json, mapnik::feature_impl & feature);
}}

View file

@ -0,0 +1,45 @@
/*****************************************************************************
*
* 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
*
*****************************************************************************/
// mapnik
#include <mapnik/json/geometry_parser.hpp>
#include <mapnik/json/parse_feature.hpp>
namespace mapnik { namespace json {
bool from_geojson(std::string const& json, feature_impl & feature)
{
try
{
const char* start = json.c_str();
const char* end = start + json.length();
mapnik::json::parse_feature(start, end, feature);
}
catch (...)
{
return false;
}
return true;
}
}}

View file

@ -0,0 +1,49 @@
/*****************************************************************************
*
* 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
*
*****************************************************************************/
// mapnik
#include <mapnik/json/geometry_parser.hpp>
#include <mapnik/json/parse_feature.hpp>
#include <mapnik/feature_factory.hpp>
namespace mapnik { namespace json {
bool from_geojson(std::string const& json, mapnik::geometry::geometry<double> & geom)
{
try
{
mapnik::context_ptr ctx = std::make_shared<mapnik::context_type>();
mapnik::feature_ptr feature(mapnik::feature_factory::create(ctx, -1)); // temp geometry holder
const char* start = json.c_str();
const char* end = start + json.length();
mapnik::json::parse_geometry(start, end, *feature);
geom = std::move(feature->get_geometry());
}
catch (...)
{
return false;
}
return true;
}
}}