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-2015 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>
52 #include <utils/geom/Boundary.h>
58 #include <polyconvert/PCTypeMap.h>
61 #include <utils/xml/XMLSubSys.h>
63 
64 #ifdef CHECK_MEMORY_LEAKS
65 #include <foreign/nvwa/debug_new.h>
66 #endif // CHECK_MEMORY_LEAKS
67 
68 
69 // ===========================================================================
70 // method definitions
71 // ===========================================================================
72 void
75  oc.addCallExample("-c <CONFIGURATION>", "run with configuration options set in file");
76 
77  // insert options sub-topics
78  SystemFrame::addConfigurationOptions(oc); // fill this subtopic, too
79  oc.addOptionSubTopic("Input");
80  oc.addOptionSubTopic("Output");
82  oc.addOptionSubTopic("Pruning");
83  oc.addOptionSubTopic("Processing");
84  oc.addOptionSubTopic("Building Defaults");
85  SystemFrame::addReportOptions(oc); // fill this subtopic, too
86 
87 
88  // register options
89  // add i/o options
90  // original network
91  oc.doRegister("net-file", 'n', new Option_FileName());
92  oc.addSynonyme("net-file", "net");
93  oc.addDescription("net-file", "Input", "Loads SUMO-network FILE as reference to offset and projection");
94 
95  // dlrnavteq import
96  oc.doRegister("dlr-navteq-poly-files", new Option_FileName());
97  oc.addDescription("dlr-navteq-poly-files", "Input", "Reads polygons from FILE assuming they're coded in DLR-Navteq (Elmar)-format");
98  oc.doRegister("dlr-navteq-poi-files", new Option_FileName());
99  oc.addDescription("dlr-navteq-poi-files", "Input", "Reads pois from FILE+ assuming they're coded in DLR-Navteq (Elmar)-format");
100 
101  // visum import
102  oc.doRegister("visum-files", new Option_FileName());
103  oc.addSynonyme("visum-files", "visum");
104  oc.addDescription("visum-files", "Input", "Reads polygons from FILE assuming it's a Visum-net");
105 
106  // xml import
107  oc.doRegister("xml-files", new Option_FileName());
108  oc.addSynonyme("xml-files", "xml");
109  oc.addDescription("xml-files", "Input", "Reads pois and shapes from FILE assuming they're coded in XML");
110 
111  // osm import
112  oc.doRegister("osm-files", new Option_FileName());
113  oc.addSynonyme("osm-files", "osm");
114  oc.addDescription("osm-files", "Input", "Reads pois from FILE+ assuming they're coded in OSM");
115  oc.doRegister("osm.keep-full-type", new Option_Bool(false));
116  oc.addDescription("osm.keep-full-type", "Input", "The type will be made of the key-value - pair");
117  oc.doRegister("osm.use-name", new Option_Bool(false));
118  oc.addDescription("osm.use-name", "Input", "The id will be set from the given 'name' attribute");
119 
120  // arcview import
121  oc.doRegister("shapefile-prefixes", new Option_FileName());
122  oc.addSynonyme("shapefile-prefixes", "shapefile-prefix");
123  oc.addSynonyme("shapefile-prefixes", "shapefile");
124  oc.addSynonyme("shapefile-prefixes", "shape-files", true);
125  oc.addDescription("shapefile-prefixes", "Input", "Reads shapes from shapefiles FILE+");
126 
127  oc.doRegister("shapefile.guess-projection", new Option_Bool(false));
128  oc.addSynonyme("shapefile.guess-projection", "arcview.guess-projection", true);
129  oc.addDescription("shapefile.guess-projection", "Input", "Guesses the shapefile's projection");
130 
131  oc.doRegister("shapefile.id-column", new Option_String());
132  oc.addSynonyme("shapefile.id-column", "shapefile.id-name", true);
133  oc.addSynonyme("shapefile.id-column", "shape-files.id-name", true);
134  oc.addDescription("shapefile.id-column", "Input", "Defines in which column the id can be found");
135 
136  oc.doRegister("shapefile.use-running-id", new Option_Bool());
137  oc.addDescription("shapefile.use-running-id", "Input", "A running number will be used as id");
138 
139  oc.doRegister("shapefile.add-param", new Option_Bool());
140  oc.addDescription("shapefile.add-param", "Input", "Extract all additonal columns as params");
141 
142  oc.doRegister("shapefile.fill", new Option_String());
143  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");
144 
145  // typemap reading
146  oc.doRegister("type-file", new Option_FileName());
147  oc.addSynonyme("type-file", "typemap", true);
148  oc.addDescription("type-file", "Input", "Reads types from FILE");
149 
150 
151  // output
152  oc.doRegister("output-file", 'o', new Option_FileName("polygons.xml"));
153  oc.addSynonyme("output-file", "output");
154  oc.addDescription("output-file", "Output", "Write generated polygons/pois to FILE");
155 
156 
157  // prunning options
158  oc.doRegister("prune.in-net", new Option_Bool(false));
159  oc.addSynonyme("prune.in-net", "prune.on-net", true);
160  oc.addDescription("prune.in-net", "Pruning", "Enables pruning on net boundaries");
161 
162  oc.doRegister("prune.in-net.offsets", new Option_String("0,0,0,0"));
163  oc.addSynonyme("prune.in-net.offsets", "prune.on-net.offsets", true);
164  oc.addDescription("prune.in-net.offsets", "Pruning", "Uses STR as offset definition added to the net boundaries");
165 
166  oc.doRegister("prune.boundary", new Option_String());
167  oc.addDescription("prune.boundary", "Pruning", "Uses STR as pruning boundary");
168 
169  oc.doRegister("prune.keep-list", new Option_String());
170  oc.addSynonyme("prune.keep-list", "prune.keep");
171  oc.addSynonyme("prune.keep-list", "prune.ignore", true);
172  oc.addDescription("prune.keep-list", "Pruning", "Items in STR will be kept though out of boundary");
173 
174  oc.doRegister("prune.explicit", new Option_String(""));
175  oc.addSynonyme("prune.explicit", "remove");
176  oc.addDescription("prune.explicit", "Pruning", "Items with names in STR will be removed");
177 
178 
179  oc.doRegister("offset.x", new Option_Float(0));
180  oc.addSynonyme("offset.x", "x-offset-to-apply", true);
181  oc.addDescription("offset.x", "Processing", "Adds FLOAT to net x-positions");
182 
183  oc.doRegister("offset.y", new Option_Float(0));
184  oc.addSynonyme("offset.y", "y-offset-to-apply", true);
185  oc.addDescription("offset.y", "Processing", "Adds FLOAT to net y-positions");
186 
187  oc.doRegister("all-attributes", new Option_Bool(false));
188  oc.addDescription("all-attributes", "Processing", "Imports all attributes as key/value pairs");
189 
190  oc.doRegister("ignore-errors", new Option_Bool(false));
191  oc.addDescription("ignore-errors", "Processing", "Continue on broken input");
192 
193  oc.doRegister("poi-layer-offset", new Option_Float(0));
194  oc.addDescription("poi-layer-offset", "Processing", "Adds FLOAT to the layer value for each poi (i.e. to raise it above polygons)");
195 
196  // building defaults options
197  oc.doRegister("color", new Option_String("0.2,0.5,1."));
198  oc.addDescription("color", "Building Defaults", "Sets STR as default color");
199 
200  oc.doRegister("prefix", new Option_String(""));
201  oc.addDescription("prefix", "Building Defaults", "Sets STR as default prefix");
202 
203  oc.doRegister("type", new Option_String("unknown"));
204  oc.addDescription("type", "Building Defaults", "Sets STR as default type");
205 
206  oc.doRegister("fill", new Option_Bool("false"));
207  oc.addDescription("fill", "Building Defaults", "Fills polygons by default");
208 
209  oc.doRegister("layer", new Option_Integer(-1));
210  oc.addDescription("layer", "Building Defaults", "Sets INT as default layer");
211 
212  oc.doRegister("discard", new Option_Bool(false));
213  oc.addDescription("discard", "Building Defaults", "Sets default action to discard");
214 
215  // projection
216  oc.doRegister("proj.plain-geo", new Option_Bool(false));
217  oc.addDescription("proj.plain-geo", "Projection", "Write geo coordinates in output");
218 }
219 
220 
221 int
222 main(int argc, char** argv) {
224  oc.setApplicationDescription("Importer of polygons and POIs for the road traffic simulation SUMO.");
225  oc.setApplicationName("polyconvert", "SUMO polyconvert Version " + getBuildName(VERSION_STRING));
226  int ret = 0;
227  try {
228  // initialise subsystems
229  XMLSubSys::init();
230  fillOptions();
231  OptionsIO::setArgs(argc, argv);
233  if (oc.processMetaOptions(argc < 2)) {
235  return 0;
236  }
237  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"));
239  // build the projection
240  int shift = 0;
241  if ((oc.isSet("dlr-navteq-poly-files") || oc.isSet("dlr-navteq-poi-files")) && oc.isDefault("proj.scale")) {
242  shift = 5;
243  }
244  if (!oc.isSet("net")) {
245  // from the given options
246 #ifdef HAVE_PROJ
247  unsigned numProjections = oc.getBool("simple-projection") + oc.getBool("proj.utm") + oc.getBool("proj.dhdn") + (oc.getString("proj").length() > 1);
248  if ((oc.isSet("osm-files") || oc.isSet("dlr-navteq-poly-files") || oc.isSet("dlr-navteq-poi-files")) && numProjections == 0) {
249  oc.set("proj.utm", "true");
250  }
251  oc.set("proj.scale", toString(shift));
252 #endif
253  if (!GeoConvHelper::init(oc)) {
254  throw ProcessError("Could not build projection!");
255  }
256  } else {
257  // from the supplied network
258  // @todo warn about given options being ignored
259  PCNetProjectionLoader::load(oc.getString("net"), shift);
260  }
261  Boundary pruningBoundary = GeoConvHelper::getFinal().getConvBoundary();
262  // check whether the input shall be pruned
263  bool prune = false;
264  if (oc.getBool("prune.in-net")) {
265  if (!oc.isSet("net")) {
266  throw ProcessError("In order to prune the input on the net, you have to supply a network.");
267  }
268  bool ok = true;
269  // !!! no proper error handling
270  Boundary offsets = GeomConvHelper::parseBoundaryReporting(oc.getString("prune.in-net.offsets"), "--prune.on-net.offsets", 0, ok);
271  pruningBoundary = Boundary(
272  pruningBoundary.xmin() + offsets.xmin(),
273  pruningBoundary.ymin() + offsets.ymin(),
274  pruningBoundary.xmax() + offsets.xmax(),
275  pruningBoundary.ymax() + offsets.ymax());
276  prune = true;
277  }
278  if (oc.isSet("prune.boundary")) {
279  bool ok = true;
280  // !!! no proper error handling
281  pruningBoundary = GeomConvHelper::parseBoundaryReporting(oc.getString("prune.boundary"), "--prune.boundary", 0, ok);
282  prune = true;
283  }
284  if (oc.isSet("osm-files") && oc.isDefault("poi-layer-offset")) {
285  oc.set("poi-layer-offset", "5"); // sufficient when using the default typemap
286  }
287 
288  PCPolyContainer toFill(prune, pruningBoundary, oc.getStringVector("remove"));
289 
290  // read in the type defaults
291  PCTypeMap tm(oc);
292  if (oc.isSet("type-file")) {
293  PCTypeDefHandler handler(oc, tm);
294  if (!XMLSubSys::runParser(handler, oc.getString("type-file"))) {
295  // something failed
296  throw ProcessError();
297  }
298  }
299 
300  // read in the data
301  PCLoaderXML::loadIfSet(oc, toFill, tm); // SUMO-XML
302  PCLoaderOSM::loadIfSet(oc, toFill, tm); // OSM-XML
303  PCLoaderDlrNavteq::loadIfSet(oc, toFill, tm); // Elmar-files
304  PCLoaderVisum::loadIfSet(oc, toFill, tm); // VISUM
305  PCLoaderArcView::loadIfSet(oc, toFill, tm); // shape-files
307  // error processing
308  if (MsgHandler::getErrorInstance()->wasInformed() && oc.getBool("ignore-errors")) {
310  }
311  if (!MsgHandler::getErrorInstance()->wasInformed()) {
312  // no? ok, save
313  toFill.save(oc.getString("output-file"), oc.getBool("proj.plain-geo"));
314  } else {
315  throw ProcessError();
316  }
317  } catch (const ProcessError& e) {
318  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
319  WRITE_ERROR(e.what());
320  }
321  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
322  ret = 1;
323 #ifndef _DEBUG
324  } catch (const std::exception& e) {
325  if (std::string(e.what()) != std::string("")) {
326  WRITE_ERROR(e.what());
327  }
328  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
329  ret = 1;
330  } catch (...) {
331  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
332  ret = 1;
333 #endif
334  }
336  // report about ending
337  if (ret == 0) {
338  std::cout << "Success." << std::endl;
339  }
340  return ret;
341 }
342 
343 
344 
345 /****************************************************************************/
346 
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 void addReportOptions(OptionsCont &oc)
Adds reporting options to the given container.
Definition: SystemFrame.cpp:74
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:124
void setApplicationDescription(const std::string &appDesc)
Sets the application description.
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:112
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.
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:50
SUMOReal xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:118
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:48
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:64
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
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 getBuildName(const std::string &version)
attach some build flags to the version string
Definition: StdDefs.cpp:91
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.
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.
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:53
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:226
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:205
static void getOptions()
Parses the command line arguments and loads the configuration.
Definition: OptionsIO.cpp:71
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.
An integer-option.
Definition: Option.h:309
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:108
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
SUMOReal ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:130
void addDescription(const std::string &name, const std::string &subtopic, const std::string &description)
Adds a description for an option.
void clear()
Clears information whether an error occured previously.
Definition: MsgHandler.cpp:149
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.