add basic read/write header support (quad_tree + spatial_index) ref (#3216)

This commit is contained in:
artemp 2016-01-05 10:20:21 +00:00
parent 869bff43f0
commit ee0395df7a
2 changed files with 16 additions and 10 deletions

View file

@ -179,12 +179,7 @@ public:
"Values stored in quad-tree must be standard layout types to allow serialisation");
char header[16];
std::memset(header,0,16);
header[0]='m';
header[1]='a';
header[2]='p';
header[3]='n';
header[4]='i';
header[5]='k';
std::strcpy(header,"mapnik-index");
out.write(header,16);
write_node(out,root_);
}

View file

@ -30,6 +30,7 @@
#include <mapnik/geom_util.hpp>
// stl
#include <type_traits>
#include <cstring>
using mapnik::box2d;
using mapnik::query;
@ -44,7 +45,6 @@ public:
static box2d<double> 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();
~spatial_index();
spatial_index(spatial_index const&);
@ -53,12 +53,23 @@ private:
static void read_envelope(InputStream& in, box2d<double>& 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);
static bool check_header(InputStream& in);
};
template <typename Value, typename Filter, typename InputStream>
bool spatial_index<Value, Filter, InputStream>::check_header(InputStream& in)
{
static_assert(std::is_standard_layout<Value>::value, "Values stored in quad-tree must be standard layout type");
char header[17]; // mapnik-index
std::memset(header, 0, 17);
in.read(header,16);
return (std::strncmp(header, "mapnik-index",12) == 0);
}
template <typename Value, typename Filter, typename InputStream>
box2d<double> spatial_index<Value, Filter, InputStream>::bounding_box(InputStream& in)
{
static_assert(std::is_standard_layout<Value>::value, "Values stored in quad-tree must be standard layout type");
if (!check_header(in)) throw std::runtime_error("Invalid index file");
in.seekg(16 + 4, std::ios::beg);
box2d<double> box;
read_envelope(in, box);
@ -69,7 +80,7 @@ box2d<double> spatial_index<Value, Filter, InputStream>::bounding_box(InputStrea
template <typename Value, typename Filter, typename InputStream>
void spatial_index<Value, Filter, InputStream>::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 types");
if (!check_header(in)) throw std::runtime_error("Invalid index file");
in.seekg(16, std::ios::beg);
query_node(filter, in, results);
}
@ -104,7 +115,7 @@ 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)
{
static_assert(std::is_standard_layout<Value>::value, "Values stored in quad-tree must be standard layout types");
if (!check_header(in)) throw std::runtime_error("Invalid index file");
in.seekg(16, std::ios::beg);
query_first_n_impl(filter, in, results, count);
}