SUMO - Simulation of Urban MObility
|
00001 /****************************************************************************/ 00010 // A reader of pois and polygons stored in DLR-Navteq (Elmar)-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 <sstream> 00038 #include <iostream> 00039 #include <utils/common/UtilExceptions.h> 00040 #include <utils/common/MsgHandler.h> 00041 #include <utils/common/StringUtils.h> 00042 #include <utils/common/TplConvert.h> 00043 #include <utils/common/ToString.h> 00044 #include <utils/common/StringTokenizer.h> 00045 #include <utils/common/FileHelpers.h> 00046 #include <utils/importio/LineReader.h> 00047 #include <utils/options/OptionsCont.h> 00048 #include <utils/options/Option.h> 00049 #include <utils/common/StdDefs.h> 00050 #include <polyconvert/PCPolyContainer.h> 00051 #include "PCLoaderDlrNavteq.h" 00052 #include <utils/common/RGBColor.h> 00053 #include <utils/geom/GeomHelper.h> 00054 #include <utils/geom/Boundary.h> 00055 #include <utils/geom/Position.h> 00056 #include <utils/geom/GeoConvHelper.h> 00057 00058 #ifdef CHECK_MEMORY_LEAKS 00059 #include <foreign/nvwa/debug_new.h> 00060 #endif // CHECK_MEMORY_LEAKS 00061 00062 00063 // =========================================================================== 00064 // method definitions 00065 // =========================================================================== 00066 void 00067 PCLoaderDlrNavteq::loadIfSet(OptionsCont& oc, PCPolyContainer& toFill, 00068 PCTypeMap& tm) { 00069 if (oc.isSet("dlr-navteq-poly-files")) { 00070 loadPolyFiles(oc, toFill, tm); 00071 } 00072 if (oc.isSet("dlr-navteq-poi-files")) { 00073 loadPOIFiles(oc, toFill, tm); 00074 } 00075 } 00076 00077 00078 void 00079 PCLoaderDlrNavteq::loadPOIFiles(OptionsCont& oc, PCPolyContainer& toFill, 00080 PCTypeMap& tm) { 00081 std::vector<std::string> files = oc.getStringVector("dlr-navteq-poi-files"); 00082 for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) { 00083 if (!FileHelpers::exists(*file)) { 00084 throw ProcessError("Could not open dlr-navteq-poi-file '" + *file + "'."); 00085 } 00086 PROGRESS_BEGIN_MESSAGE("Parsing pois from dlr-navteq-poi-file '" + *file + "'"); 00087 loadPOIFile(*file, oc, toFill, tm); 00088 PROGRESS_DONE_MESSAGE(); 00089 } 00090 } 00091 00092 00093 void 00094 PCLoaderDlrNavteq::loadPolyFiles(OptionsCont& oc, PCPolyContainer& toFill, 00095 PCTypeMap& tm) { 00096 std::vector<std::string> files = oc.getStringVector("dlr-navteq-poly-files"); 00097 for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) { 00098 if (!FileHelpers::exists(*file)) { 00099 throw ProcessError("Could not open dlr-navteq-poly-file '" + *file + "'."); 00100 } 00101 PROGRESS_BEGIN_MESSAGE("Parsing pois from dlr-navteq-poly-file '" + *file + "'"); 00102 loadPolyFile(*file, oc, toFill, tm); 00103 PROGRESS_DONE_MESSAGE(); 00104 } 00105 } 00106 00107 00108 void 00109 PCLoaderDlrNavteq::loadPOIFile(const std::string& file, 00110 OptionsCont& oc, PCPolyContainer& toFill, 00111 PCTypeMap& tm) { 00112 // get the defaults 00113 RGBColor c = RGBColor::parseColor(oc.getString("color")); 00114 // parse 00115 int l = 0; 00116 LineReader lr(file); 00117 while (lr.hasMore()) { 00118 std::string line = lr.readLine(); 00119 ++l; 00120 // skip invalid/empty lines 00121 if (line.length() == 0 || line.find("#") != std::string::npos) { 00122 continue; 00123 } 00124 if (StringUtils::prune(line) == "") { 00125 continue; 00126 } 00127 // parse the poi 00128 std::istringstream stream(line); 00129 // attributes of the poi 00130 std::string name, skip, type, desc; 00131 std::getline(stream, name, '\t'); 00132 std::getline(stream, skip, '\t'); 00133 std::getline(stream, type, '\t'); 00134 std::getline(stream, desc, '\t'); 00135 if (stream.fail()) { 00136 throw ProcessError("Invalid dlr-navteq-poi in line " + toString(l) + ":\n" + line); 00137 } 00138 double x, y; 00139 stream >> x; 00140 if (stream.fail()) { 00141 throw ProcessError("Invalid x coordinate for POI '" + name + "'."); 00142 } 00143 stream >> y; 00144 if (stream.fail()) { 00145 throw ProcessError("Invalid y coordinate for POI '" + name + "'."); 00146 } 00147 Position pos(x, y); 00148 // check the poi 00149 if (name == "") { 00150 throw ProcessError("The name of a POI is missing."); 00151 } 00152 if (!GeoConvHelper::getProcessing().x2cartesian(pos, true)) { 00153 throw ProcessError("Unable to project coordinates for POI '" + name + "'."); 00154 } 00155 00156 // patch the values 00157 bool discard = oc.getBool("discard"); 00158 int layer = oc.getInt("layer"); 00159 RGBColor color; 00160 if (tm.has(type)) { 00161 const PCTypeMap::TypeDef& def = tm.get(type); 00162 name = def.prefix + name; 00163 type = def.id; 00164 color = RGBColor::parseColor(def.color); 00165 discard = def.discard; 00166 layer = def.layer; 00167 } else { 00168 name = oc.getString("prefix") + name; 00169 type = oc.getString("type"); 00170 color = c; 00171 } 00172 if (!discard) { 00173 bool ignorePrunning = false; 00174 if (OptionsCont::getOptions().isInStringVector("prune.keep-list", name)) { 00175 ignorePrunning = true; 00176 } 00177 PointOfInterest* poi = new PointOfInterest(name, type, pos, color); 00178 if (!toFill.insert(name, poi, layer, ignorePrunning)) { 00179 WRITE_ERROR("POI '" + name + "' could not been added."); 00180 delete poi; 00181 } 00182 } 00183 } 00184 } 00185 00186 00187 void 00188 PCLoaderDlrNavteq::loadPolyFile(const std::string& file, 00189 OptionsCont& oc, PCPolyContainer& toFill, 00190 PCTypeMap& tm) { 00191 // get the defaults 00192 RGBColor c = RGBColor::parseColor(oc.getString("color")); 00193 // attributes of the poly 00194 // parse 00195 int l = 0; 00196 LineReader lr(file); 00197 while (lr.hasMore()) { 00198 std::string line = lr.readLine(); 00199 ++l; 00200 // skip invalid/empty lines 00201 if (line.length() == 0 || line.find("#") != std::string::npos) { 00202 continue; 00203 } 00204 if (StringUtils::prune(line) == "") { 00205 continue; 00206 } 00207 // parse the poi 00208 StringTokenizer st(line, "\t"); 00209 std::vector<std::string> values = st.getVector(); 00210 if (values.size() < 6 || values.size() % 2 != 0) { 00211 throw ProcessError("Invalid dlr-navteq-polygon - line: '" + line + "'."); 00212 } 00213 std::string id = values[0]; 00214 std::string ort = values[1]; 00215 std::string type = values[2]; 00216 std::string name = values[3]; 00217 PositionVector vec; 00218 size_t index = 4; 00219 // now collect the positions 00220 while (values.size() > index) { 00221 std::string xpos = values[index]; 00222 std::string ypos = values[index + 1]; 00223 index += 2; 00224 SUMOReal x = TplConvert<char>::_2SUMOReal(xpos.c_str()); 00225 SUMOReal y = TplConvert<char>::_2SUMOReal(ypos.c_str()); 00226 Position pos(x, y); 00227 if (!GeoConvHelper::getProcessing().x2cartesian(pos)) { 00228 WRITE_WARNING("Unable to project coordinates for polygon '" + id + "'."); 00229 } 00230 vec.push_back(pos); 00231 } 00232 00233 name = StringUtils::convertUmlaute(name); 00234 if (name == "noname" || toFill.containsPolygon(name)) { 00235 name = name + "#" + toString(toFill.getEnumIDFor(name)); 00236 } 00237 00238 // check the polygon 00239 if (vec.size() == 0) { 00240 WRITE_WARNING("The polygon '" + id + "' is empty."); 00241 continue; 00242 } 00243 if (id == "") { 00244 WRITE_WARNING("The name of a polygon is missing; it will be discarded."); 00245 continue; 00246 } 00247 00248 // patch the values 00249 bool fill = vec.getBegin() == vec.getEnd(); 00250 bool discard = oc.getBool("discard"); 00251 int layer = oc.getInt("layer"); 00252 RGBColor color; 00253 if (tm.has(type)) { 00254 const PCTypeMap::TypeDef& def = tm.get(type); 00255 name = def.prefix + name; 00256 type = def.id; 00257 color = RGBColor::parseColor(def.color); 00258 fill = fill && def.allowFill; 00259 discard = def.discard; 00260 layer = def.layer; 00261 } else { 00262 name = oc.getString("prefix") + name; 00263 type = oc.getString("type"); 00264 color = c; 00265 } 00266 if (!discard) { 00267 Polygon* poly = new Polygon(name, type, color, vec, fill); 00268 if (!toFill.insert(name, poly, layer)) { 00269 WRITE_ERROR("Polygon '" + name + "' could not been added."); 00270 delete poly; 00271 } 00272 } 00273 vec.clear(); 00274 } 00275 } 00276 00277 00278 00279 00280 00281 /****************************************************************************/ 00282