SUMO - Simulation of Urban MObility
NIImporter_ArcView.cpp
Go to the documentation of this file.
1 /****************************************************************************/
11 // Importer for networks stored in ArcView-shape format
12 /****************************************************************************/
13 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
14 // Copyright (C) 2001-2017 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 #include <string>
37 #include <utils/common/ToString.h>
41 #include <utils/geom/GeomHelper.h>
42 #include <netbuild/NBNetBuilder.h>
43 #include <netbuild/NBHelpers.h>
44 #include <netbuild/NBEdge.h>
45 #include <netbuild/NBEdgeCont.h>
46 #include <netbuild/NBTypeCont.h>
47 #include <netbuild/NBNode.h>
48 #include <netbuild/NBNodeCont.h>
52 #include "NILoader.h"
53 #include "NIImporter_ArcView.h"
54 
55 #ifdef HAVE_GDAL
56 #if __GNUC__ > 3
57 #pragma GCC diagnostic push
58 #pragma GCC diagnostic ignored "-Wpedantic"
59 #endif
60 #include <ogrsf_frmts.h>
61 #if __GNUC__ > 3
62 #pragma GCC diagnostic pop
63 #endif
64 #endif
65 
66 
67 // ===========================================================================
68 // method definitions
69 // ===========================================================================
70 // ---------------------------------------------------------------------------
71 // static methods (interface in this case)
72 // ---------------------------------------------------------------------------
73 void
75  if (!oc.isSet("shapefile-prefix")) {
76  return;
77  }
78  // check whether the correct set of entries is given
79  // and compute both file names
80  std::string dbf_file = oc.getString("shapefile-prefix") + ".dbf";
81  std::string shp_file = oc.getString("shapefile-prefix") + ".shp";
82  std::string shx_file = oc.getString("shapefile-prefix") + ".shx";
83  // check whether the files do exist
84  if (!FileHelpers::isReadable(dbf_file)) {
85  WRITE_ERROR("File not accessible: " + dbf_file);
86  }
87  if (!FileHelpers::isReadable(shp_file)) {
88  WRITE_ERROR("File not accessible: " + shp_file);
89  }
90  if (!FileHelpers::isReadable(shx_file)) {
91  WRITE_ERROR("File not accessible: " + shx_file);
92  }
93  if (MsgHandler::getErrorInstance()->wasInformed()) {
94  return;
95  }
96  // load the arcview files
97  NIImporter_ArcView loader(oc,
98  nb.getNodeCont(), nb.getEdgeCont(), nb.getTypeCont(),
99  dbf_file, shp_file, oc.getBool("speed-in-kmh"));
100  loader.load();
101 }
102 
103 
104 
105 // ---------------------------------------------------------------------------
106 // loader methods
107 // ---------------------------------------------------------------------------
109  NBNodeCont& nc,
110  NBEdgeCont& ec,
111  NBTypeCont& tc,
112  const std::string& dbf_name,
113  const std::string& shp_name,
114  bool speedInKMH)
115  : myOptions(oc), mySHPName(shp_name),
116  myNameAddition(0),
117  myNodeCont(nc), myEdgeCont(ec), myTypeCont(tc),
118  mySpeedInKMH(speedInKMH),
119  myRunningNodeID(0) {
120  UNUSED_PARAMETER(dbf_name);
121 }
122 
123 
125 
126 
127 void
129 #ifdef HAVE_GDAL
130  PROGRESS_BEGIN_MESSAGE("Loading data from '" + mySHPName + "'");
131 #if GDAL_VERSION_MAJOR < 2
132  OGRRegisterAll();
133  OGRDataSource* poDS = OGRSFDriverRegistrar::Open(mySHPName.c_str(), FALSE);
134 #else
135  GDALAllRegister();
136  GDALDataset* poDS = (GDALDataset*)GDALOpenEx(mySHPName.c_str(), GDAL_OF_VECTOR | GA_ReadOnly, NULL, NULL, NULL);
137 #endif
138  if (poDS == NULL) {
139  WRITE_ERROR("Could not open shape description '" + mySHPName + "'.");
140  return;
141  }
142 
143  // begin file parsing
144  OGRLayer* poLayer = poDS->GetLayer(0);
145  poLayer->ResetReading();
146 
147  // build coordinate transformation
148  OGRSpatialReference* origTransf = poLayer->GetSpatialRef();
149  OGRSpatialReference destTransf;
150  // use wgs84 as destination
151  destTransf.SetWellKnownGeogCS("WGS84");
152  OGRCoordinateTransformation* poCT = OGRCreateCoordinateTransformation(origTransf, &destTransf);
153  if (poCT == NULL) {
154  if (myOptions.isSet("shapefile.guess-projection")) {
155  OGRSpatialReference origTransf2;
156  origTransf2.SetWellKnownGeogCS("WGS84");
157  poCT = OGRCreateCoordinateTransformation(&origTransf2, &destTransf);
158  }
159  if (poCT == 0) {
160  WRITE_WARNING("Could not create geocoordinates converter; check whether proj.4 is installed.");
161  }
162  }
163 
164  OGRFeature* poFeature;
165  poLayer->ResetReading();
166  while ((poFeature = poLayer->GetNextFeature()) != NULL) {
167  // read in edge attributes
168  std::string id, name, from_node, to_node;
169  if (!getStringEntry(poFeature, "shapefile.street-id", "LINK_ID", true, id)) {
170  WRITE_ERROR("Needed field '" + id + "' (from node id) is missing.");
171  }
172  if (id == "") {
173  WRITE_ERROR("Could not obtain edge id.");
174  return;
175  }
176 
177  getStringEntry(poFeature, "shapefile.street-id", "ST_NAME", true, name);
178  name = StringUtils::replace(name, "&", "&amp;");
179 
180  if (!getStringEntry(poFeature, "shapefile.from-id", "REF_IN_ID", true, from_node)) {
181  WRITE_ERROR("Needed field '" + from_node + "' (from node id) is missing.");
182  }
183  if (!getStringEntry(poFeature, "shapefile.to-id", "NREF_IN_ID", true, to_node)) {
184  WRITE_ERROR("Needed field '" + to_node + "' (to node id) is missing.");
185  }
186 
187  if (from_node == "" || to_node == "") {
188  from_node = toString(myRunningNodeID++);
189  to_node = toString(myRunningNodeID++);
190  }
191 
192  std::string type;
193  if (myOptions.isSet("shapefile.type-id") && poFeature->GetFieldIndex(myOptions.getString("shapefile.type-id").c_str()) >= 0) {
194  type = poFeature->GetFieldAsString(myOptions.getString("shapefile.type-id").c_str());
195  } else if (poFeature->GetFieldIndex("ST_TYP_AFT") >= 0) {
196  type = poFeature->GetFieldAsString("ST_TYP_AFT");
197  }
198  double width = myTypeCont.getWidth(type);
199  double speed = getSpeed(*poFeature, id);
200  int nolanes = getLaneNo(*poFeature, id, speed);
201  int priority = getPriority(*poFeature, id);
202  if (nolanes == 0 || speed == 0) {
203  if (myOptions.getBool("shapefile.use-defaults-on-failure")) {
204  nolanes = myTypeCont.getNumLanes("");
205  speed = myTypeCont.getSpeed("");
206  } else {
207  OGRFeature::DestroyFeature(poFeature);
208  WRITE_ERROR("Required field 'nolanes' or 'speed' is missing (add fields or set option --shapefile.use-defaults-on-failure).");
209  return;
210  }
211  }
212  if (mySpeedInKMH) {
213  speed = speed / (double) 3.6;
214  }
215 
216 
217  // read in the geometry
218  OGRGeometry* poGeometry = poFeature->GetGeometryRef();
219  OGRwkbGeometryType gtype = poGeometry->getGeometryType();
220  if (gtype != wkbLineString) {
221  OGRFeature::DestroyFeature(poFeature);
222  WRITE_ERROR("Road geometry must be of type 'linestring'.");
223  return;
224  }
225  OGRLineString* cgeom = (OGRLineString*) poGeometry;
226  if (poCT != 0) {
227  // try transform to wgs84
228  cgeom->transform(poCT);
229  }
230 
231  PositionVector shape;
232  for (int j = 0; j < cgeom->getNumPoints(); j++) {
233  Position pos((double) cgeom->getX(j), (double) cgeom->getY(j));
235  WRITE_WARNING("Unable to project coordinates for edge '" + id + "'.");
236  }
237  shape.push_back_noDoublePos(pos);
238  }
239 
240  // build from-node
241  NBNode* from = myNodeCont.retrieve(from_node);
242  if (from == 0) {
243  Position from_pos = shape[0];
244  from = myNodeCont.retrieve(from_pos);
245  if (from == 0) {
246  from = new NBNode(from_node, from_pos);
247  if (!myNodeCont.insert(from)) {
248  WRITE_ERROR("Node '" + from_node + "' could not be added");
249  delete from;
250  continue;
251  }
252  }
253  }
254  // build to-node
255  NBNode* to = myNodeCont.retrieve(to_node);
256  if (to == 0) {
257  Position to_pos = shape[-1];
258  to = myNodeCont.retrieve(to_pos);
259  if (to == 0) {
260  to = new NBNode(to_node, to_pos);
261  if (!myNodeCont.insert(to)) {
262  WRITE_ERROR("Node '" + to_node + "' could not be added");
263  delete to;
264  continue;
265  }
266  }
267  }
268 
269  if (from == to) {
270  WRITE_WARNING("Edge '" + id + "' connects identical nodes, skipping.");
271  continue;
272  }
273 
274  // retrieve the information whether the street is bi-directional
275  std::string dir;
276  int index = poFeature->GetDefnRef()->GetFieldIndex("DIR_TRAVEL");
277  if (index >= 0 && poFeature->IsFieldSet(index)) {
278  dir = poFeature->GetFieldAsString(index);
279  }
280  // add positive direction if wanted
281  if (dir == "B" || dir == "F" || dir == "" || myOptions.getBool("shapefile.all-bidirectional")) {
282  if (myEdgeCont.retrieve(id) == 0) {
283  LaneSpreadFunction spread = dir == "B" || dir == "FALSE" ? LANESPREAD_RIGHT : LANESPREAD_CENTER;
284  NBEdge* edge = new NBEdge(id, from, to, type, speed, nolanes, priority, width, NBEdge::UNSPECIFIED_OFFSET, shape, name, id, spread);
285  myEdgeCont.insert(edge);
286  checkSpread(edge);
287  }
288  }
289  // add negative direction if wanted
290  if (dir == "B" || dir == "T" || myOptions.getBool("shapefile.all-bidirectional")) {
291  if (myEdgeCont.retrieve("-" + id) == 0) {
292  LaneSpreadFunction spread = dir == "B" || dir == "FALSE" ? LANESPREAD_RIGHT : LANESPREAD_CENTER;
293  NBEdge* edge = new NBEdge("-" + id, to, from, type, speed, nolanes, priority, width, NBEdge::UNSPECIFIED_OFFSET, shape.reverse(), name, id, spread);
294  myEdgeCont.insert(edge);
295  checkSpread(edge);
296  }
297  }
298  //
299  OGRFeature::DestroyFeature(poFeature);
300  }
301 #if GDAL_VERSION_MAJOR < 2
302  OGRDataSource::DestroyDataSource(poDS);
303 #else
304  GDALClose(poDS);
305 #endif
307 #else
308  WRITE_ERROR("SUMO was compiled without GDAL support.");
309 #endif
310 }
311 
312 #ifdef HAVE_GDAL
313 double
314 NIImporter_ArcView::getSpeed(OGRFeature& poFeature, const std::string& edgeid) {
315  if (myOptions.isSet("shapefile.type-id")) {
316  return myTypeCont.getSpeed(poFeature.GetFieldAsString((char*)(myOptions.getString("shapefile.type-id").c_str())));
317  }
318  // try to get definitions as to be found in SUMO-XML-definitions
319  // idea by John Michael Calandrino
320  int index = poFeature.GetDefnRef()->GetFieldIndex("speed");
321  if (index >= 0 && poFeature.IsFieldSet(index)) {
322  return (double) poFeature.GetFieldAsDouble(index);
323  }
324  index = poFeature.GetDefnRef()->GetFieldIndex("SPEED");
325  if (index >= 0 && poFeature.IsFieldSet(index)) {
326  return (double) poFeature.GetFieldAsDouble(index);
327  }
328  // try to get the NavTech-information
329  index = poFeature.GetDefnRef()->GetFieldIndex("SPEED_CAT");
330  if (index >= 0 && poFeature.IsFieldSet(index)) {
331  std::string def = poFeature.GetFieldAsString(index);
332  return NINavTeqHelper::getSpeed(edgeid, def);
333  }
334  return -1;
335 }
336 
337 
338 int
339 NIImporter_ArcView::getLaneNo(OGRFeature& poFeature, const std::string& edgeid,
340  double speed) {
341  if (myOptions.isSet("shapefile.type-id")) {
342  return (int) myTypeCont.getNumLanes(poFeature.GetFieldAsString((char*)(myOptions.getString("shapefile.type-id").c_str())));
343  }
344  // try to get definitions as to be found in SUMO-XML-definitions
345  // idea by John Michael Calandrino
346  int index = poFeature.GetDefnRef()->GetFieldIndex("nolanes");
347  if (index >= 0 && poFeature.IsFieldSet(index)) {
348  return (int) poFeature.GetFieldAsInteger(index);
349  }
350  index = poFeature.GetDefnRef()->GetFieldIndex("NOLANES");
351  if (index >= 0 && poFeature.IsFieldSet(index)) {
352  return (int) poFeature.GetFieldAsInteger(index);
353  }
354  index = poFeature.GetDefnRef()->GetFieldIndex("rnol");
355  if (index >= 0 && poFeature.IsFieldSet(index)) {
356  return (int) poFeature.GetFieldAsInteger(index);
357  }
358  index = poFeature.GetDefnRef()->GetFieldIndex("LANE_CAT");
359  if (index >= 0 && poFeature.IsFieldSet(index)) {
360  std::string def = poFeature.GetFieldAsString(index);
361  return NINavTeqHelper::getLaneNumber(edgeid, def, speed);
362  }
363  return 0;
364 }
365 
366 
367 int
368 NIImporter_ArcView::getPriority(OGRFeature& poFeature, const std::string& /*edgeid*/) {
369  if (myOptions.isSet("shapefile.type-id")) {
370  return myTypeCont.getPriority(poFeature.GetFieldAsString((char*)(myOptions.getString("shapefile.type-id").c_str())));
371  }
372  // try to get definitions as to be found in SUMO-XML-definitions
373  // idea by John Michael Calandrino
374  int index = poFeature.GetDefnRef()->GetFieldIndex("priority");
375  if (index >= 0 && poFeature.IsFieldSet(index)) {
376  return poFeature.GetFieldAsInteger(index);
377  }
378  index = poFeature.GetDefnRef()->GetFieldIndex("PRIORITY");
379  if (index >= 0 && poFeature.IsFieldSet(index)) {
380  return poFeature.GetFieldAsInteger(index);
381  }
382  // try to determine priority from NavTechs FUNC_CLASS attribute
383  index = poFeature.GetDefnRef()->GetFieldIndex("FUNC_CLASS");
384  if (index >= 0 && poFeature.IsFieldSet(index)) {
385  return poFeature.GetFieldAsInteger(index);
386  }
387  return 0;
388 }
389 
390 void
391 NIImporter_ArcView::checkSpread(NBEdge* e) {
392  NBEdge* ret = e->getToNode()->getConnectionTo(e->getFromNode());
393  if (ret != 0) {
396  }
397 }
398 
399 bool
400 NIImporter_ArcView::getStringEntry(OGRFeature* poFeature, const std::string& optionName, const char* defaultName, bool prune, std::string& into) {
401  std::string v(defaultName);
402  if (myOptions.isSet(optionName)) {
403  v = myOptions.getString(optionName);
404  }
405  if (poFeature->GetFieldIndex(v.c_str()) < 0) {
406  if (myOptions.isSet(optionName)) {
407  into = v;
408  return false;
409  }
410  into = "";
411  return true;
412  }
413  into = poFeature->GetFieldAsString((char*)v.c_str());
414  if (prune) {
415  into = StringUtils::prune(into);
416  }
417  return true;
418 }
419 
420 
421 
422 #endif
423 
424 
425 
426 /****************************************************************************/
427 
NBNode * retrieve(const std::string &id) const
Returns the node with the given name.
Definition: NBNodeCont.cpp:106
int myNameAddition
A running number to assure unique edge ids.
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:76
double getSpeed(const std::string &type) const
Returns the maximal velocity for the given type [m/s].
Definition: NBTypeCont.cpp:181
NBTypeCont & getTypeCont()
Returns a reference to the type container.
Definition: NBNetBuilder.h:165
~NIImporter_ArcView()
Destructor.
static bool transformCoordinate(Position &from, bool includeInBoundary=true, GeoConvHelper *from_srs=0)
transforms loaded coordinates handles projections, offsets (using GeoConvHelper) and import of height...
int myRunningNodeID
A running number to assure unique node ids.
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:54
static double getSpeed(const std::string &id, const std::string &speedClassS)
Returns the speed evaluating the given Navteq-description.
std::string mySHPName
The name of the shape file.
NBTypeCont & myTypeCont
The container to get the types from.
The representation of a single edge during network building.
Definition: NBEdge.h:71
static void loadNetwork(const OptionsCont &oc, NBNetBuilder &nb)
Loads content of the optionally given ArcView Shape files.
int getPriority(const std::string &type) const
Returns the priority for the given type.
Definition: NBTypeCont.cpp:187
const OptionsCont & myOptions
The options to use.
static const double UNSPECIFIED_OFFSET
unspecified lane offset
Definition: NBEdge.h:258
void load()
Loads the shape files.
PositionVector reverse() const
reverse position vector
int getNumLanes(const std::string &type) const
Returns the number of lanes for the given type.
Definition: NBTypeCont.cpp:175
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:38
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
double getWidth(const std::string &type) const
Returns the lane width for the given type [m].
Definition: NBTypeCont.cpp:217
NBEdge * getConnectionTo(NBNode *n) const
get connection to certain node
Definition: NBNode.cpp:1739
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
bool insert(NBEdge *edge, bool ignorePrunning=false)
Adds an edge to the dictionary.
Definition: NBEdgeCont.cpp:158
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:56
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
NBEdgeCont & getEdgeCont()
Definition: NBNetBuilder.h:155
A list of positions.
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
Storage for edges, including some functionality operating on multiple edges.
Definition: NBEdgeCont.h:66
Importer for networks stored in ArcView-shape format.
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:202
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:206
static std::string replace(std::string str, const char *what, const char *by)
NIImporter_ArcView(const OptionsCont &oc, NBNodeCont &nc, NBEdgeCont &ec, NBTypeCont &tc, const std::string &dbf_name, const std::string &shp_name, bool speedInKMH)
Constructor.
static std::string prune(const std::string &str)
Removes trailing and leading whitechars.
Definition: StringUtils.cpp:52
NBNodeCont & getNodeCont()
Returns a reference to the node container.
Definition: NBNetBuilder.h:160
Instance responsible for building networks.
Definition: NBNetBuilder.h:114
NBNodeCont & myNodeCont
The container to add nodes to.
NBEdge * retrieve(const std::string &id, bool retrieveExtracted=false) const
Returns the edge that has the given id.
Definition: NBEdgeCont.cpp:251
A storage for options typed value containers)
Definition: OptionsCont.h:99
bool insert(const std::string &id, const Position &position, NBDistrict *district=0)
Inserts a node into the map.
Definition: NBNodeCont.cpp:77
LaneSpreadFunction
Numbers representing special SUMO-XML-attribute values Information how the edge&#39;s lateral offset shal...
Represents a single node (junction) during network building.
Definition: NBNode.h:75
static int getLaneNumber(const std::string &id, const std::string &laneNoS, double speed)
Returns the lane number evaluating the given Navteq-description.
void push_back_noDoublePos(const Position &p)
insert in back a non double position
NBNode * getFromNode() const
Returns the origin node of the edge.
Definition: NBEdge.h:427
Container for nodes during the netbuilding process.
Definition: NBNodeCont.h:63
NBEdgeCont & myEdgeCont
The container to add edges to.
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:203
void setLaneSpreadFunction(LaneSpreadFunction spread)
(Re)sets how the lanes lateral offset shall be computed
Definition: NBEdge.cpp:763
NBNode * getToNode() const
Returns the destination node of the edge.
Definition: NBEdge.h:434
bool mySpeedInKMH
Whether the speed is given in km/h.
A storage for available types of edges.
Definition: NBTypeCont.h:62