shape.input/shapeindex - update to use bounding box per item *.index format.

This commit is contained in:
artemp 2017-08-21 11:02:34 +01:00
parent 6c19efa990
commit 40c51c469c
5 changed files with 77 additions and 40 deletions

View file

@ -212,21 +212,25 @@ struct bounding_box_filter
using filter_in_box = bounding_box_filter<double>;
struct filter_at_point
template <typename T>
struct at_point_filter
{
box2d<double> box_;
explicit filter_at_point(coord2d const& pt, double tol = 0)
using value_type = T;
box2d<value_type> box_;
explicit at_point_filter(coord<value_type, 2> const& pt, double tol = 0)
: box_(pt, pt)
{
box_.pad(tol);
}
bool pass(box2d<double> const& extent) const
bool pass(box2d<value_type> const& extent) const
{
return extent.intersects(box_);
}
};
using filter_at_point = at_point_filter<double>;
////////////////////////////////////////////////////////////////////////////
template <typename PathType>
double path_length(PathType & path)

View file

@ -236,21 +236,24 @@ featureset_ptr shape_datasource::features(query const& q) const
mapnik::progress_timer __stats__(std::clog, "shape_datasource::features");
#endif
filter_in_box filter(q.get_bbox());
auto const& query_box = q.get_bbox();
if (indexed_)
{
std::unique_ptr<shape_io> shape_ptr = std::make_unique<shape_io>(shape_name_);
mapnik::bounding_box_filter<float> filter(mapnik::box2d<float>(query_box.minx(), query_box.miny(), query_box.maxx(), query_box.maxy()));
return featureset_ptr
(new shape_index_featureset<filter_in_box>(filter,
std::move(shape_ptr),
q.property_names(),
desc_.get_encoding(),
shape_name_,
row_limit_));
(new shape_index_featureset<mapnik::bounding_box_filter<float>>(filter,
std::move(shape_ptr),
q.property_names(),
desc_.get_encoding(),
shape_name_,
row_limit_));
}
else
{
return std::make_shared<shape_featureset<filter_in_box> >(filter,
mapnik::bounding_box_filter<double> filter(q.get_bbox());
return std::make_shared<shape_featureset< mapnik::bounding_box_filter<double>>>(filter,
shape_name_,
q.property_names(),
desc_.get_encoding(),
@ -264,7 +267,7 @@ featureset_ptr shape_datasource::features_at_point(coord2d const& pt, double tol
mapnik::progress_timer __stats__(std::clog, "shape_datasource::features_at_point");
#endif
filter_at_point filter(pt,tol);
// collect all attribute names
auto const& desc = desc_.get_descriptors();
std::set<std::string> names;
@ -277,16 +280,18 @@ featureset_ptr shape_datasource::features_at_point(coord2d const& pt, double tol
if (indexed_)
{
std::unique_ptr<shape_io> shape_ptr = std::make_unique<shape_io>(shape_name_);
mapnik::at_point_filter<float> filter(mapnik::coord2f(pt.x, pt.y));
return featureset_ptr
(new shape_index_featureset<filter_at_point>(filter,
std::move(shape_ptr),
names,
desc_.get_encoding(),
shape_name_,
row_limit_));
(new shape_index_featureset<mapnik::at_point_filter<float>>(filter,
std::move(shape_ptr),
names,
desc_.get_encoding(),
shape_name_,
row_limit_));
}
else
{
filter_at_point filter(pt,tol);
return std::make_shared<shape_featureset<filter_at_point> >(filter,
shape_name_,
names,

View file

@ -52,7 +52,7 @@ shape_index_featureset<filterT>::shape_index_featureset(filterT const& filter,
ctx_(std::make_shared<mapnik::context_type>()),
shape_ptr_(std::move(shape_ptr)),
tr_(new mapnik::transcoder(encoding)),
offsets_(),
positions_(),
itr_(),
attr_ids_(),
row_limit_(row_limit),
@ -60,21 +60,33 @@ shape_index_featureset<filterT>::shape_index_featureset(filterT const& filter,
feature_bbox_()
{
shape_ptr_->shp().skip(100);
setup_attributes(ctx_, attribute_names, shape_name, *shape_ptr_,attr_ids_);
setup_attributes(ctx_, attribute_names, shape_name, *shape_ptr_, attr_ids_);
auto index = shape_ptr_->index();
if (index)
{
#if defined(MAPNIK_MEMORY_MAPPED_FILE)
mapnik::util::spatial_index<mapnik::detail::node, filterT,boost::interprocess::ibufferstream>::query(filter, index->file(), offsets_);
mapnik::util::spatial_index<mapnik::detail::node,
filterT,
boost::interprocess::ibufferstream,
mapnik::box2d<typename filterT::value_type>>::query(filter, index->file(), positions_);
#else
mapnik::util::spatial_index<mapnik::detail::node, filterT, std::ifstream>::query(filter, index->file(), offsets_);
mapnik::util::spatial_index<mapnik::detail::node,
filterT,
std::ifstream,
mapnik::box2d<typename filterT::value_type>>::query(filter, index->file(), positions_);
#endif
}
std::sort(offsets_.begin(), offsets_.end(), [](mapnik::detail::node const& n0, mapnik::detail::node const& n1)
// filter
positions_.erase(std::remove_if(positions_.begin(),
positions_.end(),
[&](mapnik::detail::node const& pos)
{ return !pos.box.intersects(filter.box_);}),
positions_.end());
std::sort(positions_.begin(), positions_.end(), [](mapnik::detail::node const& n0, mapnik::detail::node const& n1)
{return n0.offset != n1.offset ? n0.offset < n1.offset : n0.start < n1.start;});
MAPNIK_LOG_DEBUG(shape) << "shape_index_featureset: Query size=" << offsets_.size();
itr_ = offsets_.begin();
MAPNIK_LOG_DEBUG(shape) << "shape_index_featureset: Query size=" << positions_.size();
itr_ = positions_.begin();
}
template <typename filterT>
@ -85,12 +97,12 @@ feature_ptr shape_index_featureset<filterT>::next()
return feature_ptr();
}
while ( itr_ != offsets_.end())
while ( itr_ != positions_.end())
{
std::uint64_t offset = itr_->offset;
shape_ptr_->move_to(offset);
std::vector<std::pair<int,int>> parts;
while (itr_ != offsets_.end() && itr_->offset == offset)
while (itr_ != positions_.end() && itr_->offset == offset)
{
if (itr_->start!= -1) parts.emplace_back(itr_->start, itr_->end);
++itr_;
@ -117,7 +129,7 @@ feature_ptr shape_index_featureset<filterT>::next()
case shape_io::shape_multipointz:
{
shape_io::read_bbox(record, feature_bbox_);
if (!filter_.pass(feature_bbox_)) continue;
//if (!filter_.pass(feature_bbox_)) continue;
int num_points = record.read_ndr_integer();
mapnik::geometry::multi_point<double> multi_point;
for (int i = 0; i < num_points; ++i)
@ -134,7 +146,7 @@ feature_ptr shape_index_featureset<filterT>::next()
case shape_io::shape_polylinez:
{
shape_io::read_bbox(record, feature_bbox_);
if (!filter_.pass(feature_bbox_)) continue;
//if (!filter_.pass(feature_bbox_)) continue;
if (parts.size() < 2) feature->set_geometry(shape_io::read_polyline(record));
else feature->set_geometry(shape_io::read_polyline_parts(record, parts));
break;
@ -144,7 +156,7 @@ feature_ptr shape_index_featureset<filterT>::next()
case shape_io::shape_polygonz:
{
shape_io::read_bbox(record, feature_bbox_);
if (!filter_.pass(feature_bbox_)) continue;
//if (!filter_.pass(feature_bbox_)) continue;
if (parts.size() < 2) feature->set_geometry(shape_io::read_polygon(record));
else feature->set_geometry(shape_io::read_polygon_parts(record, parts));
break;
@ -181,5 +193,5 @@ feature_ptr shape_index_featureset<filterT>::next()
template <typename filterT>
shape_index_featureset<filterT>::~shape_index_featureset() {}
template class shape_index_featureset<mapnik::filter_in_box>;
template class shape_index_featureset<mapnik::filter_at_point>;
template class shape_index_featureset<mapnik::bounding_box_filter<float>>;
template class shape_index_featureset<mapnik::at_point_filter<float>>;

View file

@ -50,13 +50,15 @@ namespace mapnik { namespace detail
struct node
{
node() = default;
node(std::uint64_t offset_, std::int32_t start_, std::int32_t end_)
node(std::uint64_t offset_, std::int32_t start_, std::int32_t end_, box2d<float> && box_)
: offset(offset_),
start(start_),
end(end_) {}
end(end_),
box(std::move(box_)) {}
std::uint64_t offset;
std::int32_t start;
std::int32_t end;
mapnik::box2d<float> box;
};
}} // ns
@ -78,7 +80,7 @@ private:
context_ptr ctx_;
std::unique_ptr<shape_io> shape_ptr_;
const std::unique_ptr<mapnik::transcoder> tr_;
std::vector<mapnik::detail::node> offsets_;
std::vector<mapnik::detail::node> positions_;
std::vector<mapnik::detail::node>::iterator itr_;
std::vector<int> attr_ids_;
mapnik::value_integer row_limit_;

View file

@ -26,7 +26,7 @@
#include <mapnik/version.hpp>
#include <mapnik/util/fs.hpp>
#include <mapnik/quad_tree.hpp>
#include <mapnik/util/spatial_index.hpp>
//#include <mapnik/util/spatial_index.hpp>
#include <mapnik/geometry/envelope.hpp>
#include "shapefile.hpp"
#include "shape_io.hpp"
@ -172,7 +172,12 @@ int main (int argc,char** argv)
}
int pos = 50;
shx.seek(pos * 2);
mapnik::quad_tree<mapnik::detail::node> tree(extent, depth, ratio);
mapnik::box2d<float> extent_f { static_cast<float>(extent.minx()),
static_cast<float>(extent.miny()),
static_cast<float>(extent.maxx()),
static_cast<float>(extent.maxy())};
mapnik::quad_tree<mapnik::detail::node, mapnik::box2d<float> > tree(extent_f, depth, ratio);
int count = 0;
if (shape_type != shape_io::shape_null)
@ -238,7 +243,11 @@ int main (int argc,char** argv)
{
std::clog << "record number " << record_number << " box=" << item_ext << std::endl;
}
tree.insert(mapnik::detail::node(offset * 2, start, end),item_ext);
mapnik::box2d<float> ext_f {static_cast<float>(item_ext.minx()),
static_cast<float>(item_ext.miny()),
static_cast<float>(item_ext.maxx()),
static_cast<float>(item_ext.maxy())};
tree.insert(mapnik::detail::node(offset * 2, start, end, std::move(ext_f)), ext_f);
++count;
}
}
@ -255,7 +264,12 @@ int main (int argc,char** argv)
{
std::clog << "record number " << record_number << " box=" << item_ext << std::endl;
}
tree.insert(mapnik::detail::node(offset * 2,-1,0),item_ext);
mapnik::box2d<float> ext_f {static_cast<float>(item_ext.minx()),
static_cast<float>(item_ext.miny()),
static_cast<float>(item_ext.maxx()),
static_cast<float>(item_ext.maxy())};
tree.insert(mapnik::detail::node(offset * 2, -1, 0, std::move(ext_f)), ext_f);
++count;
}
}