From 0bb8adbbdc6370d9698df5f9c840fdb68ea95c24 Mon Sep 17 00:00:00 2001 From: Artem Pavlenko Date: Thu, 12 Jan 2012 12:18:50 +0000 Subject: [PATCH] factor out common code to reduce duplication (shape_utils) --- include/mapnik/feature.hpp | 1 - plugins/input/shape/build.py | 1 + plugins/input/shape/shape_featureset.cpp | 50 +++--------------- plugins/input/shape/shape_featureset.hpp | 6 +-- .../input/shape/shape_index_featureset.cpp | 52 ++++--------------- .../input/shape/shape_index_featureset.hpp | 2 +- 6 files changed, 20 insertions(+), 92 deletions(-) diff --git a/include/mapnik/feature.hpp b/include/mapnik/feature.hpp index 00819d5f3..2ddc5343e 100644 --- a/include/mapnik/feature.hpp +++ b/include/mapnik/feature.hpp @@ -138,7 +138,6 @@ public: { return raster_; } - void set_raster(raster_ptr const& raster) { diff --git a/plugins/input/shape/build.py b/plugins/input/shape/build.py index 70771927e..5501bddf7 100644 --- a/plugins/input/shape/build.py +++ b/plugins/input/shape/build.py @@ -34,6 +34,7 @@ shape_src = Split( shape_featureset.cpp shape_index_featureset.cpp shape_io.cpp + shape_utils.cpp """ ) diff --git a/plugins/input/shape/shape_featureset.cpp b/plugins/input/shape/shape_featureset.cpp index 13ef0ce65..650aceb93 100644 --- a/plugins/input/shape/shape_featureset.cpp +++ b/plugins/input/shape/shape_featureset.cpp @@ -1,4 +1,3 @@ - /***************************************************************************** * * This file is part of Mapnik (c++ mapping toolkit) @@ -27,19 +26,17 @@ // mapnik #include -// boost -#include - #include "shape_featureset.hpp" +#include "shape_utils.hpp" using mapnik::geometry_type; using mapnik::feature_factory; using mapnik::context_ptr; - + template -shape_featureset::shape_featureset(const filterT& filter, - const std::string& shape_name, - const std::set& attribute_names, +shape_featureset::shape_featureset(filterT const& filter, + std::string const& shape_name, + std::set const& attribute_names, std::string const& encoding, long file_length, int row_limit) @@ -53,42 +50,7 @@ shape_featureset::shape_featureset(const filterT& filter, { ctx_ = boost::make_shared(); shape_.shp().skip(100); - - //attributes - typename std::set::const_iterator pos = attribute_names.begin(); - - while (pos != attribute_names.end()) - { - bool found_name = false; - for (int i = 0; i < shape_.dbf().num_fields(); ++i) - { - if (shape_.dbf().descriptor(i).name_ == *pos) - { - ctx_->push(*pos); - attr_ids_.push_back(i); - found_name = true; - break; - } - } - - if (! found_name) - { - std::ostringstream s; - - s << "no attribute '" << *pos << "' in '" - << shape_name << "'. Valid attributes are: "; - - std::vector list; - for (int i = 0; i < shape_.dbf().num_fields(); ++i) - { - list.push_back(shape_.dbf().descriptor(i).name_); - } - s << boost::algorithm::join(list, ",") << "."; - - throw mapnik::datasource_exception("Shape Plugin: " + s.str()); - } - ++pos; - } + setup_attributes(ctx_, attribute_names, shape_name, shape_,attr_ids_); } template diff --git a/plugins/input/shape/shape_featureset.hpp b/plugins/input/shape/shape_featureset.hpp index 6f3f02ab8..f6780be05 100644 --- a/plugins/input/shape/shape_featureset.hpp +++ b/plugins/input/shape/shape_featureset.hpp @@ -56,9 +56,9 @@ class shape_featureset : public Featureset, const int row_limit_; public: - shape_featureset(const filterT& filter, - const std::string& shape_file, - const std::set& attribute_names, + shape_featureset(filterT const& filter, + std::string const& shape_file, + std::set const& attribute_names, std::string const& encoding, long file_length, int row_limit); diff --git a/plugins/input/shape/shape_index_featureset.cpp b/plugins/input/shape/shape_index_featureset.cpp index 8f72d34f1..fba16f73b 100644 --- a/plugins/input/shape/shape_index_featureset.cpp +++ b/plugins/input/shape/shape_index_featureset.cpp @@ -31,19 +31,19 @@ #include #include "shape_index_featureset.hpp" +#include "shape_utils.hpp" using mapnik::feature_factory; using mapnik::geometry_type; template -shape_index_featureset::shape_index_featureset(const filterT& filter, +shape_index_featureset::shape_index_featureset(filterT const& filter, shape_io& shape, - const std::set& attribute_names, + std::set const& attribute_names, std::string const& encoding, std::string const& shape_name, int row_limit) : filter_(filter), - //shape_type_(0), shape_(shape), tr_(new transcoder(encoding)), count_(0), @@ -51,7 +51,8 @@ shape_index_featureset::shape_index_featureset(const filterT& filter, { ctx_ = boost::make_shared(); shape_.shp().skip(100); - + setup_attributes(ctx_, attribute_names, shape_name, shape_,attr_ids_); + boost::shared_ptr index = shape_.index(); if (index) { @@ -64,47 +65,12 @@ shape_index_featureset::shape_index_featureset(const filterT& filter, } std::sort(ids_.begin(), ids_.end()); - + #ifdef MAPNIK_DEBUG std::clog << "Shape Plugin: query size=" << ids_.size() << std::endl; #endif - - itr_ = ids_.begin(); - // deal with attributes - std::set::const_iterator pos = attribute_names.begin(); - while (pos != attribute_names.end()) - { - bool found_name = false; - for (int i = 0; i < shape_.dbf().num_fields(); ++i) - { - if (shape_.dbf().descriptor(i).name_ == *pos) - { - ctx_->push(*pos); - attr_ids_.insert(i); - found_name = true; - break; - } - } - - if (! found_name) - { - std::ostringstream s; - s << "no attribute '" << *pos << "' in '" << shape_name << "'. Valid attributes are: "; - - std::vector list; - for (int i = 0; i < shape_.dbf().num_fields(); ++i) - { - list.push_back(shape_.dbf().descriptor(i).name_); - } - - s << boost::algorithm::join(list, ",") << "."; - - throw mapnik::datasource_exception("Shape Plugin: " + s.str()); - } - - ++pos; - } + itr_ = ids_.begin(); } template @@ -217,8 +183,8 @@ feature_ptr shape_index_featureset::next() if (attr_ids_.size()) { shape_.dbf().move_to(shape_.id_); - std::set::const_iterator itr = attr_ids_.begin(); - std::set::const_iterator end = attr_ids_.end(); + std::vector::const_iterator itr = attr_ids_.begin(); + std::vector::const_iterator end = attr_ids_.end(); try { for (; itr!=end; ++itr) diff --git a/plugins/input/shape/shape_index_featureset.hpp b/plugins/input/shape/shape_index_featureset.hpp index fb3dab3af..cbdaf4e6d 100644 --- a/plugins/input/shape/shape_index_featureset.hpp +++ b/plugins/input/shape/shape_index_featureset.hpp @@ -63,7 +63,7 @@ private: boost::scoped_ptr tr_; std::vector ids_; std::vector::iterator itr_; - std::set attr_ids_; + std::vector attr_ids_; mutable box2d feature_ext_; mutable int total_geom_size; mutable int count_;