mapnik/util/shapeindex/shapeindex.cpp

201 lines
5 KiB
C++
Raw Normal View History

2005-06-14 17:06:59 +02:00
/* This file is part of Mapnik (c++ mapping toolkit)
* Copyright (C) 2005 Artem Pavlenko
*
* Mapnik is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
//$Id: shapeindex.cc 27 2005-03-30 21:45:40Z pavlenko $
#include "shape.hpp"
#include "quadtree.hpp"
#include <boost/tokenizer.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/program_options.hpp>
2005-06-14 17:06:59 +02:00
#include <iostream>
#include <vector>
#include <string>
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;
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
{
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)")
("shape_files",po::value<vector<string> >(),"shape files to index file1 file2 ...fileN")
;
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);
if (vm.count("version"))
{
cout<<"version 0.2.0a" <<endl;
return 1;
}
2005-06-14 17:06:59 +02:00
if (vm.count("help"))
{
cout << desc << "\n";
return 1;
}
if (vm.count("depth"))
{
depth = vm["depth"].as<unsigned int>();
}
if (vm.count("ratio"))
{
ratio = vm["ratio"].as<double>();
}
if (vm.count("shape_files"))
{
shape_files=vm["shape_files"].as< vector<string> >();
}
}
catch (...)
{
cerr << "Exception of unknown type!"<<endl;
2005-06-14 17:06:59 +02:00
return -1;
}
std::cout<<"max tree depth:"<<depth<<std::endl;
std::cout<<"split ratio:"<<ratio<<std::endl;
2005-06-14 17:06:59 +02:00
vector<string>::const_iterator itr=shape_files.begin();
if (itr==shape_files.end())
{
std::cout << "no shape files to index"<<std::endl;
return 0;
}
while (itr != shape_files.end())
{
std::cout<<"processing "<<*itr << std::endl;
shape_file shp;
std::string shapename(*itr++);
if (!shp.open(shapename+".shp")) {
std::cerr<<"error : cannot open "<< (shapename+".shp") <<"\n";
continue;
}
2005-06-14 17:06:59 +02:00
shp.read_xdr_integer(); //file_code == 9994
shp.skip(5*4);
int file_length=shp.read_xdr_integer();
int version=shp.read_ndr_integer();
int shape_type=shp.read_ndr_integer();
Envelope<double> extent;
shp.read_envelope(extent);
std::cout<<"length="<<file_length<<std::endl;
std::cout<<"version="<<version<<std::endl;
2005-06-14 17:06:59 +02:00
std::cout<<"type="<<shape_type<<std::endl;
std::cout<<"extent:"<<extent<<std::endl;
int pos=50;
shp.seek(pos*2);
quadtree<int> tree(extent,depth,ratio);
int count=0;
while (true) {
int offset=shp.pos();
int record_number=shp.read_xdr_integer();
int content_length=shp.read_xdr_integer();
2005-06-14 17:06:59 +02:00
shp.skip(4);
Envelope<double> item_ext;
if (shape_type==shape_io::shape_point)
{
double x=shp.read_double();
double y=shp.read_double();
shp.skip(2*content_length-2*8-4);
item_ext=Envelope<double>(x,y,x,y);
2005-06-14 17:06:59 +02:00
}
else if (shape_type==shape_io::shape_pointz)
{
double x=shp.read_double();
double y=shp.read_double();
shp.read_double();
shp.skip(2*content_length-2*8-4);
item_ext=Envelope<double>(x,y,x,y);
}
2005-06-14 17:06:59 +02:00
else
{
2005-06-14 17:06:59 +02:00
shp.read_envelope(item_ext);
shp.skip(2*content_length-4*8-4);
2005-06-14 17:06:59 +02:00
}
2005-06-14 17:06:59 +02:00
tree.insert(offset,item_ext);
if (verbose) {
std::cout<<"record number "<<record_number<<" box="<<item_ext<<std::endl;
}
2005-06-14 17:06:59 +02:00
pos+=4+content_length;
++count;
2005-06-14 17:06:59 +02:00
if (pos>=file_length) {
break;
}
}
shp.close();
2005-06-14 17:06:59 +02:00
std::cout<<" number shapes="<<count<<std::endl;
2005-06-14 17:06:59 +02:00
std::fstream file((shapename+".index").c_str(),
std::ios::in | std::ios::out | std::ios::trunc | std::ios::binary);
if (!file) {
std::cerr << "cannot open index file for writing file \""
<<(shapename+".index")<<"\""<<std::endl;
} else {
tree.trim();
std::cout<<" number nodes="<<tree.count()<<std::endl;
tree.write(file);
file.close();
}
2005-06-14 17:06:59 +02:00
}
std::cout<<"done!"<<std::endl;
return 0;
}