SUMO - Simulation of Urban MObility
|
00001 /****************************************************************************/ 00010 // A reader for polygons and pois stored in XML-format 00011 /****************************************************************************/ 00012 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/ 00013 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors 00014 /****************************************************************************/ 00015 // 00016 // This file is part of SUMO. 00017 // SUMO is free software: you can redistribute it and/or modify 00018 // it under the terms of the GNU General Public License as published by 00019 // the Free Software Foundation, either version 3 of the License, or 00020 // (at your option) any later version. 00021 // 00022 /****************************************************************************/ 00023 00024 00025 // =========================================================================== 00026 // included modules 00027 // =========================================================================== 00028 #ifdef _MSC_VER 00029 #include <windows_config.h> 00030 #else 00031 #include <config.h> 00032 #endif 00033 00034 #include <string> 00035 #include <map> 00036 #include <fstream> 00037 #include <utils/options/OptionsCont.h> 00038 #include <utils/options/Option.h> 00039 #include <utils/common/StdDefs.h> 00040 #include <polyconvert/PCPolyContainer.h> 00041 #include <utils/common/RGBColor.h> 00042 #include <utils/geom/GeomHelper.h> 00043 #include <utils/geom/Boundary.h> 00044 #include <utils/geom/Position.h> 00045 #include <utils/geom/GeoConvHelper.h> 00046 #include <utils/xml/XMLSubSys.h> 00047 #include <utils/geom/GeomConvHelper.h> 00048 #include <utils/common/MsgHandler.h> 00049 #include <utils/common/FileHelpers.h> 00050 #include <utils/xml/SUMOXMLDefinitions.h> 00051 #include "PCLoaderXML.h" 00052 00053 #ifdef CHECK_MEMORY_LEAKS 00054 #include <foreign/nvwa/debug_new.h> 00055 #endif // CHECK_MEMORY_LEAKS 00056 00057 00058 // =========================================================================== 00059 // method definitions 00060 // =========================================================================== 00061 // --------------------------------------------------------------------------- 00062 // static interface 00063 // --------------------------------------------------------------------------- 00064 void 00065 PCLoaderXML::loadIfSet(OptionsCont& oc, PCPolyContainer& toFill, 00066 PCTypeMap& tm) { 00067 if (!oc.isSet("xml-files")) { 00068 return; 00069 } 00070 PCLoaderXML handler(toFill, tm, oc); 00071 // parse file(s) 00072 std::vector<std::string> files = oc.getStringVector("xml"); 00073 for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) { 00074 if (!FileHelpers::exists(*file)) { 00075 throw ProcessError("Could not open xml-file '" + *file + "'."); 00076 } 00077 PROGRESS_BEGIN_MESSAGE("Parsing XML from '" + *file + "'"); 00078 if (!XMLSubSys::runParser(handler, *file)) { 00079 throw ProcessError(); 00080 } 00081 PROGRESS_DONE_MESSAGE(); 00082 } 00083 } 00084 00085 00086 00087 // --------------------------------------------------------------------------- 00088 // handler methods 00089 // --------------------------------------------------------------------------- 00090 PCLoaderXML::PCLoaderXML(PCPolyContainer& toFill, 00091 PCTypeMap& tm, OptionsCont& oc) 00092 : SUMOSAXHandler("xml-poi-definition"), 00093 myCont(toFill), myTypeMap(tm), myOptions(oc) {} 00094 00095 00096 PCLoaderXML::~PCLoaderXML() {} 00097 00098 00099 void 00100 PCLoaderXML::myStartElement(int element, 00101 const SUMOSAXAttributes& attrs) { 00102 if (element != SUMO_TAG_POI && element != SUMO_TAG_POLY) { 00103 return; 00104 } 00105 if (element == SUMO_TAG_POI) { 00106 bool ok = true; 00107 // get the id, report an error if not given or empty... 00108 std::string id = attrs.getStringReporting(SUMO_ATTR_ID, 0, ok); 00109 SUMOReal x = attrs.getSUMORealReporting(SUMO_ATTR_X, id.c_str(), ok); 00110 SUMOReal y = attrs.getSUMORealReporting(SUMO_ATTR_Y, id.c_str(), ok); 00111 std::string type = attrs.getOptStringReporting(SUMO_ATTR_TYPE, id.c_str(), ok, ""); 00112 if (!ok) { 00113 return; 00114 } 00115 Position pos(x, y); 00116 if (!GeoConvHelper::getProcessing().x2cartesian(pos)) { 00117 WRITE_WARNING("Unable to project coordinates for POI '" + id + "'."); 00118 } 00119 // patch the values 00120 bool discard = myOptions.getBool("discard"); 00121 int layer = myOptions.getInt("layer"); 00122 RGBColor color; 00123 if (myTypeMap.has(type)) { 00124 const PCTypeMap::TypeDef& def = myTypeMap.get(type); 00125 id = def.prefix + id; 00126 type = def.id; 00127 color = RGBColor::parseColor(def.color); 00128 discard = def.discard; 00129 layer = def.layer; 00130 } else { 00131 id = myOptions.getString("prefix") + id; 00132 type = myOptions.getString("type"); 00133 color = RGBColor::parseColor(myOptions.getString("color")); 00134 } 00135 if (!discard) { 00136 bool ignorePrunning = false; 00137 if (OptionsCont::getOptions().isInStringVector("prune.keep-list", id)) { 00138 ignorePrunning = true; 00139 } 00140 PointOfInterest* poi = new PointOfInterest(id, type, pos, color); 00141 if (!myCont.insert(id, poi, layer, ignorePrunning)) { 00142 WRITE_ERROR("POI '" + id + "' could not been added."); 00143 delete poi; 00144 } 00145 } 00146 } 00147 if (element == SUMO_TAG_POLY) { 00148 bool discard = myOptions.getBool("discard"); 00149 int layer = myOptions.getInt("layer"); 00150 bool ok = true; 00151 std::string id = attrs.getOptStringReporting(SUMO_ATTR_ID, myCurrentID.c_str(), ok, ""); 00152 std::string type = attrs.getOptStringReporting(SUMO_ATTR_TYPE, myCurrentID.c_str(), ok, ""); 00153 if (!ok) { 00154 return; 00155 } 00156 RGBColor color; 00157 if (myTypeMap.has(type)) { 00158 const PCTypeMap::TypeDef& def = myTypeMap.get(type); 00159 id = def.prefix + id; 00160 type = def.id; 00161 color = RGBColor::parseColor(def.color); 00162 discard = def.discard; 00163 layer = def.layer; 00164 } else { 00165 id = myOptions.getString("prefix") + id; 00166 type = myOptions.getString("type"); 00167 color = RGBColor::parseColor(myOptions.getString("color")); 00168 } 00169 if (!discard) { 00170 bool ignorePrunning = false; 00171 if (OptionsCont::getOptions().isInStringVector("prune.keep-list", id)) { 00172 ignorePrunning = true; 00173 } 00174 myCurrentID = id; 00175 myCurrentType = type; 00176 myCurrentColor = color; 00177 myCurrentIgnorePrunning = ignorePrunning; 00178 myCurrentLayer = layer; 00179 if (attrs.hasAttribute(SUMO_ATTR_SHAPE)) { 00180 // @deprecated At some time, no shape definition using characters will be allowed 00181 myCharacters(element, attrs.getStringReporting(SUMO_ATTR_SHAPE, myCurrentID.c_str(), ok)); 00182 } 00183 } 00184 } 00185 } 00186 00187 00188 void 00189 PCLoaderXML::myCharacters(int element, 00190 const std::string& chars) { 00191 if (element == SUMO_TAG_POLY) { 00192 bool ok = true; 00193 PositionVector pshape = GeomConvHelper::parseShapeReporting(chars, "poly", myCurrentID.c_str(), ok, false); 00194 if (!ok) { 00195 return; 00196 } 00197 PositionVector shape; 00198 for (PositionVector::ContType::const_iterator i = pshape.begin(); i != pshape.end(); ++i) { 00199 Position pos((*i)); 00200 if (!GeoConvHelper::getProcessing().x2cartesian(pos)) { 00201 WRITE_WARNING("Unable to project coordinates for polygon '" + myCurrentID + "'."); 00202 } 00203 shape.push_back(pos); 00204 } 00205 Polygon* poly = new Polygon(myCurrentID, myCurrentType, myCurrentColor, shape, false); 00206 if (!myCont.insert(myCurrentID, poly, myCurrentLayer, myCurrentIgnorePrunning)) { 00207 WRITE_ERROR("Polygon '" + myCurrentID + "' could not been added."); 00208 delete poly; 00209 } 00210 } 00211 } 00212 00213 00214 /****************************************************************************/ 00215