From 2d5a1810daed4c7ea16778a4bd2b6e1080fe4526 Mon Sep 17 00:00:00 2001 From: Lucio Asnaghi Date: Mon, 11 May 2009 22:19:02 +0000 Subject: [PATCH] + added ogrindex utility (needs some scons wizard to add it to main SConstruct) --- utils/ogrindex/Makefile.am | 28 +++++ utils/ogrindex/SConscript | 50 +++++++++ utils/ogrindex/ogrindex.cpp | 198 ++++++++++++++++++++++++++++++++++++ 3 files changed, 276 insertions(+) create mode 100644 utils/ogrindex/Makefile.am create mode 100644 utils/ogrindex/SConscript create mode 100644 utils/ogrindex/ogrindex.cpp diff --git a/utils/ogrindex/Makefile.am b/utils/ogrindex/Makefile.am new file mode 100644 index 000000000..31aa70ccb --- /dev/null +++ b/utils/ogrindex/Makefile.am @@ -0,0 +1,28 @@ +if HAVE_BOOST_PROGRAM_OPTIONS + +bin_PROGRAMS = \ + ogrindex + +ogrindex_SOURCES = \ + quadtree.hpp\ + ogrindex.cpp + +ogrindex_LDFLAGS = \ + $(BOOST_PROGRAM_OPTIONS_LIB) \ + ../../src/libmapnik.la \ + ${AGG_LIBS} + +ogrindex_DEPENDENCIES = \ + ../../src/libmapnik.la + +ogrindex_CXXFLAGS = \ + ${PROFILING_CFLAGS} \ + ${TRACING_CFLAGS} \ + -I../../include \ + -I../../plugins/input/ogr \ + ${AGG_CFLAGS} + +endif + +## File created by the gnome-build tools + diff --git a/utils/ogrindex/SConscript b/utils/ogrindex/SConscript new file mode 100644 index 000000000..fae00df57 --- /dev/null +++ b/utils/ogrindex/SConscript @@ -0,0 +1,50 @@ +# +# This file is part of Mapnik (c++ mapping toolkit) +# +# Copyright (C) 2006 Artem Pavlenko, Jean-Francois Doyon +# +# Mapnik 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 +# +# $Id$ + +import glob + +Import ('env') + +prefix = env['PREFIX'] +install_prefix = env['DESTDIR'] + '/' + prefix + +source = Split( + """ + ogrindex.cpp + """ + ) + +headers = ['#plugins/input/ogr'] + env['CPPPATH'] + +boost_program_options = 'boost_program_options%s' % env['BOOST_APPEND'] +boost_iostreams = 'boost_iostreams%s' % env['BOOST_APPEND'] +boost_filesystem = 'boost_filesystem%s' % env['BOOST_APPEND'] +libraries = [boost_program_options,boost_iostreams,boost_filesystem,env['PLUGINS']['ogr']['lib'],'mapnik'] + +if env['PLATFORM'] == 'Darwin' and env['BOOST_SYSTEM_REQUIRED']: + boost_system = 'boost_system%s' % env['BOOST_APPEND'] + libraries.append(boost_system) + + +ogrindex = env.Program('ogrindex', source, CPPPATH=headers, LIBS=libraries) + +env.Install(install_prefix + '/bin', ogrindex) +env.Alias('install', install_prefix + '/bin') diff --git a/utils/ogrindex/ogrindex.cpp b/utils/ogrindex/ogrindex.cpp new file mode 100644 index 000000000..58f5f104f --- /dev/null +++ b/utils/ogrindex/ogrindex.cpp @@ -0,0 +1,198 @@ +/***************************************************************************** + * + * This file is part of Mapnik (c++ mapping toolkit) + * + * Copyright (C) 2006 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 + * + *****************************************************************************/ +//$Id: ogrindex.cc 27 2005-03-30 21:45:40Z pavlenko $ + + +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "../shapeindex/quadtree.hpp" + +#include "ogr_converter.cpp" +#include "ogr_datasource.cpp" +#include "ogr_featureset.cpp" +#include "ogr_index_featureset.cpp" + +const int MAXDEPTH = 64; +const int DEFAULT_DEPTH = 8; +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 ogr_files; + + try + { + po::options_description desc("ogrindex utility"); + desc.add_options() + ("help,h", "produce usage message") + ("version,V", "print version string") + ("verbose,v", "verbose output") + ("depth,d", po::value(), "max tree depth\n(default 8)") + ("ratio,r", po::value(), "split ratio (default 0.55)") + ("ogr_files", po::value >(), "ogr supported files to index: file1 file2 ...fileN") + ; + + po::positional_options_description p; + p.add("ogr_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")) + { + std::clog<<"version 0.3.0" <(); + } + if (vm.count("ratio")) + { + ratio = vm["ratio"].as(); + } + if (vm.count("ogr_files")) + { + ogr_files=vm["ogr_files"].as< vector >(); + } + } + catch (...) + { + std::clog << "Exception of unknown type!" << std::endl; + return -1; + } + + std::clog << "max tree depth:" << depth << std::endl; + std::clog << "split ratio:" << ratio << std::endl; + + vector::const_iterator itr = ogr_files.begin(); + if (itr == ogr_files.end()) + { + std::clog << "no ogr files to index" << std::endl; + return 0; + } + while (itr != ogr_files.end()) + { + std::clog << "processing " << *itr << std::endl; + + std::string ogrname (*itr++); + + if (! boost::filesystem::exists (ogrname)) + { + std::clog << "error : file " << ogrname << " doesn't exists" << std::endl; + continue; + } + + int breakpoint = ogrname.find_last_of ("."); + if (breakpoint == string::npos) breakpoint = ogrname.length(); + std::string ogrlayername (ogrname.substr(0, breakpoint)); + + if (boost::filesystem::exists (ogrlayername + ".index")) + { + std::clog << "error : " << ogrlayername << ".index file already exists for " << ogrname << std::endl; + continue; + } + + mapnik::parameters params; + params["type"] = "ogr"; + params["file"] = ogrname; + params["layer"] = ogrlayername; + + ogr_datasource ogr (params); + + Envelope extent = ogr.envelope(); + quadtree tree (extent, depth, ratio); + int count=0; + + std::clog << "file:" << ogrname << std::endl; + std::clog << "layer:" << ogrlayername << std::endl; + std::clog << "extent:" << extent << std::endl; + + mapnik::query q (extent, 1.0); + mapnik::featureset_ptr itr = ogr.features (q); + + while (true) + { + mapnik::feature_ptr fp = itr->next(); + if (!fp) + { + break; + } + + Envelope item_ext = fp->envelope(); + + tree.insert (count, item_ext); + if (verbose) { + std::clog << "record number " << (count + 1) << " box=" << item_ext << std::endl; + } + + ++count; + } + + std::clog << " number shapes=" << count << std::endl; + + std::fstream file((ogrlayername+".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 \"" + << (ogrlayername+".index") << "\"" << std::endl; + } else { + tree.trim(); + std::clog<<" number nodes="<