2006-03-31 10:32:02 +00:00
|
|
|
/*****************************************************************************
|
2011-11-14 03:54:32 +00:00
|
|
|
*
|
2006-03-31 10:32:02 +00:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
2005-06-14 15:06:59 +00:00
|
|
|
*
|
2021-01-05 14:39:07 +00:00
|
|
|
* Copyright (C) 2021 Artem Pavlenko
|
2005-06-14 15:06:59 +00:00
|
|
|
*
|
2006-03-31 10:32:02 +00: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 15:06:59 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2006-03-31 10:32:02 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2005-06-14 15:06:59 +00:00
|
|
|
*
|
2006-03-31 10:32:02 +00:00
|
|
|
* 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 15:06:59 +00:00
|
|
|
|
2005-06-17 12:40:51 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
2022-02-02 16:20:29 +00:00
|
|
|
#include <mapnik/mapnik.hpp>
|
2017-02-27 12:22:55 +00:00
|
|
|
#include <mapnik/version.hpp>
|
2013-06-03 02:28:24 +00:00
|
|
|
#include <mapnik/util/fs.hpp>
|
2015-09-29 11:21:02 +00:00
|
|
|
#include <mapnik/quad_tree.hpp>
|
2017-08-21 10:02:34 +00:00
|
|
|
//#include <mapnik/util/spatial_index.hpp>
|
2016-08-02 13:43:02 +00:00
|
|
|
#include <mapnik/geometry/envelope.hpp>
|
2008-02-04 11:12:32 +00:00
|
|
|
#include "shapefile.hpp"
|
|
|
|
#include "shape_io.hpp"
|
2016-01-04 13:19:16 +00:00
|
|
|
#include "shape_index_featureset.hpp"
|
2020-11-19 14:30:30 +00:00
|
|
|
#include <mapnik/warning.hpp>
|
|
|
|
MAPNIK_DISABLE_WARNING_PUSH
|
2015-11-08 01:53:09 +00:00
|
|
|
#include <mapnik/warning_ignore.hpp>
|
2014-10-21 23:37:27 +00:00
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
#include <boost/program_options.hpp>
|
2020-11-19 14:30:30 +00:00
|
|
|
MAPNIK_DISABLE_WARNING_POP
|
2014-10-21 23:37:27 +00:00
|
|
|
|
2005-06-17 12:40:51 +00:00
|
|
|
const int DEFAULT_DEPTH = 8;
|
2017-06-01 13:27:17 +00:00
|
|
|
const double DEFAULT_RATIO = 0.55;
|
2005-06-14 15:06:59 +00:00
|
|
|
|
2021-10-21 16:22:53 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define NOMINMAX
|
2018-01-20 20:45:30 +00:00
|
|
|
#include <windows.h>
|
2022-01-26 19:41:37 +00:00
|
|
|
int main()
|
2018-01-20 20:45:30 +00:00
|
|
|
#else
|
2022-01-26 19:41:37 +00:00
|
|
|
int main(int argc, char** argv)
|
2018-01-20 20:45:30 +00:00
|
|
|
#endif
|
2005-06-17 12:40:51 +00:00
|
|
|
{
|
|
|
|
using namespace mapnik;
|
|
|
|
namespace po = boost::program_options;
|
2011-11-14 03:54:32 +00:00
|
|
|
|
2022-01-26 19:41:37 +00:00
|
|
|
bool verbose = false;
|
2016-01-04 11:43:27 +00:00
|
|
|
bool index_parts = false;
|
2016-01-04 13:23:50 +00:00
|
|
|
unsigned int depth = DEFAULT_DEPTH;
|
|
|
|
double ratio = DEFAULT_RATIO;
|
2015-10-13 12:29:16 +00:00
|
|
|
std::vector<std::string> shape_files;
|
2011-11-14 03:54:32 +00:00
|
|
|
|
2022-02-02 16:20:29 +00:00
|
|
|
mapnik::setup();
|
2005-06-17 12:40:51 +00:00
|
|
|
try
|
2005-06-14 15:06:59 +00:00
|
|
|
{
|
2006-05-12 16:35:36 +00:00
|
|
|
po::options_description desc("shapeindex utility");
|
2022-01-26 19:41:37 +00:00
|
|
|
// clang-format off
|
2006-05-12 16:35:36 +00:00
|
|
|
desc.add_options()
|
|
|
|
("help,h", "produce usage message")
|
|
|
|
("version,V","print version string")
|
2016-01-04 13:23:50 +00:00
|
|
|
("index-parts","index individual shape parts (default: no)")
|
2006-05-12 16:35:36 +00:00
|
|
|
("verbose,v","verbose output")
|
2011-11-14 03:54:32 +00:00
|
|
|
("depth,d", po::value<unsigned int>(), "max tree depth\n(default 8)")
|
2006-05-12 16:35:36 +00:00
|
|
|
("ratio,r",po::value<double>(),"split ratio (default 0.55)")
|
2015-10-13 12:29:16 +00:00
|
|
|
("shape_files",po::value<std::vector<std::string> >(),"shape files to index: file1 file2 ...fileN")
|
2006-05-12 16:35:36 +00:00
|
|
|
;
|
2022-01-26 19:41:37 +00:00
|
|
|
// clang-format on
|
2006-05-12 16:35:36 +00:00
|
|
|
po::positional_options_description p;
|
2022-01-26 19:41:37 +00:00
|
|
|
p.add("shape_files", -1);
|
2011-11-14 03:54:32 +00:00
|
|
|
po::variables_map vm;
|
2021-10-21 16:22:53 +00:00
|
|
|
#ifdef _WIN32
|
2018-01-20 20:45:30 +00:00
|
|
|
std::vector<std::string> args;
|
|
|
|
const auto wargs = po::split_winmain(GetCommandLineW());
|
2022-01-26 19:41:37 +00:00
|
|
|
for (auto it = wargs.begin() + 1; it != wargs.end(); ++it)
|
2018-01-20 20:45:30 +00:00
|
|
|
args.push_back(mapnik::utf16_to_utf8(*it));
|
|
|
|
po::store(po::command_line_parser(args).options(desc).positional(p).run(), vm);
|
|
|
|
#else
|
2006-05-12 16:35:36 +00:00
|
|
|
po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
|
2018-01-20 20:45:30 +00:00
|
|
|
#endif
|
2006-05-12 16:35:36 +00:00
|
|
|
po::notify(vm);
|
2011-11-14 03:54:32 +00:00
|
|
|
|
2006-05-12 16:35:36 +00:00
|
|
|
if (vm.count("version"))
|
|
|
|
{
|
2017-02-27 12:22:55 +00:00
|
|
|
std::clog << "version " << MAPNIK_VERSION_STRING << std::endl;
|
2016-03-07 12:52:58 +00:00
|
|
|
return EXIT_FAILURE;
|
2006-05-12 16:35:36 +00:00
|
|
|
}
|
2005-06-14 15:06:59 +00:00
|
|
|
|
2011-11-14 03:54:32 +00:00
|
|
|
if (vm.count("help"))
|
2006-05-12 16:35:36 +00:00
|
|
|
{
|
2015-10-13 12:29:16 +00:00
|
|
|
std::clog << desc << std::endl;
|
2016-03-07 12:52:58 +00:00
|
|
|
return EXIT_FAILURE;
|
2006-05-12 16:35:36 +00:00
|
|
|
}
|
2011-11-14 03:54:32 +00:00
|
|
|
if (vm.count("verbose"))
|
2010-11-10 11:55:22 +00:00
|
|
|
{
|
|
|
|
verbose = true;
|
|
|
|
}
|
2016-01-04 11:43:27 +00:00
|
|
|
if (vm.count("index-parts"))
|
|
|
|
{
|
|
|
|
index_parts = true;
|
|
|
|
}
|
2006-05-12 16:35:36 +00:00
|
|
|
if (vm.count("depth"))
|
|
|
|
{
|
|
|
|
depth = vm["depth"].as<unsigned int>();
|
|
|
|
}
|
|
|
|
if (vm.count("ratio"))
|
|
|
|
{
|
|
|
|
ratio = vm["ratio"].as<double>();
|
|
|
|
}
|
2011-11-14 03:54:32 +00:00
|
|
|
|
2005-06-17 12:40:51 +00:00
|
|
|
if (vm.count("shape_files"))
|
2006-05-12 16:35:36 +00:00
|
|
|
{
|
2022-01-26 19:41:37 +00:00
|
|
|
shape_files = vm["shape_files"].as<std::vector<std::string>>();
|
2006-05-12 16:35:36 +00:00
|
|
|
}
|
2022-11-10 15:57:38 +00:00
|
|
|
}
|
|
|
|
catch (std::exception const& ex)
|
2005-06-17 12:40:51 +00:00
|
|
|
{
|
2015-10-13 12:29:16 +00:00
|
|
|
std::clog << "Error: " << ex.what() << std::endl;
|
2016-03-07 12:52:58 +00:00
|
|
|
return EXIT_FAILURE;
|
2005-06-14 15:06:59 +00:00
|
|
|
}
|
2011-11-14 03:54:32 +00:00
|
|
|
|
2015-10-13 12:29:16 +00:00
|
|
|
std::clog << "max tree depth:" << depth << std::endl;
|
|
|
|
std::clog << "split ratio:" << ratio << std::endl;
|
2011-11-14 03:54:32 +00:00
|
|
|
|
2015-02-19 10:04:13 +00:00
|
|
|
if (shape_files.size() == 0)
|
2005-06-17 12:40:51 +00:00
|
|
|
{
|
2015-10-13 12:29:16 +00:00
|
|
|
std::clog << "no shape files to index" << std::endl;
|
2016-03-07 12:52:58 +00:00
|
|
|
return EXIT_FAILURE;
|
2005-06-17 12:40:51 +00:00
|
|
|
}
|
2015-02-19 10:04:13 +00:00
|
|
|
for (auto const& filename : shape_files)
|
2005-06-17 12:40:51 +00:00
|
|
|
{
|
2015-10-13 12:29:16 +00:00
|
|
|
std::clog << "processing " << filename << std::endl;
|
2022-01-26 19:41:37 +00:00
|
|
|
std::string shapename(filename);
|
|
|
|
boost::algorithm::ireplace_last(shapename, ".shp", "");
|
|
|
|
std::string shapename_full(shapename + ".shp");
|
2015-10-13 12:29:16 +00:00
|
|
|
std::string shxname(shapename + ".shx");
|
2022-01-26 19:41:37 +00:00
|
|
|
if (!mapnik::util::exists(shapename_full))
|
2009-04-28 19:54:34 +00:00
|
|
|
{
|
2015-10-13 12:29:16 +00:00
|
|
|
std::clog << "Error : file " << shapename_full << " does not exist" << std::endl;
|
|
|
|
continue;
|
|
|
|
}
|
2022-01-26 19:41:37 +00:00
|
|
|
if (!mapnik::util::exists(shxname))
|
2015-10-13 12:29:16 +00:00
|
|
|
{
|
|
|
|
std::clog << "Error : shapefile index file (*.shx) " << shxname << " does not exist" << std::endl;
|
2009-04-28 19:54:34 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-01-26 19:41:37 +00:00
|
|
|
shape_file shp(shapename_full);
|
2008-02-04 11:12:32 +00:00
|
|
|
|
2022-01-26 19:41:37 +00:00
|
|
|
if (!shp.is_open())
|
2015-10-13 12:29:16 +00:00
|
|
|
{
|
|
|
|
std::clog << "Error : cannot open " << shapename_full << std::endl;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-01-26 19:41:37 +00:00
|
|
|
shape_file shx(shxname);
|
2015-10-13 12:29:16 +00:00
|
|
|
if (!shx.is_open())
|
|
|
|
{
|
|
|
|
std::clog << "Error : cannot open " << shxname << std::endl;
|
2006-05-12 16:35:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
2011-11-14 03:54:32 +00:00
|
|
|
|
2022-01-26 19:41:37 +00:00
|
|
|
int code = shx.read_xdr_integer(); // file_code == 9994
|
2015-10-13 12:29:16 +00:00
|
|
|
std::clog << code << std::endl;
|
2022-01-26 19:41:37 +00:00
|
|
|
shx.skip(5 * 4);
|
2011-11-14 03:54:32 +00:00
|
|
|
|
2022-01-26 19:41:37 +00:00
|
|
|
int file_length = shx.read_xdr_integer();
|
|
|
|
int version = shx.read_ndr_integer();
|
|
|
|
int shape_type = shx.read_ndr_integer();
|
2009-12-16 20:02:06 +00:00
|
|
|
box2d<double> extent;
|
2015-10-13 12:29:16 +00:00
|
|
|
shx.read_envelope(extent);
|
2011-11-14 03:54:32 +00:00
|
|
|
|
2015-10-13 12:29:16 +00:00
|
|
|
std::clog << "length=" << file_length << std::endl;
|
|
|
|
std::clog << "version=" << version << std::endl;
|
|
|
|
std::clog << "type=" << shape_type << std::endl;
|
|
|
|
std::clog << "extent:" << extent << std::endl;
|
2011-11-14 03:54:32 +00:00
|
|
|
|
2016-03-07 12:52:58 +00:00
|
|
|
if (!extent.valid() || std::isnan(extent.width()) || std::isnan(extent.height()))
|
|
|
|
{
|
|
|
|
std::clog << "Invalid extent aborting..." << std::endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2015-10-13 12:29:16 +00:00
|
|
|
int pos = 50;
|
|
|
|
shx.seek(pos * 2);
|
2022-01-26 19:41:37 +00:00
|
|
|
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())};
|
2017-08-21 10:02:34 +00:00
|
|
|
|
2022-01-26 19:41:37 +00:00
|
|
|
mapnik::quad_tree<mapnik::detail::node, mapnik::box2d<float>> tree(extent_f, depth, ratio);
|
2015-10-13 12:29:16 +00:00
|
|
|
int count = 0;
|
2011-11-14 03:54:32 +00:00
|
|
|
|
2015-12-14 14:11:38 +00:00
|
|
|
if (shape_type != shape_io::shape_null)
|
2015-10-13 12:29:16 +00:00
|
|
|
{
|
2015-12-14 14:11:38 +00:00
|
|
|
while (shx.is_good() && pos <= file_length - 4)
|
2011-08-12 16:43:12 +00:00
|
|
|
{
|
2015-12-14 14:11:38 +00:00
|
|
|
int offset = shx.read_xdr_integer();
|
|
|
|
int shx_content_length = shx.read_xdr_integer();
|
|
|
|
pos += 4;
|
|
|
|
box2d<double> item_ext;
|
|
|
|
shp.seek(offset * 2);
|
|
|
|
int record_number = shp.read_xdr_integer();
|
|
|
|
int shp_content_length = shp.read_xdr_integer();
|
|
|
|
if (shx_content_length != shp_content_length)
|
|
|
|
{
|
2017-04-25 12:03:18 +00:00
|
|
|
if (verbose)
|
|
|
|
{
|
|
|
|
std::clog << "Content length mismatch for record number " << record_number << std::endl;
|
|
|
|
}
|
2015-12-14 14:11:38 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
shape_type = shp.read_ndr_integer();
|
|
|
|
|
2022-01-26 19:41:37 +00:00
|
|
|
if (shape_type == shape_io::shape_null)
|
|
|
|
continue;
|
2016-02-08 14:45:03 +00:00
|
|
|
|
2022-01-26 19:41:37 +00:00
|
|
|
if (shape_type == shape_io::shape_point || shape_type == shape_io::shape_pointm ||
|
|
|
|
shape_type == shape_io::shape_pointz)
|
2015-12-14 14:11:38 +00:00
|
|
|
{
|
2022-01-26 19:41:37 +00:00
|
|
|
double x = shp.read_double();
|
|
|
|
double y = shp.read_double();
|
|
|
|
item_ext = box2d<double>(x, y, x, y);
|
2015-12-14 14:11:38 +00:00
|
|
|
}
|
2016-01-04 11:43:27 +00:00
|
|
|
else if (index_parts &&
|
2022-01-26 19:41:37 +00:00
|
|
|
(shape_type == shape_io::shape_polygon || shape_type == shape_io::shape_polygonm ||
|
|
|
|
shape_type == shape_io::shape_polygonz || shape_type == shape_io::shape_polyline ||
|
|
|
|
shape_type == shape_io::shape_polylinem || shape_type == shape_io::shape_polylinez))
|
2015-12-14 14:11:38 +00:00
|
|
|
{
|
|
|
|
shp.read_envelope(item_ext);
|
2016-01-04 11:43:27 +00:00
|
|
|
int num_parts = shp.read_ndr_integer();
|
|
|
|
int num_points = shp.read_ndr_integer();
|
|
|
|
std::vector<int> parts;
|
|
|
|
parts.resize(num_parts);
|
2022-01-26 19:41:37 +00:00
|
|
|
std::for_each(parts.begin(), parts.end(), [&](int& part) { part = shp.read_ndr_integer(); });
|
2016-01-04 11:43:27 +00:00
|
|
|
for (int k = 0; k < num_parts; ++k)
|
|
|
|
{
|
|
|
|
int start = parts[k];
|
|
|
|
int end;
|
2022-01-26 19:41:37 +00:00
|
|
|
if (k == num_parts - 1)
|
|
|
|
end = num_points;
|
|
|
|
else
|
|
|
|
end = parts[k + 1];
|
2016-01-04 11:43:27 +00:00
|
|
|
|
|
|
|
mapnik::geometry::linear_ring<double> ring;
|
|
|
|
ring.reserve(end - start);
|
|
|
|
for (int j = start; j < end; ++j)
|
|
|
|
{
|
|
|
|
double x = shp.read_double();
|
|
|
|
double y = shp.read_double();
|
|
|
|
ring.emplace_back(x, y);
|
|
|
|
}
|
|
|
|
item_ext = mapnik::geometry::envelope(ring);
|
|
|
|
if (item_ext.valid())
|
|
|
|
{
|
|
|
|
if (verbose)
|
|
|
|
{
|
|
|
|
std::clog << "record number " << record_number << " box=" << item_ext << std::endl;
|
|
|
|
}
|
2022-01-26 19:41:37 +00:00
|
|
|
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())};
|
2017-08-21 10:02:34 +00:00
|
|
|
tree.insert(mapnik::detail::node(offset * 2, start, end, std::move(ext_f)), ext_f);
|
2016-01-04 11:43:27 +00:00
|
|
|
++count;
|
|
|
|
}
|
|
|
|
}
|
2022-01-26 19:41:37 +00:00
|
|
|
item_ext = mapnik::box2d<double>(); // invalid
|
2015-12-14 14:11:38 +00:00
|
|
|
}
|
2016-01-04 11:43:27 +00:00
|
|
|
else
|
2015-12-14 14:11:38 +00:00
|
|
|
{
|
2016-01-04 11:43:27 +00:00
|
|
|
shp.read_envelope(item_ext);
|
2015-12-14 14:11:38 +00:00
|
|
|
}
|
2016-01-04 11:43:27 +00:00
|
|
|
|
2015-12-14 14:11:38 +00:00
|
|
|
if (item_ext.valid())
|
|
|
|
{
|
2016-01-04 11:43:27 +00:00
|
|
|
if (verbose)
|
|
|
|
{
|
|
|
|
std::clog << "record number " << record_number << " box=" << item_ext << std::endl;
|
|
|
|
}
|
2022-01-26 19:41:37 +00:00
|
|
|
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())};
|
2017-08-21 10:02:34 +00:00
|
|
|
|
|
|
|
tree.insert(mapnik::detail::node(offset * 2, -1, 0, std::move(ext_f)), ext_f);
|
2015-12-14 14:11:38 +00:00
|
|
|
++count;
|
|
|
|
}
|
2015-11-23 09:58:27 +00:00
|
|
|
}
|
2011-11-14 03:54:32 +00:00
|
|
|
}
|
|
|
|
|
2015-11-23 09:58:27 +00:00
|
|
|
if (count > 0)
|
2015-10-13 12:29:16 +00:00
|
|
|
{
|
2015-11-23 09:58:27 +00:00
|
|
|
std::clog << " number shapes=" << count << std::endl;
|
2021-10-21 16:22:53 +00:00
|
|
|
#ifdef _WIN32
|
2022-01-26 19:41:37 +00:00
|
|
|
std::ofstream file(mapnik::utf8_to_utf16(shapename + ".index").c_str(), std::ios::trunc | std::ios::binary);
|
2018-01-20 20:45:30 +00:00
|
|
|
#else
|
2022-01-26 19:41:37 +00:00
|
|
|
std::ofstream file((shapename + ".index").c_str(), std::ios::trunc | std::ios::binary);
|
2018-01-20 20:45:30 +00:00
|
|
|
#endif
|
2015-11-23 09:58:27 +00:00
|
|
|
if (!file)
|
|
|
|
{
|
2022-01-26 19:41:37 +00:00
|
|
|
std::clog << "cannot open index file for writing file \"" << (shapename + ".index") << "\""
|
|
|
|
<< std::endl;
|
2015-11-23 09:58:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tree.trim();
|
|
|
|
std::clog << " number nodes=" << tree.count() << std::endl;
|
|
|
|
file.exceptions(std::ios::failbit | std::ios::badbit);
|
|
|
|
tree.write(file);
|
|
|
|
file.flush();
|
|
|
|
file.close();
|
|
|
|
}
|
2015-10-13 12:29:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-01 13:27:17 +00:00
|
|
|
std::clog << "Failed to read any features from \"" << filename << "\"" << std::endl;
|
|
|
|
return EXIT_FAILURE;
|
2006-05-12 16:35:36 +00:00
|
|
|
}
|
2005-06-14 15:06:59 +00:00
|
|
|
}
|
2011-11-14 03:54:32 +00:00
|
|
|
|
2015-10-13 12:29:16 +00:00
|
|
|
std::clog << "done!" << std::endl;
|
2016-03-07 12:52:58 +00:00
|
|
|
return EXIT_SUCCESS;
|
2005-06-14 15:06:59 +00:00
|
|
|
}
|