Eclipse SUMO - Simulation of Urban MObility
polyconvert_main.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2005-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
19 // Main for POLYCONVERT
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #include <config.h>
27 
28 #ifdef HAVE_VERSION_H
29 #include <version.h>
30 #endif
31 
32 #include <iostream>
33 #include <string>
41 #include <utils/common/ToString.h>
46 #include <utils/geom/Boundary.h>
48 #include <utils/xml/XMLSubSys.h>
54 #include <polyconvert/PCTypeMap.h>
57 #include <polyconvert/pc_typemap.h>
58 
59 
60 // ===========================================================================
61 // method definitions
62 // ===========================================================================
63 void
66  oc.addCallExample("-c <CONFIGURATION>", "run with configuration options set in file");
67 
68  // insert options sub-topics
69  SystemFrame::addConfigurationOptions(oc); // fill this subtopic, too
70  oc.addOptionSubTopic("Input");
71  oc.addOptionSubTopic("Output");
73  oc.addOptionSubTopic("Pruning");
74  oc.addOptionSubTopic("Processing");
75  oc.addOptionSubTopic("Building Defaults");
76  SystemFrame::addReportOptions(oc); // fill this subtopic, too
77 
78 
79  // register options
80  // add i/o options
81  // original network
82  oc.doRegister("net-file", 'n', new Option_FileName());
83  oc.addSynonyme("net-file", "net");
84  oc.addDescription("net-file", "Input", "Loads SUMO-network FILE as reference to offset and projection");
85 
86  // dlrnavteq import
87  oc.doRegister("dlr-navteq-poly-files", new Option_FileName());
88  oc.addDescription("dlr-navteq-poly-files", "Input", "Reads polygons from FILE assuming they're coded in DLR-Navteq (Elmar)-format");
89  oc.doRegister("dlr-navteq-poi-files", new Option_FileName());
90  oc.addDescription("dlr-navteq-poi-files", "Input", "Reads pois from FILE+ assuming they're coded in DLR-Navteq (Elmar)-format");
91 
92  // visum import
93  oc.doRegister("visum-files", new Option_FileName());
94  oc.addSynonyme("visum-files", "visum");
95  oc.addDescription("visum-files", "Input", "Reads polygons from FILE assuming it's a Visum-net");
96 
97  // xml import
98  oc.doRegister("xml-files", new Option_FileName());
99  oc.addSynonyme("xml-files", "xml");
100  oc.addDescription("xml-files", "Input", "Reads pois and shapes from FILE assuming they're coded in XML");
101 
102  // osm import
103  oc.doRegister("osm-files", new Option_FileName());
104  oc.addSynonyme("osm-files", "osm");
105  oc.addDescription("osm-files", "Input", "Reads pois from FILE+ assuming they're coded in OSM");
106  oc.doRegister("osm.keep-full-type", new Option_Bool(false));
107  oc.addDescription("osm.keep-full-type", "Input", "The type will be made of the key-value - pair");
108  oc.doRegister("osm.use-name", new Option_Bool(false));
109  oc.addDescription("osm.use-name", "Input", "The id will be set from the given 'name' attribute");
110  oc.doRegister("osm.merge-relations", new Option_Float(-1));
111  oc.addDescription("osm.merge-relations", "Input", "If FLOAT >= 0, assemble one polygon from all ways of a relation if they all connect with gaps below FLOAT");
112 
113  // arcview import
114  oc.doRegister("shapefile-prefixes", new Option_FileName());
115  oc.addSynonyme("shapefile-prefixes", "shapefile-prefix");
116  oc.addSynonyme("shapefile-prefixes", "shapefile");
117  oc.addSynonyme("shapefile-prefixes", "shape-files", true);
118  oc.addDescription("shapefile-prefixes", "Input", "Reads shapes from shapefiles FILE+");
119 
120  oc.doRegister("shapefile.guess-projection", new Option_Bool(false));
121  oc.addSynonyme("shapefile.guess-projection", "arcview.guess-projection", true);
122  oc.addDescription("shapefile.guess-projection", "Input", "Guesses the shapefile's projection");
123 
124  oc.doRegister("shapefile.id-column", new Option_String());
125  oc.addSynonyme("shapefile.id-column", "shapefile.id-name", true);
126  oc.addSynonyme("shapefile.id-column", "shape-files.id-name", true);
127  oc.addDescription("shapefile.id-column", "Input", "Defines in which column the id can be found");
128 
129  oc.doRegister("shapefile.type-columns", new Option_String());
130  oc.addSynonyme("shapefile.type-columns", "shapefile.type-column");
131  oc.addDescription("shapefile.type-columns", "Input", "Defines which columns form the type id (comma separated list)");
132 
133  oc.doRegister("shapefile.use-running-id", new Option_Bool(false));
134  oc.addDescription("shapefile.use-running-id", "Input", "A running number will be used as id");
135 
136  oc.doRegister("shapefile.add-param", new Option_Bool(false));
137  oc.addDescription("shapefile.add-param", "Input", "Extract all additional columns as params");
138 
139  oc.doRegister("shapefile.fill", new Option_String());
140  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");
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());
150  oc.addSynonyme("output-file", "output");
151  oc.addDescription("output-file", "Output", "Write generated polygons/pois to FILE");
152 
153  oc.doRegister("dlr-tdp-output", new Option_FileName());
154  oc.addDescription("dlr-tdp-output", "Output", "Write generated polygons/pois to a dlr-tdp file with the given prefix");
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_Float(-1));
210  oc.addDescription("layer", "Building Defaults", "Sets FLOAT 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 microscopic, multi-modal traffic simulation SUMO.");
225  oc.setApplicationName("polyconvert", "Eclipse SUMO polyconvert Version " 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  double scale = 1.0;
241  if ((oc.isSet("dlr-navteq-poly-files") || oc.isSet("dlr-navteq-poi-files")) && oc.isDefault("proj.scale")) {
242  scale = 1e-5;
243  }
244  if (!oc.isSet("net")) {
245  // from the given options
246 #ifdef PROJ_API_FILE
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  if (oc.isDefault("proj.scale")) {
252  oc.set("proj.scale", toString(scale, 5));
253  }
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"), scale);
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", nullptr, 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", nullptr, 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 == nullptr) {
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.setDefault("type-file", path + "navteqPolyconvert.typ.xml");
301  }
302  if (oc.isSet("osm-files")) {
303  oc.setDefault("type-file", path + "osmPolyconvert.typ.xml");
304  }
305  if (oc.isSet("visum-files")) {
306  oc.setDefault("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  }
332  // read in the data
333  PCLoaderXML::loadIfSet(oc, toFill, tm); // SUMO-XML
334  PCLoaderOSM::loadIfSet(oc, toFill, tm); // OSM-XML
335  PCLoaderDlrNavteq::loadIfSet(oc, toFill, tm); // Elmar-files
336  PCLoaderVisum::loadIfSet(oc, toFill, tm); // VISUM
337  PCLoaderArcView::loadIfSet(oc, toFill, tm); // shape-files
339  // error processing
340  if (MsgHandler::getErrorInstance()->wasInformed() && !oc.getBool("ignore-errors")) {
341  throw ProcessError();
342  }
343  // output
344  if (!oc.isSet("output-file") && !oc.isSet("dlr-tdp-output")) {
345  std::string out = "polygons.xml";
346  if (oc.isSet("configuration-file")) {
347  out = FileHelpers::getConfigurationRelative(oc.getString("configuration-file"), out);
348  }
349  oc.setDefault("output-file", out);
350  }
351  if (oc.isSet("output-file")) {
352  toFill.save(oc.getString("output-file"), oc.getBool("proj.plain-geo"));
353  }
354  if (oc.isSet("dlr-tdp-output")) {
355  toFill.saveDlrTDP(oc.getString("dlr-tdp-output"));
356  }
357 
358  } catch (const ProcessError& e) {
359  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
360  WRITE_ERROR(e.what());
361  }
362  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
363  ret = 1;
364 #ifndef _DEBUG
365  } catch (const std::exception& e) {
366  if (std::string(e.what()) != std::string("")) {
367  WRITE_ERROR(e.what());
368  }
369  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
370  ret = 1;
371  } catch (...) {
372  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
373  ret = 1;
374 #endif
375  }
377  // report about ending
378  if (ret == 0) {
379  std::cout << "Success." << std::endl;
380  }
381  return ret;
382 }
383 
384 
385 
386 /****************************************************************************/
387 
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:75
double ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:131
static void init()
Initialises the xml-subsystem.
Definition: XMLSubSys.cpp:48
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:81
static void loadIfSet(OptionsCont &oc, PCPolyContainer &toFill, PCTypeMap &tm)
Loads pois/polygons assumed to be stored as OSM-XML.
Definition: PCLoaderOSM.cpp:91
double xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:125
static std::string getConfigurationRelative(const std::string &configPath, const std::string &path)
Returns the second path as a relative path to the first file.
static void getOptions(const bool commandLineOnly=false)
Parses the command line arguments and loads the configuration.
Definition: OptionsIO.cpp:76
static void addReportOptions(OptionsCont &oc)
Adds reporting options to the given container.
Definition: SystemFrame.cpp:65
int main(int argc, char **argv)
const Boundary & getConvBoundary() const
Returns the converted boundary.
static void setValidation(const std::string &validationScheme, const std::string &netValidationScheme)
Enables or disables validation.
Definition: XMLSubSys.cpp:59
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.
void setApplicationDescription(const std::string &appDesc)
Sets the application description.
static void load(const std::string &file, double scale)
Loads network projection if wished.
const std::string osmTypemap
Definition: typemap.h:38
SAX-reader encapsulation containing binary reader.
Definition: SUMOSAXReader.h:56
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 occurred errors.
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
bool isDefault(const std::string &name) const
Returns the information whether the named option has still the default value.
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:113
static void close()
Closes all of an applications subsystems.
static void addConfigurationOptions(OptionsCont &oc)
Adds configuration options to the given container.
Definition: SystemFrame.cpp:40
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:42
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:239
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:55
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
void addSynonyme(const std::string &name1, const std::string &name2, bool isDeprecated=false)
Adds a synonyme for an options name (any order)
Definition: OptionsCont.cpp:96
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
A storage for type mappings.
Definition: PCTypeMap.h:45
void setFileName(const std::string &name)
Sets the current file name.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:48
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.
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
bool processMetaOptions(bool missingOptions)
Checks for help and configuration output, returns whether we should exit.
const std::string visumTypemap
Definition: pc_typemap.h:62
static SUMOSAXReader * getSAXReader(SUMOSAXHandler &handler)
Builds a reader and assigns the handler to it.
Definition: XMLSubSys.cpp:101
static bool init(OptionsCont &oc)
Initialises the processing and the final instance using the given options.
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) ...
double xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:119
void addOptionSubTopic(const std::string &topic)
Adds an option subtopic.
static bool checkOptions()
checks shared options and sets StdDefs
static void addProjectionOptions(OptionsCont &oc)
Adds projection options to the given container.
A handler for loading polygon type maps.
#define VERSION_STRING
Definition: config.h:207
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:245
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.
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:118
A storage for options typed value containers)
Definition: OptionsCont.h:90
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:55
const std::string navteqTypemap
Definition: pc_typemap.h:1
void addDescription(const std::string &name, const std::string &subtopic, const std::string &description)
Adds a description for an option.
double ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:137
static void initOutputOptions()
init output options
Definition: MsgHandler.cpp:208
void fillOptions()
void setApplicationName(const std::string &appName, const std::string &fullName)
Sets the application name.