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 #ifdef CHECK_MEMORY_LEAKS
67 #include <foreign/nvwa/debug_new.h>
68 #endif // CHECK_MEMORY_LEAKS
69 
70 
71 // ===========================================================================
72 // method definitions
73 // ===========================================================================
74 void
77  oc.addCallExample("-c <CONFIGURATION>", "run with configuration options set in file");
78 
79  // insert options sub-topics
80  SystemFrame::addConfigurationOptions(oc); // fill this subtopic, too
81  oc.addOptionSubTopic("Input");
82  oc.addOptionSubTopic("Output");
84  oc.addOptionSubTopic("Pruning");
85  oc.addOptionSubTopic("Processing");
86  oc.addOptionSubTopic("Building Defaults");
87  SystemFrame::addReportOptions(oc); // fill this subtopic, too
88 
89 
90  // register options
91  // add i/o options
92  // original network
93  oc.doRegister("net-file", 'n', new Option_FileName());
94  oc.addSynonyme("net-file", "net");
95  oc.addDescription("net-file", "Input", "Loads SUMO-network FILE as reference to offset and projection");
96 
97  // dlrnavteq import
98  oc.doRegister("dlr-navteq-poly-files", new Option_FileName());
99  oc.addDescription("dlr-navteq-poly-files", "Input", "Reads polygons from FILE assuming they're coded in DLR-Navteq (Elmar)-format");
100  oc.doRegister("dlr-navteq-poi-files", new Option_FileName());
101  oc.addDescription("dlr-navteq-poi-files", "Input", "Reads pois from FILE+ assuming they're coded in DLR-Navteq (Elmar)-format");
102 
103  // visum import
104  oc.doRegister("visum-files", new Option_FileName());
105  oc.addSynonyme("visum-files", "visum");
106  oc.addDescription("visum-files", "Input", "Reads polygons from FILE assuming it's a Visum-net");
107 
108  // xml import
109  oc.doRegister("xml-files", new Option_FileName());
110  oc.addSynonyme("xml-files", "xml");
111  oc.addDescription("xml-files", "Input", "Reads pois and shapes from FILE assuming they're coded in XML");
112 
113  // osm import
114  oc.doRegister("osm-files", new Option_FileName());
115  oc.addSynonyme("osm-files", "osm");
116  oc.addDescription("osm-files", "Input", "Reads pois from FILE+ assuming they're coded in OSM");
117  oc.doRegister("osm.keep-full-type", new Option_Bool(false));
118  oc.addDescription("osm.keep-full-type", "Input", "The type will be made of the key-value - pair");
119  oc.doRegister("osm.use-name", new Option_Bool(false));
120  oc.addDescription("osm.use-name", "Input", "The id will be set from the given 'name' attribute");
121 
122  // arcview import
123  oc.doRegister("shapefile-prefixes", new Option_FileName());
124  oc.addSynonyme("shapefile-prefixes", "shapefile-prefix");
125  oc.addSynonyme("shapefile-prefixes", "shapefile");
126  oc.addSynonyme("shapefile-prefixes", "shape-files", true);
127  oc.addDescription("shapefile-prefixes", "Input", "Reads shapes from shapefiles FILE+");
128 
129  oc.doRegister("shapefile.guess-projection", new Option_Bool(false));
130  oc.addSynonyme("shapefile.guess-projection", "arcview.guess-projection", true);
131  oc.addDescription("shapefile.guess-projection", "Input", "Guesses the shapefile's projection");
132 
133  oc.doRegister("shapefile.id-column", new Option_String());
134  oc.addSynonyme("shapefile.id-column", "shapefile.id-name", true);
135  oc.addSynonyme("shapefile.id-column", "shape-files.id-name", true);
136  oc.addDescription("shapefile.id-column", "Input", "Defines in which column the id can be found");
137 
138  oc.doRegister("shapefile.use-running-id", new Option_Bool(false));
139  oc.addDescription("shapefile.use-running-id", "Input", "A running number will be used as id");
140 
141  oc.doRegister("shapefile.add-param", new Option_Bool(false));
142  oc.addDescription("shapefile.add-param", "Input", "Extract all additonal columns as params");
143 
144  oc.doRegister("shapefile.fill", new Option_String());
145  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");
146 
147  // typemap reading
148  oc.doRegister("type-file", new Option_FileName());
149  oc.addSynonyme("type-file", "typemap", true);
150  oc.addDescription("type-file", "Input", "Reads types from FILE");
151 
152 
153  // output
154  oc.doRegister("output-file", 'o', new Option_FileName());
155  oc.addSynonyme("output-file", "output");
156  oc.addDescription("output-file", "Output", "Write generated polygons/pois to FILE");
157 
158  oc.doRegister("dlr-tdp-output", new Option_FileName());
159  oc.addDescription("dlr-tdp-output", "Output", "Write generated polygons/pois to a dlr-tdp file with the given prefix");
160 
161 
162  // prunning options
163  oc.doRegister("prune.in-net", new Option_Bool(false));
164  oc.addSynonyme("prune.in-net", "prune.on-net", true);
165  oc.addDescription("prune.in-net", "Pruning", "Enables pruning on net boundaries");
166 
167  oc.doRegister("prune.in-net.offsets", new Option_String("0,0,0,0"));
168  oc.addSynonyme("prune.in-net.offsets", "prune.on-net.offsets", true);
169  oc.addDescription("prune.in-net.offsets", "Pruning", "Uses STR as offset definition added to the net boundaries");
170 
171  oc.doRegister("prune.boundary", new Option_String());
172  oc.addDescription("prune.boundary", "Pruning", "Uses STR as pruning boundary");
173 
174  oc.doRegister("prune.keep-list", new Option_String());
175  oc.addSynonyme("prune.keep-list", "prune.keep");
176  oc.addSynonyme("prune.keep-list", "prune.ignore", true);
177  oc.addDescription("prune.keep-list", "Pruning", "Items in STR will be kept though out of boundary");
178 
179  oc.doRegister("prune.explicit", new Option_String(""));
180  oc.addSynonyme("prune.explicit", "remove");
181  oc.addDescription("prune.explicit", "Pruning", "Items with names in STR will be removed");
182 
183 
184  oc.doRegister("offset.x", new Option_Float(0));
185  oc.addSynonyme("offset.x", "x-offset-to-apply", true);
186  oc.addDescription("offset.x", "Processing", "Adds FLOAT to net x-positions");
187 
188  oc.doRegister("offset.y", new Option_Float(0));
189  oc.addSynonyme("offset.y", "y-offset-to-apply", true);
190  oc.addDescription("offset.y", "Processing", "Adds FLOAT to net y-positions");
191 
192  oc.doRegister("all-attributes", new Option_Bool(false));
193  oc.addDescription("all-attributes", "Processing", "Imports all attributes as key/value pairs");
194 
195  oc.doRegister("ignore-errors", new Option_Bool(false));
196  oc.addDescription("ignore-errors", "Processing", "Continue on broken input");
197 
198  oc.doRegister("poi-layer-offset", new Option_Float(0));
199  oc.addDescription("poi-layer-offset", "Processing", "Adds FLOAT to the layer value for each poi (i.e. to raise it above polygons)");
200 
201  // building defaults options
202  oc.doRegister("color", new Option_String("0.2,0.5,1."));
203  oc.addDescription("color", "Building Defaults", "Sets STR as default color");
204 
205  oc.doRegister("prefix", new Option_String(""));
206  oc.addDescription("prefix", "Building Defaults", "Sets STR as default prefix");
207 
208  oc.doRegister("type", new Option_String("unknown"));
209  oc.addDescription("type", "Building Defaults", "Sets STR as default type");
210 
211  oc.doRegister("fill", new Option_Bool("false"));
212  oc.addDescription("fill", "Building Defaults", "Fills polygons by default");
213 
214  oc.doRegister("layer", new Option_Float(-1));
215  oc.addDescription("layer", "Building Defaults", "Sets FLOAT as default layer");
216 
217  oc.doRegister("discard", new Option_Bool(false));
218  oc.addDescription("discard", "Building Defaults", "Sets default action to discard");
219 
220  // projection
221  oc.doRegister("proj.plain-geo", new Option_Bool(false));
222  oc.addDescription("proj.plain-geo", "Projection", "Write geo coordinates in output");
223 }
224 
225 
226 int
227 main(int argc, char** argv) {
229  oc.setApplicationDescription("Importer of polygons and POIs for the road traffic simulation SUMO.");
230  oc.setApplicationName("polyconvert", "SUMO polyconvert Version " VERSION_STRING);
231  int ret = 0;
232  try {
233  // initialise subsystems
234  XMLSubSys::init();
235  fillOptions();
236  OptionsIO::setArgs(argc, argv);
238  if (oc.processMetaOptions(argc < 2)) {
240  return 0;
241  }
242  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"));
244  // build the projection
245  int shift = 0;
246  if ((oc.isSet("dlr-navteq-poly-files") || oc.isSet("dlr-navteq-poi-files")) && oc.isDefault("proj.scale")) {
247  shift = 5;
248  }
249  if (!oc.isSet("net")) {
250  // from the given options
251 #ifdef HAVE_PROJ
252  unsigned numProjections = oc.getBool("simple-projection") + oc.getBool("proj.utm") + oc.getBool("proj.dhdn") + (oc.getString("proj").length() > 1);
253  if ((oc.isSet("osm-files") || oc.isSet("dlr-navteq-poly-files") || oc.isSet("dlr-navteq-poi-files")) && numProjections == 0) {
254  oc.set("proj.utm", "true");
255  }
256  oc.set("proj.scale", toString(shift));
257 #endif
258  if (!GeoConvHelper::init(oc)) {
259  throw ProcessError("Could not build projection!");
260  }
261  } else {
262  // from the supplied network
263  // @todo warn about given options being ignored
264  PCNetProjectionLoader::load(oc.getString("net"), shift);
265  }
266  Boundary pruningBoundary = GeoConvHelper::getFinal().getConvBoundary();
267  // check whether the input shall be pruned
268  bool prune = false;
269  if (oc.getBool("prune.in-net")) {
270  if (!oc.isSet("net")) {
271  throw ProcessError("In order to prune the input on the net, you have to supply a network.");
272  }
273  bool ok = true;
274  // !!! no proper error handling
275  Boundary offsets = GeomConvHelper::parseBoundaryReporting(oc.getString("prune.in-net.offsets"), "--prune.on-net.offsets", 0, ok);
276  pruningBoundary = Boundary(
277  pruningBoundary.xmin() + offsets.xmin(),
278  pruningBoundary.ymin() + offsets.ymin(),
279  pruningBoundary.xmax() + offsets.xmax(),
280  pruningBoundary.ymax() + offsets.ymax());
281  prune = true;
282  }
283  if (oc.isSet("prune.boundary")) {
284  bool ok = true;
285  // !!! no proper error handling
286  pruningBoundary = GeomConvHelper::parseBoundaryReporting(oc.getString("prune.boundary"), "--prune.boundary", 0, ok);
287  prune = true;
288  }
289  if (oc.isSet("osm-files") && oc.isDefault("poi-layer-offset")) {
290  oc.set("poi-layer-offset", "5"); // sufficient when using the default typemap
291  }
292 
293  PCPolyContainer toFill(prune, pruningBoundary, oc.getStringVector("remove"));
294 
295  // read in the type defaults
296  if (!oc.isSet("type-file")) {
297  const char* sumoPath = std::getenv("SUMO_HOME");
298  if (sumoPath == 0) {
299  WRITE_WARNING("Environment variable SUMO_HOME is not set, using built in type maps.");
300  } else {
301  const std::string path = sumoPath + std::string("/data/typemap/");
302  if (oc.isSet("dlr-navteq-poly-files")) {
303  oc.setDefault("type-file", path + "navteqPolyconvert.typ.xml");
304  }
305  if (oc.isSet("osm-files")) {
306  oc.setDefault("type-file", path + "osmPolyconvert.typ.xml");
307  }
308  if (oc.isSet("visum-files")) {
309  oc.setDefault("type-file", path + "visumPolyconvert.typ.xml");
310  }
311  }
312  }
313  PCTypeMap tm(oc);
314  PCTypeDefHandler handler(oc, tm);
315  if (oc.isSet("type-file")) {
316  if (!XMLSubSys::runParser(handler, oc.getString("type-file"))) {
317  // something failed
318  throw ProcessError();
319  }
320  } else {
321  handler.setFileName("built in type map");
322  SUMOSAXReader* reader = XMLSubSys::getSAXReader(handler);
323  if (oc.isSet("dlr-navteq-poly-files")) {
324  reader->parseString(navteqTypemap);
325  }
326  if (oc.isSet("osm-files")) {
327  reader->parseString(osmTypemap);
328  }
329  if (oc.isSet("visum-files")) {
330  reader->parseString(visumTypemap);
331  }
332  delete reader;
333  }
335  // read in the data
336  PCLoaderXML::loadIfSet(oc, toFill, tm); // SUMO-XML
337  PCLoaderOSM::loadIfSet(oc, toFill, tm); // OSM-XML
338  PCLoaderDlrNavteq::loadIfSet(oc, toFill, tm); // Elmar-files
339  PCLoaderVisum::loadIfSet(oc, toFill, tm); // VISUM
340  PCLoaderArcView::loadIfSet(oc, toFill, tm); // shape-files
342  // error processing
343  if (MsgHandler::getErrorInstance()->wasInformed() && !oc.getBool("ignore-errors")) {
344  throw ProcessError();
345  }
346  // output
347  if (!oc.isSet("output-file") && !oc.isSet("dlr-tdp-output")) {
348  std::string out = "polygons.xml";
349  if (oc.isSet("configuration-file")) {
350  out = FileHelpers::getConfigurationRelative(oc.getString("configuration-file"), out);
351  }
352  oc.setDefault("output-file", out);
353  }
354  if (oc.isSet("output-file")) {
355  toFill.save(oc.getString("output-file"), oc.getBool("proj.plain-geo"));
356  }
357  if (oc.isSet("dlr-tdp-output")) {
358  toFill.saveDlrTDP(oc.getString("dlr-tdp-output"));
359  }
360 
361  } catch (const ProcessError& e) {
362  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
363  WRITE_ERROR(e.what());
364  }
365  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
366  ret = 1;
367 #ifndef _DEBUG
368  } catch (const std::exception& e) {
369  if (std::string(e.what()) != std::string("")) {
370  WRITE_ERROR(e.what());
371  }
372  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
373  ret = 1;
374  } catch (...) {
375  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
376  ret = 1;
377 #endif
378  }
380  // report about ending
381  if (ret == 0) {
382  std::cout << "Success." << std::endl;
383  }
384  return ret;
385 }
386 
387 
388 
389 /****************************************************************************/
390 
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:86
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) ...
static void init()
Initialises the xml-subsystem.
Definition: XMLSubSys.cpp:58
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:80
static void loadIfSet(OptionsCont &oc, PCPolyContainer &toFill, PCTypeMap &tm)
Loads pois/polygons assumed to be stored as OSM-XML.
Definition: PCLoaderOSM.cpp:98
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:86
static void getOptions(const bool commandLineOnly=false)
Parses the command line arguments and loads the configuration.
Definition: OptionsIO.cpp:72
static void addReportOptions(OptionsCont &oc)
Adds reporting options to the given container.
Definition: SystemFrame.cpp:76
int main(int argc, char **argv)
static void setValidation(const std::string &validationScheme, const std::string &netValidationScheme)
Enables or disables validation.
Definition: XMLSubSys.cpp:69
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.
SUMOReal ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:142
void setApplicationDescription(const std::string &appDesc)
Sets the application description.
const std::string osmTypemap
Definition: typemap.h:38
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
SUMOReal xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:130
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.
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:114
static void close()
Closes all of an applications subsystems.
static void addConfigurationOptions(OptionsCont &oc)
Adds configuration options to the given container.
Definition: SystemFrame.cpp:51
SUMOReal xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:136
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:65
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:69
void addSynonyme(const std::string &name1, const std::string &name2, bool isDeprecated=false)
Adds a synonyme for an options name (any order)
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
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:55
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.
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:102
static bool init(OptionsCont &oc)
Initialises the processing and the final instance using the given options.
void addOptionSubTopic(const std::string &topic)
Adds an option subtopic.
static bool checkOptions()
checks shared options and sets StdDefs
#define VERSION_STRING
Definition: version.h:1
bool isDefault(const std::string &name) const
Returns the information whether the named option has still the default value.
const Boundary & getConvBoundary() const
Returns the converted boundary.
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 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:89
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:65
const std::string navteqTypemap
Definition: pc_typemap.h:1
SUMOReal ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:148
void addDescription(const std::string &name, const std::string &subtopic, const std::string &description)
Adds a description for an option.
static void initOutputOptions()
Definition: MsgHandler.cpp:197
void fillOptions()
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
void setApplicationName(const std::string &appName, const std::string &fullName)
Sets the application name.