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 /****************************************************************************/
11 // Main for POLYCONVERT
12 /****************************************************************************/
13 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
14 // Copyright (C) 2005-2014 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  // typemap reading
143  oc.doRegister("type-file", new Option_FileName());
144  oc.addSynonyme("type-file", "typemap", true);
145  oc.addDescription("type-file", "Input", "Reads types from FILE");
146 
147 
148  // output
149  oc.doRegister("output-file", 'o', new Option_FileName("polygons.xml"));
150  oc.addSynonyme("output-file", "output");
151  oc.addDescription("output-file", "Output", "Write generated polygons/pois to FILE");
152 
153 
154  // prunning options
155  oc.doRegister("prune.in-net", new Option_Bool(false));
156  oc.addSynonyme("prune.in-net", "prune.on-net", true);
157  oc.addDescription("prune.in-net", "Pruning", "Enables pruning on net boundaries");
158 
159  oc.doRegister("prune.in-net.offsets", new Option_String("0,0,0,0"));
160  oc.addSynonyme("prune.in-net.offsets", "prune.on-net.offsets", true);
161  oc.addDescription("prune.in-net.offsets", "Pruning", "Uses STR as offset definition added to the net boundaries");
162 
163  oc.doRegister("prune.boundary", new Option_String());
164  oc.addDescription("prune.boundary", "Pruning", "Uses STR as pruning boundary");
165 
166  oc.doRegister("prune.keep-list", new Option_String());
167  oc.addSynonyme("prune.keep-list", "prune.keep");
168  oc.addSynonyme("prune.keep-list", "prune.ignore", true);
169  oc.addDescription("prune.keep-list", "Pruning", "Items in STR will be kept though out of boundary");
170 
171  oc.doRegister("prune.explicit", new Option_String(""));
172  oc.addSynonyme("prune.explicit", "remove");
173  oc.addDescription("prune.explicit", "Pruning", "Items with names in STR will be removed");
174 
175 
176  oc.doRegister("offset.x", new Option_Float(0));
177  oc.addSynonyme("offset.x", "x-offset-to-apply", true);
178  oc.addDescription("offset.x", "Processing", "Adds FLOAT to net x-positions");
179 
180  oc.doRegister("offset.y", new Option_Float(0));
181  oc.addSynonyme("offset.y", "y-offset-to-apply", true);
182  oc.addDescription("offset.y", "Processing", "Adds FLOAT to net y-positions");
183 
184  oc.doRegister("all-attributes", new Option_Bool(false));
185  oc.addDescription("all-attributes", "Processing", "Imports all attributes as key/value pairs");
186 
187  oc.doRegister("ignore-errors", new Option_Bool(false));
188  oc.addDescription("ignore-errors", "Processing", "Continue on broken input");
189 
190 
191  // building defaults options
192  oc.doRegister("color", new Option_String("0.2,0.5,1."));
193  oc.addDescription("color", "Building Defaults", "Sets STR as default color");
194 
195  oc.doRegister("prefix", new Option_String(""));
196  oc.addDescription("prefix", "Building Defaults", "Sets STR as default prefix");
197 
198  oc.doRegister("type", new Option_String("unknown"));
199  oc.addDescription("type", "Building Defaults", "Sets STR as default type");
200 
201  oc.doRegister("layer", new Option_Integer(-1));
202  oc.addDescription("layer", "Building Defaults", "Sets INT as default layer");
203 
204  oc.doRegister("discard", new Option_Bool(false));
205  oc.addDescription("discard", "Building Defaults", "Sets default action to discard");
206 }
207 
208 
209 int
210 main(int argc, char** argv) {
212  oc.setApplicationDescription("Importer of polygons and POIs for the road traffic simulation SUMO.");
213  oc.setApplicationName("polyconvert", "SUMO polyconvert Version " + (std::string)VERSION_STRING);
214  int ret = 0;
215  try {
216  // initialise subsystems
217  XMLSubSys::init();
218  fillOptions();
219  OptionsIO::getOptions(true, argc, argv);
220  if (oc.processMetaOptions(argc < 2)) {
222  return 0;
223  }
224  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"));
226  // build the projection
227  int shift = 0;
228  if ((oc.isSet("dlr-navteq-poly-files") || oc.isSet("dlr-navteq-poi-files")) && oc.isDefault("proj.scale")) {
229  shift = 5;
230  }
231  if (!oc.isSet("net")) {
232  // from the given options
233 #ifdef HAVE_PROJ
234  unsigned numProjections = oc.getBool("simple-projection") + oc.getBool("proj.utm") + oc.getBool("proj.dhdn") + (oc.getString("proj").length() > 1);
235  if ((oc.isSet("osm-files") || oc.isSet("dlr-navteq-poly-files") || oc.isSet("dlr-navteq-poi-files")) && numProjections == 0) {
236  oc.set("proj.utm", "true");
237  }
238  oc.set("proj.scale", toString(shift));
239 #endif
240  if (!GeoConvHelper::init(oc)) {
241  throw ProcessError("Could not build projection!");
242  }
243  } else {
244  // from the supplied network
245  // @todo warn about given options being ignored
246  PCNetProjectionLoader::load(oc.getString("net"), shift);
247  }
248  Boundary pruningBoundary = GeoConvHelper::getFinal().getConvBoundary();
249  // check whether the input shall be pruned
250  bool prune = false;
251  if (oc.getBool("prune.in-net")) {
252  if (!oc.isSet("net")) {
253  throw ProcessError("In order to prune the input on the net, you have to supply a network.");
254  }
255  bool ok = true;
256  // !!! no proper error handling
257  Boundary offsets = GeomConvHelper::parseBoundaryReporting(oc.getString("prune.in-net.offsets"), "--prune.on-net.offsets", 0, ok);
258  pruningBoundary = Boundary(
259  pruningBoundary.xmin() + offsets.xmin(),
260  pruningBoundary.ymin() + offsets.ymin(),
261  pruningBoundary.xmax() + offsets.xmax(),
262  pruningBoundary.ymax() + offsets.ymax());
263  prune = true;
264  }
265  if (oc.isSet("prune.boundary")) {
266  bool ok = true;
267  // !!! no proper error handling
268  pruningBoundary = GeomConvHelper::parseBoundaryReporting(oc.getString("prune.boundary"), "--prune.boundary", 0, ok);
269  prune = true;
270  }
271 
272  PCPolyContainer toFill(prune, pruningBoundary, oc.getStringVector("remove"));
273 
274  // read in the type defaults
275  PCTypeMap tm(oc);
276  if (oc.isSet("type-file")) {
277  PCTypeDefHandler handler(oc, tm);
278  if (!XMLSubSys::runParser(handler, oc.getString("type-file"))) {
279  // something failed
280  throw ProcessError();
281  }
282  }
283 
284  // read in the data
285  PCLoaderXML::loadIfSet(oc, toFill, tm); // SUMO-XML
286  PCLoaderOSM::loadIfSet(oc, toFill, tm); // OSM-XML
287  PCLoaderDlrNavteq::loadIfSet(oc, toFill, tm); // Elmar-files
288  PCLoaderVisum::loadIfSet(oc, toFill, tm); // VISUM
289  PCLoaderArcView::loadIfSet(oc, toFill, tm); // shape-files
290  // error processing
291  if (MsgHandler::getErrorInstance()->wasInformed() && oc.getBool("ignore-errors")) {
293  }
294  if (!MsgHandler::getErrorInstance()->wasInformed()) {
295  // no? ok, save
296  toFill.save(oc.getString("output-file"));
297  } else {
298  throw ProcessError();
299  }
300  } catch (const ProcessError& e) {
301  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
302  WRITE_ERROR(e.what());
303  }
304  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
305  ret = 1;
306 #ifndef _DEBUG
307  } catch (const std::exception& e) {
308  if (std::string(e.what()) != std::string("")) {
309  WRITE_ERROR(e.what());
310  }
311  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
312  ret = 1;
313  } catch (...) {
314  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
315  ret = 1;
316 #endif
317  }
319  // report about ending
320  if (ret == 0) {
321  std::cout << "Success." << std::endl;
322  }
323  return ret;
324 }
325 
326 
327 
328 /****************************************************************************/
329 
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: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 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: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.
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:227
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:205
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.