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-2020 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
22 // Main for POLYCONVERT
23 /****************************************************************************/
24 #include <config.h>
25 
26 #ifdef HAVE_VERSION_H
27 #include <version.h>
28 #endif
29 
30 #include <iostream>
31 #include <string>
39 #include <utils/common/ToString.h>
44 #include <utils/geom/Boundary.h>
46 #include <utils/xml/XMLSubSys.h>
52 #include <polyconvert/PCTypeMap.h>
55 #include <polyconvert/pc_typemap.h>
56 
57 
58 // ===========================================================================
59 // method definitions
60 // ===========================================================================
61 void
64  oc.addCallExample("-c <CONFIGURATION>", "run with configuration options set in file");
65 
66  // insert options sub-topics
67  SystemFrame::addConfigurationOptions(oc); // fill this subtopic, too
68  oc.addOptionSubTopic("Input");
69  oc.addOptionSubTopic("Output");
71  oc.addOptionSubTopic("Pruning");
72  oc.addOptionSubTopic("Processing");
73  oc.addOptionSubTopic("Building Defaults");
74 
75 
76  // register options
77  // add i/o options
78  // original network
79  oc.doRegister("net-file", 'n', new Option_FileName());
80  oc.addSynonyme("net-file", "net");
81  oc.addDescription("net-file", "Input", "Loads SUMO-network FILE as reference to offset and projection");
82 
83  // dlrnavteq import
84  oc.doRegister("dlr-navteq-poly-files", new Option_FileName());
85  oc.addDescription("dlr-navteq-poly-files", "Input", "Reads polygons from FILE assuming they're coded in DLR-Navteq (Elmar)-format");
86  oc.doRegister("dlr-navteq-poi-files", new Option_FileName());
87  oc.addDescription("dlr-navteq-poi-files", "Input", "Reads pois from FILE+ assuming they're coded in DLR-Navteq (Elmar)-format");
88 
89  // visum import
90  oc.doRegister("visum-files", new Option_FileName());
91  oc.addSynonyme("visum-files", "visum");
92  oc.addDescription("visum-files", "Input", "Reads polygons from FILE assuming it's a Visum-net");
93 
94  // xml import
95  oc.doRegister("xml-files", new Option_FileName());
96  oc.addSynonyme("xml-files", "xml");
97  oc.addDescription("xml-files", "Input", "Reads pois and shapes from FILE assuming they're coded in XML");
98 
99  // osm import
100  oc.doRegister("osm-files", new Option_FileName());
101  oc.addSynonyme("osm-files", "osm");
102  oc.addDescription("osm-files", "Input", "Reads pois from FILE+ assuming they're coded in OSM");
103  oc.doRegister("osm.keep-full-type", new Option_Bool(false));
104  oc.addDescription("osm.keep-full-type", "Input", "The type will be made of the key-value - pair");
105  oc.doRegister("osm.use-name", new Option_Bool(false));
106  oc.addDescription("osm.use-name", "Input", "The id will be set from the given 'name' attribute");
107  oc.doRegister("osm.merge-relations", new Option_Float(-1));
108  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");
109 
110  // arcview import
111  oc.doRegister("shapefile-prefixes", new Option_FileName());
112  oc.addSynonyme("shapefile-prefixes", "shapefile-prefix");
113  oc.addSynonyme("shapefile-prefixes", "shapefile");
114  oc.addSynonyme("shapefile-prefixes", "shape-files", true);
115  oc.addDescription("shapefile-prefixes", "Input", "Reads shapes from shapefiles FILE+");
116 
117  oc.doRegister("shapefile.guess-projection", new Option_Bool(false));
118  oc.addSynonyme("shapefile.guess-projection", "arcview.guess-projection", true);
119  oc.addDescription("shapefile.guess-projection", "Input", "Guesses the shapefile's projection");
120 
121  oc.doRegister("shapefile.traditional-axis-mapping", new Option_Bool(false));
122  oc.addDescription("shapefile.traditional-axis-mapping", "Input", "Use traditional axis order (lon, lat)");
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_StringVector());
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  // need to do this here to be able to check for network and route input options
149 
150  // output
151  oc.doRegister("output-file", 'o', new Option_FileName());
152  oc.addSynonyme("output-file", "output");
153  oc.addDescription("output-file", "Output", "Write generated polygons/pois to FILE");
154 
155  oc.doRegister("dlr-tdp-output", new Option_FileName());
156  oc.addDescription("dlr-tdp-output", "Output", "Write generated polygons/pois to a dlr-tdp file with the given prefix");
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_StringVector(StringVector({ "" })));
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("offset.z", new Option_Float(0));
190  oc.addDescription("offset.z", "Processing", "Adds FLOAT to net z-positions");
191 
192  oc.doRegister("all-attributes", new Option_Bool(false));
193  oc.addDescription("all-attributes", "Processing", "Imports all attributes as key/value pairs");
194 
195  oc.doRegister("ignore-errors", new Option_Bool(false));
196  oc.addDescription("ignore-errors", "Processing", "Continue on broken input");
197 
198  oc.doRegister("poi-layer-offset", new Option_Float(0));
199  oc.addDescription("poi-layer-offset", "Processing", "Adds FLOAT to the layer value for each poi (i.e. to raise it above polygons)");
200 
201  // building defaults options
202  oc.doRegister("color", new Option_String("0.2,0.5,1."));
203  oc.addDescription("color", "Building Defaults", "Sets STR as default color");
204 
205  oc.doRegister("prefix", new Option_String(""));
206  oc.addDescription("prefix", "Building Defaults", "Sets STR as default prefix");
207 
208  oc.doRegister("type", new Option_String("unknown"));
209  oc.addDescription("type", "Building Defaults", "Sets STR as default type");
210 
211  oc.doRegister("fill", new Option_Bool("false"));
212  oc.addDescription("fill", "Building Defaults", "Fills polygons by default");
213 
214  oc.doRegister("layer", new Option_Float(-1));
215  oc.addDescription("layer", "Building Defaults", "Sets FLOAT as default layer");
216 
217  oc.doRegister("discard", new Option_Bool(false));
218  oc.addDescription("discard", "Building Defaults", "Sets default action to discard");
219 
220  // projection
221  oc.doRegister("proj.plain-geo", new Option_Bool(false));
222  oc.addDescription("proj.plain-geo", "Projection", "Write geo coordinates in output");
223 }
224 
225 
226 int
227 main(int argc, char** argv) {
229  oc.setApplicationDescription("Importer of polygons and POIs for the microscopic, multi-modal traffic simulation SUMO.");
230  oc.setApplicationName("polyconvert", "Eclipse SUMO polyconvert Version " VERSION_STRING);
231  int ret = 0;
232  try {
233  // initialise subsystems
234  XMLSubSys::init();
235  fillOptions();
236  OptionsIO::setArgs(argc, argv);
238  if (oc.processMetaOptions(argc < 2)) {
240  return 0;
241  }
243  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"), "never");
245  // build the projection
246  double scale = 1.0;
247  if ((oc.isSet("dlr-navteq-poly-files") || oc.isSet("dlr-navteq-poi-files")) && oc.isDefault("proj.scale")) {
248  scale = 1e-5;
249  }
250  if (!oc.isSet("net")) {
251  // from the given options
252 #ifdef PROJ_API_FILE
253  const int numProjections = oc.getBool("simple-projection") + oc.getBool("proj.utm") + oc.getBool("proj.dhdn") + (oc.getString("proj").length() > 1);
254  if ((oc.isSet("osm-files") || oc.isSet("dlr-navteq-poly-files") || oc.isSet("dlr-navteq-poi-files")) && numProjections == 0) {
255  oc.set("proj.utm", "true");
256  }
257  if (oc.isDefault("proj.scale")) {
258  oc.set("proj.scale", toString(scale, 5));
259  }
260 #endif
261  if (!GeoConvHelper::init(oc)) {
262  throw ProcessError("Could not build projection!");
263  }
264  } else {
265  // from the supplied network
266  // @todo warn about given options being ignored
267  PCNetProjectionLoader::load(oc.getString("net"), scale);
268  }
269  Boundary pruningBoundary = GeoConvHelper::getFinal().getConvBoundary();
270  // check whether the input shall be pruned
271  bool prune = false;
272  if (oc.getBool("prune.in-net")) {
273  if (!oc.isSet("net")) {
274  throw ProcessError("In order to prune the input on the net, you have to supply a network.");
275  }
276  bool ok = true;
277  // !!! no proper error handling
278  Boundary offsets = GeomConvHelper::parseBoundaryReporting(oc.getString("prune.in-net.offsets"), "--prune.on-net.offsets", nullptr, ok);
279  pruningBoundary = Boundary(
280  pruningBoundary.xmin() + offsets.xmin(),
281  pruningBoundary.ymin() + offsets.ymin(),
282  pruningBoundary.xmax() + offsets.xmax(),
283  pruningBoundary.ymax() + offsets.ymax());
284  prune = true;
285  }
286  if (oc.isSet("prune.boundary")) {
287  bool ok = true;
288  // !!! no proper error handling
289  pruningBoundary = GeomConvHelper::parseBoundaryReporting(oc.getString("prune.boundary"), "--prune.boundary", nullptr, ok);
290  prune = true;
291  }
292  if (oc.isSet("osm-files") && oc.isDefault("poi-layer-offset")) {
293  oc.set("poi-layer-offset", "5"); // sufficient when using the default typemap
294  }
295 
296  PCPolyContainer toFill(prune, pruningBoundary, oc.getStringVector("remove"));
297 
298  // read in the type defaults
299  if (!oc.isSet("type-file")) {
300  const char* sumoPath = std::getenv("SUMO_HOME");
301  if (sumoPath == nullptr) {
302  WRITE_WARNING("Environment variable SUMO_HOME is not set, using built in type maps.");
303  } else {
304  const std::string path = sumoPath + std::string("/data/typemap/");
305  if (oc.isSet("dlr-navteq-poly-files")) {
306  oc.setDefault("type-file", path + "navteqPolyconvert.typ.xml");
307  }
308  if (oc.isSet("osm-files")) {
309  oc.setDefault("type-file", path + "osmPolyconvert.typ.xml");
310  }
311  if (oc.isSet("visum-files")) {
312  oc.setDefault("type-file", path + "visumPolyconvert.typ.xml");
313  }
314  }
315  }
316  PCTypeMap tm(oc);
317  PCTypeDefHandler handler(oc, tm);
318  if (oc.isSet("type-file")) {
319  if (!XMLSubSys::runParser(handler, oc.getString("type-file"))) {
320  // something failed
321  throw ProcessError();
322  }
323  } else {
324  handler.setFileName("built in type map");
325  SUMOSAXReader* reader = XMLSubSys::getSAXReader(handler);
326  if (oc.isSet("dlr-navteq-poly-files")) {
327  reader->parseString(navteqTypemap);
328  }
329  if (oc.isSet("osm-files")) {
330  reader->parseString(osmTypemap);
331  }
332  if (oc.isSet("visum-files")) {
333  reader->parseString(visumTypemap);
334  }
335  delete reader;
336  }
337  // read in the data
338  PCLoaderXML::loadIfSet(oc, toFill, tm); // SUMO-XML
339  PCLoaderOSM::loadIfSet(oc, toFill, tm); // OSM-XML
340  PCLoaderDlrNavteq::loadIfSet(oc, toFill, tm); // Elmar-files
341  PCLoaderVisum::loadIfSet(oc, toFill, tm); // VISUM
342  PCLoaderArcView::loadIfSet(oc, toFill, tm); // shape-files
344  // error processing
345  if (MsgHandler::getErrorInstance()->wasInformed() && !oc.getBool("ignore-errors")) {
346  throw ProcessError();
347  }
348  // output
349  if (!oc.isSet("output-file") && !oc.isSet("dlr-tdp-output")) {
350  std::string out = "polygons.xml";
351  if (oc.isSet("configuration-file")) {
352  out = FileHelpers::getConfigurationRelative(oc.getString("configuration-file"), out);
353  }
354  oc.setDefault("output-file", out);
355  }
356  if (oc.isSet("output-file")) {
357  toFill.save(oc.getString("output-file"), oc.getBool("proj.plain-geo"));
358  }
359  if (oc.isSet("dlr-tdp-output")) {
360  toFill.saveDlrTDP(oc.getString("dlr-tdp-output"));
361  }
362 
363  } catch (const ProcessError& e) {
364  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
365  WRITE_ERROR(e.what());
366  }
367  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
368  ret = 1;
369 #ifndef _DEBUG
370  } catch (const std::exception& e) {
371  if (std::string(e.what()) != std::string("")) {
372  WRITE_ERROR(e.what());
373  }
374  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
375  ret = 1;
376  } catch (...) {
377  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
378  ret = 1;
379 #endif
380  }
382  // report about ending
383  if (ret == 0) {
384  std::cout << "Success." << std::endl;
385  }
386  return ret;
387 }
388 
389 
390 /****************************************************************************/
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:284
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:276
std::vector< std::string > StringVector
Definition of a vector of strings.
Definition: Option.h:43
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:44
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:39
double ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:129
double xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:117
double ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:135
double xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:123
static std::string getConfigurationRelative(const std::string &configPath, const std::string &path)
Returns the second path as a relative path to the first file.
void setFileName(const std::string &name)
Sets the current file name.
static void addProjectionOptions(OptionsCont &oc)
Adds projection options to the given container.
static const GeoConvHelper & getFinal()
the coordinate transformation for writing the location element and for tracking the original coordina...
static bool init(OptionsCont &oc)
Initialises the processing and the final instance using the given options.
static void computeFinal(bool lefthand=false)
compute the location attributes which will be used for output based on the loaded location data,...
const Boundary & getConvBoundary() const
Returns the converted boundary.
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.
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:80
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:117
static void initOutputOptions()
init output options
Definition: MsgHandler.cpp:217
A storage for options typed value containers)
Definition: OptionsCont.h:89
void addDescription(const std::string &name, const std::string &subtopic, const std::string &description)
Adds a description for an option.
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:75
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
bool set(const std::string &name, const std::string &value)
Sets the given value for the named option.
void setApplicationName(const std::string &appName, const std::string &fullName)
Sets the application name.
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
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 isDefault(const std::string &name) const
Returns the information whether the named option has still the default value.
bool setDefault(const std::string &name, const std::string &value)
Sets the given value for the named option as new default value.
void setApplicationDescription(const std::string &appDesc)
Sets the application description.
void addOptionSubTopic(const std::string &topic)
Adds an option subtopic.
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
const StringVector & getStringVector(const std::string &name) const
Returns the list of string-value of the named option (only for Option_StringVector)
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
bool processMetaOptions(bool missingOptions)
Checks for help and configuration output, returns whether we should exit.
void addCallExample(const std::string &example, const std::string &desc)
Add a call example.
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition: OptionsIO.cpp:58
static void getOptions(const bool commandLineOnly=false)
Parses the command line arguments and loads the configuration.
Definition: OptionsIO.cpp:79
static void loadIfSet(OptionsCont &oc, PCPolyContainer &toFill, PCTypeMap &tm)
Loads pois/polygons assumed to be stored as shape files-files.
static void loadIfSet(OptionsCont &oc, PCPolyContainer &toFill, PCTypeMap &tm)
Loads pois/polygons assumed to be stored as according DLR-Navteq (Elmar)-files.
static void loadIfSet(OptionsCont &oc, PCPolyContainer &toFill, PCTypeMap &tm)
Loads pois/polygons assumed to be stored as OSM-XML.
Definition: PCLoaderOSM.cpp:90
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 XML.
Definition: PCLoaderXML.cpp:53
static void load(const std::string &file, double scale)
Loads network projection if wished.
A storage for loaded polygons and pois.
void save(const std::string &file, bool useGeo)
Saves the stored polygons and pois into the given file.
void saveDlrTDP(const std::string &prefix)
Saves the stored polygons and pois into the given file in dlrTDP format.
A handler for loading polygon type maps.
A storage for type mappings.
Definition: PCTypeMap.h:42
SAX-reader encapsulation containing binary reader.
Definition: SUMOSAXReader.h:52
void parseString(std::string content)
static void close()
Closes all of an applications subsystems.
static void addConfigurationOptions(OptionsCont &oc)
Adds configuration options to the given container.
Definition: SystemFrame.cpp:38
static void addReportOptions(OptionsCont &oc)
Adds reporting options to the given container.
Definition: SystemFrame.cpp:63
static bool checkOptions()
checks shared options and sets StdDefs
static void setValidation(const std::string &validationScheme, const std::string &netValidationScheme, const std::string &routeValidationScheme)
Enables or disables validation.
Definition: XMLSubSys.cpp:65
static SUMOSAXReader * getSAXReader(SUMOSAXHandler &handler, const bool isNet=false, const bool isRoute=false)
Builds a reader and assigns the handler to it.
Definition: XMLSubSys.cpp:132
static void init()
Initialises the xml-subsystem.
Definition: XMLSubSys.cpp:54
static bool runParser(GenericSAXHandler &handler, const std::string &file, const bool isNet=false, const bool isRoute=false)
Runs the given handler on the given file; returns if everything's ok.
Definition: XMLSubSys.cpp:148
void fillOptions()
int main(int argc, char **argv)