2006-03-31 12:32:02 +02:00
|
|
|
/*****************************************************************************
|
2011-11-14 04:54:32 +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.
|
2005-06-14 17:06:59 +02:00
|
|
|
*
|
2006-03-31 12:32:02 +02: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 17:06:59 +02:00
|
|
|
|
|
|
|
|
2005-06-17 14:40:51 +02:00
|
|
|
#include <iostream>
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
2013-06-03 04:28:24 +02:00
|
|
|
#include <mapnik/util/fs.hpp>
|
2015-09-29 13:21:02 +02:00
|
|
|
#include <mapnik/quad_tree.hpp>
|
2008-02-04 12:12:32 +01:00
|
|
|
#include "shapefile.hpp"
|
|
|
|
#include "shape_io.hpp"
|
2006-10-04 13:22:18 +02:00
|
|
|
|
2014-10-22 01:37:27 +02:00
|
|
|
#pragma GCC diagnostic push
|
2015-11-08 02:53:09 +01:00
|
|
|
#include <mapnik/warning_ignore.hpp>
|
2014-10-22 01:37:27 +02:00
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
#include <boost/program_options.hpp>
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
|
2005-06-17 14:40:51 +02:00
|
|
|
const int DEFAULT_DEPTH = 8;
|
2005-06-14 17:06:59 +02:00
|
|
|
const double DEFAULT_RATIO=0.55;
|
|
|
|
|
2011-11-14 04:54:32 +01:00
|
|
|
int main (int argc,char** argv)
|
2005-06-17 14:40:51 +02:00
|
|
|
{
|
|
|
|
using namespace mapnik;
|
|
|
|
namespace po = boost::program_options;
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2005-06-17 14:40:51 +02:00
|
|
|
bool verbose=false;
|
|
|
|
unsigned int depth=DEFAULT_DEPTH;
|
|
|
|
double ratio=DEFAULT_RATIO;
|
2015-10-13 14:29:16 +02:00
|
|
|
std::vector<std::string> shape_files;
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2005-06-17 14:40:51 +02:00
|
|
|
try
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2006-05-12 18:35:36 +02:00
|
|
|
po::options_description desc("shapeindex utility");
|
|
|
|
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)")
|
2006-05-12 18:35:36 +02:00
|
|
|
("ratio,r",po::value<double>(),"split ratio (default 0.55)")
|
2015-10-13 14:29:16 +02:00
|
|
|
("shape_files",po::value<std::vector<std::string> >(),"shape files to index: file1 file2 ...fileN")
|
2006-05-12 18:35:36 +02:00
|
|
|
;
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2006-05-12 18:35:36 +02:00
|
|
|
po::positional_options_description p;
|
|
|
|
p.add("shape_files",-1);
|
2011-11-14 04:54:32 +01:00
|
|
|
po::variables_map vm;
|
2006-05-12 18:35:36 +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
|
|
|
|
2006-05-12 18:35:36 +02:00
|
|
|
if (vm.count("version"))
|
|
|
|
{
|
2015-10-13 14:29:16 +02:00
|
|
|
std::clog << "version 0.3.0" <<std::endl;
|
2006-05-12 18:35:36 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2011-11-14 04:54:32 +01:00
|
|
|
if (vm.count("help"))
|
2006-05-12 18:35:36 +02:00
|
|
|
{
|
2015-10-13 14:29:16 +02:00
|
|
|
std::clog << desc << std::endl;
|
2006-05-12 18:35:36 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2011-11-14 04:54:32 +01:00
|
|
|
if (vm.count("verbose"))
|
2010-11-10 12:55:22 +01:00
|
|
|
{
|
|
|
|
verbose = true;
|
|
|
|
}
|
2006-05-12 18:35:36 +02:00
|
|
|
if (vm.count("depth"))
|
|
|
|
{
|
|
|
|
depth = vm["depth"].as<unsigned int>();
|
|
|
|
}
|
|
|
|
if (vm.count("ratio"))
|
|
|
|
{
|
|
|
|
ratio = vm["ratio"].as<double>();
|
|
|
|
}
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2005-06-17 14:40:51 +02:00
|
|
|
if (vm.count("shape_files"))
|
2006-05-12 18:35:36 +02:00
|
|
|
{
|
2015-10-13 14:29:16 +02:00
|
|
|
shape_files=vm["shape_files"].as< std::vector<std::string> >();
|
2006-05-12 18:35:36 +02:00
|
|
|
}
|
2005-06-17 14:40:51 +02:00
|
|
|
}
|
2014-05-11 21:40:24 +02:00
|
|
|
catch (std::exception const& ex)
|
2005-06-17 14:40:51 +02:00
|
|
|
{
|
2015-10-13 14:29:16 +02:00
|
|
|
std::clog << "Error: " << ex.what() << std::endl;
|
2006-05-12 18:35:36 +02:00
|
|
|
return -1;
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2015-10-13 14:29:16 +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
|
|
|
|
2015-02-19 11:04:13 +01:00
|
|
|
if (shape_files.size() == 0)
|
2005-06-17 14:40:51 +02:00
|
|
|
{
|
2015-10-13 14:29:16 +02:00
|
|
|
std::clog << "no shape files to index" << std::endl;
|
2006-05-12 18:35:36 +02:00
|
|
|
return 0;
|
2005-06-17 14:40:51 +02:00
|
|
|
}
|
2015-02-19 11:04:13 +01:00
|
|
|
for (auto const& filename : shape_files)
|
2005-06-17 14:40:51 +02:00
|
|
|
{
|
2015-10-13 14:29:16 +02:00
|
|
|
std::clog << "processing " << filename << std::endl;
|
2015-02-19 11:04:13 +01:00
|
|
|
std::string shapename (filename);
|
2010-07-17 01:28:53 +02:00
|
|
|
boost::algorithm::ireplace_last(shapename,".shp","");
|
2015-02-19 11:04:13 +01:00
|
|
|
std::string shapename_full (shapename + ".shp");
|
2015-10-13 14:29:16 +02:00
|
|
|
std::string shxname(shapename + ".shx");
|
2013-06-03 04:28:24 +02:00
|
|
|
if (! mapnik::util::exists (shapename_full))
|
2009-04-28 21:54:34 +02:00
|
|
|
{
|
2015-10-13 14:29:16 +02:00
|
|
|
std::clog << "Error : file " << shapename_full << " does not exist" << std::endl;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (! mapnik::util::exists(shxname))
|
|
|
|
{
|
|
|
|
std::clog << "Error : shapefile index file (*.shx) " << shxname << " does not exist" << std::endl;
|
2009-04-28 21:54:34 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
shape_file shp (shapename_full);
|
2008-02-04 12:12:32 +01:00
|
|
|
|
2015-10-13 14:29:16 +02:00
|
|
|
if (! shp.is_open())
|
|
|
|
{
|
|
|
|
std::clog << "Error : cannot open " << shapename_full << std::endl;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
shape_file shx (shxname);
|
|
|
|
if (!shx.is_open())
|
|
|
|
{
|
|
|
|
std::clog << "Error : cannot open " << shxname << std::endl;
|
2006-05-12 18:35:36 +02:00
|
|
|
continue;
|
|
|
|
}
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2015-10-13 14:29:16 +02:00
|
|
|
int code = shx.read_xdr_integer(); //file_code == 9994
|
|
|
|
std::clog << code << std::endl;
|
|
|
|
shx.skip(5*4);
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2015-10-13 14:29:16 +02:00
|
|
|
int file_length=shx.read_xdr_integer();
|
|
|
|
int version=shx.read_ndr_integer();
|
|
|
|
int shape_type=shx.read_ndr_integer();
|
2009-12-16 21:02:06 +01:00
|
|
|
box2d<double> extent;
|
2015-10-13 14:29:16 +02:00
|
|
|
shx.read_envelope(extent);
|
2011-11-14 04:54:32 +01:00
|
|
|
|
|
|
|
|
2015-10-13 14:29:16 +02: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 04:54:32 +01:00
|
|
|
|
2015-10-13 14:29:16 +02:00
|
|
|
int pos = 50;
|
|
|
|
shx.seek(pos * 2);
|
|
|
|
mapnik::quad_tree<int> tree(extent, depth, ratio);
|
|
|
|
int count = 0;
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2015-11-23 11:32:14 +01:00
|
|
|
while (shx.is_good() && pos <= file_length - 4)
|
2015-10-13 14:29:16 +02:00
|
|
|
{
|
|
|
|
int offset = shx.read_xdr_integer();
|
|
|
|
int content_length = shx.read_xdr_integer();
|
|
|
|
pos += 4;
|
2009-12-16 21:02:06 +01:00
|
|
|
box2d<double> item_ext;
|
2015-10-13 14:29:16 +02:00
|
|
|
shp.seek(offset * 2);
|
|
|
|
int record_number = shp.read_xdr_integer();
|
|
|
|
if (content_length != shp.read_xdr_integer())
|
2011-08-12 18:43:12 +02:00
|
|
|
{
|
2015-10-13 14:29:16 +02:00
|
|
|
std::clog << "Content length mismatch for record number " << record_number << std::endl;
|
|
|
|
continue;
|
2011-08-12 18:43:12 +02:00
|
|
|
}
|
2015-10-13 14:29:16 +02:00
|
|
|
shape_type = shp.read_ndr_integer();
|
|
|
|
|
|
|
|
if (shape_type==shape_io::shape_point
|
|
|
|
|| shape_type==shape_io::shape_pointm
|
|
|
|
|| shape_type == shape_io::shape_pointz)
|
2006-05-12 18:35:36 +02:00
|
|
|
{
|
|
|
|
double x=shp.read_double();
|
|
|
|
double y=shp.read_double();
|
2009-12-16 21:02:06 +01:00
|
|
|
item_ext=box2d<double>(x,y,x,y);
|
2015-02-12 22:16:04 +01:00
|
|
|
}
|
2011-11-14 04:54:32 +01:00
|
|
|
else
|
|
|
|
{
|
2006-05-12 18:35:36 +02:00
|
|
|
shp.read_envelope(item_ext);
|
|
|
|
}
|
2015-02-12 22:16:04 +01:00
|
|
|
if (verbose)
|
|
|
|
{
|
2015-10-13 14:29:16 +02:00
|
|
|
std::clog << "record number " << record_number << " box=" << item_ext << std::endl;
|
2006-05-12 18:35:36 +02:00
|
|
|
}
|
2015-11-23 10:58:27 +01:00
|
|
|
if (item_ext.valid())
|
|
|
|
{
|
|
|
|
tree.insert(offset * 2,item_ext);
|
|
|
|
++count;
|
|
|
|
}
|
2011-11-14 04:54:32 +01:00
|
|
|
}
|
|
|
|
|
2015-11-23 10:58:27 +01:00
|
|
|
if (count > 0)
|
2015-10-13 14:29:16 +02:00
|
|
|
{
|
2015-11-23 10:58:27 +01:00
|
|
|
std::clog << " number shapes=" << count << std::endl;
|
|
|
|
std::fstream file((shapename+".index").c_str(),
|
|
|
|
std::ios::in | std::ios::out | std::ios::trunc | std::ios::binary);
|
|
|
|
if (!file)
|
|
|
|
{
|
|
|
|
std::clog << "cannot open index file for writing file \""
|
|
|
|
<< (shapename+".index") << "\"" << std::endl;
|
|
|
|
}
|
|
|
|
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 14:29:16 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-11-23 10:58:27 +01:00
|
|
|
std::clog << "No non-empty geometries in shapefile" << std::endl;
|
2006-05-12 18:35:36 +02:00
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2015-10-13 14:29:16 +02:00
|
|
|
std::clog << "done!" << std::endl;
|
2005-06-14 17:06:59 +02:00
|
|
|
return 0;
|
|
|
|
}
|