polyconvert_main.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // Main for POLYCONVERT
00008 /****************************************************************************/
00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00010 // Copyright 2001-2010 DLR (http://www.dlr.de/) and contributors
00011 /****************************************************************************/
00012 //
00013 //   This program is free software; you can redistribute it and/or modify
00014 //   it under the terms of the GNU General Public License as published by
00015 //   the Free Software Foundation; either version 2 of the License, or
00016 //   (at your option) any later version.
00017 //
00018 /****************************************************************************/
00019 
00020 
00021 // ===========================================================================
00022 // included modules
00023 // ===========================================================================
00024 #ifdef _MSC_VER
00025 #include <windows_config.h>
00026 #else
00027 #include <config.h>
00028 #endif
00029 
00030 #ifdef HAVE_VERSION_H
00031 #include <version.h>
00032 #endif
00033 
00034 #include <iostream>
00035 #include <string>
00036 #include <utils/options/OptionsIO.h>
00037 #include <utils/options/OptionsCont.h>
00038 #include <utils/common/UtilExceptions.h>
00039 #include <utils/common/StringTokenizer.h>
00040 #include <utils/common/SystemFrame.h>
00041 #include <utils/common/MsgHandler.h>
00042 #include <utils/common/TplConvert.h>
00043 #include <utils/common/ToString.h>
00044 #include <utils/importio/LineReader.h>
00045 #include <utils/geom/GeomConvHelper.h>
00046 #include <utils/geom/Boundary.h>
00047 #include <polyconvert/PCLoaderVisum.h>
00048 #include <polyconvert/PCLoaderDlrNavteq.h>
00049 #include <polyconvert/PCLoaderXML.h>
00050 #include <polyconvert/PCLoaderOSM.h>
00051 #include <polyconvert/PCLoaderArcView.h>
00052 #include <polyconvert/PCTypeMap.h>
00053 #include <polyconvert/PCTypeDefHandler.h>
00054 #include <polyconvert/PCNetProjectionLoader.h>
00055 #include <utils/xml/XMLSubSys.h>
00056 #include <utils/geom/GeoConvHelper.h>
00057 
00058 #ifdef CHECK_MEMORY_LEAKS
00059 #include <foreign/nvwa/debug_new.h>
00060 #endif // CHECK_MEMORY_LEAKS
00061 
00062 
00063 // ===========================================================================
00064 // method definitions
00065 // ===========================================================================
00066 void
00067 fillOptions() throw() {
00068     OptionsCont &oc = OptionsCont::getOptions();
00069     oc.addCallExample("-c <CONFIGURATION>");
00070 
00071     // insert options sub-topics
00072     SystemFrame::addConfigurationOptions(oc); // fill this subtopic, too
00073     oc.addOptionSubTopic("Input");
00074     oc.addOptionSubTopic("Output");
00075     GeoConvHelper::addProjectionOptions(oc);
00076     oc.addOptionSubTopic("Pruning");
00077     oc.addOptionSubTopic("Processing");
00078     oc.addOptionSubTopic("Building Defaults");
00079     SystemFrame::addReportOptions(oc); // fill this subtopic, too
00080 
00081 
00082     // register options
00083     // add i/o options
00084     // original network
00085     oc.doRegister("net-file", 'n', new Option_FileName());
00086     oc.addSynonyme("net-file", "net");
00087     oc.addDescription("net-file", "Input", "Loads SUMO-network FILE as reference to offset and projection");
00088 
00089     // dlrnavteq import
00090     oc.doRegister("dlr-navteq-poly-files", new Option_FileName());
00091     oc.addDescription("dlr-navteq-poly-files", "Input", "Reads polygons from FILE assuming they're coded in DLR-Navteq (Elmar)-format");
00092     oc.doRegister("dlr-navteq-poi-files", new Option_FileName());
00093     oc.addDescription("dlr-navteq-poi-files", "Input", "Reads pois from FILE+ assuming they're coded in DLR-Navteq (Elmar)-format");
00094 
00095     // visum import
00096     oc.doRegister("visum-files", new Option_FileName());
00097     oc.addSynonyme("visum-files", "visum");
00098     oc.addDescription("visum-files", "Input", "Reads polygons from FILE assuming it's a Visum-net");
00099 
00100     // xml import
00101     oc.doRegister("xml", new Option_FileName());
00102     oc.addDescription("xml", "Input", "Reads pois from FILE assuming they're coded in XML");
00103 
00104     // osm import
00105     oc.doRegister("osm-files", new Option_FileName());
00106     oc.addSynonyme("osm-files", "osm");
00107     oc.addDescription("osm-files", "Input", "Reads pois from FILE+ assuming they're coded in OSM");
00108     oc.doRegister("osm.keep-full-type", new Option_Bool(false));
00109     oc.addDescription("osm.keep-full-type", "Input", "The type will be made of the key-value - pair.");
00110 
00111     // arcview import
00112     oc.doRegister("shapefile", new Option_FileName());
00113     oc.addSynonyme("shapefile", "shape-files");
00114     oc.addSynonyme("shapefile", "shape");
00115     oc.addDescription("shapefile", "Input", "Reads shapes from shape-files FILE+");
00116     oc.doRegister("shapefile.guess-projection", new Option_Bool(false));
00117     oc.addSynonyme("shapefile.guess-projection", "arcview.guess-projection");
00118     oc.addDescription("shapefile.guess-projection", "Input", "Guesses the shapefile's projection");
00119     oc.doRegister("shapefile.id-name", new Option_FileName());
00120     oc.addDescription("shapefile.id-name", "Input", "Defines where to find the id");
00121 
00122     // typemap reading
00123     oc.doRegister("typemap", new Option_FileName());
00124     oc.addDescription("typemap", "Input", "Reads types from FILE");
00125 
00126 
00127     // output
00128     oc.doRegister("output", 'o', new Option_FileName("polygons.xml"));
00129     oc.addDescription("output", "Output", "Write generated polygons/pois to FILE");
00130 
00131 
00132     // prunning options
00133     oc.doRegister("prune.on-net", new Option_Bool(false));
00134     oc.addDescription("prune.on-net", "Pruning", "Enables pruning on net boundaries");
00135 
00136     oc.doRegister("prune.on-net.offsets", new Option_String("0;0;0;0"));
00137     oc.addDescription("prune.on-net.offsets", "Pruning", "Uses STR as offset definition added to the net boundaries");
00138 
00139     oc.doRegister("prune.boundary", new Option_String());
00140     oc.addDescription("prune.boundary", "Pruning", "Uses STR as pruning boundary");
00141 
00142     oc.doRegister("prune.ignore", new Option_String());
00143     oc.addDescription("prune.ignore", "Pruning", "Items in STR will be kept though out of boundary");
00144 
00145     oc.doRegister("remove", new Option_String(""));
00146     oc.addDescription("remove", "Pruning", "Items with names in STR will be removed");
00147 
00148 
00149     oc.doRegister("x-offset-to-apply", new Option_Float(0));
00150     oc.addDescription("x-offset-to-apply", "Processing", "Adds FLOAT to net x-positions");
00151 
00152     oc.doRegister("y-offset-to-apply", new Option_Float(0));
00153     oc.addDescription("y-offset-to-apply", "Processing", "Adds FLOAT to net y-positions");
00154 
00155 
00156     // building defaults options
00157     oc.doRegister("color", new Option_String("0.2,0.5,1."));
00158     oc.addDescription("color", "Building Defaults", "Sets STR as default color");
00159 
00160     oc.doRegister("prefix", new Option_String(""));
00161     oc.addDescription("prefix", "Building Defaults", "Sets STR as default prefix");
00162 
00163     oc.doRegister("type", new Option_String("unknown"));
00164     oc.addDescription("type", "Building Defaults", "Sets STR as default type");
00165 
00166     oc.doRegister("layer", new Option_Integer(-1));
00167     oc.addDescription("layer", "Building Defaults", "Sets INT as default layer");
00168 }
00169 
00170 
00171 int
00172 main(int argc, char **argv) {
00173     OptionsCont &oc = OptionsCont::getOptions();
00174     oc.setApplicationDescription("Importer of polygons and POIs for the road traffic simulation SUMO.");
00175     oc.setApplicationName("polyconvert", "SUMO polyconvert Version " + (std::string)VERSION_STRING);
00176     int ret = 0;
00177     try {
00178         // initialise subsystems
00179         XMLSubSys::init(false);
00180         fillOptions();
00181         OptionsIO::getOptions(true, argc, argv);
00182         if (oc.processMetaOptions(argc < 2)) {
00183             SystemFrame::close();
00184             return 0;
00185         }
00186         MsgHandler::initOutputOptions();
00187         // build the projection
00188         Boundary origNetBoundary, pruningBoundary;
00189         Position2D netOffset;
00190         std::string proj;
00191         PCNetProjectionLoader::loadIfSet(oc, netOffset, origNetBoundary, pruningBoundary, proj);
00192         if (proj != "") {
00193             if (oc.isDefault("proj")) {
00194                 oc.set("proj", proj);
00195             }
00196             if (oc.isDefault("x-offset-to-apply")) {
00197                 oc.set("x-offset-to-apply", toString(netOffset.x()));
00198             }
00199             if (oc.isDefault("y-offset-to-apply")) {
00200                 oc.set("y-offset-to-apply", toString(netOffset.y()));
00201             }
00202         }
00203 #ifdef HAVE_PROJ
00204         unsigned numProjections = oc.getBool("proj.simple") + oc.getBool("proj.utm") + oc.getBool("proj.dhdn") + (oc.getString("proj").length() > 1);
00205         if ((oc.isSet("osm-files") || oc.isSet("dlr-navteq-poly-files") || oc.isSet("dlr-navteq-poi-files")) && numProjections == 0) {
00206             oc.set("proj.utm", true);
00207         }
00208         if ((oc.isSet("dlr-navteq-poly-files") || oc.isSet("dlr-navteq-poi-files")) && oc.isDefault("proj.shift")) {
00209             oc.set("proj.shift", std::string("5"));
00210         }
00211 #endif
00212         if (!GeoConvHelper::init(oc)) {
00213             throw ProcessError("Could not build projection!");
00214         }
00215 
00216         // check whether the input shall be pruned
00217         bool prune = false;
00218         if (oc.getBool("prune.on-net")) {
00219             if (!oc.isSet("net")) {
00220                 throw ProcessError("In order to prune the input on the net, you have to supply a network.");
00221             }
00222             bool ok = true;
00223             // !!! no proper error handling
00224             Boundary offsets = GeomConvHelper::parseBoundaryReporting(oc.getString("prune.on-net.offsets"), "--prune.on-net.offsets", 0, ok);
00225             pruningBoundary = Boundary(
00226                                   pruningBoundary.xmin()+offsets.xmin(),
00227                                   pruningBoundary.ymin()+offsets.ymin(),
00228                                   pruningBoundary.xmax()+offsets.xmax(),
00229                                   pruningBoundary.ymax()+offsets.ymax());
00230             prune = true;
00231         }
00232         if (oc.isSet("prune.boundary")) {
00233             bool ok = true;
00234             // !!! no proper error handling
00235             pruningBoundary = GeomConvHelper::parseBoundaryReporting(oc.getString("prune.boundary"), "--prune.boundary", 0, ok);
00236             prune = true;
00237         }
00238 
00239         PCPolyContainer toFill(prune, pruningBoundary, oc.getStringVector("remove"));
00240 
00241         // read in the type defaults
00242         PCTypeMap tm;
00243         if (oc.isSet("typemap")) {
00244             PCTypeDefHandler handler(oc, tm);
00245             if (!XMLSubSys::runParser(handler, oc.getString("typemap"))) {
00246                 // something failed
00247                 throw ProcessError();
00248             }
00249         }
00250 
00251         // read in the data
00252         PCLoaderXML::loadIfSet(oc, toFill, tm); // SUMO-XML
00253         PCLoaderOSM::loadIfSet(oc, toFill, tm); // OSM-XML
00254         PCLoaderDlrNavteq::loadIfSet(oc, toFill, tm); // Elmar-files
00255         PCLoaderVisum::loadIfSet(oc, toFill, tm); // VISUM
00256         PCLoaderArcView::loadIfSet(oc, toFill, tm); // shape-files
00257         // check whether any errors occured
00258         if (!MsgHandler::getErrorInstance()->wasInformed()) {
00259             // no? ok, save
00260             toFill.save(oc.getString("output"));
00261         } else {
00262             throw ProcessError();
00263         }
00264     } catch (ProcessError &e) {
00265         if (std::string(e.what())!=std::string("Process Error") && std::string(e.what())!=std::string("")) {
00266             MsgHandler::getErrorInstance()->inform(e.what());
00267         }
00268         MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
00269         ret = 1;
00270 #ifndef _DEBUG
00271     } catch (...) {
00272         MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
00273         ret = 1;
00274 #endif
00275     }
00276     SystemFrame::close();
00277     // report about ending
00278     if (ret==0) {
00279         std::cout << "Success." << std::endl;
00280     }
00281     return ret;
00282 }
00283 
00284 
00285 
00286 /****************************************************************************/
00287 

Generated on Wed May 5 00:06:35 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6