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-2016 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());
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());
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("polygons.xml"));
155  oc.addSynonyme("output-file", "output");
156  oc.addDescription("output-file", "Output", "Write generated polygons/pois to FILE");
157 
158 
159  // prunning options
160  oc.doRegister("prune.in-net", new Option_Bool(false));
161  oc.addSynonyme("prune.in-net", "prune.on-net", true);
162  oc.addDescription("prune.in-net", "Pruning", "Enables pruning on net boundaries");
163 
164  oc.doRegister("prune.in-net.offsets", new Option_String("0,0,0,0"));
165  oc.addSynonyme("prune.in-net.offsets", "prune.on-net.offsets", true);
166  oc.addDescription("prune.in-net.offsets", "Pruning", "Uses STR as offset definition added to the net boundaries");
167 
168  oc.doRegister("prune.boundary", new Option_String());
169  oc.addDescription("prune.boundary", "Pruning", "Uses STR as pruning boundary");
170 
171  oc.doRegister("prune.keep-list", new Option_String());
172  oc.addSynonyme("prune.keep-list", "prune.keep");
173  oc.addSynonyme("prune.keep-list", "prune.ignore", true);
174  oc.addDescription("prune.keep-list", "Pruning", "Items in STR will be kept though out of boundary");
175 
176  oc.doRegister("prune.explicit", new Option_String(""));
177  oc.addSynonyme("prune.explicit", "remove");
178  oc.addDescription("prune.explicit", "Pruning", "Items with names in STR will be removed");
179 
180 
181  oc.doRegister("offset.x", new Option_Float(0));
182  oc.addSynonyme("offset.x", "x-offset-to-apply", true);
183  oc.addDescription("offset.x", "Processing", "Adds FLOAT to net x-positions");
184 
185  oc.doRegister("offset.y", new Option_Float(0));
186  oc.addSynonyme("offset.y", "y-offset-to-apply", true);
187  oc.addDescription("offset.y", "Processing", "Adds FLOAT to net y-positions");
188 
189  oc.doRegister("all-attributes", new Option_Bool(false));
190  oc.addDescription("all-attributes", "Processing", "Imports all attributes as key/value pairs");
191 
192  oc.doRegister("ignore-errors", new Option_Bool(false));
193  oc.addDescription("ignore-errors", "Processing", "Continue on broken input");
194 
195  oc.doRegister("poi-layer-offset", new Option_Float(0));
196  oc.addDescription("poi-layer-offset", "Processing", "Adds FLOAT to the layer value for each poi (i.e. to raise it above polygons)");
197 
198  // building defaults options
199  oc.doRegister("color", new Option_String("0.2,0.5,1."));
200  oc.addDescription("color", "Building Defaults", "Sets STR as default color");
201 
202  oc.doRegister("prefix", new Option_String(""));
203  oc.addDescription("prefix", "Building Defaults", "Sets STR as default prefix");
204 
205  oc.doRegister("type", new Option_String("unknown"));
206  oc.addDescription("type", "Building Defaults", "Sets STR as default type");
207 
208  oc.doRegister("fill", new Option_Bool("false"));
209  oc.addDescription("fill", "Building Defaults", "Fills polygons by default");
210 
211  oc.doRegister("layer", new Option_Integer(-1));
212  oc.addDescription("layer", "Building Defaults", "Sets INT as default layer");
213 
214  oc.doRegister("discard", new Option_Bool(false));
215  oc.addDescription("discard", "Building Defaults", "Sets default action to discard");
216 
217  // projection
218  oc.doRegister("proj.plain-geo", new Option_Bool(false));
219  oc.addDescription("proj.plain-geo", "Projection", "Write geo coordinates in output");
220 }
221 
222 
223 int
224 main(int argc, char** argv) {
226  oc.setApplicationDescription("Importer of polygons and POIs for the road traffic simulation SUMO.");
227  oc.setApplicationName("polyconvert", "SUMO polyconvert Version " VERSION_STRING);
228  int ret = 0;
229  try {
230  // initialise subsystems
231  XMLSubSys::init();
232  fillOptions();
233  OptionsIO::setArgs(argc, argv);
235  if (oc.processMetaOptions(argc < 2)) {
237  return 0;
238  }
239  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"));
241  // build the projection
242  int shift = 0;
243  if ((oc.isSet("dlr-navteq-poly-files") || oc.isSet("dlr-navteq-poi-files")) && oc.isDefault("proj.scale")) {
244  shift = 5;
245  }
246  if (!oc.isSet("net")) {
247  // from the given options
248 #ifdef HAVE_PROJ
249  unsigned numProjections = oc.getBool("simple-projection") + oc.getBool("proj.utm") + oc.getBool("proj.dhdn") + (oc.getString("proj").length() > 1);
250  if ((oc.isSet("osm-files") || oc.isSet("dlr-navteq-poly-files") || oc.isSet("dlr-navteq-poi-files")) && numProjections == 0) {
251  oc.set("proj.utm", "true");
252  }
253  oc.set("proj.scale", toString(shift));
254 #endif
255  if (!GeoConvHelper::init(oc)) {
256  throw ProcessError("Could not build projection!");
257  }
258  } else {
259  // from the supplied network
260  // @todo warn about given options being ignored
261  PCNetProjectionLoader::load(oc.getString("net"), shift);
262  }
263  Boundary pruningBoundary = GeoConvHelper::getFinal().getConvBoundary();
264  // check whether the input shall be pruned
265  bool prune = false;
266  if (oc.getBool("prune.in-net")) {
267  if (!oc.isSet("net")) {
268  throw ProcessError("In order to prune the input on the net, you have to supply a network.");
269  }
270  bool ok = true;
271  // !!! no proper error handling
272  Boundary offsets = GeomConvHelper::parseBoundaryReporting(oc.getString("prune.in-net.offsets"), "--prune.on-net.offsets", 0, ok);
273  pruningBoundary = Boundary(
274  pruningBoundary.xmin() + offsets.xmin(),
275  pruningBoundary.ymin() + offsets.ymin(),
276  pruningBoundary.xmax() + offsets.xmax(),
277  pruningBoundary.ymax() + offsets.ymax());
278  prune = true;
279  }
280  if (oc.isSet("prune.boundary")) {
281  bool ok = true;
282  // !!! no proper error handling
283  pruningBoundary = GeomConvHelper::parseBoundaryReporting(oc.getString("prune.boundary"), "--prune.boundary", 0, ok);
284  prune = true;
285  }
286  if (oc.isSet("osm-files") && oc.isDefault("poi-layer-offset")) {
287  oc.set("poi-layer-offset", "5"); // sufficient when using the default typemap
288  }
289 
290  PCPolyContainer toFill(prune, pruningBoundary, oc.getStringVector("remove"));
291 
292  // read in the type defaults
293  if (!oc.isSet("type-file")) {
294  const char* sumoPath = std::getenv("SUMO_HOME");
295  if (sumoPath == 0) {
296  WRITE_WARNING("Environment variable SUMO_HOME is not set, using built in type maps.");
297  } else {
298  const std::string path = sumoPath + std::string("/data/typemap/");
299  if (oc.isSet("dlr-navteq-poly-files")) {
300  oc.set("type-file", path + "navteqPolyconvert.typ.xml");
301  }
302  if (oc.isSet("osm-files")) {
303  oc.set("type-file", path + "osmPolyconvert.typ.xml");
304  }
305  if (oc.isSet("visum-files")) {
306  oc.set("type-file", path + "visumPolyconvert.typ.xml");
307  }
308  }
309  }
310  PCTypeMap tm(oc);
311  PCTypeDefHandler handler(oc, tm);
312  if (oc.isSet("type-file")) {
313  if (!XMLSubSys::runParser(handler, oc.getString("type-file"))) {
314  // something failed
315  throw ProcessError();
316  }
317  } else {
318  handler.setFileName("built in type map");
319  SUMOSAXReader* reader = XMLSubSys::getSAXReader(handler);
320  if (oc.isSet("dlr-navteq-poly-files")) {
321  reader->parseString(navteqTypemap);
322  }
323  if (oc.isSet("osm-files")) {
324  reader->parseString(osmTypemap);
325  }
326  if (oc.isSet("visum-files")) {
327  reader->parseString(visumTypemap);
328  }
329  delete reader;
330  }
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")) {
341  }
342  if (!MsgHandler::getErrorInstance()->wasInformed()) {
343  // no? ok, save
344  toFill.save(oc.getString("output-file"), oc.getBool("proj.plain-geo"));
345  } else {
346  throw ProcessError();
347  }
348  } catch (const ProcessError& e) {
349  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
350  WRITE_ERROR(e.what());
351  }
352  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
353  ret = 1;
354 #ifndef _DEBUG
355  } catch (const std::exception& e) {
356  if (std::string(e.what()) != std::string("")) {
357  WRITE_ERROR(e.what());
358  }
359  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
360  ret = 1;
361  } catch (...) {
362  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
363  ret = 1;
364 #endif
365  }
367  // report about ending
368  if (ret == 0) {
369  std::cout << "Success." << std::endl;
370  }
371  return ret;
372 }
373 
374 
375 
376 /****************************************************************************/
377 
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:75
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.
const std::string osmTypemap
Definition: typemap.h:11
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
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:50
SUMOReal xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:118
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.
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.
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:54
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:225
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:206
static void getOptions()
Parses the command line arguments and loads the configuration.
Definition: OptionsIO.cpp:72
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:313
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
const std::string navteqTypemap
Definition: pc_typemap.h:1
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.