Line data Source code
1 : /****************************************************************************/ 2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo 3 : // Copyright (C) 2001-2024 German Aerospace Center (DLR) and others. 4 : // This program and the accompanying materials are made available under the 5 : // terms of the Eclipse Public License 2.0 which is available at 6 : // https://www.eclipse.org/legal/epl-2.0/ 7 : // This Source Code may also be made available under the following Secondary 8 : // Licenses when the conditions for such availability set forth in the Eclipse 9 : // Public License 2.0 are satisfied: GNU General Public License, version 2 10 : // or later which is available at 11 : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html 12 : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later 13 : /****************************************************************************/ 14 : /// @file PCNetProjectionLoader.cpp 15 : /// @author Daniel Krajzewicz 16 : /// @author Jakob Erdmann 17 : /// @author Michael Behrisch 18 : /// @date Thu, 02.11.2006 19 : /// 20 : // A reader for a SUMO network's projection description 21 : /****************************************************************************/ 22 : #include <config.h> 23 : 24 : #include <string> 25 : #include <map> 26 : #include <fstream> 27 : #include <utils/options/OptionsCont.h> 28 : #include <utils/options/Option.h> 29 : #include <utils/common/FileHelpers.h> 30 : #include <utils/common/MsgHandler.h> 31 : #include <utils/common/RGBColor.h> 32 : #include <utils/common/StdDefs.h> 33 : #include <utils/common/SysUtils.h> 34 : #include <utils/geom/GeomHelper.h> 35 : #include <utils/geom/Boundary.h> 36 : #include <utils/geom/Position.h> 37 : #include <utils/geom/GeoConvHelper.h> 38 : #include <utils/xml/XMLSubSys.h> 39 : #include <utils/xml/SUMOXMLDefinitions.h> 40 : #include <utils/xml/SUMOSAXReader.h> 41 : #include <utils/geom/GeomConvHelper.h> 42 : #include <polyconvert/PCPolyContainer.h> 43 : #include "PCNetProjectionLoader.h" 44 : 45 : 46 : // =========================================================================== 47 : // method definitions 48 : // =========================================================================== 49 : // --------------------------------------------------------------------------- 50 : // static interface 51 : // --------------------------------------------------------------------------- 52 : void 53 13 : PCNetProjectionLoader::load(const std::string& file, double scale) { 54 26 : if (!FileHelpers::isReadable(file)) { 55 0 : throw ProcessError(TLF("Could not open net-file '%'.", file)); 56 : } 57 : // build handler and parser 58 13 : PCNetProjectionLoader handler(scale); 59 13 : handler.setFileName(file); 60 13 : SUMOSAXReader* parser = XMLSubSys::getSAXReader(handler, true); 61 26 : const long before = PROGRESS_BEGIN_TIME_MESSAGE("Parsing network projection from '" + file + "'"); 62 26 : if (!parser->parseFirst(file)) { 63 0 : delete parser; 64 0 : throw ProcessError(TLF("Can not read XML-file '%'.", handler.getFileName())); 65 : } 66 : // parse 67 39 : while (parser->parseNext() && !handler.hasReadAll()); 68 : // clean up 69 13 : PROGRESS_TIME_MESSAGE(before); 70 13 : if (!handler.hasReadAll()) { 71 0 : throw ProcessError(TL("Could not find projection parameter in net.")); 72 : } 73 13 : delete parser; 74 13 : } 75 : 76 : 77 : // --------------------------------------------------------------------------- 78 : // handler methods 79 : // --------------------------------------------------------------------------- 80 13 : PCNetProjectionLoader::PCNetProjectionLoader(double scale) : 81 : SUMOSAXHandler("sumo-network"), 82 13 : myFoundLocation(false), 83 26 : myScale(scale) { 84 13 : } 85 : 86 : 87 13 : PCNetProjectionLoader::~PCNetProjectionLoader() {} 88 : 89 : 90 : void 91 26 : PCNetProjectionLoader::myStartElement(int element, 92 : const SUMOSAXAttributes& attrs) { 93 26 : if (element != SUMO_TAG_LOCATION) { 94 13 : return; 95 : } 96 : // @todo refactor parsing of location since its duplicated in NLHandler and PCNetProjectionLoader 97 13 : myFoundLocation = true; 98 13 : PositionVector s = attrs.get<PositionVector>(SUMO_ATTR_NET_OFFSET, nullptr, myFoundLocation); 99 13 : Boundary convBoundary = attrs.get<Boundary>(SUMO_ATTR_CONV_BOUNDARY, nullptr, myFoundLocation); 100 13 : Boundary origBoundary = attrs.get<Boundary>(SUMO_ATTR_ORIG_BOUNDARY, nullptr, myFoundLocation); 101 13 : std::string proj = attrs.get<std::string>(SUMO_ATTR_ORIG_PROJ, nullptr, myFoundLocation); 102 13 : if (myFoundLocation) { 103 13 : OptionsCont& oc = OptionsCont::getOptions(); 104 26 : Position networkOffset = s[0] + Position(oc.getFloat("offset.x"), oc.getFloat("offset.y")); 105 13 : GeoConvHelper::init(proj, networkOffset, origBoundary, convBoundary, myScale); 106 : } 107 13 : } 108 : 109 : 110 : bool 111 0 : PCNetProjectionLoader::hasReadAll() const { 112 52 : return myFoundLocation; 113 : } 114 : 115 : 116 : /****************************************************************************/