2015-10-01 16:16:32 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015 Artem Pavlenko
|
|
|
|
*
|
|
|
|
* 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 <fstream>
|
|
|
|
|
|
|
|
#include <mapnik/util/fs.hpp>
|
|
|
|
#include <mapnik/quad_tree.hpp>
|
2015-10-09 12:27:35 +02:00
|
|
|
|
|
|
|
#include "process_csv_file.hpp"
|
2015-10-09 13:49:58 +02:00
|
|
|
#include "process_geojson_file.hpp"
|
2015-11-08 02:53:09 +01:00
|
|
|
|
2015-10-01 16:16:32 +02:00
|
|
|
#pragma GCC diagnostic push
|
2015-11-08 02:53:09 +01:00
|
|
|
#include <mapnik/warning_ignore.hpp>
|
2015-10-01 16:16:32 +02:00
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
#include <boost/program_options.hpp>
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
|
|
|
|
const int DEFAULT_DEPTH = 8;
|
|
|
|
const double DEFAULT_RATIO = 0.55;
|
|
|
|
|
2015-10-09 12:27:35 +02:00
|
|
|
namespace mapnik { namespace detail {
|
|
|
|
|
|
|
|
bool is_csv(std::string const& filename)
|
|
|
|
{
|
|
|
|
return boost::iends_with(filename,".csv")
|
|
|
|
|| boost::iends_with(filename,".tsv");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_geojson(std::string const& filename)
|
|
|
|
{
|
|
|
|
return boost::iends_with(filename,".geojson")
|
|
|
|
|| boost::iends_with(filename,".json");
|
|
|
|
}
|
|
|
|
|
|
|
|
}}
|
|
|
|
|
2015-10-01 16:16:32 +02:00
|
|
|
int main (int argc, char** argv)
|
|
|
|
{
|
|
|
|
//using namespace mapnik;
|
|
|
|
namespace po = boost::program_options;
|
|
|
|
bool verbose = false;
|
2015-11-12 14:45:25 +01:00
|
|
|
bool validate_features = false;
|
2015-10-01 16:16:32 +02:00
|
|
|
unsigned int depth = DEFAULT_DEPTH;
|
|
|
|
double ratio = DEFAULT_RATIO;
|
2015-10-09 12:27:35 +02:00
|
|
|
std::vector<std::string> files;
|
2015-10-05 17:56:33 +02:00
|
|
|
char separator = 0;
|
|
|
|
char quote = 0;
|
2015-10-01 16:16:32 +02:00
|
|
|
std::string manual_headers;
|
|
|
|
try
|
|
|
|
{
|
2015-10-09 12:27:35 +02:00
|
|
|
po::options_description desc("Mapnik CSV/GeoJSON index utility");
|
2015-10-01 16:16:32 +02:00
|
|
|
desc.add_options()
|
|
|
|
("help,h", "produce usage message")
|
|
|
|
("version,V","print version string")
|
|
|
|
("verbose,v","verbose output")
|
|
|
|
("depth,d", po::value<unsigned int>(), "max tree depth\n(default 8)")
|
|
|
|
("ratio,r",po::value<double>(),"split ratio (default 0.55)")
|
2015-10-05 10:34:02 +02:00
|
|
|
("separator,s", po::value<char>(), "CSV columns separator")
|
|
|
|
("quote,q", po::value<char>(), "CSV columns quote")
|
2015-10-01 16:16:32 +02:00
|
|
|
("manual-headers,H", po::value<std::string>(), "CSV manual headers string")
|
2015-10-09 12:27:35 +02:00
|
|
|
("files",po::value<std::vector<std::string> >(),"Files to index: file1 file2 ...fileN")
|
2015-11-12 14:45:25 +01:00
|
|
|
("validate-features", "Validate GeoJSON features")
|
2015-10-01 16:16:32 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
po::positional_options_description p;
|
2015-10-09 12:27:35 +02:00
|
|
|
p.add("files",-1);
|
2015-10-01 16:16:32 +02:00
|
|
|
po::variables_map vm;
|
|
|
|
po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
|
|
|
|
po::notify(vm);
|
|
|
|
|
|
|
|
if (vm.count("version"))
|
|
|
|
{
|
2015-10-09 12:27:35 +02:00
|
|
|
std::clog << "version 1.0.0" << std::endl;
|
2015-10-01 16:16:32 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (vm.count("help"))
|
|
|
|
{
|
2015-10-09 12:27:35 +02:00
|
|
|
std::clog << desc << std::endl;
|
2015-10-01 16:16:32 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (vm.count("verbose"))
|
|
|
|
{
|
|
|
|
verbose = true;
|
|
|
|
}
|
2015-11-12 14:45:25 +01:00
|
|
|
if (vm.count("validate-features"))
|
|
|
|
{
|
|
|
|
validate_features = true;
|
|
|
|
}
|
2015-10-01 16:16:32 +02:00
|
|
|
if (vm.count("depth"))
|
|
|
|
{
|
|
|
|
depth = vm["depth"].as<unsigned int>();
|
|
|
|
}
|
|
|
|
if (vm.count("ratio"))
|
|
|
|
{
|
|
|
|
ratio = vm["ratio"].as<double>();
|
|
|
|
}
|
|
|
|
if (vm.count("separator"))
|
|
|
|
{
|
2015-10-05 10:34:02 +02:00
|
|
|
separator = vm["separator"].as<char>();
|
2015-10-01 16:16:32 +02:00
|
|
|
}
|
|
|
|
if (vm.count("quote"))
|
|
|
|
{
|
2015-10-05 10:34:02 +02:00
|
|
|
quote = vm["quote"].as<char>();
|
2015-10-01 16:16:32 +02:00
|
|
|
}
|
|
|
|
if (vm.count("manual-headers"))
|
|
|
|
{
|
|
|
|
manual_headers = vm["manual-headers"].as<std::string>();
|
|
|
|
}
|
2015-10-09 12:27:35 +02:00
|
|
|
if (vm.count("files"))
|
2015-10-01 16:16:32 +02:00
|
|
|
{
|
2015-10-09 12:27:35 +02:00
|
|
|
files=vm["files"].as<std::vector<std::string> >();
|
2015-10-01 16:16:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (std::exception const& ex)
|
|
|
|
{
|
2015-10-09 12:27:35 +02:00
|
|
|
std::clog << "Error: " << ex.what() << std::endl;
|
|
|
|
return EXIT_FAILURE;
|
2015-10-01 16:16:32 +02:00
|
|
|
}
|
|
|
|
|
2015-10-20 23:49:58 +02:00
|
|
|
std::vector<std::string> files_to_process;
|
2015-10-01 16:16:32 +02:00
|
|
|
|
2015-10-20 23:49:58 +02:00
|
|
|
for (auto const& filename : files)
|
|
|
|
{
|
|
|
|
if (!mapnik::util::exists(filename))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mapnik::detail::is_csv(filename) || mapnik::detail::is_geojson(filename))
|
|
|
|
{
|
|
|
|
files_to_process.push_back(filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (files_to_process.size() == 0)
|
2015-10-01 16:16:32 +02:00
|
|
|
{
|
2015-10-09 12:27:35 +02:00
|
|
|
std::clog << "no files to index" << std::endl;
|
|
|
|
return EXIT_FAILURE;
|
2015-10-01 16:16:32 +02:00
|
|
|
}
|
|
|
|
|
2015-10-20 23:49:58 +02:00
|
|
|
std::clog << "max tree depth:" << depth << std::endl;
|
|
|
|
std::clog << "split ratio:" << ratio << std::endl;
|
|
|
|
|
2015-10-09 12:27:35 +02:00
|
|
|
using box_type = mapnik::box2d<double>;
|
2015-10-09 13:49:58 +02:00
|
|
|
using item_type = std::pair<box_type, std::pair<std::size_t, std::size_t>>;
|
2015-10-09 12:27:35 +02:00
|
|
|
|
2015-10-20 23:49:58 +02:00
|
|
|
for (auto const& filename : files_to_process)
|
2015-10-01 16:16:32 +02:00
|
|
|
{
|
2015-10-20 21:15:51 +02:00
|
|
|
if (!mapnik::util::exists(filename))
|
2015-10-01 16:16:32 +02:00
|
|
|
{
|
2015-10-09 12:27:35 +02:00
|
|
|
std::clog << "Error : file " << filename << " does not exist" << std::endl;
|
2015-10-01 16:16:32 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-10-09 12:27:35 +02:00
|
|
|
std::vector<item_type> boxes;
|
|
|
|
mapnik::box2d<double> extent;
|
|
|
|
if (mapnik::detail::is_csv(filename))
|
2015-10-01 16:16:32 +02:00
|
|
|
{
|
2015-10-20 21:15:51 +02:00
|
|
|
std::clog << "processing '" << filename << "' as CSV\n";
|
2015-10-09 12:27:35 +02:00
|
|
|
auto result = mapnik::detail::process_csv_file(boxes, filename, manual_headers, separator, quote);
|
|
|
|
if (!result.first) continue;
|
|
|
|
extent = result.second;
|
2015-10-01 16:16:32 +02:00
|
|
|
}
|
2015-10-09 12:27:35 +02:00
|
|
|
else if (mapnik::detail::is_geojson(filename))
|
2015-10-01 16:16:32 +02:00
|
|
|
{
|
2015-10-20 21:15:51 +02:00
|
|
|
std::clog << "processing '" << filename << "' as GeoJSON\n";
|
2015-12-14 12:36:21 +01:00
|
|
|
auto result = mapnik::detail::process_geojson_file(boxes, filename, validate_features, verbose);
|
2015-11-12 17:12:06 +01:00
|
|
|
if (!result.first)
|
|
|
|
{
|
|
|
|
std::clog << "Error: failed to process " << filename << std::endl;
|
|
|
|
continue;
|
|
|
|
}
|
2015-10-09 13:49:58 +02:00
|
|
|
extent = result.second;
|
2015-10-01 16:16:32 +02:00
|
|
|
}
|
|
|
|
|
2015-10-09 12:27:35 +02:00
|
|
|
if (extent.valid())
|
2015-10-01 16:16:32 +02:00
|
|
|
{
|
2015-10-09 12:27:35 +02:00
|
|
|
std::clog << extent << std::endl;
|
|
|
|
mapnik::quad_tree<std::pair<std::size_t, std::size_t>> tree(extent, depth, ratio);
|
|
|
|
for (auto const& item : boxes)
|
2015-10-01 16:16:32 +02:00
|
|
|
{
|
2015-10-09 12:27:35 +02:00
|
|
|
tree.insert(std::get<1>(item), std::get<0>(item));
|
2015-10-01 16:16:32 +02:00
|
|
|
}
|
|
|
|
|
2015-10-09 12:27:35 +02:00
|
|
|
std::fstream file((filename + ".index").c_str(),
|
|
|
|
std::ios::in | std::ios::out | std::ios::trunc | std::ios::binary);
|
|
|
|
if (!file)
|
2015-10-01 16:16:32 +02:00
|
|
|
{
|
2015-10-09 12:27:35 +02:00
|
|
|
std::clog << "cannot open index file for writing file \""
|
|
|
|
<< (filename + ".index") << "\"" << std::endl;
|
2015-10-01 16:16:32 +02:00
|
|
|
}
|
2015-10-09 12:27:35 +02:00
|
|
|
else
|
2015-10-01 16:16:32 +02:00
|
|
|
{
|
2015-10-09 12:27:35 +02:00
|
|
|
tree.trim();
|
|
|
|
std::clog << "number nodes=" << tree.count() << std::endl;
|
2015-10-13 18:12:02 +02:00
|
|
|
std::clog << "number element=" << tree.count_items() << std::endl;
|
2015-10-09 12:27:35 +02:00
|
|
|
file.exceptions(std::ios::failbit | std::ios::badbit);
|
|
|
|
tree.write(file);
|
|
|
|
file.flush();
|
|
|
|
file.close();
|
2015-10-01 16:16:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-09 12:27:35 +02:00
|
|
|
std::clog << "done!" << std::endl;
|
|
|
|
return EXIT_SUCCESS;
|
2015-10-01 16:16:32 +02:00
|
|
|
}
|