LCOV - code coverage report
Current view: top level - src/polyconvert - polyconvert_main.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 90.3 % 216 195
Test Date: 2024-12-21 15:45:41 Functions: 100.0 % 2 2

            Line data    Source code
       1              : /****************************************************************************/
       2              : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3              : // Copyright (C) 2005-2024 German Aerospace Center (DLR) and others.
       4              : // This program and the accompanying materials are made available under the
       5              : // terms of the Eclipse Public License 2.0 which is available at
       6              : // https://www.eclipse.org/legal/epl-2.0/
       7              : // This Source Code may also be made available under the following Secondary
       8              : // Licenses when the conditions for such availability set forth in the Eclipse
       9              : // Public License 2.0 are satisfied: GNU General Public License, version 2
      10              : // or later which is available at
      11              : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
      12              : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
      13              : /****************************************************************************/
      14              : /// @file    polyconvert_main.cpp
      15              : /// @author  Daniel Krajzewicz
      16              : /// @author  Jakob Erdmann
      17              : /// @author  Christoph Sommer
      18              : /// @author  Michael Behrisch
      19              : /// @author  Melanie Knocke
      20              : /// @date    Mon, 05 Dec 2005
      21              : ///
      22              : // Main for POLYCONVERT
      23              : /****************************************************************************/
      24              : #include <config.h>
      25              : 
      26              : #ifdef HAVE_VERSION_H
      27              : #include <version.h>
      28              : #endif
      29              : 
      30              : #include <iostream>
      31              : #include <string>
      32              : #include <utils/options/OptionsIO.h>
      33              : #include <utils/options/OptionsCont.h>
      34              : #include <utils/common/UtilExceptions.h>
      35              : #include <utils/common/StringTokenizer.h>
      36              : #include <utils/common/SystemFrame.h>
      37              : #include <utils/common/MsgHandler.h>
      38              : #include <utils/common/StringUtils.h>
      39              : #include <utils/common/ToString.h>
      40              : #include <utils/iodevices/OutputDevice.h>
      41              : #include <utils/importio/LineReader.h>
      42              : #include <utils/geom/GeoConvHelper.h>
      43              : #include <utils/geom/GeomConvHelper.h>
      44              : #include <utils/geom/Boundary.h>
      45              : #include <utils/xml/SUMOSAXReader.h>
      46              : #include <utils/xml/XMLSubSys.h>
      47              : #include <polyconvert/PCLoaderVisum.h>
      48              : #include <polyconvert/PCLoaderDlrNavteq.h>
      49              : #include <polyconvert/PCLoaderXML.h>
      50              : #include <polyconvert/PCLoaderOSM.h>
      51              : #include <polyconvert/PCLoaderArcView.h>
      52              : #include <polyconvert/PCTypeMap.h>
      53              : #include <polyconvert/PCTypeDefHandler.h>
      54              : #include <polyconvert/PCNetProjectionLoader.h>
      55              : #include <polyconvert/pc_typemap.h>
      56              : 
      57              : 
      58              : // ===========================================================================
      59              : // method definitions
      60              : // ===========================================================================
      61              : void
      62           49 : fillOptions() {
      63           49 :     OptionsCont& oc = OptionsCont::getOptions();
      64           98 :     oc.addCallExample("-c <CONFIGURATION>", "run with configuration options set in file");
      65              : 
      66              :     // insert options sub-topics
      67           49 :     SystemFrame::addConfigurationOptions(oc); // fill this subtopic, too
      68           49 :     oc.addOptionSubTopic("Input");
      69           49 :     oc.addOptionSubTopic("Output");
      70           49 :     GeoConvHelper::addProjectionOptions(oc);
      71           49 :     oc.addOptionSubTopic("Pruning");
      72           49 :     oc.addOptionSubTopic("Processing");
      73           49 :     oc.addOptionSubTopic("Building Defaults");
      74              : 
      75              : 
      76              :     // register options
      77              :     // add i/o options
      78              :     // original network
      79           49 :     oc.doRegister("net-file", 'n', new Option_FileName());
      80           98 :     oc.addSynonyme("net-file", "net");
      81           98 :     oc.addDescription("net-file", "Input", TL("Loads SUMO-network FILE as reference to offset and projection"));
      82              : 
      83              :     // dlrnavteq import
      84           49 :     oc.doRegister("dlr-navteq-poly-files", new Option_FileName());
      85           98 :     oc.addDescription("dlr-navteq-poly-files", "Input", TL("Reads polygons from FILE assuming they're coded in DLR-Navteq (Elmar)-format"));
      86           49 :     oc.doRegister("dlr-navteq-poi-files", new Option_FileName());
      87           98 :     oc.addDescription("dlr-navteq-poi-files", "Input", TL("Reads pois from FILE assuming they're coded in DLR-Navteq (Elmar)-format"));
      88              : 
      89              :     // visum import
      90           49 :     oc.doRegister("visum-files", new Option_FileName());
      91           98 :     oc.addSynonyme("visum-files", "visum");
      92           98 :     oc.addDescription("visum-files", "Input", TL("Reads polygons from FILE assuming it's a Visum-net"));
      93              : 
      94           49 :     oc.doRegister("visum.language-file", new Option_FileName());
      95           98 :     oc.addDescription("visum.language-file", "Input", TL("Load language mappings from FILE"));
      96              : 
      97              :     // xml import
      98           49 :     oc.doRegister("xml-files", new Option_FileName());
      99           98 :     oc.addSynonyme("xml-files", "xml");
     100           98 :     oc.addDescription("xml-files", "Input", TL("Reads pois and shapes from FILE assuming they're coded in XML"));
     101              : 
     102              :     // osm import
     103           49 :     oc.doRegister("osm-files", new Option_FileName());
     104           98 :     oc.addSynonyme("osm-files", "osm");
     105           98 :     oc.addDescription("osm-files", "Input", TL("Reads pois from FILE assuming they're coded in OSM"));
     106           49 :     oc.doRegister("osm.keep-full-type", new Option_Bool(false));
     107           98 :     oc.addDescription("osm.keep-full-type", "Input", TL("The type will be made of the key-value - pair"));
     108           49 :     oc.doRegister("osm.use-name", new Option_Bool(false));
     109           98 :     oc.addDescription("osm.use-name", "Input", TL("The id will be set from the given 'name' attribute"));
     110           49 :     oc.doRegister("osm.merge-relations", new Option_Float(-1));
     111           98 :     oc.addDescription("osm.merge-relations", "Input", TL("If FLOAT >= 0, assemble one polygon from all ways of a relation if they all connect with gaps below FLOAT"));
     112              : 
     113              :     // arcview import
     114           49 :     oc.doRegister("shapefile-prefixes", new Option_FileName());
     115           98 :     oc.addSynonyme("shapefile-prefixes", "shapefile-prefix");
     116           98 :     oc.addSynonyme("shapefile-prefixes", "shapefile");
     117           98 :     oc.addSynonyme("shapefile-prefixes", "shape-files", true);
     118           98 :     oc.addDescription("shapefile-prefixes", "Input", TL("Reads shapes from shapefiles FILE"));
     119              : 
     120           49 :     oc.doRegister("shapefile.guess-projection", new Option_Bool(false));
     121           98 :     oc.addSynonyme("shapefile.guess-projection", "arcview.guess-projection", true);
     122           98 :     oc.addDescription("shapefile.guess-projection", "Input", TL("Guesses the shapefile's projection"));
     123              : 
     124           49 :     oc.doRegister("shapefile.traditional-axis-mapping", new Option_Bool(false));
     125           98 :     oc.addDescription("shapefile.traditional-axis-mapping", "Input", TL("Use traditional axis order (lon, lat)"));
     126              : 
     127           49 :     oc.doRegister("shapefile.id-column", new Option_String());
     128           98 :     oc.addSynonyme("shapefile.id-column", "shapefile.id-name", true);
     129           98 :     oc.addSynonyme("shapefile.id-column", "shape-files.id-name", true);
     130           98 :     oc.addDescription("shapefile.id-column", "Input", TL("Defines in which column the id can be found"));
     131              : 
     132           49 :     oc.doRegister("shapefile.type-columns", new Option_StringVector());
     133           98 :     oc.addSynonyme("shapefile.type-columns", "shapefile.type-column");
     134           98 :     oc.addDescription("shapefile.type-columns", "Input", TL("Defines which columns form the type id (comma separated list)"));
     135              : 
     136           49 :     oc.doRegister("shapefile.use-running-id", new Option_Bool(false));
     137           98 :     oc.addDescription("shapefile.use-running-id", "Input", TL("A running number will be used as id"));
     138              : 
     139           49 :     oc.doRegister("shapefile.add-param", new Option_Bool(false));
     140           98 :     oc.addDescription("shapefile.add-param", "Input", TL("Extract all additional columns as params"));
     141              : 
     142           49 :     oc.doRegister("shapefile.fill", new Option_String());
     143           98 :     oc.addDescription("shapefile.fill", "Input", TL("[auto|true|false]. Forces the 'fill' status to the given value. Default 'auto' tries to determine it from the data type"));
     144              : 
     145              :     // typemap reading
     146           49 :     oc.doRegister("type-file", new Option_FileName());
     147           98 :     oc.addSynonyme("type-file", "typemap", true);
     148           98 :     oc.addDescription("type-file", "Input", TL("Reads types from FILE"));
     149              : 
     150              :     // need to do this here to be able to check for network and route input options
     151           49 :     SystemFrame::addReportOptions(oc);
     152              : 
     153              :     // output
     154           49 :     oc.doRegister("output-file", 'o', new Option_FileName());
     155           98 :     oc.addSynonyme("output-file", "output");
     156           98 :     oc.addDescription("output-file", "Output", TL("Write generated polygons/pois to FILE"));
     157              : 
     158           49 :     oc.doRegister("dlr-tdp-output", new Option_FileName());
     159           98 :     oc.addDescription("dlr-tdp-output", "Output", TL("Write generated polygons/pois to a dlr-tdp file with the given prefix"));
     160              : 
     161              : 
     162              :     // pruning options
     163           49 :     oc.doRegister("prune.in-net", new Option_Bool(false));
     164           98 :     oc.addSynonyme("prune.in-net", "prune.on-net", true);
     165           98 :     oc.addDescription("prune.in-net", TL("Pruning"), TL("Enables pruning on net boundaries"));
     166              : 
     167           98 :     oc.doRegister("prune.in-net.offsets", new Option_String("0,0,0,0"));
     168           98 :     oc.addSynonyme("prune.in-net.offsets", "prune.on-net.offsets", true);
     169           98 :     oc.addDescription("prune.in-net.offsets", TL("Pruning"), TL("Uses FLOAT,FLOAT,FLOAT,FLOAT as offset definition added to the net boundary. Positive values grow the boundary on all sides while negative values shrink it."));
     170              : 
     171           49 :     oc.doRegister("prune.boundary", new Option_String());
     172           98 :     oc.addDescription("prune.boundary", TL("Pruning"), TL("Uses STR as pruning boundary"));
     173              : 
     174           49 :     oc.doRegister("prune.keep-list", new Option_String());
     175           98 :     oc.addSynonyme("prune.keep-list", "prune.keep");
     176           98 :     oc.addSynonyme("prune.keep-list", "prune.ignore", true);
     177           98 :     oc.addDescription("prune.keep-list", TL("Pruning"), TL("Items in STR will be kept though out of boundary"));
     178              : 
     179          196 :     oc.doRegister("prune.explicit", new Option_StringVector(StringVector({ "" })));
     180           98 :     oc.addSynonyme("prune.explicit", "remove");
     181           98 :     oc.addDescription("prune.explicit", TL("Pruning"), TL("Items with names in STR[] will be removed"));
     182              : 
     183              : 
     184           49 :     oc.doRegister("offset.x", new Option_Float(0));
     185           98 :     oc.addSynonyme("offset.x", "x-offset-to-apply", true);
     186           98 :     oc.addDescription("offset.x", "Processing", TL("Adds FLOAT to net x-positions"));
     187              : 
     188           49 :     oc.doRegister("offset.y", new Option_Float(0));
     189           98 :     oc.addSynonyme("offset.y", "y-offset-to-apply", true);
     190           98 :     oc.addDescription("offset.y", "Processing", TL("Adds FLOAT to net y-positions"));
     191              : 
     192           49 :     oc.doRegister("offset.z", new Option_Float(0));
     193           98 :     oc.addDescription("offset.z", "Processing", TL("Adds FLOAT to net z-positions"));
     194              : 
     195           49 :     oc.doRegister("all-attributes", new Option_Bool(false));
     196           98 :     oc.addDescription("all-attributes", "Processing", TL("Imports all attributes as key/value pairs"));
     197              : 
     198           49 :     oc.doRegister("ignore-errors", new Option_Bool(false));
     199           98 :     oc.addDescription("ignore-errors", "Processing", TL("Continue on broken input"));
     200              : 
     201           49 :     oc.doRegister("poi-layer-offset", new Option_Float(0));
     202           98 :     oc.addDescription("poi-layer-offset", "Processing", TL("Adds FLOAT to the layer value for each poi (i.e. to raise it above polygons)"));
     203              : 
     204           49 :     oc.doRegister("flatten", new Option_Bool(false));
     205           98 :     oc.addDescription("flatten", "Processing", TL("Remove all z-data"));
     206              : 
     207              :     // building defaults options
     208           98 :     oc.doRegister("color", new Option_String("0.2,0.5,1."));
     209           98 :     oc.addDescription("color", "Building Defaults", TL("Sets STR as default color"));
     210              : 
     211           98 :     oc.doRegister("prefix", new Option_String(""));
     212           98 :     oc.addDescription("prefix", "Building Defaults", TL("Sets STR as default prefix"));
     213              : 
     214           98 :     oc.doRegister("type", new Option_String("unknown"));
     215           98 :     oc.addDescription("type", "Building Defaults", TL("Sets STR as default type"));
     216              : 
     217           49 :     oc.doRegister("fill", new Option_Bool(true));
     218           98 :     oc.addDescription("fill", "Building Defaults", TL("Fills polygons by default"));
     219              : 
     220           98 :     oc.doRegister("icon", new Option_String(SUMOXMLDefinitions::POIIcons.getString(POIIcon::NONE)));
     221           98 :     oc.addDescription("icon", "Building Defaults", TL("Sets STR as default icon"));
     222              : 
     223           49 :     oc.doRegister("layer", new Option_Float(-1));
     224           98 :     oc.addDescription("layer", "Building Defaults", TL("Sets FLOAT as default layer"));
     225              : 
     226           49 :     oc.doRegister("discard", new Option_Bool(false));
     227           98 :     oc.addDescription("discard", "Building Defaults", TL("Sets default action to discard"));
     228              : 
     229              :     // projection
     230           49 :     oc.doRegister("proj.plain-geo", new Option_Bool(false));
     231           98 :     oc.addDescription("proj.plain-geo", "Projection", TL("Write geo coordinates in output"));
     232          147 : }
     233              : 
     234              : 
     235              : int
     236           49 : main(int argc, char** argv) {
     237           49 :     OptionsCont& oc = OptionsCont::getOptions();
     238           49 :     oc.setApplicationDescription(TL("Importer of polygons and POIs for the microscopic, multi-modal traffic simulation SUMO."));
     239           98 :     oc.setApplicationName("polyconvert", "Eclipse SUMO polyconvert Version " VERSION_STRING);
     240              :     int ret = 0;
     241              :     try {
     242              :         // initialise subsystems
     243           49 :         XMLSubSys::init();
     244           49 :         fillOptions();
     245           49 :         OptionsIO::setArgs(argc, argv);
     246           49 :         OptionsIO::getOptions();
     247           49 :         if (oc.processMetaOptions(argc < 2)) {
     248            6 :             SystemFrame::close();
     249            6 :             return 0;
     250              :         }
     251           43 :         SystemFrame::checkOptions(oc);
     252          129 :         XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"), "never");
     253           86 :         if (oc.isDefault("aggregate-warnings")) {
     254           86 :             oc.setDefault("aggregate-warnings", "5");
     255              :         }
     256           43 :         MsgHandler::initOutputOptions();
     257              :         // build the projection
     258           43 :         double scale = 1.0;
     259           88 :         if ((oc.isSet("dlr-navteq-poly-files") || oc.isSet("dlr-navteq-poi-files")) && oc.isDefault("proj.scale")) {
     260            6 :             scale = 1e-5;
     261              :         }
     262           86 :         if (!oc.isSet("net")) {
     263              :             // from the given options
     264              : #ifdef PROJ_API_FILE
     265           64 :             const int numProjections = oc.getBool("simple-projection") + oc.getBool("proj.utm") + oc.getBool("proj.dhdn") + (oc.getString("proj").length() > 1);
     266           75 :             if ((oc.isSet("osm-files") || oc.isSet("dlr-navteq-poly-files") || oc.isSet("dlr-navteq-poi-files") || oc.isSet("shapefile-prefixes")) && numProjections == 0) {
     267              :                 // input is lon,lat and projecting it to UTM ensures accurate handling of geometry
     268           42 :                 oc.set("proj.utm", "true");
     269           42 :                 if (oc.isDefault("proj.plain-geo")) {
     270              :                     // without reference to a network, raw UTM isn't helpful so we better write the data out as lon,lat
     271           40 :                     oc.set("proj.plain-geo", "true");
     272              :                 }
     273              :             }
     274           64 :             if (oc.isDefault("proj.scale")) {
     275           64 :                 oc.set("proj.scale", toString(scale, 5));
     276              :             }
     277              : #endif
     278           32 :             if (!GeoConvHelper::init(oc)) {
     279            0 :                 throw ProcessError(TL("Could not build projection!"));
     280              :             }
     281              :         } else {
     282              :             // from the supplied network
     283              :             // @todo warn about given options being ignored
     284           22 :             PCNetProjectionLoader::load(oc.getString("net"), scale);
     285              :         }
     286           43 :         Boundary pruningBoundary = GeoConvHelper::getFinal().getConvBoundary();
     287              :         // check whether the input shall be pruned
     288              :         bool prune = false;
     289           86 :         if (oc.getBool("prune.in-net")) {
     290            2 :             if (!oc.isSet("net")) {
     291            0 :                 throw ProcessError(TL("In order to prune the input on the net, you have to supply a network."));
     292              :             }
     293            1 :             bool ok = true;
     294              :             // !!! no proper error handling
     295              :             Boundary offsets = GeomConvHelper::parseBoundaryReporting(
     296            2 :                                    oc.getString("prune.in-net.offsets"), "--prune.on-net.offsets", nullptr, ok, true, true);
     297            1 :             pruningBoundary.setOffsets(
     298            1 :                 pruningBoundary.xmin() - offsets.xmin(),
     299            1 :                 pruningBoundary.ymin() - offsets.ymin(),
     300            1 :                 pruningBoundary.xmax() + offsets.xmax(),
     301            1 :                 pruningBoundary.ymax() + offsets.ymax());
     302              :             prune = true;
     303            1 :         }
     304           86 :         if (oc.isSet("prune.boundary")) {
     305            0 :             bool ok = true;
     306              :             // !!! no proper error handling
     307            0 :             pruningBoundary = GeomConvHelper::parseBoundaryReporting(oc.getString("prune.boundary"), "--prune.boundary", nullptr, ok);
     308              :             prune = true;
     309              :         }
     310           66 :         if (oc.isSet("osm-files") && oc.isDefault("poi-layer-offset")) {
     311           46 :             oc.setDefault("poi-layer-offset", "5"); // sufficient when using the default typemap
     312              :         }
     313              : 
     314           43 :         PCPolyContainer toFill(prune, pruningBoundary, oc.getStringVector("remove"));
     315              : 
     316              :         // read in the type defaults
     317           86 :         if (!oc.isSet("type-file")) {
     318           38 :             const char* sumoPath = std::getenv("SUMO_HOME");
     319           38 :             if (sumoPath == nullptr) {
     320            0 :                 WRITE_WARNING(TL("Environment variable SUMO_HOME is not set, using built in type maps."));
     321              :             } else {
     322           38 :                 const std::string path = sumoPath + std::string("/data/typemap/");
     323           76 :                 if (oc.isSet("dlr-navteq-poly-files")) {
     324            8 :                     oc.setDefault("type-file", path + "navteqPolyconvert.typ.xml");
     325              :                 }
     326           76 :                 if (oc.isSet("osm-files")) {
     327           36 :                     oc.setDefault("type-file", path + "osmPolyconvert.typ.xml");
     328              :                 }
     329           76 :                 if (oc.isSet("visum-files")) {
     330            0 :                     oc.setDefault("type-file", path + "visumPolyconvert.typ.xml");
     331              :                 }
     332              :             }
     333              :         }
     334           43 :         PCTypeMap tm(oc);
     335           43 :         PCTypeDefHandler handler(oc, tm);
     336           86 :         if (oc.isSet("type-file")) {
     337           54 :             if (!XMLSubSys::runParser(handler, oc.getString("type-file"))) {
     338              :                 // something failed
     339            0 :                 throw ProcessError();
     340              :             }
     341              :         } else {
     342           16 :             handler.setFileName("built in type map");
     343           16 :             SUMOSAXReader* reader = XMLSubSys::getSAXReader(handler);
     344           32 :             if (oc.isSet("dlr-navteq-poly-files")) {
     345            0 :                 reader->parseString(navteqTypemap);
     346              :             }
     347           32 :             if (oc.isSet("osm-files")) {
     348            0 :                 reader->parseString(osmTypemap);
     349              :             }
     350           32 :             if (oc.isSet("visum-files")) {
     351            0 :                 reader->parseString(visumTypemap);
     352              :             }
     353           16 :             delete reader;
     354              :         }
     355              :         // read in the data
     356           43 :         PCLoaderXML::loadIfSet(oc, toFill, tm); // SUMO-XML
     357           42 :         PCLoaderOSM::loadIfSet(oc, toFill, tm); // OSM-XML
     358           41 :         PCLoaderDlrNavteq::loadIfSet(oc, toFill, tm); // Elmar-files
     359           41 :         PCLoaderVisum::loadIfSet(oc, toFill, tm); // VISUM
     360           41 :         PCLoaderArcView::loadIfSet(oc, toFill, tm); // shape-files
     361           41 :         GeoConvHelper::computeFinal();
     362              :         // error processing
     363           42 :         if (MsgHandler::getErrorInstance()->wasInformed() && !oc.getBool("ignore-errors")) {
     364            1 :             throw ProcessError();
     365              :         }
     366              :         // output
     367           42 :         if (!oc.isSet("output-file") && !oc.isSet("dlr-tdp-output")) {
     368            0 :             std::string out = "polygons.xml";
     369            0 :             if (oc.isSet("configuration-file")) {
     370            0 :                 out = FileHelpers::getConfigurationRelative(oc.getString("configuration-file"), out);
     371              :             }
     372            0 :             oc.setDefault("output-file", out);
     373              :         }
     374           80 :         if (oc.isSet("output-file")) {
     375           78 :             toFill.save(oc.getString("output-file"), oc.getBool("proj.plain-geo"));
     376              :         }
     377           80 :         if (oc.isSet("dlr-tdp-output")) {
     378            2 :             toFill.saveDlrTDP(oc.getString("dlr-tdp-output"));
     379              :         }
     380              : 
     381           55 :     } catch (const ProcessError& e) {
     382            3 :         if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
     383            0 :             WRITE_ERROR(e.what());
     384              :         }
     385            3 :         MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
     386              :         ret = 1;
     387              : #ifndef _DEBUG
     388            3 :     } catch (const std::exception& e) {
     389            0 :         if (std::string(e.what()) != std::string("")) {
     390            0 :             WRITE_ERROR(e.what());
     391              :         }
     392            0 :         MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
     393              :         ret = 1;
     394            0 :     } catch (...) {
     395            0 :         MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
     396              :         ret = 1;
     397              : #endif
     398            0 :     }
     399           43 :     SystemFrame::close();
     400              :     // report about ending
     401              :     if (ret == 0) {
     402              :         std::cout << "Success." << std::endl;
     403              :     }
     404              :     return ret;
     405              : }
     406              : 
     407              : 
     408              : /****************************************************************************/
        

Generated by: LCOV version 2.0-1