#include <config.h>
#include <string>
#include <map>
#include <fstream>
#include <sstream>
#include <iostream>
#include <utils/common/UtilExceptions.h>
#include <utils/common/MsgHandler.h>
#include <utils/common/StringUtils.h>
#include <utils/common/StringUtils.h>
#include <utils/common/ToString.h>
#include <utils/common/StringTokenizer.h>
#include <utils/common/FileHelpers.h>
#include <utils/importio/LineReader.h>
#include <utils/options/OptionsCont.h>
#include <utils/options/Option.h>
#include <utils/common/StdDefs.h>
#include <polyconvert/PCPolyContainer.h>
#include "PCLoaderDlrNavteq.h"
#include <utils/common/RGBColor.h>
#include <utils/geom/GeomHelper.h>
#include <utils/geom/Boundary.h>
#include <utils/geom/Position.h>
#include <utils/geom/GeoConvHelper.h>
void
PCLoaderDlrNavteq::loadIfSet(OptionsCont& oc, PCPolyContainer& toFill,
PCTypeMap& tm) {
if (oc.isSet("dlr-navteq-poly-files")) {
loadPolyFiles(oc, toFill, tm);
}
if (oc.isSet("dlr-navteq-poi-files")) {
loadPOIFiles(oc, toFill, tm);
}
}
void
PCLoaderDlrNavteq::loadPOIFiles(OptionsCont& oc, PCPolyContainer& toFill,
PCTypeMap& tm) {
std::vector<std::string> files = oc.getStringVector("dlr-navteq-poi-files");
for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
if (!FileHelpers::isReadable(*file)) {
throw ProcessError(TLF("Could not open dlr-navteq-poi-file '%'.", *file));
}
PROGRESS_BEGIN_MESSAGE("Parsing pois from dlr-navteq-poi-file '" + *file + "'");
loadPOIFile(*file, oc, toFill, tm);
PROGRESS_DONE_MESSAGE();
}
}
void
PCLoaderDlrNavteq::loadPolyFiles(OptionsCont& oc, PCPolyContainer& toFill,
PCTypeMap& tm) {
std::vector<std::string> files = oc.getStringVector("dlr-navteq-poly-files");
for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
if (!FileHelpers::isReadable(*file)) {
throw ProcessError(TLF("Could not open dlr-navteq-poly-file '%'.", *file));
}
PROGRESS_BEGIN_MESSAGE("Parsing pois from dlr-navteq-poly-file '" + *file + "'");
loadPolyFile(*file, oc, toFill, tm);
PROGRESS_DONE_MESSAGE();
}
}
void
PCLoaderDlrNavteq::loadPOIFile(const std::string& file,
OptionsCont& oc, PCPolyContainer& toFill,
PCTypeMap& tm) {
RGBColor c = RGBColor::parseColor(oc.getString("color"));
int l = 0;
LineReader lr(file);
while (lr.hasMore()) {
std::string line = lr.readLine();
++l;
if (line.length() == 0 || line.find("#") != std::string::npos) {
continue;
}
if (StringUtils::prune(line) == "") {
continue;
}
std::istringstream stream(line);
std::string name, skip, type, desc;
std::getline(stream, name, '\t');
std::getline(stream, skip, '\t');
std::getline(stream, type, '\t');
std::getline(stream, desc, '\t');
if (stream.fail()) {
throw ProcessError("Invalid dlr-navteq-poi in line " + toString(l) + ":\n" + line);
}
double x, y;
stream >> x;
if (stream.fail()) {
throw ProcessError(TLF("Invalid x coordinate for POI '%'.", name));
}
stream >> y;
if (stream.fail()) {
throw ProcessError(TLF("Invalid y coordinate for POI '%'.", name));
}
Position pos(x, y);
if (name == "") {
throw ProcessError(TL("The name of a POI is missing."));
}
if (!GeoConvHelper::getProcessing().x2cartesian(pos, true)) {
throw ProcessError(TLF("Unable to project coordinates for POI '%'.", name));
}
bool discard = oc.getBool("discard");
std::string icon = oc.getString("icon");
double layer = oc.getFloat("layer");
RGBColor color;
if (tm.has(type)) {
const PCTypeMap::TypeDef& def = tm.get(type);
name = def.prefix + name;
type = def.id;
color = def.color;
discard = def.discard;
icon = def.icon;
layer = def.layer;
} else {
name = oc.getString("prefix") + name;
type = oc.getString("type");
color = c;
}
if (!discard) {
PointOfInterest* poi = new PointOfInterest(name, type, color, pos, false, "", 0, false, 0, icon, layer);
toFill.add(poi, OptionsCont::getOptions().isInStringVector("prune.keep-list", name));
}
}
}
void
PCLoaderDlrNavteq::loadPolyFile(const std::string& file,
OptionsCont& oc, PCPolyContainer& toFill,
PCTypeMap& tm) {
RGBColor c = RGBColor::parseColor(oc.getString("color"));
LineReader lr(file);
while (lr.hasMore()) {
std::string line = lr.readLine();
if (line.length() == 0 || line.find("#") != std::string::npos) {
continue;
}
if (StringUtils::prune(line) == "") {
continue;
}
StringTokenizer st(line, "\t");
std::vector<std::string> values = st.getVector();
if (values.size() < 6 || values.size() % 2 != 0) {
throw ProcessError(TLF("Invalid dlr-navteq-polygon - line: '%'.", line));
}
std::string id = values[0];
std::string ort = values[1];
std::string type = values[2];
std::string name = values[3];
PositionVector vec;
int index = 4;
while ((int)values.size() > index) {
std::string xpos = values[index];
std::string ypos = values[index + 1];
index += 2;
double x = StringUtils::toDouble(xpos);
double y = StringUtils::toDouble(ypos);
Position pos(x, y);
if (!GeoConvHelper::getProcessing().x2cartesian(pos)) {
WRITE_WARNINGF(TL("Unable to project coordinates for polygon '%'."), id);
}
vec.push_back(pos);
}
name = StringUtils::convertUmlaute(name);
if (name == "noname" || toFill.getPolygons().get(name) != 0) {
name = name + "#" + toString(toFill.getEnumIDFor(name));
}
if (vec.size() == 0) {
WRITE_WARNINGF(TL("The polygon '%' is empty."), id);
continue;
}
if (id == "") {
WRITE_WARNING(TL("The name of a polygon is missing; it will be discarded."));
continue;
}
bool fill = vec.front() == vec.back();
bool discard = oc.getBool("discard");
std::string icon = oc.getString("icon");
double layer = oc.getFloat("layer");
RGBColor color;
if (tm.has(type)) {
const PCTypeMap::TypeDef& def = tm.get(type);
name = def.prefix + name;
type = def.id;
color = def.color;
fill = fill && def.allowFill != PCTypeMap::Filltype::NOFILL;
discard = def.discard;
icon = def.icon;
layer = def.layer;
} else {
name = oc.getString("prefix") + name;
type = oc.getString("type");
color = c;
}
if (!discard) {
SUMOPolygon* poly = new SUMOPolygon(name, type, color, vec, false, fill, 1, layer);
toFill.add(poly);
}
vec.clear();
}
}