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
|
|
|
*
|
2014-11-20 14:25:50 +00:00
|
|
|
* Copyright (C) 2014 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>
|
2013-06-03 02:28:24 +00:00
|
|
|
#include <mapnik/util/fs.hpp>
|
2006-10-04 11:22:18 +00:00
|
|
|
#include "quadtree.hpp"
|
2008-02-04 11:12:32 +00:00
|
|
|
#include "shapefile.hpp"
|
|
|
|
#include "shape_io.hpp"
|
2006-10-04 11:22:18 +00:00
|
|
|
|
2014-10-21 23:37:27 +00:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
|
|
|
#pragma GCC diagnostic ignored "-Wunused-local-typedef"
|
|
|
|
#include <boost/tokenizer.hpp>
|
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
#include <boost/program_options.hpp>
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
|
2005-06-17 12:40:51 +00:00
|
|
|
const int DEFAULT_DEPTH = 8;
|
2005-06-14 15:06:59 +00:00
|
|
|
const double DEFAULT_RATIO=0.55;
|
|
|
|
|
2011-11-14 03:54:32 +00:00
|
|
|
int main (int argc,char** argv)
|
2005-06-17 12:40:51 +00:00
|
|
|
{
|
|
|
|
using namespace mapnik;
|
|
|
|
namespace po = boost::program_options;
|
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
2008-02-04 11:12:32 +00:00
|
|
|
using std::clog;
|
2009-04-28 19:54:34 +00:00
|
|
|
using std::endl;
|
2011-11-14 03:54:32 +00:00
|
|
|
|
2005-06-17 12:40:51 +00:00
|
|
|
bool verbose=false;
|
|
|
|
unsigned int depth=DEFAULT_DEPTH;
|
|
|
|
double ratio=DEFAULT_RATIO;
|
|
|
|
vector<string> shape_files;
|
2011-11-14 03:54:32 +00:00
|
|
|
|
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");
|
|
|
|
desc.add_options()
|
|
|
|
("help,h", "produce usage message")
|
|
|
|
("version,V","print version string")
|
|
|
|
("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)")
|
2007-06-05 09:10:40 +00:00
|
|
|
("shape_files",po::value<vector<string> >(),"shape files to index: file1 file2 ...fileN")
|
2006-05-12 16:35:36 +00:00
|
|
|
;
|
2011-11-14 03:54:32 +00:00
|
|
|
|
2006-05-12 16:35:36 +00:00
|
|
|
po::positional_options_description p;
|
|
|
|
p.add("shape_files",-1);
|
2011-11-14 03:54:32 +00:00
|
|
|
po::variables_map vm;
|
2006-05-12 16:35:36 +00:00
|
|
|
po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
|
|
|
|
po::notify(vm);
|
2011-11-14 03:54:32 +00:00
|
|
|
|
2006-05-12 16:35:36 +00:00
|
|
|
if (vm.count("version"))
|
|
|
|
{
|
2007-02-08 00:36:14 +00:00
|
|
|
clog<<"version 0.3.0" <<std::endl;
|
2006-05-12 16:35:36 +00:00
|
|
|
return 1;
|
|
|
|
}
|
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
|
|
|
{
|
2009-04-28 19:54:34 +00:00
|
|
|
clog << desc << endl;
|
2006-05-12 16:35:36 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2011-11-14 03:54:32 +00:00
|
|
|
if (vm.count("verbose"))
|
2010-11-10 11:55:22 +00:00
|
|
|
{
|
|
|
|
verbose = 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
|
|
|
{
|
|
|
|
shape_files=vm["shape_files"].as< vector<string> >();
|
|
|
|
}
|
2005-06-17 12:40:51 +00:00
|
|
|
}
|
2014-05-11 19:40:24 +00:00
|
|
|
catch (std::exception const& ex)
|
2005-06-17 12:40:51 +00:00
|
|
|
{
|
2014-05-11 19:41:08 +00:00
|
|
|
clog << "Error: " << ex.what() << endl;
|
2006-05-12 16:35:36 +00:00
|
|
|
return -1;
|
2005-06-14 15:06:59 +00:00
|
|
|
}
|
2011-11-14 03:54:32 +00:00
|
|
|
|
2009-04-28 19:54:34 +00:00
|
|
|
clog << "max tree depth:" << depth << endl;
|
|
|
|
clog << "split ratio:" << ratio << endl;
|
2011-11-14 03:54:32 +00:00
|
|
|
|
2015-02-19 10:04:13 +00:00
|
|
|
//vector<string>::const_iterator itr = shape_files.begin();
|
|
|
|
if (shape_files.size() == 0)
|
2005-06-17 12:40:51 +00:00
|
|
|
{
|
2009-04-28 19:54:34 +00:00
|
|
|
clog << "no shape files to index" << endl;
|
2006-05-12 16:35:36 +00:00
|
|
|
return 0;
|
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-02-19 10:04:13 +00:00
|
|
|
clog << "processing " << filename << endl;
|
|
|
|
std::string shapename (filename);
|
2010-07-16 23:28:53 +00:00
|
|
|
boost::algorithm::ireplace_last(shapename,".shp","");
|
2015-02-19 10:04:13 +00:00
|
|
|
std::string shapename_full (shapename + ".shp");
|
2009-04-28 19:54:34 +00:00
|
|
|
|
2013-06-03 02:28:24 +00:00
|
|
|
if (! mapnik::util::exists (shapename_full))
|
2009-04-28 19:54:34 +00:00
|
|
|
{
|
2014-05-11 19:41:08 +00:00
|
|
|
clog << "Error : file " << shapename_full << " does not exist" << endl;
|
2009-04-28 19:54:34 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
shape_file shp (shapename_full);
|
2008-02-04 11:12:32 +00:00
|
|
|
|
2009-04-28 19:54:34 +00:00
|
|
|
if (! shp.is_open()) {
|
2014-05-11 19:41:08 +00:00
|
|
|
clog << "Error : cannot open " << shapename_full << endl;
|
2006-05-12 16:35:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
2011-11-14 03:54:32 +00:00
|
|
|
|
2008-02-04 11:12:32 +00:00
|
|
|
int code = shp.read_xdr_integer(); //file_code == 9994
|
2009-04-28 19:54:34 +00:00
|
|
|
clog << code << endl;
|
2011-11-14 03:54:32 +00:00
|
|
|
shp.skip(5*4);
|
|
|
|
|
2006-05-12 16:35:36 +00:00
|
|
|
int file_length=shp.read_xdr_integer();
|
|
|
|
int version=shp.read_ndr_integer();
|
|
|
|
int shape_type=shp.read_ndr_integer();
|
2009-12-16 20:02:06 +00:00
|
|
|
box2d<double> extent;
|
2006-05-12 16:35:36 +00:00
|
|
|
shp.read_envelope(extent);
|
2011-11-14 03:54:32 +00:00
|
|
|
|
|
|
|
|
2009-04-28 19:54:34 +00:00
|
|
|
clog << "length=" << file_length << endl;
|
|
|
|
clog << "version=" << version << endl;
|
|
|
|
clog << "type=" << shape_type << endl;
|
|
|
|
clog << "extent:" << extent << endl;
|
2011-11-14 03:54:32 +00:00
|
|
|
|
2006-05-12 16:35:36 +00:00
|
|
|
int pos=50;
|
2011-11-14 03:54:32 +00:00
|
|
|
shp.seek(pos*2);
|
2006-05-12 16:35:36 +00:00
|
|
|
quadtree<int> tree(extent,depth,ratio);
|
|
|
|
int count=0;
|
|
|
|
while (true) {
|
2011-11-14 03:54:32 +00:00
|
|
|
|
2006-05-12 16:35:36 +00:00
|
|
|
long offset=shp.pos();
|
|
|
|
int record_number=shp.read_xdr_integer();
|
|
|
|
int content_length=shp.read_xdr_integer();
|
2011-11-14 03:54:32 +00:00
|
|
|
shape_type = shp.read_ndr_integer();
|
2009-12-16 20:02:06 +00:00
|
|
|
box2d<double> item_ext;
|
2011-08-12 16:43:12 +00:00
|
|
|
if (shape_type==shape_io::shape_null)
|
|
|
|
{
|
2015-02-13 08:04:09 +00:00
|
|
|
if (pos >= file_length)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// still need to increment pos, or the pos counter
|
|
|
|
// won't indicate EOF until too late.
|
|
|
|
pos+=4+content_length;
|
|
|
|
continue;
|
|
|
|
}
|
2011-08-12 16:43:12 +00:00
|
|
|
}
|
|
|
|
else if (shape_type==shape_io::shape_point)
|
2006-05-12 16:35:36 +00:00
|
|
|
{
|
|
|
|
double x=shp.read_double();
|
|
|
|
double y=shp.read_double();
|
2009-12-16 20:02:06 +00:00
|
|
|
item_ext=box2d<double>(x,y,x,y);
|
2006-05-12 16:35:36 +00:00
|
|
|
}
|
|
|
|
else if (shape_type==shape_io::shape_pointm)
|
|
|
|
{
|
|
|
|
double x=shp.read_double();
|
|
|
|
double y=shp.read_double();
|
2010-01-30 01:02:04 +00:00
|
|
|
// skip m
|
2006-05-12 16:35:36 +00:00
|
|
|
shp.read_double();
|
2009-12-16 20:02:06 +00:00
|
|
|
item_ext=box2d<double>(x,y,x,y);
|
2006-05-12 16:35:36 +00:00
|
|
|
}
|
|
|
|
else if (shape_type==shape_io::shape_pointz)
|
|
|
|
{
|
|
|
|
double x=shp.read_double();
|
|
|
|
double y=shp.read_double();
|
2010-01-30 01:02:04 +00:00
|
|
|
// skip z
|
2006-05-12 16:35:36 +00:00
|
|
|
shp.read_double();
|
2015-02-12 21:16:04 +00:00
|
|
|
// According to ESRI shapefile doc
|
|
|
|
// A PointZ consists of a triplet of double-precision coordinates in the order X, Y, Z plus a
|
|
|
|
// measure.
|
|
|
|
// PointZ
|
|
|
|
// {
|
|
|
|
// Double X // X coordinate
|
|
|
|
// Double Y // Y coordinate
|
|
|
|
// Double Z // Z coordinate
|
|
|
|
// Double M // Measure
|
|
|
|
// }
|
2015-02-19 10:04:13 +00:00
|
|
|
// But OGR creates shapefiles with M missing so we need to skip M only if present
|
2015-02-12 21:16:04 +00:00
|
|
|
// NOTE: content_length is in 16-bit words
|
|
|
|
if ( content_length == 18)
|
2010-01-30 01:02:04 +00:00
|
|
|
{
|
|
|
|
shp.read_double();
|
|
|
|
}
|
2009-12-16 20:02:06 +00:00
|
|
|
item_ext=box2d<double>(x,y,x,y);
|
2015-02-12 21:16:04 +00:00
|
|
|
}
|
2011-11-14 03:54:32 +00:00
|
|
|
else
|
|
|
|
{
|
2006-05-12 16:35:36 +00:00
|
|
|
shp.read_envelope(item_ext);
|
|
|
|
shp.skip(2*content_length-4*8-4);
|
|
|
|
}
|
|
|
|
tree.insert(offset,item_ext);
|
2015-02-12 21:16:04 +00:00
|
|
|
if (verbose)
|
|
|
|
{
|
2009-04-28 19:54:34 +00:00
|
|
|
clog << "record number " << record_number << " box=" << item_ext << endl;
|
2006-05-12 16:35:36 +00:00
|
|
|
}
|
2005-06-14 15:06:59 +00:00
|
|
|
|
2006-05-12 16:35:36 +00:00
|
|
|
pos+=4+content_length;
|
|
|
|
++count;
|
2005-06-14 15:06:59 +00:00
|
|
|
|
2015-02-12 21:16:04 +00:00
|
|
|
if (pos >= file_length) break;
|
2011-11-14 03:54:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
clog << " number shapes=" << count << endl;
|
|
|
|
|
2006-05-12 16:35:36 +00:00
|
|
|
std::fstream file((shapename+".index").c_str(),
|
|
|
|
std::ios::in | std::ios::out | std::ios::trunc | std::ios::binary);
|
|
|
|
if (!file) {
|
2011-11-14 03:54:32 +00:00
|
|
|
clog << "cannot open index file for writing file \""
|
2009-04-28 19:54:34 +00:00
|
|
|
<< (shapename+".index") << "\"" << endl;
|
2006-05-12 16:35:36 +00:00
|
|
|
} else {
|
|
|
|
tree.trim();
|
|
|
|
std::clog<<" number nodes="<<tree.count()<<std::endl;
|
2008-02-20 09:57:37 +00:00
|
|
|
file.exceptions(std::ios::failbit | std::ios::badbit);
|
2006-05-12 16:35:36 +00:00
|
|
|
tree.write(file);
|
2008-02-20 09:34:48 +00:00
|
|
|
file.flush();
|
2006-05-12 16:35:36 +00:00
|
|
|
file.close();
|
|
|
|
}
|
2005-06-14 15:06:59 +00:00
|
|
|
}
|
2011-11-14 03:54:32 +00:00
|
|
|
|
2009-04-28 19:54:34 +00:00
|
|
|
clog << "done!" << endl;
|
2005-06-14 15:06:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|