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