#include <config.h>
#include <string>
#include <utils/xml/SUMOXMLDefinitions.h>
#include <utils/xml/SUMOSAXHandler.h>
#include <utils/xml/XMLSubSys.h>
#include <utils/common/MsgHandler.h>
#include <utils/common/StringUtils.h>
#include <utils/common/StringTokenizer.h>
#include <utils/common/RGBColor.h>
#include <utils/geom/GeomConvHelper.h>
#include <utils/iodevices/OutputDevice.h>
#include <utils/common/UtilExceptions.h>
#include <utils/geom/GeoConvHelper.h>
#include <utils/gui/globjects/GUIGlObjectTypes.h>
#include "Shape.h"
#include "ShapeContainer.h"
#include "ShapeHandler.h"
ShapeHandler::ShapeHandler(const std::string& file, ShapeContainer& sc, const GeoConvHelper* geoConvHelper) :
SUMOSAXHandler(file),
myShapeContainer(sc),
myPrefix(""),
myDefaultColor(RGBColor::RED),
myDefaultIcon(SUMOXMLDefinitions::POIIcons.getString(POIIcon::NONE)),
myDefaultLayer(0),
myDefaultFill(false),
myLastParameterised(nullptr),
myGeoConvHelper(geoConvHelper) {
}
ShapeHandler::~ShapeHandler() {}
void
ShapeHandler::myStartElement(int element, const SUMOSAXAttributes& attrs) {
try {
switch (element) {
case SUMO_TAG_POLY:
myDefaultLayer = Shape::DEFAULT_LAYER;
addPoly(attrs, false, false);
break;
case SUMO_TAG_POI:
myDefaultLayer = Shape::DEFAULT_LAYER_POI;
addPOI(attrs, false, false);
break;
case SUMO_TAG_PARAM:
if (myLastParameterised != nullptr) {
bool ok = true;
const std::string key = attrs.get<std::string>(SUMO_ATTR_KEY, nullptr, ok);
if (ok) {
const std::string val = attrs.hasAttribute(SUMO_ATTR_VALUE) ? attrs.getString(SUMO_ATTR_VALUE) : "";
if (key.empty()) {
WRITE_WARNING(TL("Error parsing key from shape generic parameter. Key cannot be empty"));
} else if (!SUMOXMLDefinitions::isValidParameterKey(key)) {
WRITE_WARNING(TL("Error parsing key from shape generic parameter. Key contains invalid characters"));
} else {
myLastParameterised->setParameter(key, val);
}
}
}
break;
default:
break;
}
} catch (InvalidArgument& e) {
WRITE_ERROR(e.what());
}
}
void
ShapeHandler::myEndElement(int element) {
if (element != SUMO_TAG_PARAM) {
myLastParameterised = nullptr;
}
}
void
ShapeHandler::addPOI(const SUMOSAXAttributes& attrs, const bool ignorePruning, const bool useProcessing) {
bool ok = true;
const double INVALID_POSITION(-1000000);
const std::string id = myPrefix + attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
double x = attrs.getOpt<double>(SUMO_ATTR_X, id.c_str(), ok, INVALID_POSITION);
const double y = attrs.getOpt<double>(SUMO_ATTR_Y, id.c_str(), ok, INVALID_POSITION);
const double z = attrs.getOpt<double>(SUMO_ATTR_Z, id.c_str(), ok, INVALID_POSITION);
double lon = attrs.getOpt<double>(SUMO_ATTR_LON, id.c_str(), ok, INVALID_POSITION);
double lat = attrs.getOpt<double>(SUMO_ATTR_LAT, id.c_str(), ok, INVALID_POSITION);
const double lanePos = attrs.getOpt<double>(SUMO_ATTR_POSITION, id.c_str(), ok, 0);
const bool friendlyPos = attrs.getOpt<bool>(SUMO_ATTR_FRIENDLY_POS, id.c_str(), ok, false);
const double lanePosLat = attrs.getOpt<double>(SUMO_ATTR_POSITION_LAT, id.c_str(), ok, 0);
std::string icon = attrs.getOpt<std::string>(SUMO_ATTR_ICON, id.c_str(), ok, myDefaultIcon);
if (!SUMOXMLDefinitions::POIIcons.hasString(icon)) {
WRITE_WARNING(TLF("Invalid icon % for POI '%', using default", icon, id));
icon = "none";
}
const double layer = attrs.getOpt<double>(SUMO_ATTR_LAYER, id.c_str(), ok, myDefaultLayer);
const std::string type = attrs.getOpt<std::string>(SUMO_ATTR_TYPE, id.c_str(), ok, "");
const std::string laneID = attrs.getOpt<std::string>(SUMO_ATTR_LANE, id.c_str(), ok, "");
const double angle = attrs.getOpt<double>(SUMO_ATTR_ANGLE, id.c_str(), ok, Shape::DEFAULT_ANGLE);
std::string imgFile = attrs.getOpt<std::string>(SUMO_ATTR_IMGFILE, id.c_str(), ok, Shape::DEFAULT_IMG_FILE);
const RGBColor color = attrs.hasAttribute(SUMO_ATTR_COLOR) ? attrs.get<RGBColor>(SUMO_ATTR_COLOR, id.c_str(), ok) : (imgFile != "" ? RGBColor::WHITE : myDefaultColor);
if (imgFile != "" && !FileHelpers::isAbsolute(imgFile)) {
imgFile = FileHelpers::getConfigurationRelative(getFileName(), imgFile);
}
const double width = attrs.getOpt<double>(SUMO_ATTR_WIDTH, id.c_str(), ok, Shape::DEFAULT_IMG_WIDTH);
const double height = attrs.getOpt<double>(SUMO_ATTR_HEIGHT, id.c_str(), ok, Shape::DEFAULT_IMG_HEIGHT);
if (!SUMOXMLDefinitions::isValidTypeID(id)) {
WRITE_WARNINGF(TL("Invalid characters for PoI ID '%'"), id);
ok = false;
}
if (ok) {
const GeoConvHelper* gch;
if (myGeoConvHelper != nullptr) {
gch = myGeoConvHelper;
} else if (useProcessing) {
gch = &GeoConvHelper::getProcessing();
} else {
gch = &GeoConvHelper::getFinal();
}
if (useProcessing && gch->usingGeoProjection()) {
if ((lat == INVALID_POSITION) || (lon == INVALID_POSITION)) {
lon = x;
lat = y;
x = INVALID_POSITION;
}
}
Position pos(x, y);
bool useGeo = false;
if ((x == INVALID_POSITION) || (y == INVALID_POSITION)) {
if (laneID != "") {
pos = getLanePos(id, laneID, lanePos, friendlyPos, lanePosLat);
} else {
if ((lat == INVALID_POSITION) || (lon == INVALID_POSITION)) {
WRITE_ERRORF(TL("Either (x, y), (lon, lat) or (lane, pos) must be specified for PoI '%'."), id);
return;
} else if (!gch->usingGeoProjection()) {
WRITE_ERRORF(TL("(lon, lat) is specified for PoI '%' but no geo-conversion is specified for the network."), id);
return;
}
pos.set(lon, lat);
useGeo = true;
bool success = true;
if (useProcessing) {
success = GeoConvHelper::getProcessing().x2cartesian(pos);
} else {
success = gch->x2cartesian_const(pos);
}
if (!success) {
WRITE_ERRORF(TL("Unable to project coordinates for POI '%'."), id);
return;
}
}
}
if (z != INVALID_POSITION) {
pos.setz(z);
}
if (!myShapeContainer.addPOI(id, type, color, pos, useGeo, laneID, lanePos, friendlyPos, lanePosLat, icon,
layer, angle, imgFile, width, height, ignorePruning)) {
WRITE_ERRORF(TL("PoI '%' already exists."), id);
}
myLastParameterised = myShapeContainer.getPOIs().get(id);
if ((laneID != "") && addLanePosParams()) {
myLastParameterised->setParameter(toString(SUMO_ATTR_LANE), laneID);
myLastParameterised->setParameter(toString(SUMO_ATTR_POSITION), toString(lanePos));
myLastParameterised->setParameter(toString(SUMO_ATTR_POSITION_LAT), toString(lanePosLat));
}
}
}
void
ShapeHandler::addPoly(const SUMOSAXAttributes& attrs, const bool ignorePruning, const bool useProcessing) {
bool ok = true;
const std::string id = myPrefix + attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
if (!SUMOXMLDefinitions::isValidTypeID(id)) {
WRITE_WARNINGF(TL("Invalid characters for Poly ID '%'"), id);
ok = false;
}
if (ok) {
const double layer = attrs.getOpt<double>(SUMO_ATTR_LAYER, id.c_str(), ok, myDefaultLayer);
const bool fill = attrs.getOpt<bool>(SUMO_ATTR_FILL, id.c_str(), ok, myDefaultFill);
const double lineWidth = attrs.getOpt<double>(SUMO_ATTR_LINEWIDTH, id.c_str(), ok, Shape::DEFAULT_LINEWIDTH);
const std::string type = attrs.getOpt<std::string>(SUMO_ATTR_TYPE, id.c_str(), ok, Shape::DEFAULT_TYPE);
const RGBColor color = attrs.hasAttribute(SUMO_ATTR_COLOR) ? attrs.get<RGBColor>(SUMO_ATTR_COLOR, id.c_str(), ok) : myDefaultColor;
PositionVector shape = attrs.get<PositionVector>(SUMO_ATTR_SHAPE, id.c_str(), ok);
const bool geo = attrs.getOpt<bool>(SUMO_ATTR_GEO, id.c_str(), ok, false);
const GeoConvHelper* gch;
if (myGeoConvHelper != nullptr) {
gch = myGeoConvHelper;
} else {
gch = &GeoConvHelper::getFinal();
}
if (geo || useProcessing) {
bool success = true;
for (int i = 0; i < (int)shape.size(); i++) {
if (useProcessing) {
success &= GeoConvHelper::getProcessing().x2cartesian(shape[i]);
} else {
success &= gch->x2cartesian_const(shape[i]);
}
}
if (!success) {
WRITE_WARNINGF(TL("Unable to project coordinates for polygon '%'."), id);
return;
}
}
const double angle = attrs.getOpt<double>(SUMO_ATTR_ANGLE, id.c_str(), ok, Shape::DEFAULT_ANGLE);
std::string imgFile = attrs.getOpt<std::string>(SUMO_ATTR_IMGFILE, id.c_str(), ok, Shape::DEFAULT_IMG_FILE);
if (imgFile != "" && !FileHelpers::isAbsolute(imgFile)) {
imgFile = FileHelpers::getConfigurationRelative(getFileName(), imgFile);
}
if (shape.size() == 0) {
WRITE_ERROR(TL("Polygon's shape cannot be empty."));
return;
}
if (lineWidth <= 0) {
WRITE_ERROR(TL("Polygon's lineWidth must be greater than 0."));
return;
}
if (!myShapeContainer.addPolygon(id, type, color, layer, angle, imgFile, shape, geo, fill, lineWidth, ignorePruning)) {
WRITE_ERRORF(TL("Polygon '%' already exists."), id);
}
myLastParameterised = myShapeContainer.getPolygons().get(id);
}
}
Parameterised*
ShapeHandler::getLastParameterised() const {
return myLastParameterised;
}
bool
ShapeHandler::loadFiles(const std::vector<std::string>& files, ShapeHandler& sh) {
for (const auto& fileIt : files) {
if (!XMLSubSys::runParser(sh, fileIt, false)) {
WRITE_MESSAGEF(TL("Loading of shapes from % failed."), fileIt);
return false;
}
}
return true;
}
void
ShapeHandler::setDefaults(const std::string& prefix, const RGBColor& color, const std::string& icon, const double layer, const bool fill) {
myPrefix = prefix;
myDefaultColor = color;
myDefaultIcon = icon;
myDefaultLayer = layer;
myDefaultFill = fill;
}
bool
ShapeHandler::addLanePosParams() {
return false;
}