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