Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/polyconvert/PCLoaderDlrNavteq.cpp
169666 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2001-2025 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 PCLoaderDlrNavteq.cpp
15
/// @author Daniel Krajzewicz
16
/// @author Jakob Erdmann
17
/// @author Christoph Sommer
18
/// @author Michael Behrisch
19
/// @date Thu, 02.11.2006
20
///
21
// A reader of pois and polygons stored in DLR-Navteq (Elmar)-format
22
/****************************************************************************/
23
#include <config.h>
24
25
#include <string>
26
#include <map>
27
#include <fstream>
28
#include <sstream>
29
#include <iostream>
30
#include <utils/common/UtilExceptions.h>
31
#include <utils/common/MsgHandler.h>
32
#include <utils/common/StringUtils.h>
33
#include <utils/common/StringUtils.h>
34
#include <utils/common/ToString.h>
35
#include <utils/common/StringTokenizer.h>
36
#include <utils/common/FileHelpers.h>
37
#include <utils/importio/LineReader.h>
38
#include <utils/options/OptionsCont.h>
39
#include <utils/options/Option.h>
40
#include <utils/common/StdDefs.h>
41
#include <polyconvert/PCPolyContainer.h>
42
#include "PCLoaderDlrNavteq.h"
43
#include <utils/common/RGBColor.h>
44
#include <utils/geom/GeomHelper.h>
45
#include <utils/geom/Boundary.h>
46
#include <utils/geom/Position.h>
47
#include <utils/geom/GeoConvHelper.h>
48
49
50
// ===========================================================================
51
// method definitions
52
// ===========================================================================
53
void
54
PCLoaderDlrNavteq::loadIfSet(OptionsCont& oc, PCPolyContainer& toFill,
55
PCTypeMap& tm) {
56
if (oc.isSet("dlr-navteq-poly-files")) {
57
loadPolyFiles(oc, toFill, tm);
58
}
59
if (oc.isSet("dlr-navteq-poi-files")) {
60
loadPOIFiles(oc, toFill, tm);
61
}
62
}
63
64
65
void
66
PCLoaderDlrNavteq::loadPOIFiles(OptionsCont& oc, PCPolyContainer& toFill,
67
PCTypeMap& tm) {
68
std::vector<std::string> files = oc.getStringVector("dlr-navteq-poi-files");
69
for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
70
if (!FileHelpers::isReadable(*file)) {
71
throw ProcessError(TLF("Could not open dlr-navteq-poi-file '%'.", *file));
72
}
73
PROGRESS_BEGIN_MESSAGE("Parsing pois from dlr-navteq-poi-file '" + *file + "'");
74
loadPOIFile(*file, oc, toFill, tm);
75
PROGRESS_DONE_MESSAGE();
76
}
77
}
78
79
80
void
81
PCLoaderDlrNavteq::loadPolyFiles(OptionsCont& oc, PCPolyContainer& toFill,
82
PCTypeMap& tm) {
83
std::vector<std::string> files = oc.getStringVector("dlr-navteq-poly-files");
84
for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
85
if (!FileHelpers::isReadable(*file)) {
86
throw ProcessError(TLF("Could not open dlr-navteq-poly-file '%'.", *file));
87
}
88
PROGRESS_BEGIN_MESSAGE("Parsing pois from dlr-navteq-poly-file '" + *file + "'");
89
loadPolyFile(*file, oc, toFill, tm);
90
PROGRESS_DONE_MESSAGE();
91
}
92
}
93
94
95
void
96
PCLoaderDlrNavteq::loadPOIFile(const std::string& file,
97
OptionsCont& oc, PCPolyContainer& toFill,
98
PCTypeMap& tm) {
99
// get the defaults
100
RGBColor c = RGBColor::parseColor(oc.getString("color"));
101
// parse
102
int l = 0;
103
LineReader lr(file);
104
while (lr.hasMore()) {
105
std::string line = lr.readLine();
106
++l;
107
// skip invalid/empty lines
108
if (line.length() == 0 || line.find("#") != std::string::npos) {
109
continue;
110
}
111
if (StringUtils::prune(line) == "") {
112
continue;
113
}
114
// parse the poi
115
std::istringstream stream(line);
116
// attributes of the poi
117
std::string name, skip, type, desc;
118
std::getline(stream, name, '\t');
119
std::getline(stream, skip, '\t');
120
std::getline(stream, type, '\t');
121
std::getline(stream, desc, '\t');
122
if (stream.fail()) {
123
throw ProcessError("Invalid dlr-navteq-poi in line " + toString(l) + ":\n" + line);
124
}
125
double x, y;
126
stream >> x;
127
if (stream.fail()) {
128
throw ProcessError(TLF("Invalid x coordinate for POI '%'.", name));
129
}
130
stream >> y;
131
if (stream.fail()) {
132
throw ProcessError(TLF("Invalid y coordinate for POI '%'.", name));
133
}
134
Position pos(x, y);
135
// check the poi
136
if (name == "") {
137
throw ProcessError(TL("The name of a POI is missing."));
138
}
139
if (!GeoConvHelper::getProcessing().x2cartesian(pos, true)) {
140
throw ProcessError(TLF("Unable to project coordinates for POI '%'.", name));
141
}
142
143
// patch the values
144
bool discard = oc.getBool("discard");
145
std::string icon = oc.getString("icon");
146
double layer = oc.getFloat("layer");
147
RGBColor color;
148
if (tm.has(type)) {
149
const PCTypeMap::TypeDef& def = tm.get(type);
150
name = def.prefix + name;
151
type = def.id;
152
color = def.color;
153
discard = def.discard;
154
icon = def.icon;
155
layer = def.layer;
156
} else {
157
name = oc.getString("prefix") + name;
158
type = oc.getString("type");
159
color = c;
160
}
161
if (!discard) {
162
PointOfInterest* poi = new PointOfInterest(name, type, color, pos, false, "", 0, false, 0, icon, layer);
163
toFill.add(poi, OptionsCont::getOptions().isInStringVector("prune.keep-list", name));
164
}
165
}
166
}
167
168
169
void
170
PCLoaderDlrNavteq::loadPolyFile(const std::string& file,
171
OptionsCont& oc, PCPolyContainer& toFill,
172
PCTypeMap& tm) {
173
// get the defaults
174
RGBColor c = RGBColor::parseColor(oc.getString("color"));
175
// attributes of the poly
176
// parse
177
LineReader lr(file);
178
while (lr.hasMore()) {
179
std::string line = lr.readLine();
180
// skip invalid/empty lines
181
if (line.length() == 0 || line.find("#") != std::string::npos) {
182
continue;
183
}
184
if (StringUtils::prune(line) == "") {
185
continue;
186
}
187
// parse the poi
188
StringTokenizer st(line, "\t");
189
std::vector<std::string> values = st.getVector();
190
if (values.size() < 6 || values.size() % 2 != 0) {
191
throw ProcessError(TLF("Invalid dlr-navteq-polygon - line: '%'.", line));
192
}
193
std::string id = values[0];
194
std::string ort = values[1];
195
std::string type = values[2];
196
std::string name = values[3];
197
PositionVector vec;
198
int index = 4;
199
// now collect the positions
200
while ((int)values.size() > index) {
201
std::string xpos = values[index];
202
std::string ypos = values[index + 1];
203
index += 2;
204
double x = StringUtils::toDouble(xpos);
205
double y = StringUtils::toDouble(ypos);
206
Position pos(x, y);
207
if (!GeoConvHelper::getProcessing().x2cartesian(pos)) {
208
WRITE_WARNINGF(TL("Unable to project coordinates for polygon '%'."), id);
209
}
210
vec.push_back(pos);
211
}
212
213
name = StringUtils::convertUmlaute(name);
214
if (name == "noname" || toFill.getPolygons().get(name) != 0) {
215
name = name + "#" + toString(toFill.getEnumIDFor(name));
216
}
217
218
// check the polygon
219
if (vec.size() == 0) {
220
WRITE_WARNINGF(TL("The polygon '%' is empty."), id);
221
continue;
222
}
223
if (id == "") {
224
WRITE_WARNING(TL("The name of a polygon is missing; it will be discarded."));
225
continue;
226
}
227
228
// patch the values
229
bool fill = vec.front() == vec.back();
230
bool discard = oc.getBool("discard");
231
std::string icon = oc.getString("icon");
232
double layer = oc.getFloat("layer");
233
RGBColor color;
234
if (tm.has(type)) {
235
const PCTypeMap::TypeDef& def = tm.get(type);
236
name = def.prefix + name;
237
type = def.id;
238
color = def.color;
239
fill = fill && def.allowFill != PCTypeMap::Filltype::NOFILL;
240
discard = def.discard;
241
icon = def.icon;
242
layer = def.layer;
243
} else {
244
name = oc.getString("prefix") + name;
245
type = oc.getString("type");
246
color = c;
247
}
248
if (!discard) {
249
SUMOPolygon* poly = new SUMOPolygon(name, type, color, vec, false, fill, 1, layer);
250
toFill.add(poly);
251
}
252
vec.clear();
253
}
254
}
255
256
257
/****************************************************************************/
258
259