SUMO - Simulation of Urban MObility
|
00001 /****************************************************************************/ 00008 // Importer for networks stored in ITSUMO format 00009 /****************************************************************************/ 00010 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/ 00011 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors 00012 /****************************************************************************/ 00013 // 00014 // This file is part of SUMO. 00015 // SUMO is free software: you can redistribute it and/or modify 00016 // it under the terms of the GNU General Public License as published by 00017 // the Free Software Foundation, either version 3 of the License, or 00018 // (at your option) any later version. 00019 // 00020 /****************************************************************************/ 00021 00022 00023 // =========================================================================== 00024 // included modules 00025 // =========================================================================== 00026 #ifdef _MSC_VER 00027 #include <windows_config.h> 00028 #else 00029 #include <config.h> 00030 #endif 00031 #include <set> 00032 #include <functional> 00033 #include <sstream> 00034 #include <utils/xml/SUMOSAXHandler.h> 00035 #include <utils/common/MsgHandler.h> 00036 #include <netbuild/NBEdge.h> 00037 #include <netbuild/NBEdgeCont.h> 00038 #include <netbuild/NBNode.h> 00039 #include <netbuild/NBNodeCont.h> 00040 #include <netbuild/NBNetBuilder.h> 00041 #include <utils/geom/GeoConvHelper.h> 00042 #include <utils/options/OptionsCont.h> 00043 #include <utils/common/StringUtils.h> 00044 #include <utils/common/FileHelpers.h> 00045 #include <utils/common/StringTokenizer.h> 00046 #include <utils/common/TplConvert.h> 00047 #include <utils/xml/XMLSubSys.h> 00048 #include "NILoader.h" 00049 #include "NIImporter_ITSUMO.h" 00050 00051 #ifdef CHECK_MEMORY_LEAKS 00052 #include <foreign/nvwa/debug_new.h> 00053 #endif // CHECK_MEMORY_LEAKS 00054 00055 00056 00057 // =========================================================================== 00058 // static variables 00059 // =========================================================================== 00060 StringBijection<int>::Entry NIImporter_ITSUMO::itsumoTags[] = { 00061 { "simulation", NIImporter_ITSUMO::ITSUMO_TAG_SIMULATION }, 00062 { "network_id", NIImporter_ITSUMO::ITSUMO_TAG_NETWORK_ID }, 00063 { "network_name", NIImporter_ITSUMO::ITSUMO_TAG_NETWORK_NAME }, 00064 { "nodes", NIImporter_ITSUMO::ITSUMO_TAG_NODES }, 00065 { "node", NIImporter_ITSUMO::ITSUMO_TAG_NODE }, 00066 { "node_id", NIImporter_ITSUMO::ITSUMO_TAG_NODE_ID }, 00067 { "node_name", NIImporter_ITSUMO::ITSUMO_TAG_NODE_NAME }, 00068 { "x_coord", NIImporter_ITSUMO::ITSUMO_TAG_X_COORD }, 00069 { "y_coord", NIImporter_ITSUMO::ITSUMO_TAG_Y_COORD }, 00070 { "sources", NIImporter_ITSUMO::ITSUMO_TAG_SOURCES }, 00071 { "sinks", NIImporter_ITSUMO::ITSUMO_TAG_SINKS }, 00072 { "traffic_lights", NIImporter_ITSUMO::ITSUMO_TAG_TRAFFIC_LIGHTS }, 00073 { "streets", NIImporter_ITSUMO::ITSUMO_TAG_STREETS }, 00074 { "street", NIImporter_ITSUMO::ITSUMO_TAG_STREET }, 00075 { "street_id", NIImporter_ITSUMO::ITSUMO_TAG_STREET_ID }, 00076 { "street_name", NIImporter_ITSUMO::ITSUMO_TAG_STREET_NAME }, 00077 { "sections", NIImporter_ITSUMO::ITSUMO_TAG_SECTIONS }, 00078 { "section", NIImporter_ITSUMO::ITSUMO_TAG_SECTION }, 00079 { "section_id", NIImporter_ITSUMO::ITSUMO_TAG_SECTION_ID }, 00080 { "section_name", NIImporter_ITSUMO::ITSUMO_TAG_SECTION_NAME }, 00081 { "is_preferencial", NIImporter_ITSUMO::ITSUMO_TAG_IS_PREFERENCIAL }, 00082 { "delimiting_node", NIImporter_ITSUMO::ITSUMO_TAG_DELIMITING_NODE }, 00083 { "lanesets", NIImporter_ITSUMO::ITSUMO_TAG_LANESETS }, 00084 { "laneset", NIImporter_ITSUMO::ITSUMO_TAG_LANESET }, 00085 { "laneset_id", NIImporter_ITSUMO::ITSUMO_TAG_LANESET_ID }, 00086 { "laneset_position", NIImporter_ITSUMO::ITSUMO_TAG_LANESET_POSITION }, 00087 { "start_node", NIImporter_ITSUMO::ITSUMO_TAG_START_NODE }, 00088 { "end_node", NIImporter_ITSUMO::ITSUMO_TAG_END_NODE }, 00089 { "turning_probabilities", NIImporter_ITSUMO::ITSUMO_TAG_TURNING_PROBABILITIES }, 00090 { "direction", NIImporter_ITSUMO::ITSUMO_TAG_DIRECTION }, 00091 { "destination_laneset", NIImporter_ITSUMO::ITSUMO_TAG_DESTINATION_LANESET }, 00092 { "probability", NIImporter_ITSUMO::ITSUMO_TAG_PROBABILITY }, 00093 { "lanes", NIImporter_ITSUMO::ITSUMO_TAG_LANES }, 00094 { "lane", NIImporter_ITSUMO::ITSUMO_TAG_LANE }, 00095 { "lane_id", NIImporter_ITSUMO::ITSUMO_TAG_LANE_ID }, 00096 { "lane_position", NIImporter_ITSUMO::ITSUMO_TAG_LANE_POSITION }, 00097 { "maximum_speed", NIImporter_ITSUMO::ITSUMO_TAG_MAXIMUM_SPEED }, 00098 { "deceleration_prob", NIImporter_ITSUMO::ITSUMO_TAG_DECELERATION_PROB }, 00099 { "", NIImporter_ITSUMO::ITSUMO_TAG_NOTHING } 00100 }; 00101 00102 00103 StringBijection<int>::Entry NIImporter_ITSUMO::itsumoAttrs[] = { 00104 { "", NIImporter_ITSUMO::ITSUMO_ATTR_NOTHING } 00105 }; 00106 00107 00108 // =========================================================================== 00109 // method definitions 00110 // =========================================================================== 00111 // --------------------------------------------------------------------------- 00112 // static methods 00113 // --------------------------------------------------------------------------- 00114 void 00115 NIImporter_ITSUMO::loadNetwork(const OptionsCont& oc, NBNetBuilder& nb) { 00116 // check whether the option is set (properly) 00117 if (!oc.isSet("itsumo-files")) { 00118 return; 00119 } 00120 /* Parse file(s) 00121 * Each file is parsed twice: first for nodes, second for edges. */ 00122 std::vector<std::string> files = oc.getStringVector("itsumo-files"); 00123 // load nodes, first 00124 Handler Handler(nb); 00125 for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) { 00126 // nodes 00127 if (!FileHelpers::exists(*file)) { 00128 WRITE_ERROR("Could not open itsumo-file '" + *file + "'."); 00129 return; 00130 } 00131 Handler.setFileName(*file); 00132 PROGRESS_BEGIN_MESSAGE("Parsing nodes from itsumo-file '" + *file + "'"); 00133 if (!XMLSubSys::runParser(Handler, *file)) { 00134 return; 00135 } 00136 PROGRESS_DONE_MESSAGE(); 00137 } 00138 } 00139 00140 00141 // --------------------------------------------------------------------------- 00142 // definitions of NIImporter_ITSUMO::Handler-methods 00143 // --------------------------------------------------------------------------- 00144 NIImporter_ITSUMO::Handler::Handler(NBNetBuilder& toFill) 00145 : GenericSAXHandler(itsumoTags, ITSUMO_TAG_NOTHING, itsumoAttrs, ITSUMO_ATTR_NOTHING, "itsumo - file"), myNetBuilder(toFill) { 00146 } 00147 00148 00149 NIImporter_ITSUMO::Handler::~Handler() {} 00150 00151 00152 void 00153 NIImporter_ITSUMO::Handler::myStartElement(int element, const SUMOSAXAttributes& attrs) { 00154 switch (element) { 00155 case ITSUMO_TAG_NODE: 00156 myParameter.clear(); 00157 break; 00158 case ITSUMO_TAG_LANESET: 00159 myParameter.clear(); 00160 break; 00161 default: 00162 break; 00163 } 00164 } 00165 00166 00167 void 00168 NIImporter_ITSUMO::Handler::myCharacters(int element, const std::string& chars) { 00169 std::string mc = StringUtils::prune(chars); 00170 switch (element) { 00171 // node parsing 00172 case ITSUMO_TAG_NODE_ID: 00173 myParameter["id"] = mc; 00174 break; 00175 case ITSUMO_TAG_NODE_NAME: 00176 myParameter["name"] = mc; 00177 break; 00178 case ITSUMO_TAG_X_COORD: 00179 myParameter["x"] = mc; 00180 break; 00181 case ITSUMO_TAG_Y_COORD: 00182 myParameter["y"] = mc; 00183 break; 00184 // section parsing 00185 case ITSUMO_TAG_SECTION_ID: 00186 myParameter["sectionID"] = mc; 00187 break; 00188 // laneset parsing 00189 case ITSUMO_TAG_LANESET_ID: 00190 myParameter["lanesetID"] = mc; 00191 break; 00192 case ITSUMO_TAG_LANESET_POSITION: 00193 myParameter["pos"] = mc; 00194 break; 00195 case ITSUMO_TAG_START_NODE: 00196 myParameter["from"] = mc; 00197 break; 00198 case ITSUMO_TAG_END_NODE: 00199 myParameter["to"] = mc; 00200 break; 00201 // lane parsing 00202 case ITSUMO_TAG_LANE_ID: 00203 myParameter["laneID"] = mc; 00204 break; 00205 case ITSUMO_TAG_LANE_POSITION: 00206 myParameter["i"] = mc; 00207 break; 00208 case ITSUMO_TAG_MAXIMUM_SPEED: 00209 myParameter["v"] = mc; 00210 break; 00211 default: 00212 break; 00213 } 00214 } 00215 00216 00217 void 00218 NIImporter_ITSUMO::Handler::myEndElement(int element) { 00219 switch (element) { 00220 case ITSUMO_TAG_SIMULATION: { 00221 for (std::vector<Section*>::iterator i = mySections.begin(); i != mySections.end(); ++i) { 00222 for (std::vector<LaneSet*>::iterator j = (*i)->myLaneSets.begin(); j != (*i)->myLaneSets.end(); ++j) { 00223 LaneSet* ls = (*j); 00224 NBEdge* edge = new NBEdge(ls->myID, ls->myFrom, ls->myTo, "", ls->myV, (unsigned int)ls->myLanes.size(), -1, -1, -1); 00225 if (!myNetBuilder.getEdgeCont().insert(edge)) { 00226 delete edge; 00227 WRITE_ERROR("Could not add edge '" + ls->myID + "'. Probably declared twice."); 00228 } 00229 delete ls; 00230 } 00231 delete *i; 00232 } 00233 } 00234 break; 00235 case ITSUMO_TAG_NODE: { 00236 try { 00237 std::string id = myParameter["id"]; 00238 SUMOReal x = TplConvert<char>::_2SUMOReal(myParameter["x"].c_str()); 00239 SUMOReal y = TplConvert<char>::_2SUMOReal(myParameter["y"].c_str()); 00240 Position pos(x, y); 00241 if (!NILoader::transformCoordinates(pos)) { 00242 WRITE_ERROR("Unable to project coordinates for node '" + id + "'."); 00243 } 00244 NBNode* node = new NBNode(id, pos); 00245 if (!myNetBuilder.getNodeCont().insert(node)) { 00246 delete node; 00247 WRITE_ERROR("Could not add node '" + id + "'. Probably declared twice."); 00248 } 00249 } catch (NumberFormatException&) { 00250 WRITE_ERROR("Not numeric position information for node '" + myParameter["id"] + "'."); 00251 } catch (EmptyData&) { 00252 WRITE_ERROR("Missing data in node '" + myParameter["id"] + "'."); 00253 } 00254 } 00255 break; 00256 case ITSUMO_TAG_SECTION: { 00257 mySections.push_back(new Section(myParameter["sectionID"], myCurrentLaneSets)); 00258 myCurrentLaneSets.clear(); 00259 } 00260 break; 00261 case ITSUMO_TAG_LANESET: { 00262 try { 00263 std::string id = myParameter["lanesetID"]; 00264 int i = TplConvert<char>::_2int(myParameter["i"].c_str()); 00265 std::string fromID = myParameter["from"]; 00266 std::string toID = myParameter["to"]; 00267 NBNode* from = myNetBuilder.getNodeCont().retrieve(fromID); 00268 NBNode* to = myNetBuilder.getNodeCont().retrieve(toID); 00269 if (from == 0 || to == 0) { 00270 WRITE_ERROR("Missing node in laneset '" + myParameter["lanesetID"] + "'."); 00271 } else { 00272 if (myLaneSets.find(id) != myLaneSets.end()) { 00273 WRITE_ERROR("Fond laneset-id '" + id + "' twice."); 00274 } else { 00275 SUMOReal vSum = 0; 00276 for (std::vector<Lane>::iterator j = myCurrentLanes.begin(); j != myCurrentLanes.end(); ++j) { 00277 vSum += (*j).myV; 00278 } 00279 vSum /= (SUMOReal) myCurrentLanes.size(); 00280 LaneSet* ls = new LaneSet(id, myCurrentLanes, vSum, i, from, to); 00281 myLaneSets[id] = ls; 00282 myCurrentLaneSets.push_back(ls); 00283 myCurrentLanes.clear(); 00284 } 00285 } 00286 } catch (NumberFormatException&) { 00287 WRITE_ERROR("Not numeric value in laneset '" + myParameter["lanesetID"] + "'."); 00288 } catch (EmptyData&) { 00289 WRITE_ERROR("Missing data in laneset '" + myParameter["lanesetID"] + "'."); 00290 } 00291 } 00292 break; 00293 case ITSUMO_TAG_LANE: { 00294 try { 00295 std::string id = myParameter["laneID"]; 00296 int pos = TplConvert<char>::_2int(myParameter["pos"].c_str()); 00297 int i = TplConvert<char>::_2int(myParameter["i"].c_str()); 00298 SUMOReal v = TplConvert<char>::_2SUMOReal(myParameter["v"].c_str()); 00299 myCurrentLanes.push_back(Lane(id, (unsigned int) i, v)); 00300 } catch (NumberFormatException&) { 00301 WRITE_ERROR("Not numeric value in lane '" + myParameter["laneID"] + "'."); 00302 } catch (EmptyData&) { 00303 WRITE_ERROR("Missing data in lane '" + myParameter["laneID"] + "'."); 00304 } 00305 } 00306 break; 00307 default: 00308 break; 00309 } 00310 } 00311 00312 00313 /****************************************************************************/ 00314