2006-03-31 12:32:02 +02:00
|
|
|
/*****************************************************************************
|
2011-11-14 04:37:50 +01:00
|
|
|
*
|
2006-03-31 12:32:02 +02:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
2005-06-14 17:06:59 +02:00
|
|
|
*
|
2015-06-16 12:49:16 +02:00
|
|
|
* Copyright (C) 2015 Artem Pavlenko
|
2005-06-14 17:06:59 +02:00
|
|
|
*
|
2006-03-31 12:32:02 +02:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
2005-06-14 17:06:59 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2006-03-31 12:32:02 +02:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2005-06-14 17:06:59 +02:00
|
|
|
*
|
2006-03-31 12:32:02 +02:00
|
|
|
*****************************************************************************/
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2012-12-11 10:37:53 +01:00
|
|
|
#ifndef SHP_INDEX_HPP
|
|
|
|
#define SHP_INDEX_HPP
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2011-10-22 15:27:28 +02:00
|
|
|
// stl
|
2005-06-14 17:06:59 +02:00
|
|
|
#include <fstream>
|
2008-02-04 12:12:32 +01:00
|
|
|
#include <vector>
|
2011-10-22 15:27:28 +02:00
|
|
|
|
2006-10-04 13:22:18 +02:00
|
|
|
// mapnik
|
2009-12-16 21:02:06 +01:00
|
|
|
#include <mapnik/box2d.hpp>
|
2006-10-04 13:22:18 +02:00
|
|
|
#include <mapnik/query.hpp>
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2009-12-16 21:02:06 +01:00
|
|
|
using mapnik::box2d;
|
2007-03-16 11:11:37 +01:00
|
|
|
using mapnik::query;
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2008-02-04 12:12:32 +01:00
|
|
|
template <typename filterT, typename IStream = std::ifstream>
|
2005-06-14 17:06:59 +02:00
|
|
|
class shp_index
|
|
|
|
{
|
|
|
|
public:
|
2012-12-11 10:37:53 +01:00
|
|
|
static void query(filterT const& filter, IStream& file,std::vector<std::streampos>& pos);
|
2005-06-14 17:06:59 +02:00
|
|
|
private:
|
|
|
|
shp_index();
|
|
|
|
~shp_index();
|
|
|
|
shp_index(const shp_index&);
|
|
|
|
shp_index& operator=(const shp_index&);
|
2011-10-22 15:27:28 +02:00
|
|
|
static int read_ndr_integer(IStream& in);
|
|
|
|
static void read_envelope(IStream& in, box2d<double>& envelope);
|
2012-12-11 10:37:53 +01:00
|
|
|
static void query_node(const filterT& filter, IStream& in, std::vector<std::streampos>& pos);
|
2005-06-14 17:06:59 +02:00
|
|
|
};
|
|
|
|
|
2011-10-22 15:27:28 +02:00
|
|
|
template <typename filterT, typename IStream>
|
2012-12-11 10:37:53 +01:00
|
|
|
void shp_index<filterT, IStream>::query(const filterT& filter, IStream& file, std::vector<std::streampos>& pos)
|
2008-02-04 12:12:32 +01:00
|
|
|
{
|
2011-10-22 15:27:28 +02:00
|
|
|
file.seekg(16, std::ios::beg);
|
|
|
|
query_node(filter, file, pos);
|
2008-02-04 12:12:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename filterT, typename IStream>
|
2012-12-11 10:37:53 +01:00
|
|
|
void shp_index<filterT, IStream>::query_node(const filterT& filter, IStream& file, std::vector<std::streampos>& ids)
|
2008-02-04 12:12:32 +01:00
|
|
|
{
|
2011-10-22 15:27:28 +02:00
|
|
|
int offset = read_ndr_integer(file);
|
2008-02-04 12:12:32 +01:00
|
|
|
|
2009-12-16 21:02:06 +01:00
|
|
|
box2d<double> node_ext;
|
2011-10-22 15:27:28 +02:00
|
|
|
read_envelope(file, node_ext);
|
2008-02-04 12:12:32 +01:00
|
|
|
|
2011-10-22 15:27:28 +02:00
|
|
|
int num_shapes = read_ndr_integer(file);
|
2008-02-04 12:12:32 +01:00
|
|
|
|
2011-10-22 15:27:28 +02:00
|
|
|
if (! filter.pass(node_ext))
|
2008-02-04 12:12:32 +01:00
|
|
|
{
|
2011-10-22 15:27:28 +02:00
|
|
|
file.seekg(offset + num_shapes * 4 + 4, std::ios::cur);
|
2008-02-04 12:12:32 +01:00
|
|
|
return;
|
|
|
|
}
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2011-10-22 15:27:28 +02:00
|
|
|
for (int i = 0; i < num_shapes; ++i)
|
2008-02-04 12:12:32 +01:00
|
|
|
{
|
2011-10-22 15:27:28 +02:00
|
|
|
int id = read_ndr_integer(file);
|
2008-02-04 12:12:32 +01:00
|
|
|
ids.push_back(id);
|
|
|
|
}
|
|
|
|
|
2011-10-22 15:27:28 +02:00
|
|
|
int children = read_ndr_integer(file);
|
2008-02-04 12:12:32 +01:00
|
|
|
|
2011-10-22 15:27:28 +02:00
|
|
|
for (int j = 0; j < children; ++j)
|
2008-02-04 12:12:32 +01:00
|
|
|
{
|
2011-10-22 15:27:28 +02:00
|
|
|
query_node(filter, file, ids);
|
2008-02-04 12:12:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-22 15:27:28 +02:00
|
|
|
template <typename filterT, typename IStream>
|
|
|
|
int shp_index<filterT, IStream>::read_ndr_integer(IStream& file)
|
2008-02-04 12:12:32 +01:00
|
|
|
{
|
|
|
|
char b[4];
|
2011-10-22 15:27:28 +02:00
|
|
|
file.read(b, 4);
|
|
|
|
return (b[0] & 0xff) | (b[1] & 0xff) << 8 | (b[2] & 0xff) << 16 | (b[3] & 0xff) << 24;
|
2008-02-04 12:12:32 +01:00
|
|
|
}
|
|
|
|
|
2011-10-22 15:27:28 +02:00
|
|
|
template <typename filterT, typename IStream>
|
|
|
|
void shp_index<filterT, IStream>::read_envelope(IStream& file, box2d<double>& envelope)
|
2008-02-04 12:12:32 +01:00
|
|
|
{
|
2011-10-22 15:27:28 +02:00
|
|
|
file.read(reinterpret_cast<char*>(&envelope), sizeof(envelope));
|
2008-02-04 12:12:32 +01:00
|
|
|
}
|
|
|
|
|
2012-12-11 10:37:53 +01:00
|
|
|
#endif // SHP_INDEX_HPP
|