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