spatial_index - make generic

This commit is contained in:
artemp 2016-03-31 11:21:57 +02:00
parent 4faa9896bb
commit 99bfd03b22

View file

@ -47,12 +47,13 @@ bool check_spatial_index(InputStream& in)
return (std::strncmp(header, "mapnik-index",12) == 0);
}
template <typename Value, typename Filter, typename InputStream>
template <typename Value, typename Filter, typename InputStream, typename BBox = box2d<double> >
class spatial_index
{
using bbox_type = BBox;
public:
static void query(Filter const& filter, InputStream& in,std::vector<Value>& pos);
static box2d<double> bounding_box( InputStream& in );
static bbox_type bounding_box( InputStream& in );
static void query_first_n(Filter const& filter, InputStream & in, std::vector<Value>& pos, std::size_t count);
private:
spatial_index();
@ -60,25 +61,25 @@ private:
spatial_index(spatial_index const&);
spatial_index& operator=(spatial_index const&);
static int read_ndr_integer(InputStream& in);
static void read_envelope(InputStream& in, box2d<double>& envelope);
static void read_envelope(InputStream& in, bbox_type& envelope);
static void query_node(Filter const& filter, InputStream& in, std::vector<Value> & results);
static void query_first_n_impl(Filter const& filter, InputStream& in, std::vector<Value> & results, std::size_t count);
};
template <typename Value, typename Filter, typename InputStream>
box2d<double> spatial_index<Value, Filter, InputStream>::bounding_box(InputStream& in)
template <typename Value, typename Filter, typename InputStream, typename BBox>
BBox spatial_index<Value, Filter, InputStream, BBox>::bounding_box(InputStream& in)
{
static_assert(std::is_standard_layout<Value>::value, "Values stored in quad-tree must be standard layout type");
if (!check_spatial_index(in)) throw std::runtime_error("Invalid index file (regenerate with shapeindex)");
in.seekg(16 + 4, std::ios::beg);
box2d<double> box;
typename spatial_index<Value, Filter, InputStream, BBox>::bbox_type box;
read_envelope(in, box);
in.seekg(0, std::ios::beg);
return box;
}
template <typename Value, typename Filter, typename InputStream>
void spatial_index<Value, Filter, InputStream>::query(Filter const& filter, InputStream& in, std::vector<Value>& results)
template <typename Value, typename Filter, typename InputStream, typename BBox>
void spatial_index<Value, Filter, InputStream, BBox>::query(Filter const& filter, InputStream& in, std::vector<Value>& results)
{
static_assert(std::is_standard_layout<Value>::value, "Values stored in quad-tree must be standard layout type");
if (!check_spatial_index(in)) throw std::runtime_error("Invalid index file (regenerate with shapeindex)");
@ -86,11 +87,11 @@ void spatial_index<Value, Filter, InputStream>::query(Filter const& filter, Inpu
query_node(filter, in, results);
}
template <typename Value, typename Filter, typename InputStream>
void spatial_index<Value, Filter, InputStream>::query_node(Filter const& filter, InputStream& in, std::vector<Value>& results)
template <typename Value, typename Filter, typename InputStream, typename BBox>
void spatial_index<Value, Filter, InputStream, BBox>::query_node(Filter const& filter, InputStream& in, std::vector<Value>& results)
{
int offset = read_ndr_integer(in);
box2d<double> node_ext;
typename spatial_index<Value, Filter, InputStream, BBox>::bbox_type node_ext;
read_envelope(in, node_ext);
int num_shapes = read_ndr_integer(in);
if (!filter.pass(node_ext))
@ -113,8 +114,8 @@ void spatial_index<Value, Filter, InputStream>::query_node(Filter const& filter,
}
}
template <typename Value, typename Filter, typename InputStream>
void spatial_index<Value, Filter, InputStream>::query_first_n(Filter const& filter, InputStream& in, std::vector<Value>& results, std::size_t count)
template <typename Value, typename Filter, typename InputStream, typename BBox>
void spatial_index<Value, Filter, InputStream, BBox>::query_first_n(Filter const& filter, InputStream& in, std::vector<Value>& results, std::size_t count)
{
static_assert(std::is_standard_layout<Value>::value, "Values stored in quad-tree must be standard layout type");
if (!check_spatial_index(in)) throw std::runtime_error("Invalid index file (regenerate with shapeindex)");
@ -122,12 +123,12 @@ void spatial_index<Value, Filter, InputStream>::query_first_n(Filter const& filt
query_first_n_impl(filter, in, results, count);
}
template <typename Value, typename Filter, typename InputStream>
void spatial_index<Value, Filter, InputStream>::query_first_n_impl(Filter const& filter, InputStream& in, std::vector<Value>& results, std::size_t count)
template <typename Value, typename Filter, typename InputStream, typename BBox>
void spatial_index<Value, Filter, InputStream, BBox>::query_first_n_impl(Filter const& filter, InputStream& in, std::vector<Value>& results, std::size_t count)
{
if (results.size() == count) return;
int offset = read_ndr_integer(in);
box2d<double> node_ext;
typename spatial_index<Value, Filter, InputStream, BBox>::bbox_type node_ext;
read_envelope(in, node_ext);
int num_shapes = read_ndr_integer(in);
if (!filter.pass(node_ext))
@ -149,16 +150,16 @@ void spatial_index<Value, Filter, InputStream>::query_first_n_impl(Filter const&
}
}
template <typename Value, typename Filter, typename InputStream>
int spatial_index<Value, Filter, InputStream>::read_ndr_integer(InputStream& in)
template <typename Value, typename Filter, typename InputStream, typename BBox>
int spatial_index<Value, Filter, InputStream, BBox>::read_ndr_integer(InputStream& in)
{
char b[4];
in.read(b, 4);
return (b[0] & 0xff) | (b[1] & 0xff) << 8 | (b[2] & 0xff) << 16 | (b[3] & 0xff) << 24;
}
template <typename Value, typename Filter, typename InputStream>
void spatial_index<Value, Filter, InputStream>::read_envelope(InputStream& in, box2d<double>& envelope)
template <typename Value, typename Filter, typename InputStream, typename BBox>
void spatial_index<Value, Filter, InputStream, BBox>::read_envelope(InputStream& in, BBox& envelope)
{
in.read(reinterpret_cast<char*>(&envelope), sizeof(envelope));
}