2009-05-12 00:19:02 +02:00
|
|
|
/*****************************************************************************
|
2011-11-14 04:54:32 +01:00
|
|
|
*
|
2009-05-12 00:19:02 +02:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2021-01-05 15:39:07 +01:00
|
|
|
* Copyright (C) 2021 Artem Pavlenko
|
2009-05-12 00:19: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,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
#include <boost/program_options.hpp>
|
|
|
|
|
|
|
|
#include <mapnik/datasource.hpp>
|
2017-01-26 09:51:37 +01:00
|
|
|
#include <mapnik/geometry/box2d.hpp>
|
2009-05-12 00:19:02 +02:00
|
|
|
#include <mapnik/feature.hpp>
|
2013-06-03 04:28:24 +02:00
|
|
|
#include <mapnik/util/fs.hpp>
|
2009-05-12 00:19:02 +02:00
|
|
|
#include <mapnik/feature_layer_desc.hpp>
|
|
|
|
|
|
|
|
#include "../shapeindex/quadtree.hpp"
|
|
|
|
|
|
|
|
#include "ogr_converter.cpp"
|
|
|
|
#include "ogr_datasource.cpp"
|
|
|
|
#include "ogr_featureset.cpp"
|
|
|
|
#include "ogr_index_featureset.cpp"
|
|
|
|
|
2010-08-20 23:20:55 +02:00
|
|
|
using mapnik::datasource_exception;
|
|
|
|
|
2009-05-12 00:19:02 +02:00
|
|
|
const int MAXDEPTH = 64;
|
|
|
|
const int DEFAULT_DEPTH = 8;
|
2022-01-26 20:41:37 +01:00
|
|
|
const double MINRATIO = 0.5;
|
|
|
|
const double MAXRATIO = 0.8;
|
|
|
|
const double DEFAULT_RATIO = 0.55;
|
2009-05-12 00:19:02 +02:00
|
|
|
|
2022-01-26 20:41:37 +01:00
|
|
|
int main(int argc, char** argv)
|
2009-05-12 00:19:02 +02:00
|
|
|
{
|
|
|
|
using namespace mapnik;
|
|
|
|
namespace po = boost::program_options;
|
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2022-01-26 20:41:37 +01:00
|
|
|
bool verbose = false;
|
|
|
|
unsigned int depth = DEFAULT_DEPTH;
|
|
|
|
double ratio = DEFAULT_RATIO;
|
2009-05-12 00:19:02 +02:00
|
|
|
vector<string> ogr_files;
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2009-05-12 00:19:02 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
po::options_description desc("ogrindex utility");
|
2022-01-26 20:41:37 +01:00
|
|
|
// clang-format off
|
2009-05-12 00:19:02 +02:00
|
|
|
desc.add_options()
|
|
|
|
("help,h", "produce usage message")
|
|
|
|
("version,V", "print version string")
|
|
|
|
("verbose,v", "verbose output")
|
2011-11-14 04:54:32 +01:00
|
|
|
("depth,d", po::value<unsigned int>(), "max tree depth\n(default 8)")
|
2009-05-12 00:19:02 +02:00
|
|
|
("ratio,r", po::value<double>(), "split ratio (default 0.55)")
|
|
|
|
("ogr_files", po::value<vector<string> >(), "ogr supported files to index: file1 file2 ...fileN")
|
|
|
|
;
|
2022-01-26 20:41:37 +01:00
|
|
|
// clang-format on
|
2009-05-12 00:19:02 +02:00
|
|
|
po::positional_options_description p;
|
2022-01-26 20:41:37 +01:00
|
|
|
p.add("ogr_files", -1);
|
2011-11-14 04:54:32 +01:00
|
|
|
po::variables_map vm;
|
2009-05-12 00:19:02 +02:00
|
|
|
po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
|
|
|
|
po::notify(vm);
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2009-05-12 00:19:02 +02:00
|
|
|
if (vm.count("version"))
|
|
|
|
{
|
2022-01-26 20:41:37 +01:00
|
|
|
std::clog << "version 0.1.0" << std::endl;
|
2009-05-12 00:19:02 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2011-11-14 04:54:32 +01:00
|
|
|
if (vm.count("help"))
|
2009-05-12 00:19:02 +02:00
|
|
|
{
|
|
|
|
std::clog << desc << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (vm.count("depth"))
|
|
|
|
{
|
|
|
|
depth = vm["depth"].as<unsigned int>();
|
|
|
|
}
|
|
|
|
if (vm.count("ratio"))
|
|
|
|
{
|
|
|
|
ratio = vm["ratio"].as<double>();
|
|
|
|
}
|
|
|
|
if (vm.count("ogr_files"))
|
|
|
|
{
|
2022-01-26 20:41:37 +01:00
|
|
|
ogr_files = vm["ogr_files"].as<vector<string>>();
|
2009-05-12 00:19:02 +02:00
|
|
|
}
|
2022-01-26 20:41:37 +01:00
|
|
|
} catch (...)
|
2009-05-12 00:19:02 +02:00
|
|
|
{
|
|
|
|
std::clog << "Exception of unknown type!" << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2009-05-12 00:19:02 +02:00
|
|
|
std::clog << "max tree depth:" << depth << std::endl;
|
|
|
|
std::clog << "split ratio:" << ratio << std::endl;
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2009-05-12 00:19:02 +02:00
|
|
|
vector<string>::const_iterator itr = ogr_files.begin();
|
|
|
|
if (itr == ogr_files.end())
|
|
|
|
{
|
|
|
|
std::clog << "no ogr files to index" << std::endl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
while (itr != ogr_files.end())
|
|
|
|
{
|
|
|
|
std::clog << "processing " << *itr << std::endl;
|
|
|
|
|
2022-01-26 20:41:37 +01:00
|
|
|
std::string ogrname(*itr++);
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2022-01-26 20:41:37 +01:00
|
|
|
if (!mapnik::util::exists(ogrname))
|
2009-05-12 00:19:02 +02:00
|
|
|
{
|
|
|
|
std::clog << "error : file " << ogrname << " doesn't exists" << std::endl;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-08-20 23:20:55 +02:00
|
|
|
// TODO - layer names don't match dataset name, so this will break for
|
|
|
|
// any layer types of ogr than shapefiles, etc
|
2022-01-26 20:41:37 +01:00
|
|
|
size_t breakpoint = ogrname.find_last_of(".");
|
|
|
|
if (breakpoint == string::npos)
|
|
|
|
breakpoint = ogrname.length();
|
|
|
|
std::string ogrlayername(ogrname.substr(0, breakpoint));
|
2009-05-12 00:19:02 +02:00
|
|
|
|
2022-01-26 20:41:37 +01:00
|
|
|
if (mapnik::util::exists(ogrlayername + ".ogrindex"))
|
2009-05-12 00:19:02 +02:00
|
|
|
{
|
2010-08-20 23:20:55 +02:00
|
|
|
std::clog << "error : " << ogrlayername << ".ogrindex file already exists for " << ogrname << std::endl;
|
2009-05-12 00:19:02 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
mapnik::parameters params;
|
|
|
|
params["type"] = "ogr";
|
|
|
|
params["file"] = ogrname;
|
2022-01-26 20:41:37 +01:00
|
|
|
// unsigned first = 0;
|
|
|
|
params["layer_by_index"] = 0; // ogrlayername;
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2010-08-20 23:20:55 +02:00
|
|
|
try
|
|
|
|
{
|
2022-01-26 20:41:37 +01:00
|
|
|
ogr_datasource ogr(params);
|
2009-05-12 00:19:02 +02:00
|
|
|
|
2010-08-20 23:20:55 +02:00
|
|
|
box2d<double> extent = ogr.envelope();
|
2022-01-26 20:41:37 +01:00
|
|
|
quadtree<int> tree(extent, depth, ratio);
|
|
|
|
int count = 0;
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2010-08-20 23:20:55 +02:00
|
|
|
std::clog << "file:" << ogrname << std::endl;
|
|
|
|
std::clog << "layer:" << ogrlayername << std::endl;
|
|
|
|
std::clog << "extent:" << extent << std::endl;
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2022-01-26 20:41:37 +01:00
|
|
|
mapnik::query q(extent, 1.0);
|
|
|
|
mapnik::featureset_ptr itr = ogr.features(q);
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2010-08-20 23:20:55 +02:00
|
|
|
while (true)
|
2009-05-12 00:19:02 +02:00
|
|
|
{
|
2010-08-20 23:20:55 +02:00
|
|
|
mapnik::feature_ptr fp = itr->next();
|
|
|
|
if (!fp)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2010-08-20 23:20:55 +02:00
|
|
|
box2d<double> item_ext = fp->envelope();
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2022-01-26 20:41:37 +01:00
|
|
|
tree.insert(count, item_ext);
|
|
|
|
if (verbose)
|
|
|
|
{
|
2010-08-20 23:20:55 +02:00
|
|
|
std::clog << "record number " << (count + 1) << " box=" << item_ext << std::endl;
|
|
|
|
}
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2010-08-20 23:20:55 +02:00
|
|
|
++count;
|
2009-05-12 00:19:02 +02:00
|
|
|
}
|
2011-11-14 04:54:32 +01:00
|
|
|
|
|
|
|
std::clog << " number shapes=" << count << std::endl;
|
|
|
|
|
2022-01-26 20:41:37 +01:00
|
|
|
std::fstream file((ogrlayername + ".ogrindex").c_str(),
|
2010-08-20 23:20:55 +02:00
|
|
|
std::ios::in | std::ios::out | std::ios::trunc | std::ios::binary);
|
2022-01-26 20:41:37 +01:00
|
|
|
if (!file)
|
|
|
|
{
|
|
|
|
std::clog << "cannot open ogrindex file for writing file \"" << (ogrlayername + ".ogrindex") << "\""
|
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-08-20 23:20:55 +02:00
|
|
|
tree.trim();
|
2022-01-26 20:41:37 +01:00
|
|
|
std::clog << " number nodes=" << tree.count() << std::endl;
|
2010-08-20 23:20:55 +02:00
|
|
|
file.exceptions(std::ios::failbit | std::ios::badbit);
|
|
|
|
tree.write(file);
|
|
|
|
file.flush();
|
|
|
|
file.close();
|
2009-05-12 00:19:02 +02:00
|
|
|
}
|
2010-08-20 23:20:55 +02:00
|
|
|
}
|
|
|
|
// catch problem at the datasource creation
|
2022-01-26 20:41:37 +01:00
|
|
|
catch (const mapnik::datasource_exception& ex)
|
2010-08-20 23:20:55 +02:00
|
|
|
{
|
|
|
|
std::clog << ex.what() << "\n";
|
|
|
|
return -1;
|
2009-05-12 00:19:02 +02:00
|
|
|
}
|
|
|
|
|
2010-08-20 23:20:55 +02:00
|
|
|
catch (...)
|
|
|
|
{
|
2022-01-26 20:41:37 +01:00
|
|
|
std::clog << "unknown exception..."
|
|
|
|
<< "\n";
|
2010-08-20 23:20:55 +02:00
|
|
|
return -1;
|
2009-05-12 00:19:02 +02:00
|
|
|
}
|
|
|
|
}
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2009-05-12 00:19:02 +02:00
|
|
|
std::clog << "done!" << std::endl;
|
|
|
|
return 0;
|
|
|
|
}
|