PCNetProjectionLoader.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // A reader for a SUMO network's projection description
00008 /****************************************************************************/
00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00010 // Copyright 2001-2010 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 "PCNetProjectionLoader.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 // method definitions
00055 // ===========================================================================
00056 // ---------------------------------------------------------------------------
00057 // static interface
00058 // ---------------------------------------------------------------------------
00059 void
00060 PCNetProjectionLoader::loadIfSet(OptionsCont &oc,
00061                                  Position2D &netOffset, Boundary &origNetBoundary,
00062                                  Boundary &convNetBoundary,
00063                                  std::string &projParameter) throw(ProcessError) {
00064     if (!oc.isSet("net")) {
00065         return;
00066     }
00067     // check file
00068     std::string file = oc.getString("net");
00069     if (!FileHelpers::exists(file)) {
00070         throw ProcessError("Could not open net-file '" + file + "'.");
00071     }
00072     // build handler and parser
00073     PCNetProjectionLoader handler(netOffset, origNetBoundary, convNetBoundary, projParameter);
00074     handler.setFileName(file);
00075     XMLPScanToken token;
00076     XERCES_CPP_NAMESPACE_QUALIFIER SAX2XMLReader *parser = XMLSubSys::getSAXReader(handler);
00077     MsgHandler::getMessageInstance()->beginProcessMsg("Parsing network projection from '" + file + "'...");
00078     if (!parser->parseFirst(file.c_str(), token)) {
00079         delete parser;
00080         throw ProcessError("Can not read XML-file '" + handler.getFileName() + "'.");
00081     }
00082     // parse
00083     while (parser->parseNext(token) && !handler.hasReadAll());
00084     // clean up
00085     MsgHandler::getMessageInstance()->endProcessMsg("done.");
00086     if (!handler.hasReadAll()) {
00087         throw ProcessError("Could not find projection parameter in net.");
00088     }
00089     delete parser;
00090 }
00091 
00092 
00093 
00094 // ---------------------------------------------------------------------------
00095 // handler methods
00096 // ---------------------------------------------------------------------------
00097 PCNetProjectionLoader::PCNetProjectionLoader(Position2D &netOffset,
00098         Boundary &origNetBoundary, Boundary &convNetBoundary,
00099         std::string &projParameter) throw()
00100         : SUMOSAXHandler("sumo-network"), myNetOffset(netOffset),
00101         myOrigNetBoundary(origNetBoundary), myConvNetBoundary(convNetBoundary),
00102         myProjParameter(projParameter),
00103         myFoundOffset(false), myFoundOrigNetBoundary(false),
00104         myFoundConvNetBoundary(false), myFoundProj(false) {}
00105 
00106 
00107 PCNetProjectionLoader::~PCNetProjectionLoader() throw() {}
00108 
00109 
00110 void
00111 PCNetProjectionLoader::myStartElement(SumoXMLTag element,
00112                                       const SUMOSAXAttributes &attrs) throw(ProcessError) {
00113     if (element!=SUMO_TAG_LOCATION) {
00114         return;
00115     }
00116     bool ok = true;
00117     Position2DVector tmp = GeomConvHelper::parseShapeReporting(attrs.getOptStringReporting(SUMO_ATTR_NET_OFFSET, "net", 0, ok, ""), "net", 0, ok, false);
00118     if (ok) {
00119         myNetOffset = tmp[0];
00120     }
00121     myOrigNetBoundary = GeomConvHelper::parseBoundaryReporting(attrs.getOptStringReporting(SUMO_ATTR_ORIG_BOUNDARY, "net", 0, ok, ""), "net", 0, ok);
00122     myConvNetBoundary = GeomConvHelper::parseBoundaryReporting(attrs.getOptStringReporting(SUMO_ATTR_CONV_BOUNDARY, "net", 0, ok, ""), "net", 0, ok);
00123     myProjParameter = attrs.getOptStringReporting(SUMO_ATTR_ORIG_PROJ, "net", 0, ok, "");
00124     myFoundOffset = myFoundOrigNetBoundary = myFoundConvNetBoundary = myFoundProj = ok;
00125 }
00126 
00127 
00128 void
00129 PCNetProjectionLoader::myCharacters(SumoXMLTag element,
00130                                     const std::string &chars) throw(ProcessError) {
00131     bool ok = true;
00132     switch (element) {
00133     case SUMO_TAG_ORIG_BOUNDARY:
00134         myOrigNetBoundary = GeomConvHelper::parseBoundaryReporting(chars, "net", 0, ok);
00135         myFoundOrigNetBoundary = ok;
00136         break;
00137     case SUMO_TAG_CONV_BOUNDARY:
00138         myConvNetBoundary = GeomConvHelper::parseBoundaryReporting(chars, "net", 0, ok);
00139         myFoundConvNetBoundary = ok;
00140         break;
00141     case SUMO_TAG_NET_OFFSET: {
00142         Position2DVector tmp = GeomConvHelper::parseShapeReporting(chars, "net", 0, ok, false);
00143         if (ok) {
00144             myNetOffset = tmp[0];
00145         }
00146         myFoundOffset = ok;
00147     }
00148     break;
00149     case SUMO_TAG_ORIG_PROJ:
00150         myProjParameter = chars;
00151         myFoundProj = true;
00152         break;
00153     default:
00154         break;
00155     }
00156 }
00157 
00158 
00159 bool
00160 PCNetProjectionLoader::hasReadAll() const throw() {
00161     return myFoundOffset&&myFoundOrigNetBoundary&&myFoundConvNetBoundary&&myFoundProj;
00162 }
00163 
00164 
00165 /****************************************************************************/
00166 

Generated on Wed May 5 00:06:35 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6