00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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 "PCLoaderXML.h"
00038 #include <utils/common/RGBColor.h>
00039 #include <utils/geom/GeomHelper.h>
00040 #include <utils/geom/Boundary.h>
00041 #include <utils/geom/Position2D.h>
00042 #include <utils/geom/GeoConvHelper.h>
00043 #include <utils/xml/XMLSubSys.h>
00044 #include <utils/geom/GeomConvHelper.h>
00045 #include <utils/common/MsgHandler.h>
00046 #include <utils/common/FileHelpers.h>
00047
00048 #ifdef CHECK_MEMORY_LEAKS
00049 #include <foreign/nvwa/debug_new.h>
00050 #endif // CHECK_MEMORY_LEAKS
00051
00052
00053
00054
00055
00056
00057
00058
00059 void
00060 PCLoaderXML::loadIfSet(OptionsCont &oc, PCPolyContainer &toFill,
00061 PCTypeMap &tm) throw(ProcessError) {
00062 if (!oc.isSet("xml")) {
00063 return;
00064 }
00065 PCLoaderXML handler(toFill, tm, oc);
00066
00067 std::vector<std::string> files = oc.getStringVector("xml");
00068 for (std::vector<std::string>::const_iterator file=files.begin(); file!=files.end(); ++file) {
00069 if (!FileHelpers::exists(*file)) {
00070 throw ProcessError("Could not open xml-file '" + *file + "'.");
00071 }
00072 MsgHandler::getMessageInstance()->beginProcessMsg("Parsing XML from '" + *file + "'...");
00073 if (!XMLSubSys::runParser(handler, *file)) {
00074 throw ProcessError();
00075 }
00076 MsgHandler::getMessageInstance()->endProcessMsg("done.");
00077 }
00078 }
00079
00080
00081
00082
00083
00084
00085 PCLoaderXML::PCLoaderXML(PCPolyContainer &toFill,
00086 PCTypeMap &tm, OptionsCont &oc) throw()
00087 : SUMOSAXHandler("xml-poi-definition"),
00088 myCont(toFill), myTypeMap(tm), myOptions(oc) {}
00089
00090
00091 PCLoaderXML::~PCLoaderXML() throw() {}
00092
00093
00094 void
00095 PCLoaderXML::myStartElement(SumoXMLTag element,
00096 const SUMOSAXAttributes &attrs) throw(ProcessError) {
00097 if (element!=SUMO_TAG_POI && element!=SUMO_TAG_POLY) {
00098 return;
00099 }
00100 if (element==SUMO_TAG_POI) {
00101
00102 std::string id;
00103 if (!attrs.setIDFromAttributes("poi", id)) {
00104 return;
00105 }
00106 bool ok = true;
00107 SUMOReal x = attrs.getSUMORealReporting(SUMO_ATTR_X, "poi", id.c_str(), ok);
00108 SUMOReal y = attrs.getSUMORealReporting(SUMO_ATTR_Y, "poi", id.c_str(), ok);
00109 std::string type = attrs.getOptStringReporting(SUMO_ATTR_TYPE, "poi", id.c_str(), ok, "");
00110 if (!ok) {
00111 return;
00112 }
00113 Position2D pos(x, y);
00114 if (!GeoConvHelper::x2cartesian(pos)) {
00115 MsgHandler::getWarningInstance()->inform("Unable to project coordinates for POI '" + id + "'.");
00116 }
00117
00118 bool discard = false;
00119 int layer = myOptions.getInt("layer");
00120 RGBColor color;
00121 if (myTypeMap.has(type)) {
00122 const PCTypeMap::TypeDef &def = myTypeMap.get(type);
00123 id = def.prefix + id;
00124 type = def.id;
00125 color = RGBColor::parseColor(def.color);
00126 discard = def.discard;
00127 layer = def.layer;
00128 } else {
00129 id = myOptions.getString("prefix") + id;
00130 type = myOptions.getString("type");
00131 color = RGBColor::parseColor(myOptions.getString("color"));
00132 }
00133 if (!discard) {
00134 bool ignorePrunning = false;
00135 if (OptionsCont::getOptions().isInStringVector("prune.ignore", id)) {
00136 ignorePrunning = true;
00137 }
00138 PointOfInterest *poi = new PointOfInterest(id, type, pos, color);
00139 if (!myCont.insert(id, poi, layer, ignorePrunning)) {
00140 MsgHandler::getErrorInstance()->inform("POI '" + id + "' could not been added.");
00141 delete poi;
00142 }
00143 }
00144 }
00145 if (element==SUMO_TAG_POLY) {
00146 bool discard = false;
00147 int layer = myOptions.getInt("layer");
00148 bool ok = true;
00149 std::string id = attrs.getOptStringReporting(SUMO_ATTR_ID, "poly", myCurrentID.c_str(), ok, "");
00150 std::string type = attrs.getOptStringReporting(SUMO_ATTR_TYPE, "poly", myCurrentID.c_str(), ok, "");
00151 if (!ok) {
00152 return;
00153 }
00154 RGBColor color;
00155 if (myTypeMap.has(type)) {
00156 const PCTypeMap::TypeDef &def = myTypeMap.get(type);
00157 id = def.prefix + id;
00158 type = def.id;
00159 color = RGBColor::parseColor(def.color);
00160 discard = def.discard;
00161 layer = def.layer;
00162 } else {
00163 id = myOptions.getString("prefix") + id;
00164 type = myOptions.getString("type");
00165 color = RGBColor::parseColor(myOptions.getString("color"));
00166 }
00167 if (!discard) {
00168 bool ignorePrunning = false;
00169 if (OptionsCont::getOptions().isInStringVector("prune.ignore", id)) {
00170 ignorePrunning = true;
00171 }
00172 myCurrentID = id;
00173 myCurrentType = type;
00174 myCurrentColor = color;
00175 myCurrentIgnorePrunning = ignorePrunning;
00176 myCurrentLayer = layer;
00177 if (attrs.hasAttribute(SUMO_ATTR_SHAPE)) {
00178
00179 myCharacters(element, attrs.getStringReporting(SUMO_ATTR_SHAPE, "poly", myCurrentID.c_str(), ok));
00180 }
00181 }
00182 }
00183 }
00184
00185
00186 void
00187 PCLoaderXML::myCharacters(SumoXMLTag element,
00188 const std::string &chars) throw(ProcessError) {
00189 if (element==SUMO_TAG_POLY) {
00190 bool ok = true;
00191 Position2DVector pshape = GeomConvHelper::parseShapeReporting(chars, "poly", myCurrentID.c_str(), ok, false);
00192 if (!ok) {
00193 return;
00194 }
00195 const Position2DVector::ContType &cont = pshape.getCont();
00196 Position2DVector shape;
00197 for (Position2DVector::ContType::const_iterator i=cont.begin(); i!=cont.end(); ++i) {
00198 Position2D pos((*i));
00199 if (!GeoConvHelper::x2cartesian(pos)) {
00200 MsgHandler::getWarningInstance()->inform("Unable to project coordinates for polygon '" + myCurrentID + "'.");
00201 }
00202 shape.push_back(pos);
00203 }
00204 Polygon2D *poly = new Polygon2D(myCurrentID, myCurrentType, myCurrentColor, shape, false);
00205 if (!myCont.insert(myCurrentID, poly, myCurrentLayer, myCurrentIgnorePrunning)) {
00206 MsgHandler::getErrorInstance()->inform("Polygon '" + myCurrentID + "' could not been added.");
00207 delete poly;
00208 }
00209 }
00210 }
00211
00212
00213
00214