+ add from_geojson static method

TODO: better handling of feature.id() and transcoder
This commit is contained in:
artemp 2013-05-14 14:53:52 +01:00
parent 417ac21529
commit 26302e19af

View file

@ -33,10 +33,12 @@
// mapnik // mapnik
#include <mapnik/feature.hpp> #include <mapnik/feature.hpp>
#include <mapnik/feature_factory.hpp>
#include <mapnik/feature_kv_iterator.hpp> #include <mapnik/feature_kv_iterator.hpp>
#include <mapnik/datasource.hpp> #include <mapnik/datasource.hpp>
#include <mapnik/wkb.hpp> #include <mapnik/wkb.hpp>
#include <mapnik/wkt/wkt_factory.hpp> #include <mapnik/wkt/wkt_factory.hpp>
#include <mapnik/json/feature_parser.hpp>
#include <mapnik/json/geojson_generator.hpp> #include <mapnik/json/geojson_generator.hpp>
namespace { namespace {
@ -62,6 +64,18 @@ void feature_add_geometries_from_wkt(Feature &feature, std::string wkt)
if (!result) throw std::runtime_error("Failed to parse WKT"); if (!result) throw std::runtime_error("Failed to parse WKT");
} }
mapnik::feature_ptr from_geojson_impl(std::string const& json, mapnik::context_ptr const& ctx)
{
mapnik::transcoder tr("utf8");
mapnik::feature_ptr feature(mapnik::feature_factory::create(ctx,1));
mapnik::json::feature_parser<std::string::const_iterator> parser(tr);
if (!parser.parse(json.begin(), json.end(), *feature))
{
throw std::runtime_error("Failed to parse geojson feature");
}
return feature;
}
std::string feature_to_geojson(Feature const& feature) std::string feature_to_geojson(Feature const& feature)
{ {
std::string json; std::string json;
@ -231,5 +245,7 @@ void export_feature()
.def("__len__", &Feature::size) .def("__len__", &Feature::size)
.def("context",&Feature::context) .def("context",&Feature::context)
.def("to_geojson",&feature_to_geojson) .def("to_geojson",&feature_to_geojson)
.def("from_geojson",from_geojson_impl)
.staticmethod("from_geojson")
; ;
} }