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