SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
polyconvert_main.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // Main for POLYCONVERT
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
13 // Copyright (C) 2001-2014 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 /****************************************************************************/
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #ifdef HAVE_VERSION_H
35 #include <version.h>
36 #endif
37 
38 #include <iostream>
39 #include <string>
47 #include <utils/common/ToString.h>
51 #include <utils/geom/Boundary.h>
57 #include <polyconvert/PCTypeMap.h>
60 #include <utils/xml/XMLSubSys.h>
62 
63 #ifdef CHECK_MEMORY_LEAKS
64 #include <foreign/nvwa/debug_new.h>
65 #endif // CHECK_MEMORY_LEAKS
66 
67 
68 // ===========================================================================
69 // method definitions
70 // ===========================================================================
71 void
74  oc.addCallExample("-c <CONFIGURATION>", "run with configuration options set in file");
75 
76  // insert options sub-topics
77  SystemFrame::addConfigurationOptions(oc); // fill this subtopic, too
78  oc.addOptionSubTopic("Input");
79  oc.addOptionSubTopic("Output");
81  oc.addOptionSubTopic("Pruning");
82  oc.addOptionSubTopic("Processing");
83  oc.addOptionSubTopic("Building Defaults");
84  SystemFrame::addReportOptions(oc); // fill this subtopic, too
85 
86 
87  // register options
88  // add i/o options
89  // original network
90  oc.doRegister("net-file", 'n', new Option_FileName());
91  oc.addSynonyme("net-file", "net");
92  oc.addDescription("net-file", "Input", "Loads SUMO-network FILE as reference to offset and projection");
93 
94  // dlrnavteq import
95  oc.doRegister("dlr-navteq-poly-files", new Option_FileName());
96  oc.addDescription("dlr-navteq-poly-files", "Input", "Reads polygons from FILE assuming they're coded in DLR-Navteq (Elmar)-format");
97  oc.doRegister("dlr-navteq-poi-files", new Option_FileName());
98  oc.addDescription("dlr-navteq-poi-files", "Input", "Reads pois from FILE+ assuming they're coded in DLR-Navteq (Elmar)-format");
99 
100  // visum import
101  oc.doRegister("visum-files", new Option_FileName());
102  oc.addSynonyme("visum-files", "visum");
103  oc.addDescription("visum-files", "Input", "Reads polygons from FILE assuming it's a Visum-net");
104 
105  // xml import
106  oc.doRegister("xml-files", new Option_FileName());
107  oc.addSynonyme("xml-files", "xml");
108  oc.addDescription("xml-files", "Input", "Reads pois and shapes from FILE assuming they're coded in XML");
109 
110  // osm import
111  oc.doRegister("osm-files", new Option_FileName());
112  oc.addSynonyme("osm-files", "osm");
113  oc.addDescription("osm-files", "Input", "Reads pois from FILE+ assuming they're coded in OSM");
114  oc.doRegister("osm.keep-full-type", new Option_Bool(false));
115  oc.addDescription("osm.keep-full-type", "Input", "The type will be made of the key-value - pair");
116  oc.doRegister("osm.use-name", new Option_Bool(false));
117  oc.addDescription("osm.use-name", "Input", "The id will be set from the given 'name' attribute");
118 
119  // arcview import
120  oc.doRegister("shapefile-prefixes", new Option_FileName());
121  oc.addSynonyme("shapefile-prefixes", "shapefile-prefix");
122  oc.addSynonyme("shapefile-prefixes", "shapefile");
123  oc.addSynonyme("shapefile-prefixes", "shape-files", true);
124  oc.addDescription("shapefile-prefixes", "Input", "Reads shapes from shapefiles FILE+");
125 
126  oc.doRegister("shapefile.guess-projection", new Option_Bool(false));
127  oc.addSynonyme("shapefile.guess-projection", "arcview.guess-projection", true);
128  oc.addDescription("shapefile.guess-projection", "Input", "Guesses the shapefile's projection");
129 
130  oc.doRegister("shapefile.id-column", new Option_String());
131  oc.addSynonyme("shapefile.id-column", "shapefile.id-name", true);
132  oc.addSynonyme("shapefile.id-column", "shape-files.id-name", true);
133  oc.addDescription("shapefile.id-column", "Input", "Defines in which column the id can be found");
134 
135  oc.doRegister("shapefile.use-running-id", new Option_Bool());
136  oc.addDescription("shapefile.use-running-id", "Input", "A running number will be used as id");
137 
138  oc.doRegister("shapefile.add-param", new Option_Bool());
139  oc.addDescription("shapefile.add-param", "Input", "Extract all additonal columns as params");
140 
141  // typemap reading
142  oc.doRegister("type-file", new Option_FileName());
143  oc.addSynonyme("type-file", "typemap", true);
144  oc.addDescription("type-file", "Input", "Reads types from FILE");
145 
146 
147  // output
148  oc.doRegister("output-file", 'o', new Option_FileName("polygons.xml"));
149  oc.addSynonyme("output-file", "output");
150  oc.addDescription("output-file", "Output", "Write generated polygons/pois to FILE");
151 
152 
153  // prunning options
154  oc.doRegister("prune.in-net", new Option_Bool(false));
155  oc.addSynonyme("prune.in-net", "prune.on-net", true);
156  oc.addDescription("prune.in-net", "Pruning", "Enables pruning on net boundaries");
157 
158  oc.doRegister("prune.in-net.offsets", new Option_String("0,0,0,0"));
159  oc.addSynonyme("prune.in-net.offsets", "prune.on-net.offsets", true);
160  oc.addDescription("prune.in-net.offsets", "Pruning", "Uses STR as offset definition added to the net boundaries");
161 
162  oc.doRegister("prune.boundary", new Option_String());
163  oc.addDescription("prune.boundary", "Pruning", "Uses STR as pruning boundary");
164 
165  oc.doRegister("prune.keep-list", new Option_String());
166  oc.addSynonyme("prune.keep-list", "prune.keep");
167  oc.addSynonyme("prune.keep-list", "prune.ignore", true);
168  oc.addDescription("prune.keep-list", "Pruning", "Items in STR will be kept though out of boundary");
169 
170  oc.doRegister("prune.explicit", new Option_String(""));
171  oc.addSynonyme("prune.explicit", "remove");
172  oc.addDescription("prune.explicit", "Pruning", "Items with names in STR will be removed");
173 
174 
175  oc.doRegister("offset.x", new Option_Float(0));
176  oc.addSynonyme("offset.x", "x-offset-to-apply", true);
177  oc.addDescription("offset.x", "Processing", "Adds FLOAT to net x-positions");
178 
179  oc.doRegister("offset.y", new Option_Float(0));
180  oc.addSynonyme("offset.y", "y-offset-to-apply", true);
181  oc.addDescription("offset.y", "Processing", "Adds FLOAT to net y-positions");
182 
183  oc.doRegister("all-attributes", new Option_Bool(false));
184  oc.addDescription("all-attributes", "Processing", "Imports all attributes as key/value pairs");
185 
186  oc.doRegister("ignore-errors", new Option_Bool(false));
187  oc.addDescription("ignore-errors", "Processing", "Continue on broken input");
188 
189 
190  // building defaults options
191  oc.doRegister("color", new Option_String("0.2,0.5,1."));
192  oc.addDescription("color", "Building Defaults", "Sets STR as default color");
193 
194  oc.doRegister("prefix", new Option_String(""));
195  oc.addDescription("prefix", "Building Defaults", "Sets STR as default prefix");
196 
197  oc.doRegister("type", new Option_String("unknown"));
198  oc.addDescription("type", "Building Defaults", "Sets STR as default type");
199 
200  oc.doRegister("layer", new Option_Integer(-1));
201  oc.addDescription("layer", "Building Defaults", "Sets INT as default layer");
202 
203  oc.doRegister("discard", new Option_Bool(false));
204  oc.addDescription("discard", "Building Defaults", "Sets default action to discard");
205 }
206 
207 
208 int
209 main(int argc, char** argv) {
211  oc.setApplicationDescription("Importer of polygons and POIs for the road traffic simulation SUMO.");
212  oc.setApplicationName("polyconvert", "SUMO polyconvert Version " + (std::string)VERSION_STRING);
213  int ret = 0;
214  try {
215  // initialise subsystems
216  XMLSubSys::init();
217  fillOptions();
218  OptionsIO::getOptions(true, argc, argv);
219  if (oc.processMetaOptions(argc < 2)) {
221  return 0;
222  }
223  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"));
225  // build the projection
226  int shift = 0;
227  if ((oc.isSet("dlr-navteq-poly-files") || oc.isSet("dlr-navteq-poi-files")) && oc.isDefault("proj.scale")) {
228  shift = 5;
229  }
230  if (!oc.isSet("net")) {
231  // from the given options
232 #ifdef HAVE_PROJ
233  unsigned numProjections = oc.getBool("simple-projection") + oc.getBool("proj.utm") + oc.getBool("proj.dhdn") + (oc.getString("proj").length() > 1);
234  if ((oc.isSet("osm-files") || oc.isSet("dlr-navteq-poly-files") || oc.isSet("dlr-navteq-poi-files")) && numProjections == 0) {
235  oc.set("proj.utm", "true");
236  }
237  oc.set("proj.scale", toString(shift));
238 #endif
239  if (!GeoConvHelper::init(oc)) {
240  throw ProcessError("Could not build projection!");
241  }
242  } else {
243  // from the supplied network
244  // @todo warn about given options being ignored
245  PCNetProjectionLoader::load(oc.getString("net"), shift);
246  }
247  Boundary pruningBoundary = GeoConvHelper::getFinal().getConvBoundary();
248  // check whether the input shall be pruned
249  bool prune = false;
250  if (oc.getBool("prune.in-net")) {
251  if (!oc.isSet("net")) {
252  throw ProcessError("In order to prune the input on the net, you have to supply a network.");
253  }
254  bool ok = true;
255  // !!! no proper error handling
256  Boundary offsets = GeomConvHelper::parseBoundaryReporting(oc.getString("prune.in-net.offsets"), "--prune.on-net.offsets", 0, ok);
257  pruningBoundary = Boundary(
258  pruningBoundary.xmin() + offsets.xmin(),
259  pruningBoundary.ymin() + offsets.ymin(),
260  pruningBoundary.xmax() + offsets.xmax(),
261  pruningBoundary.ymax() + offsets.ymax());
262  prune = true;
263  }
264  if (oc.isSet("prune.boundary")) {
265  bool ok = true;
266  // !!! no proper error handling
267  pruningBoundary = GeomConvHelper::parseBoundaryReporting(oc.getString("prune.boundary"), "--prune.boundary", 0, ok);
268  prune = true;
269  }
270 
271  PCPolyContainer toFill(prune, pruningBoundary, oc.getStringVector("remove"));
272 
273  // read in the type defaults
274  PCTypeMap tm(oc);
275  if (oc.isSet("type-file")) {
276  PCTypeDefHandler handler(oc, tm);
277  if (!XMLSubSys::runParser(handler, oc.getString("type-file"))) {
278  // something failed
279  throw ProcessError();
280  }
281  }
282 
283  // read in the data
284  PCLoaderXML::loadIfSet(oc, toFill, tm); // SUMO-XML
285  PCLoaderOSM::loadIfSet(oc, toFill, tm); // OSM-XML
286  PCLoaderDlrNavteq::loadIfSet(oc, toFill, tm); // Elmar-files
287  PCLoaderVisum::loadIfSet(oc, toFill, tm); // VISUM
288  PCLoaderArcView::loadIfSet(oc, toFill, tm); // shape-files
289  // error processing
290  if (MsgHandler::getErrorInstance()->wasInformed() && oc.getBool("ignore-errors")) {
292  }
293  if (!MsgHandler::getErrorInstance()->wasInformed()) {
294  // no? ok, save
295  toFill.save(oc.getString("output-file"));
296  } else {
297  throw ProcessError();
298  }
299  } catch (const ProcessError& e) {
300  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
301  WRITE_ERROR(e.what());
302  }
303  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
304  ret = 1;
305 #ifndef _DEBUG
306  } catch (const std::exception& e) {
307  if (std::string(e.what()) != std::string("")) {
308  WRITE_ERROR(e.what());
309  }
310  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
311  ret = 1;
312  } catch (...) {
313  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
314  ret = 1;
315 #endif
316  }
318  // report about ending
319  if (ret == 0) {
320  std::cout << "Success." << std::endl;
321  }
322  return ret;
323 }
324 
325 
326 
327 /****************************************************************************/
328 
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:84
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:96
static void addReportOptions(OptionsCont &oc)
Adds reporting options to the given container.
Definition: SystemFrame.cpp:74
int main(int argc, char **argv)
static void getOptions(bool loadConfig, int argc=0, char **argv=0)
Parses the command line arguments and loads the configuration optionally.
Definition: OptionsIO.cpp:64
static void setValidation(const std::string &validationScheme, const std::string &netValidationScheme)
Enables or disables validation.
Definition: XMLSubSys.cpp:69
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'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 OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:67
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:51
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.
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:52
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:227
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:201
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:308
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:193
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.