From e36081a5c08f67620afcde2cdf57d4742115546c Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Fri, 20 Jul 2012 15:09:01 -0700 Subject: [PATCH] change geometry_utils::from_wkb to return a bool that signifies if at least one wkb path was parsed - refs #1333 and #1305 --- bindings/python/mapnik_geometry.cpp | 4 +- include/mapnik/wkb.hpp | 2 +- plugins/input/geos/geos_featureset.cpp | 6 +- plugins/input/postgis/postgis_featureset.cpp | 12 +-- plugins/input/sqlite/sqlite_datasource.cpp | 18 ++-- plugins/input/sqlite/sqlite_featureset.cpp | 3 +- plugins/input/sqlite/sqlite_utils.hpp | 97 ++++++++++---------- src/wkb.cpp | 8 +- utils/pgsql2sqlite/pgsql2sqlite.hpp | 4 +- 9 files changed, 77 insertions(+), 77 deletions(-) diff --git a/bindings/python/mapnik_geometry.cpp b/bindings/python/mapnik_geometry.cpp index bc261e153..5731f3098 100644 --- a/bindings/python/mapnik_geometry.cpp +++ b/bindings/python/mapnik_geometry.cpp @@ -60,9 +60,9 @@ void add_wkt_impl(path_type& p, std::string const& wkt) if (!result) throw std::runtime_error("Failed to parse WKT"); } -void add_wkb_impl(path_type& p, std::string const& wkb) +bool add_wkb_impl(path_type& p, std::string const& wkb) { - mapnik::geometry_utils::from_wkb(p, wkb.c_str(), wkb.size()); + return mapnik::geometry_utils::from_wkb(p, wkb.c_str(), wkb.size()); } boost::shared_ptr from_wkt_impl(std::string const& wkt) diff --git a/include/mapnik/wkb.hpp b/include/mapnik/wkb.hpp index a7284bb30..38fa2b259 100644 --- a/include/mapnik/wkb.hpp +++ b/include/mapnik/wkb.hpp @@ -55,7 +55,7 @@ class MAPNIK_DECL geometry_utils : private boost::noncopyable { public: - static void from_wkb (boost::ptr_vector& paths, + static bool from_wkb (boost::ptr_vector& paths, const char* wkb, unsigned size, wkbFormat format = wkbGeneric); diff --git a/plugins/input/geos/geos_featureset.cpp b/plugins/input/geos/geos_featureset.cpp index 6c5ab0ef8..d3058a989 100644 --- a/plugins/input/geos/geos_featureset.cpp +++ b/plugins/input/geos/geos_featureset.cpp @@ -117,10 +117,10 @@ feature_ptr geos_featureset::next() { feature_ptr feature(feature_factory::create(ctx_,identifier_)); - geometry_utils::from_wkb(feature->paths(), + if (geometry_utils::from_wkb(feature->paths(), wkb.data(), - wkb.size()); - if (field_ != "") + wkb.size()) + && field_ != "") { feature->put(field_name_, tr_->transcode(field_.c_str())); } diff --git a/plugins/input/postgis/postgis_featureset.cpp b/plugins/input/postgis/postgis_featureset.cpp index 7c227894e..5ac6e85f4 100644 --- a/plugins/input/postgis/postgis_featureset.cpp +++ b/plugins/input/postgis/postgis_featureset.cpp @@ -62,7 +62,7 @@ postgis_featureset::postgis_featureset(boost::shared_ptr const& rs, feature_ptr postgis_featureset::next() { - if (rs_->next()) + while (rs_->next()) { // new feature unsigned pos = 1; @@ -107,7 +107,9 @@ feature_ptr postgis_featureset::next() // parse geometry int size = rs_->getFieldLength(0); const char *data = rs_->getValue(0); - geometry_utils::from_wkb(feature->paths(), data, size); + if (!geometry_utils::from_wkb(feature->paths(), data, size)) + continue; + totalGeomSize_ += size; int num_attrs = ctx_->size() + 1; @@ -207,11 +209,7 @@ feature_ptr postgis_featureset::next() } return feature; } - else - { - rs_->close(); - return feature_ptr(); - } + return feature_ptr(); } diff --git a/plugins/input/sqlite/sqlite_datasource.cpp b/plugins/input/sqlite/sqlite_datasource.cpp index bab062485..863dea7a4 100644 --- a/plugins/input/sqlite/sqlite_datasource.cpp +++ b/plugins/input/sqlite/sqlite_datasource.cpp @@ -522,17 +522,19 @@ boost::optional sqlite_datasource::get_geometry_ if (data) { boost::ptr_vector paths; - mapnik::geometry_utils::from_wkb(paths, data, size, mapnik::wkbAuto); - mapnik::util::to_ds_type(paths,result); - if (result) + if (mapnik::geometry_utils::from_wkb(paths, data, size, mapnik::wkbAuto)) { - int type = static_cast(*result); - if (multi_type > 0 && multi_type != type) + mapnik::util::to_ds_type(paths,result); + if (result) { - result.reset(mapnik::datasource::Collection); - return result; + int type = static_cast(*result); + if (multi_type > 0 && multi_type != type) + { + result.reset(mapnik::datasource::Collection); + return result; + } + multi_type = type; } - multi_type = type; } } } diff --git a/plugins/input/sqlite/sqlite_featureset.cpp b/plugins/input/sqlite/sqlite_featureset.cpp index 5e822671d..df2203710 100644 --- a/plugins/input/sqlite/sqlite_featureset.cpp +++ b/plugins/input/sqlite/sqlite_featureset.cpp @@ -75,7 +75,8 @@ feature_ptr sqlite_featureset::next() } feature_ptr feature = feature_factory::create(ctx_,rs_->column_integer(1)); - geometry_utils::from_wkb(feature->paths(), data, size, format_); + if (!geometry_utils::from_wkb(feature->paths(), data, size, format_)) + continue; if (!spatial_index_) { diff --git a/plugins/input/sqlite/sqlite_utils.hpp b/plugins/input/sqlite/sqlite_utils.hpp index d9074d09f..891150506 100644 --- a/plugins/input/sqlite/sqlite_utils.hpp +++ b/plugins/input/sqlite/sqlite_utils.hpp @@ -190,21 +190,22 @@ public: if (data) { boost::ptr_vector paths; - mapnik::geometry_utils::from_wkb(paths, data, size, mapnik::wkbAuto); - for (unsigned i=0; i const& bbox = paths[i].envelope(); - - if (bbox.valid()) + for (unsigned i=0; i const& bbox = paths[i].envelope(); + if (bbox.valid()) { - first = false; - extent = bbox; - } - else - { - extent.expand_to_include(bbox); + if (first) + { + first = false; + extent = bbox; + } + else + { + extent.expand_to_include(bbox); + } } } } @@ -276,24 +277,24 @@ public: if (data) { boost::ptr_vector paths; - mapnik::geometry_utils::from_wkb(paths, data, size, mapnik::wkbAuto); mapnik::box2d bbox; - for (unsigned i=0; icolumn_type(1); if (type_oid != SQLITE_INTEGER) { @@ -303,7 +304,6 @@ public: << "' type was: " << type_oid << ""; throw mapnik::datasource_exception(error_msg.str()); } - const sqlite_int64 pkid = rs->column_integer64(1); ps.bind(pkid); } @@ -314,7 +314,6 @@ public: << rs->column_name(1) << "' == " << rs->column_integer64(1); throw mapnik::datasource_exception(error_msg.str()); } - ps.step_next(); one_success = true; } @@ -365,45 +364,41 @@ public: if (data) { boost::ptr_vector paths; - mapnik::geometry_utils::from_wkb(paths, data, size, mapnik::wkbAuto); - for (unsigned i=0; i const& bbox = paths[i].envelope(); - if (bbox.valid()) + for (unsigned i=0; icolumn_type(1); - if (type_oid != SQLITE_INTEGER) + mapnik::box2d const& bbox = paths[i].envelope(); + if (bbox.valid()) + { + const int type_oid = rs->column_type(1); + if (type_oid != SQLITE_INTEGER) + { + std::ostringstream error_msg; + error_msg << "Sqlite Plugin: invalid type for key field '" + << rs->column_name(1) << "' when creating index " + << "type was: " << type_oid << ""; + throw mapnik::datasource_exception(error_msg.str()); + } + const sqlite_int64 pkid = rs->column_integer64(1); + rtree_type entry = rtree_type(); + entry.pkid = pkid; + entry.bbox = bbox; + rtree_list.push_back(entry); + } + else { std::ostringstream error_msg; - error_msg << "Sqlite Plugin: invalid type for key field '" - << rs->column_name(1) << "' when creating index " - << "type was: " << type_oid << ""; + error_msg << "SQLite Plugin: encountered invalid bbox at '" + << rs->column_name(1) << "' == " << rs->column_integer64(1); throw mapnik::datasource_exception(error_msg.str()); } - - const sqlite_int64 pkid = rs->column_integer64(1); - - rtree_type entry = rtree_type(); - entry.pkid = pkid; - entry.bbox = bbox; - rtree_list.push_back(entry); - - } - else - { - std::ostringstream error_msg; - error_msg << "SQLite Plugin: encountered invalid bbox at '" - << rs->column_name(1) << "' == " << rs->column_integer64(1); - throw mapnik::datasource_exception(error_msg.str()); } } } } } - - static bool create_spatial_index2(std::string const& index_db, std::string const& index_table, std::vector const& rtree_list) diff --git a/src/wkb.cpp b/src/wkb.cpp index f7d9692f3..0551034cd 100644 --- a/src/wkb.cpp +++ b/src/wkb.cpp @@ -431,13 +431,17 @@ private: #endif }; -void geometry_utils::from_wkb (boost::ptr_vector& paths, +bool geometry_utils::from_wkb(boost::ptr_vector& paths, const char* wkb, unsigned size, wkbFormat format) { + unsigned geom_count = paths.size(); wkb_reader reader(wkb, size, format); - return reader.read(paths); + reader.read(paths); + if (paths.size() > geom_count) + return true; + return false; } } diff --git a/utils/pgsql2sqlite/pgsql2sqlite.hpp b/utils/pgsql2sqlite/pgsql2sqlite.hpp index faa3f3e40..2d8583caa 100644 --- a/utils/pgsql2sqlite/pgsql2sqlite.hpp +++ b/utils/pgsql2sqlite/pgsql2sqlite.hpp @@ -281,8 +281,8 @@ void pgsql2sqlite(Connection conn, if (oid == geometry_oid) { mapnik::Feature feat(ctx,pkid); - geometry_utils::from_wkb(feat.paths(),buf,size,wkbGeneric); - if (feat.num_geometries() > 0) + if (geometry_utils::from_wkb(feat.paths(),buf,size,wkbGeneric) + && feat.num_geometries() > 0) { geometry_type const& geom=feat.get_geometry(0); box2d bbox = geom.envelope();