From 78b49803526d676070e01a5a89fdcc9e2fa7f40d Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Tue, 30 Jul 2013 15:45:46 -0400 Subject: [PATCH] finish exposing mapnik.Feature.from_geojson --- CHANGELOG.md | 2 ++ bindings/python/mapnik_feature.cpp | 19 ++++++++++++++++++- include/mapnik/feature.hpp | 6 +++--- include/mapnik/memory_featureset.hpp | 2 +- src/agg/process_building_symbolizer.cpp | 2 +- src/agg/process_debug_symbolizer.cpp | 2 +- src/agg/process_point_symbolizer.cpp | 2 +- src/build.py | 1 + src/cairo_renderer.cpp | 6 +++--- src/grid/process_building_symbolizer.cpp | 2 +- src/grid/process_line_pattern_symbolizer.cpp | 2 +- src/grid/process_point_symbolizer.cpp | 2 +- src/memory_datasource.cpp | 2 +- src/svg/output/process_symbolizers.cpp | 2 +- src/symbolizer_helpers.cpp | 4 ++-- tests/python_tests/geometry_io_test.py | 16 ++++++++++++++++ 16 files changed, 54 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 278204bb0..30e0d1210 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ For a complete change history, see the git log. ## Future +- Added the ability to create a mapnik Feature from a geojson feature with `mapnik.Feature.from_geojson` in python. + - Added to python bindings: `has_tiff`, `has_png`, `has_webp`, `has_proj4`, `has_svg_renderer`, and `has_grid_renderer` - Made it possible to disable compilation of `grid_renderer` with `./configure GRID_RENDERER=False` (#1962) diff --git a/bindings/python/mapnik_feature.cpp b/bindings/python/mapnik_feature.cpp index 19b3b4aa2..f2271da1a 100644 --- a/bindings/python/mapnik_feature.cpp +++ b/bindings/python/mapnik_feature.cpp @@ -30,12 +30,15 @@ #include #include + // mapnik #include +#include #include #include #include #include +#include #include // stl @@ -49,7 +52,7 @@ using mapnik::context_type; using mapnik::context_ptr; using mapnik::feature_kv_iterator; -mapnik::geometry_type const& (mapnik::feature_impl::*get_geometry_by_const_ref)(unsigned) const = &mapnik::feature_impl::get_geometry; +mapnik::geometry_type const& (mapnik::feature_impl::*get_geometry_by_const_ref)(std::size_t) const = &mapnik::feature_impl::get_geometry; boost::ptr_vector const& (mapnik::feature_impl::*get_paths_by_const_ref)() const = &mapnik::feature_impl::paths; void feature_add_geometries_from_wkb(mapnik::feature_impl &feature, std::string wkb) @@ -64,6 +67,18 @@ void feature_add_geometries_from_wkt(mapnik::feature_impl &feature, std::string 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 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(mapnik::feature_impl const& feature) { std::string json; @@ -232,5 +247,7 @@ void export_feature() .def("__len__", &mapnik::feature_impl::size) .def("context",&mapnik::feature_impl::context) .def("to_geojson",&feature_to_geojson) + .def("from_geojson",from_geojson_impl) + .staticmethod("from_geojson") ; } diff --git a/include/mapnik/feature.hpp b/include/mapnik/feature.hpp index ce9a3dc79..83400a67d 100644 --- a/include/mapnik/feature.hpp +++ b/include/mapnik/feature.hpp @@ -211,17 +211,17 @@ public: geom_cont_.push_back(geom); } - unsigned num_geometries() const + std::size_t num_geometries() const { return geom_cont_.size(); } - geometry_type const& get_geometry(unsigned index) const + geometry_type const& get_geometry(std::size_t index) const { return geom_cont_[index]; } - geometry_type& get_geometry(unsigned index) + geometry_type& get_geometry(std::size_t index) { return geom_cont_[index]; } diff --git a/include/mapnik/memory_featureset.hpp b/include/mapnik/memory_featureset.hpp index f334b1d5e..51ff72155 100644 --- a/include/mapnik/memory_featureset.hpp +++ b/include/mapnik/memory_featureset.hpp @@ -76,7 +76,7 @@ public: } else { - for (unsigned i=0; i<(*pos_)->num_geometries();++i) + for (std::size_t i=0; i<(*pos_)->num_geometries();++i) { geometry_type & geom = (*pos_)->get_geometry(i); if (bbox_.intersects(geom.envelope())) diff --git a/src/agg/process_building_symbolizer.cpp b/src/agg/process_building_symbolizer.cpp index 8b8c8475f..d4d7dae05 100644 --- a/src/agg/process_building_symbolizer.cpp +++ b/src/agg/process_building_symbolizer.cpp @@ -86,7 +86,7 @@ void agg_renderer::process(building_symbolizer const& sym, height = result.to_double() * scale_factor_; } - for (unsigned i=0;i 2) diff --git a/src/agg/process_debug_symbolizer.cpp b/src/agg/process_debug_symbolizer.cpp index 277f7fea6..e85b9ce2d 100644 --- a/src/agg/process_debug_symbolizer.cpp +++ b/src/agg/process_debug_symbolizer.cpp @@ -63,7 +63,7 @@ void agg_renderer::process(debug_symbolizer const& sym, } else if (mode == DEBUG_SYM_MODE_VERTEX) { - for (unsigned i=0; i::process(point_symbolizer const& sym, agg::trans_affine recenter_tr = recenter * tr; box2d label_ext = bbox * recenter_tr * agg::trans_affine_scaling(scale_factor_); - for (unsigned i=0; i label_ext = bbox * recenter_tr * agg::trans_affine_scaling(scale_factor_); - for (unsigned i = 0; i < feature.num_geometries(); ++i) + for (std::size_t i = 0; i < feature.num_geometries(); ++i) { geometry_type const& geom = feature.get_geometry(i); double x; @@ -760,7 +760,7 @@ void cairo_renderer_base::process(line_pattern_symbolizer const& sym, pattern.set_filter(CAIRO_FILTER_BILINEAR); context_.set_line_width(height * scale_factor_); - for (unsigned i = 0; i < feature.num_geometries(); ++i) + for (std::size_t i = 0; i < feature.num_geometries(); ++i) { geometry_type & geom = feature.get_geometry(i); diff --git a/src/grid/process_building_symbolizer.cpp b/src/grid/process_building_symbolizer.cpp index dc967f0c1..5e089d272 100644 --- a/src/grid/process_building_symbolizer.cpp +++ b/src/grid/process_building_symbolizer.cpp @@ -73,7 +73,7 @@ void grid_renderer::process(building_symbolizer const& sym, height = result.to_double() * scale_factor_; } - for (unsigned i=0;i 2) diff --git a/src/grid/process_line_pattern_symbolizer.cpp b/src/grid/process_line_pattern_symbolizer.cpp index 542dbcab2..b79e39dd0 100644 --- a/src/grid/process_line_pattern_symbolizer.cpp +++ b/src/grid/process_line_pattern_symbolizer.cpp @@ -62,7 +62,7 @@ void grid_renderer::process(line_pattern_symbolizer const& sym, // TODO - actually handle image dimensions int stroke_width = 2; - for (unsigned i=0;i 1) diff --git a/src/grid/process_point_symbolizer.cpp b/src/grid/process_point_symbolizer.cpp index f0230825f..3d0d68f40 100644 --- a/src/grid/process_point_symbolizer.cpp +++ b/src/grid/process_point_symbolizer.cpp @@ -76,7 +76,7 @@ void grid_renderer::process(point_symbolizer const& sym, agg::trans_affine recenter_tr = recenter * tr; box2d label_ext = bbox * recenter_tr * agg::trans_affine_scaling(scale_factor_) ; - for (unsigned i=0; inum_geometries();++i) + for (std::size_t i=0;inum_geometries();++i) { geometry_type & geom = feat->get_geometry(i); if ( first_ ) diff --git a/src/svg/output/process_symbolizers.cpp b/src/svg/output/process_symbolizers.cpp index e4065159f..ca38a082f 100644 --- a/src/svg/output/process_symbolizers.cpp +++ b/src/svg/output/process_symbolizers.cpp @@ -71,7 +71,7 @@ bool svg_renderer::process(rule::symbolizers const& syms, if (process_path) { // generate path output for each geometry of the current feature. - for(unsigned i=0; i 0) diff --git a/src/symbolizer_helpers.cpp b/src/symbolizer_helpers.cpp index 683eeee00..5286de55e 100644 --- a/src/symbolizer_helpers.cpp +++ b/src/symbolizer_helpers.cpp @@ -211,8 +211,8 @@ template void text_symbolizer_helper::initialize_geometries() { bool largest_box_only = false; - unsigned num_geom = feature_.num_geometries(); - for (unsigned i=0; i