SUMO - Simulation of Urban MObility
polyconvert_main.cpp
Go to the documentation of this file.
1 /****************************************************************************/
11 // Main for POLYCONVERT
12 /****************************************************************************/
13 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
14 // Copyright (C) 2005-2017 DLR (http://www.dlr.de/) and contributors
15 /****************************************************************************/
16 //
17 // This file is part of SUMO.
18 // SUMO is free software: you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation, either version 3 of the License, or
21 // (at your option) any later version.
22 //
23 /****************************************************************************/
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #ifdef HAVE_VERSION_H
36 #include <version.h>
37 #endif
38 
39 #include <iostream>
40 #include <string>
48 #include <utils/common/ToString.h>
53 #include <utils/geom/Boundary.h>
55 #include <utils/xml/XMLSubSys.h>
61 #include <polyconvert/PCTypeMap.h>
64 #include "pc_typemap.h"
65 
66 
67 // ===========================================================================
68 // method definitions
69 // ===========================================================================
70 void
73  oc.addCallExample("-c <CONFIGURATION>", "run with configuration options set in file");
74 
75  // insert options sub-topics
76  SystemFrame::addConfigurationOptions(oc); // fill this subtopic, too
77  oc.addOptionSubTopic("Input");
78  oc.addOptionSubTopic("Output");
80  oc.addOptionSubTopic("Pruning");
81  oc.addOptionSubTopic("Processing");
82  oc.addOptionSubTopic("Building Defaults");
83  SystemFrame::addReportOptions(oc); // fill this subtopic, too
84 
85 
86  // register options
87  // add i/o options
88  // original network
89  oc.doRegister("net-file", 'n', new Option_FileName());
90  oc.addSynonyme("net-file", "net");
91  oc.addDescription("net-file", "Input", "Loads SUMO-network FILE as reference to offset and projection");
92 
93  // dlrnavteq import
94  oc.doRegister("dlr-navteq-poly-files", new Option_FileName());
95  oc.addDescription("dlr-navteq-poly-files", "Input", "Reads polygons from FILE assuming they're coded in DLR-Navteq (Elmar)-format");
96  oc.doRegister("dlr-navteq-poi-files", new Option_FileName());
97  oc.addDescription("dlr-navteq-poi-files", "Input", "Reads pois from FILE+ assuming they're coded in DLR-Navteq (Elmar)-format");
98 
99  // visum import
100  oc.doRegister("visum-files", new Option_FileName());
101  oc.addSynonyme("visum-files", "visum");
102  oc.addDescription("visum-files", "Input", "Reads polygons from FILE assuming it's a Visum-net");
103 
104  // xml import
105  oc.doRegister("xml-files", new Option_FileName());
106  oc.addSynonyme("xml-files", "xml");
107  oc.addDescription("xml-files", "Input", "Reads pois and shapes from FILE assuming they're coded in XML");
108 
109  // osm import
110  oc.doRegister("osm-files", new Option_FileName());
111  oc.addSynonyme("osm-files", "osm");
112  oc.addDescription("osm-files", "Input", "Reads pois from FILE+ assuming they're coded in OSM");
113  oc.doRegister("osm.keep-full-type", new Option_Bool(false));
114  oc.addDescription("osm.keep-full-type", "Input", "The type will be made of the key-value - pair");
115  oc.doRegister("osm.use-name", new Option_Bool(false));
116  oc.addDescription("osm.use-name", "Input", "The id will be set from the given 'name' attribute");
117 
118  // arcview import
119  oc.doRegister("shapefile-prefixes", new Option_FileName());
120  oc.addSynonyme("shapefile-prefixes", "shapefile-prefix");
121  oc.addSynonyme("shapefile-prefixes", "shapefile");
122  oc.addSynonyme("shapefile-prefixes", "shape-files", true);
123  oc.addDescription("shapefile-prefixes", "Input", "Reads shapes from shapefiles FILE+");
124 
125  oc.doRegister("shapefile.guess-projection", new Option_Bool(false));
126  oc.addSynonyme("shapefile.guess-projection", "arcview.guess-projection", true);
127  oc.addDescription("shapefile.guess-projection", "Input", "Guesses the shapefile's projection");
128 
129  oc.doRegister("shapefile.id-column", new Option_String());
130  oc.addSynonyme("shapefile.id-column", "shapefile.id-name", true);
131  oc.addSynonyme("shapefile.id-column", "shape-files.id-name", true);
132  oc.addDescription("shapefile.id-column", "Input", "Defines in which column the id can be found");
133 
134  oc.doRegister("shapefile.use-running-id", new Option_Bool(false));
135  oc.addDescription("shapefile.use-running-id", "Input", "A running number will be used as id");
136 
137  oc.doRegister("shapefile.add-param", new Option_Bool(false));
138  oc.addDescription("shapefile.add-param", "Input", "Extract all additional columns as params");
139 
140  oc.doRegister("shapefile.fill", new Option_String());
141  oc.addDescription("shapefile.fill", "Input", "[auto|true|false]. Forces the 'fill' status to the given value. Default 'auto' tries to determine it from the data type");
142 
143  // typemap reading
144  oc.doRegister("type-file", new Option_FileName());
145  oc.addSynonyme("type-file", "typemap", true);
146  oc.addDescription("type-file", "Input", "Reads types from FILE");
147 
148 
149  // output
150  oc.doRegister("output-file", 'o', new Option_FileName());
151  oc.addSynonyme("output-file", "output");
152  oc.addDescription("output-file", "Output", "Write generated polygons/pois to FILE");
153 
154  oc.doRegister("dlr-tdp-output", new Option_FileName());
155  oc.addDescription("dlr-tdp-output", "Output", "Write generated polygons/pois to a dlr-tdp file with the given prefix");
156 
157 
158  // prunning options
159  oc.doRegister("prune.in-net", new Option_Bool(false));
160  oc.addSynonyme("prune.in-net", "prune.on-net", true);
161  oc.addDescription("prune.in-net", "Pruning", "Enables pruning on net boundaries");
162 
163  oc.doRegister("prune.in-net.offsets", new Option_String("0,0,0,0"));
164  oc.addSynonyme("prune.in-net.offsets", "prune.on-net.offsets", true);
165  oc.addDescription("prune.in-net.offsets", "Pruning", "Uses STR as offset definition added to the net boundaries");
166 
167  oc.doRegister("prune.boundary", new Option_String());
168  oc.addDescription("prune.boundary", "Pruning", "Uses STR as pruning boundary");
169 
170  oc.doRegister("prune.keep-list", new Option_String());
171  oc.addSynonyme("prune.keep-list", "prune.keep");
172  oc.addSynonyme("prune.keep-list", "prune.ignore", true);
173  oc.addDescription("prune.keep-list", "Pruning", "Items in STR will be kept though out of boundary");
174 
175  oc.doRegister("prune.explicit", new Option_String(""));
176  oc.addSynonyme("prune.explicit", "remove");
177  oc.addDescription("prune.explicit", "Pruning", "Items with names in STR will be removed");
178 
179 
180  oc.doRegister("offset.x", new Option_Float(0));
181  oc.addSynonyme("offset.x", "x-offset-to-apply", true);
182  oc.addDescription("offset.x", "Processing", "Adds FLOAT to net x-positions");
183 
184  oc.doRegister("offset.y", new Option_Float(0));
185  oc.addSynonyme("offset.y", "y-offset-to-apply", true);
186  oc.addDescription("offset.y", "Processing", "Adds FLOAT to net y-positions");
187 
188  oc.doRegister("all-attributes", new Option_Bool(false));
189  oc.addDescription("all-attributes", "Processing", "Imports all attributes as key/value pairs");
190 
191  oc.doRegister("ignore-errors", new Option_Bool(false));
192  oc.addDescription("ignore-errors", "Processing", "Continue on broken input");
193 
194  oc.doRegister("poi-layer-offset", new Option_Float(0));
195  oc.addDescription("poi-layer-offset", "Processing", "Adds FLOAT to the layer value for each poi (i.e. to raise it above polygons)");
196 
197  // building defaults options
198  oc.doRegister("color", new Option_String("0.2,0.5,1."));
199  oc.addDescription("color", "Building Defaults", "Sets STR as default color");
200 
201  oc.doRegister("prefix", new Option_String(""));
202  oc.addDescription("prefix", "Building Defaults", "Sets STR as default prefix");
203 
204  oc.doRegister("type", new Option_String("unknown"));
205  oc.addDescription("type", "Building Defaults", "Sets STR as default type");
206 
207  oc.doRegister("fill", new Option_Bool("false"));
208  oc.addDescription("fill", "Building Defaults", "Fills polygons by default");
209 
210  oc.doRegister("layer", new Option_Float(-1));
211  oc.addDescription("layer", "Building Defaults", "Sets FLOAT as default layer");
212 
213  oc.doRegister("discard", new Option_Bool(false));
214  oc.addDescription("discard", "Building Defaults", "Sets default action to discard");
215 
216  // projection
217  oc.doRegister("proj.plain-geo", new Option_Bool(false));
218  oc.addDescription("proj.plain-geo", "Projection", "Write geo coordinates in output");
219 }
220 
221 
222 int
223 main(int argc, char** argv) {
225  oc.setApplicationDescription("Importer of polygons and POIs for the road traffic simulation SUMO.");
226  oc.setApplicationName("polyconvert", "SUMO polyconvert Version " VERSION_STRING);
227  int ret = 0;
228  try {
229  // initialise subsystems
230  XMLSubSys::init();
231  fillOptions();
232  OptionsIO::setArgs(argc, argv);
234  if (oc.processMetaOptions(argc < 2)) {
236  return 0;
237  }
238  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"));
240  // build the projection
241  int shift = 0;
242  if ((oc.isSet("dlr-navteq-poly-files") || oc.isSet("dlr-navteq-poi-files")) && oc.isDefault("proj.scale")) {
243  shift = 5;
244  }
245  if (!oc.isSet("net")) {
246  // from the given options
247 #ifdef HAVE_PROJ
248  unsigned numProjections = oc.getBool("simple-projection") + oc.getBool("proj.utm") + oc.getBool("proj.dhdn") + (oc.getString("proj").length() > 1);
249  if ((oc.isSet("osm-files") || oc.isSet("dlr-navteq-poly-files") || oc.isSet("dlr-navteq-poi-files")) && numProjections == 0) {
250  oc.set("proj.utm", "true");
251  }
252  oc.set("proj.scale", toString(shift));
253 #endif
254  if (!GeoConvHelper::init(oc)) {
255  throw ProcessError("Could not build projection!");
256  }
257  } else {
258  // from the supplied network
259  // @todo warn about given options being ignored
260  PCNetProjectionLoader::load(oc.getString("net"), shift);
261  }
262  Boundary pruningBoundary = GeoConvHelper::getFinal().getConvBoundary();
263  // check whether the input shall be pruned
264  bool prune = false;
265  if (oc.getBool("prune.in-net")) {
266  if (!oc.isSet("net")) {
267  throw ProcessError("In order to prune the input on the net, you have to supply a network.");
268  }
269  bool ok = true;
270  // !!! no proper error handling
271  Boundary offsets = GeomConvHelper::parseBoundaryReporting(oc.getString("prune.in-net.offsets"), "--prune.on-net.offsets", 0, ok);
272  pruningBoundary = Boundary(
273  pruningBoundary.xmin() + offsets.xmin(),
274  pruningBoundary.ymin() + offsets.ymin(),
275  pruningBoundary.xmax() + offsets.xmax(),
276  pruningBoundary.ymax() + offsets.ymax());
277  prune = true;
278  }
279  if (oc.isSet("prune.boundary")) {
280  bool ok = true;
281  // !!! no proper error handling
282  pruningBoundary = GeomConvHelper::parseBoundaryReporting(oc.getString("prune.boundary"), "--prune.boundary", 0, ok);
283  prune = true;
284  }
285  if (oc.isSet("osm-files") && oc.isDefault("poi-layer-offset")) {
286  oc.set("poi-layer-offset", "5"); // sufficient when using the default typemap
287  }
288 
289  PCPolyContainer toFill(prune, pruningBoundary, oc.getStringVector("remove"));
290 
291  // read in the type defaults
292  if (!oc.isSet("type-file")) {
293  const char* sumoPath = std::getenv("SUMO_HOME");
294  if (sumoPath == 0) {
295  WRITE_WARNING("Environment variable SUMO_HOME is not set, using built in type maps.");
296  } else {
297  const std::string path = sumoPath + std::string("/data/typemap/");
298  if (oc.isSet("dlr-navteq-poly-files")) {
299  oc.setDefault("type-file", path + "navteqPolyconvert.typ.xml");
300  }
301  if (oc.isSet("osm-files")) {
302  oc.setDefault("type-file", path + "osmPolyconvert.typ.xml");
303  }
304  if (oc.isSet("visum-files")) {
305  oc.setDefault("type-file", path + "visumPolyconvert.typ.xml");
306  }
307  }
308  }
309  PCTypeMap tm(oc);
310  PCTypeDefHandler handler(oc, tm);
311  if (oc.isSet("type-file")) {
312  if (!XMLSubSys::runParser(handler, oc.getString("type-file"))) {
313  // something failed
314  throw ProcessError();
315  }
316  } else {
317  handler.setFileName("built in type map");
318  SUMOSAXReader* reader = XMLSubSys::getSAXReader(handler);
319  if (oc.isSet("dlr-navteq-poly-files")) {
320  reader->parseString(navteqTypemap);
321  }
322  if (oc.isSet("osm-files")) {
323  reader->parseString(osmTypemap);
324  }
325  if (oc.isSet("visum-files")) {
326  reader->parseString(visumTypemap);
327  }
328  delete reader;
329  }
331  // read in the data
332  PCLoaderXML::loadIfSet(oc, toFill, tm); // SUMO-XML
333  PCLoaderOSM::loadIfSet(oc, toFill, tm); // OSM-XML
334  PCLoaderDlrNavteq::loadIfSet(oc, toFill, tm); // Elmar-files
335  PCLoaderVisum::loadIfSet(oc, toFill, tm); // VISUM
336  PCLoaderArcView::loadIfSet(oc, toFill, tm); // shape-files
338  // error processing
339  if (MsgHandler::getErrorInstance()->wasInformed() && !oc.getBool("ignore-errors")) {
340  throw ProcessError();
341  }
342  // output
343  if (!oc.isSet("output-file") && !oc.isSet("dlr-tdp-output")) {
344  std::string out = "polygons.xml";
345  if (oc.isSet("configuration-file")) {
346  out = FileHelpers::getConfigurationRelative(oc.getString("configuration-file"), out);
347  }
348  oc.setDefault("output-file", out);
349  }
350  if (oc.isSet("output-file")) {
351  toFill.save(oc.getString("output-file"), oc.getBool("proj.plain-geo"));
352  }
353  if (oc.isSet("dlr-tdp-output")) {
354  toFill.saveDlrTDP(oc.getString("dlr-tdp-output"));
355  }
356 
357  } catch (const ProcessError& e) {
358  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
359  WRITE_ERROR(e.what());
360  }
361  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
362  ret = 1;
363 #ifndef _DEBUG
364  } catch (const std::exception& e) {
365  if (std::string(e.what()) != std::string("")) {
366  WRITE_ERROR(e.what());
367  }
368  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
369  ret = 1;
370  } catch (...) {
371  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
372  ret = 1;
373 #endif
374  }
376  // report about ending
377  if (ret == 0) {
378  std::cout << "Success." << std::endl;
379  }
380  return ret;
381 }
382 
383 
384 
385 /****************************************************************************/
386 
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:82
double ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:138
static void init()
Initialises the xml-subsystem.
Definition: XMLSubSys.cpp:54
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:76
static void loadIfSet(OptionsCont &oc, PCPolyContainer &toFill, PCTypeMap &tm)
Loads pois/polygons assumed to be stored as OSM-XML.
Definition: PCLoaderOSM.cpp:94
double xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:132
static std::string getConfigurationRelative(const std::string &configPath, const std::string &path)
Returns the second path as a relative path to the first file.
Definition: FileHelpers.cpp:82
static void getOptions(const bool commandLineOnly=false)
Parses the command line arguments and loads the configuration.
Definition: OptionsIO.cpp:82
static void addReportOptions(OptionsCont &oc)
Adds reporting options to the given container.
Definition: SystemFrame.cpp:72
int main(int argc, char **argv)
const Boundary & getConvBoundary() const
Returns the converted boundary.
static void setValidation(const std::string &validationScheme, const std::string &netValidationScheme)
Enables or disables validation.
Definition: XMLSubSys.cpp:65
static void computeFinal(bool lefthand=false)
compute the location attributes which will be used for output based on the loaded location data...
void addCallExample(const std::string &example, const std::string &desc)
Add a call example.
void setApplicationDescription(const std::string &appDesc)
Sets the application description.
const std::string osmTypemap
Definition: typemap.h:38
SAX-reader encapsulation containing binary reader.
Definition: SUMOSAXReader.h:58
static Boundary parseBoundaryReporting(const std::string &def, const std::string &objecttype, const char *objectid, bool &ok, bool report=true)
Builds a boundary from its string representation, reporting occured errors.
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
bool isDefault(const std::string &name) const
Returns the information whether the named option has still the default value.
void parseString(std::string content)
static bool runParser(GenericSAXHandler &handler, const std::string &file, const bool isNet=false)
Runs the given handler on the given file; returns if everything&#39;s ok.
Definition: XMLSubSys.cpp:110
static void close()
Closes all of an applications subsystems.
static void addConfigurationOptions(OptionsCont &oc)
Adds configuration options to the given container.
Definition: SystemFrame.cpp:47
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:48
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
A storage for loaded polygons and pois.
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition: OptionsIO.cpp:62
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:65
void addSynonyme(const std::string &name1, const std::string &name2, bool isDeprecated=false)
Adds a synonyme for an options name (any order)
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
A storage for type mappings.
Definition: PCTypeMap.h:52
void setFileName(const std::string &name)
Sets the current file name.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:56
bool setDefault(const std::string &name, const std::string &value)
Sets the given value for the named option as new default value.
static void loadIfSet(OptionsCont &oc, PCPolyContainer &toFill, PCTypeMap &tm)
Loads pois/polygons assumed to be stored using VISUM-format.
static void loadIfSet(OptionsCont &oc, PCPolyContainer &toFill, PCTypeMap &tm)
Loads pois/polygons assumed to be stored as shape files-files.
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
bool processMetaOptions(bool missingOptions)
Checks for help and configuration output, returns whether we should exit.
const std::string visumTypemap
Definition: pc_typemap.h:54
static SUMOSAXReader * getSAXReader(SUMOSAXHandler &handler)
Builds a reader and assigns the handler to it.
Definition: XMLSubSys.cpp:98
static bool init(OptionsCont &oc)
Initialises the processing and the final instance using the given options.
std::vector< std::string > getStringVector(const std::string &name) const
Returns the list of string-vector-value of the named option (only for Option_String) ...
double xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:126
void addOptionSubTopic(const std::string &topic)
Adds an option subtopic.
static bool checkOptions()
checks shared options and sets StdDefs
static void addProjectionOptions(OptionsCont &oc)
Adds projection options to the given container.
A handler for loading polygon type maps.
static void load(const std::string &file, int shift)
Loads network projection if wished.
#define VERSION_STRING
Definition: config.h:210
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:206
static void loadIfSet(OptionsCont &oc, PCPolyContainer &toFill, PCTypeMap &tm)
Loads pois/polygons assumed to be stored as according DLR-Navteq (Elmar)-files.
bool set(const std::string &name, const std::string &value)
Sets the given value for the named option.
void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:85
A storage for options typed value containers)
Definition: OptionsCont.h:99
static const GeoConvHelper & getFinal()
the coordinate transformation for writing the location element and for tracking the original coordina...
static void loadIfSet(OptionsCont &oc, PCPolyContainer &toFill, PCTypeMap &tm)
Loads pois/polygons assumed to be stored as XML.
Definition: PCLoaderXML.cpp:61
const std::string navteqTypemap
Definition: pc_typemap.h:1
void addDescription(const std::string &name, const std::string &subtopic, const std::string &description)
Adds a description for an option.
double ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:144
static void initOutputOptions()
Definition: MsgHandler.cpp:193
void fillOptions()
void setApplicationName(const std::string &appName, const std::string &fullName)
Sets the application name.