mapnik/utils/shapeindex/shapeindex.cpp

242 lines
7.3 KiB
C++
Raw Normal View History

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
*
2006-03-31 12:32:02 +02:00
* Copyright (C) 2006 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
//$Id: shapeindex.cc 27 2005-03-30 21:45:40Z pavlenko $
#include <iostream>
#include <vector>
#include <string>
2005-06-14 17:06:59 +02:00
#include <boost/tokenizer.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/program_options.hpp>
#include "quadtree.hpp"
#include "shapefile.hpp"
#include "shape_io.hpp"
2005-06-14 17:06:59 +02:00
const int MAXDEPTH = 64;
const int DEFAULT_DEPTH = 8;
2005-06-14 17:06:59 +02:00
const double MINRATIO=0.5;
const double MAXRATIO=0.8;
const double DEFAULT_RATIO=0.55;
int main (int argc,char** argv)
{
using namespace mapnik;
namespace po = boost::program_options;
using std::string;
using std::vector;
using std::clog;
using std::endl;
bool verbose=false;
unsigned int depth=DEFAULT_DEPTH;
double ratio=DEFAULT_RATIO;
vector<string> shape_files;
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")
("depth,d", po::value<unsigned int>(), "max tree depth\n(default 8)")
("ratio,r",po::value<double>(),"split ratio (default 0.55)")
2007-06-05 11:10:40 +02:00
("shape_files",po::value<vector<string> >(),"shape files to index: file1 file2 ...fileN")
2006-05-12 18:35:36 +02:00
;
2010-06-02 13:03:30 +02:00
2006-05-12 18:35:36 +02:00
po::positional_options_description p;
p.add("shape_files",-1);
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
po::notify(vm);
2010-06-02 13:03:30 +02:00
2006-05-12 18:35:36 +02:00
if (vm.count("version"))
{
2007-02-08 01:36:14 +01:00
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
2006-05-12 18:35:36 +02:00
if (vm.count("help"))
{
clog << desc << endl;
2006-05-12 18:35:36 +02:00
return 1;
}
2010-11-10 12:55:22 +01:00
if (vm.count("verbose"))
{
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>();
}
2010-06-02 13:03:30 +02:00
if (vm.count("shape_files"))
2006-05-12 18:35:36 +02:00
{
shape_files=vm["shape_files"].as< vector<string> >();
}
}
catch (...)
{
clog << "Exception of unknown type!" << endl;
2006-05-12 18:35:36 +02:00
return -1;
2005-06-14 17:06:59 +02:00
}
clog << "max tree depth:" << depth << endl;
clog << "split ratio:" << ratio << endl;
2005-06-14 17:06:59 +02:00
vector<string>::const_iterator itr = shape_files.begin();
if (itr == shape_files.end())
{
clog << "no shape files to index" << endl;
2006-05-12 18:35:36 +02:00
return 0;
}
while (itr != shape_files.end())
{
2010-07-22 03:16:48 +02:00
clog << "processing " << *itr << endl;
std::string shapename (*itr++);
boost::algorithm::ireplace_last(shapename,".shp","");
std::string shapename_full (shapename+".shp");
if (! boost::filesystem::exists (shapename_full))
{
clog << "error : file " << shapename_full << " does not exist" << endl;
continue;
}
shape_file shp (shapename_full);
if (! shp.is_open()) {
clog << "error : cannot open " << shapename_full << endl;
2006-05-12 18:35:36 +02:00
continue;
}
int code = shp.read_xdr_integer(); //file_code == 9994
clog << code << endl;
2006-05-12 18:35:36 +02:00
shp.skip(5*4);
2010-06-02 13:03:30 +02:00
2006-05-12 18:35:36 +02:00
int file_length=shp.read_xdr_integer();
int version=shp.read_ndr_integer();
int shape_type=shp.read_ndr_integer();
2009-12-16 21:02:06 +01:00
box2d<double> extent;
2006-05-12 18:35:36 +02:00
shp.read_envelope(extent);
2010-06-02 13:03:30 +02:00
clog << "length=" << file_length << endl;
clog << "version=" << version << endl;
clog << "type=" << shape_type << endl;
clog << "extent:" << extent << endl;
2010-06-02 13:03:30 +02:00
2006-05-12 18:35:36 +02:00
int pos=50;
shp.seek(pos*2);
quadtree<int> tree(extent,depth,ratio);
int count=0;
while (true) {
2010-06-02 13:03:30 +02:00
2006-05-12 18:35:36 +02:00
long offset=shp.pos();
int record_number=shp.read_xdr_integer();
int content_length=shp.read_xdr_integer();
//std::clog << "rec number = "<< record_number << "\n";
//std::clog << "content length = "<< content_length << "\n";
shp.skip(4);
//std::clog << "offset= "<< offset << std::endl;
2010-06-02 13:03:30 +02:00
2009-12-16 21:02:06 +01:00
box2d<double> item_ext;
2006-05-12 18:35:36 +02:00
if (shape_type==shape_io::shape_point)
{
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);
2010-06-02 13:03:30 +02:00
2006-05-12 18:35:36 +02:00
}
else if (shape_type==shape_io::shape_pointm)
{
double x=shp.read_double();
double y=shp.read_double();
2010-01-30 02:02:04 +01:00
// skip m
2006-05-12 18:35:36 +02:00
shp.read_double();
2009-12-16 21:02:06 +01:00
item_ext=box2d<double>(x,y,x,y);
2010-06-02 13:03:30 +02:00
2006-05-12 18:35:36 +02:00
}
else if (shape_type==shape_io::shape_pointz)
{
double x=shp.read_double();
double y=shp.read_double();
2010-01-30 02:02:04 +01:00
// skip z
2006-05-12 18:35:36 +02:00
shp.read_double();
2010-01-30 02:02:04 +01:00
//skip m if exists
if ( content_length == 8 + 36)
{
shp.read_double();
}
2009-12-16 21:02:06 +01:00
item_ext=box2d<double>(x,y,x,y);
2006-05-12 18:35:36 +02:00
}
2010-06-02 13:03:30 +02:00
2006-05-12 18:35:36 +02:00
else
{
shp.read_envelope(item_ext);
shp.skip(2*content_length-4*8-4);
}
tree.insert(offset,item_ext);
if (verbose) {
clog << "record number " << record_number << " box=" << item_ext << endl;
2006-05-12 18:35:36 +02:00
}
2005-06-14 17:06:59 +02:00
2006-05-12 18:35:36 +02:00
pos+=4+content_length;
++count;
2005-06-14 17:06:59 +02:00
2006-05-12 18:35:36 +02:00
if (pos>=file_length) {
break;
}
}
clog << " number shapes=" << count << endl;
2005-06-14 17:06:59 +02:00
2006-05-12 18:35:36 +02:00
std::fstream file((shapename+".index").c_str(),
std::ios::in | std::ios::out | std::ios::trunc | std::ios::binary);
if (!file) {
clog << "cannot open index file for writing file \""
<< (shapename+".index") << "\"" << endl;
2006-05-12 18:35:36 +02:00
} else {
tree.trim();
std::clog<<" number nodes="<<tree.count()<<std::endl;
2008-02-20 10:57:37 +01:00
file.exceptions(std::ios::failbit | std::ios::badbit);
2006-05-12 18:35:36 +02:00
tree.write(file);
2008-02-20 10:34:48 +01:00
file.flush();
2006-05-12 18:35:36 +02:00
file.close();
}
2005-06-14 17:06:59 +02:00
}
clog << "done!" << endl;
2005-06-14 17:06:59 +02:00
return 0;
}