50 #pragma GCC diagnostic push
51 #pragma GCC diagnostic ignored "-Wpedantic"
53 #include <ogrsf_frmts.h>
55 #pragma GCC diagnostic pop
68 if (!oc.
isSet(
"shapefile-prefix")) {
73 std::string dbf_file = oc.
getString(
"shapefile-prefix") +
".dbf";
74 std::string shp_file = oc.
getString(
"shapefile-prefix") +
".shp";
75 std::string shx_file = oc.
getString(
"shapefile-prefix") +
".shx";
92 dbf_file, shp_file, oc.
getBool(
"speed-in-kmh"));
105 const std::string& dbf_name,
106 const std::string& shp_name,
108 : myOptions(oc), mySHPName(shp_name),
110 myNodeCont(nc), myEdgeCont(ec), myTypeCont(tc),
111 mySpeedInKMH(speedInKMH),
125 #if GDAL_VERSION_MAJOR < 2
127 OGRDataSource* poDS = OGRSFDriverRegistrar::Open(
mySHPName.c_str(), FALSE);
130 GDALDataset* poDS = (GDALDataset*)GDALOpenEx(
mySHPName.c_str(), GDAL_OF_VECTOR | GA_ReadOnly, NULL, NULL, NULL);
138 OGRLayer* poLayer = poDS->GetLayer(0);
139 poLayer->ResetReading();
142 OGRSpatialReference* origTransf = poLayer->GetSpatialRef();
143 OGRSpatialReference destTransf;
145 destTransf.SetWellKnownGeogCS(
"WGS84");
146 OGRCoordinateTransformation* poCT = OGRCreateCoordinateTransformation(origTransf, &destTransf);
149 OGRSpatialReference origTransf2;
150 origTransf2.SetWellKnownGeogCS(
"WGS84");
151 poCT = OGRCreateCoordinateTransformation(&origTransf2, &destTransf);
154 WRITE_WARNING(
"Could not create geocoordinates converter; check whether proj.4 is installed.");
159 OGRFeature* poFeature;
160 poLayer->ResetReading();
165 int featureIndex = 0;
166 bool warnNotUnique =
true;
167 std::string idPrefix =
"";
169 while ((poFeature = poLayer->GetNextFeature()) != NULL) {
171 if (featureIndex == 0) {
176 std::string from_node;
178 if (!getStringEntry(poFeature,
"shapefile.street-id",
"LINK_ID",
true,
id)) {
179 WRITE_ERROR(
"Needed field '" +
id +
"' (street-id) is missing.");
186 getStringEntry(poFeature,
"shapefile.name",
"ST_NAME",
true, name);
189 if (!getStringEntry(poFeature,
"shapefile.from-id",
"REF_IN_ID",
true, from_node)) {
190 WRITE_ERROR(
"Needed field '" + from_node +
"' (from node id) is missing.");
193 if (!getStringEntry(poFeature,
"shapefile.to-id",
"NREF_IN_ID",
true, to_node)) {
194 WRITE_ERROR(
"Needed field '" + to_node +
"' (to node id) is missing.");
198 if (from_node ==
"" || to_node ==
"") {
206 }
else if (poFeature->GetFieldIndex(
"ST_TYP_AFT") >= 0) {
207 type = poFeature->GetFieldAsString(
"ST_TYP_AFT");
210 WRITE_WARNING(
"Unknown type '" + type +
"' for edge '" +
id +
"'");
214 double speed = getSpeed(*poFeature,
id);
215 int nolanes = getLaneNo(*poFeature,
id, speed);
216 int priority = getPriority(*poFeature,
id);
217 if (nolanes <= 0 || speed <= 0) {
224 WRITE_ERROR(
"Required field '" + lanesField +
"' or '" + speedField +
"' is missing (add fields or set option --shapefile.use-defaults-on-failure).");
226 OGRFeature::DestroyFeature(poFeature);
231 speed = speed / (double) 3.6;
236 OGRGeometry* poGeometry = poFeature->GetGeometryRef();
237 OGRwkbGeometryType gtype = poGeometry->getGeometryType();
238 if (gtype != wkbLineString && gtype != wkbLineString25D) {
239 OGRFeature::DestroyFeature(poFeature);
240 WRITE_ERROR(
"Road geometry must be of type 'linestring' or 'linestring25D' (found '" +
toString(gtype) +
"')");
243 OGRLineString* cgeom = (OGRLineString*) poGeometry;
246 cgeom->transform(poCT);
250 for (
int j = 0; j < cgeom->getNumPoints(); j++) {
251 Position pos((
double) cgeom->getX(j), (double) cgeom->getY(j), (double) cgeom->getZ(j));
253 WRITE_WARNING(
"Unable to project coordinates for edge '" +
id +
"'.");
264 from =
new NBNode(from_node, from_pos);
266 WRITE_ERROR(
"Node '" + from_node +
"' could not be added");
278 to =
new NBNode(to_node, to_pos);
280 WRITE_ERROR(
"Node '" + to_node +
"' could not be added");
288 WRITE_WARNING(
"Edge '" +
id +
"' connects identical nodes, skipping.");
294 int index = poFeature->GetDefnRef()->GetFieldIndex(
"DIR_TRAVEL");
295 if (index >= 0 && poFeature->IsFieldSet(index)) {
296 dir = poFeature->GetFieldAsString(index);
298 const std::string origID = saveOrigIDs ? id :
"";
302 if (existing !=
nullptr || existingReverse !=
nullptr) {
303 std::string duplicateID = existing !=
nullptr ? id : existingReverse->
getID();
304 if ((existing != 0 && existing->
getGeometry() == shape)
306 WRITE_ERROR(
"Edge '" + duplicateID +
" is not unique");
308 if (
id != idPrefix) {
314 WRITE_WARNING(
"street-id '" + idPrefix +
"' is not unique. Renaming subsequent edge to '" +
id +
"'");
315 warnNotUnique =
false;
321 if (dir ==
"B" || dir ==
"F" || dir ==
"" ||
myOptions.
getBool(
"shapefile.all-bidirectional")) {
324 NBEdge* edge =
new NBEdge(
id, from, to, type, speed, nolanes, priority, width,
NBEdge::UNSPECIFIED_OFFSET, shape, name, origID, spread);
328 addParams(edge, poFeature, params);
330 WRITE_ERROR(
"Could not create edge '" +
id +
"'. An edge with the same id already exists");
334 if ((dir ==
"B" || dir ==
"T" ||
myOptions.
getBool(
"shapefile.all-bidirectional")) && !oneway) {
337 NBEdge* edge =
new NBEdge(
"-" +
id, to, from, type, speed, nolanes, priority, width,
NBEdge::UNSPECIFIED_OFFSET, shape.
reverse(), name, origID, spread);
341 addParams(edge, poFeature, params);
343 WRITE_ERROR(
"Could not create edge '-" +
id +
"'. An edge with the same id already exists");
347 OGRFeature::DestroyFeature(poFeature);
350 #if GDAL_VERSION_MAJOR < 2
351 OGRDataSource::DestroyDataSource(poDS);
357 WRITE_ERROR(
"SUMO was compiled without GDAL support.");
363 NIImporter_ArcView::getSpeed(OGRFeature& poFeature,
const std::string& edgeid) {
365 int index = poFeature.GetDefnRef()->GetFieldIndex(
myOptions.
getString(
"shapefile.speed").c_str());
366 if (index >= 0 && poFeature.IsFieldSet(index)) {
367 const double speed = poFeature.GetFieldAsDouble(index);
371 +
"': '" + std::string(poFeature.GetFieldAsString(index)) +
"'");
382 int index = poFeature.GetDefnRef()->GetFieldIndex(
"speed");
383 if (index >= 0 && poFeature.IsFieldSet(index)) {
384 return (
double) poFeature.GetFieldAsDouble(index);
386 index = poFeature.GetDefnRef()->GetFieldIndex(
"SPEED");
387 if (index >= 0 && poFeature.IsFieldSet(index)) {
388 return (
double) poFeature.GetFieldAsDouble(index);
391 index = poFeature.GetDefnRef()->GetFieldIndex(
"SPEED_CAT");
392 if (index >= 0 && poFeature.IsFieldSet(index)) {
393 std::string def = poFeature.GetFieldAsString(index);
401 NIImporter_ArcView::getLaneNo(OGRFeature& poFeature,
const std::string& edgeid,
404 int index = poFeature.GetDefnRef()->GetFieldIndex(
myOptions.
getString(
"shapefile.laneNumber").c_str());
405 if (index >= 0 && poFeature.IsFieldSet(index)) {
406 const int laneNumber = poFeature.GetFieldAsInteger(index);
407 if (laneNumber <= 0) {
410 +
"': '" + std::string(poFeature.GetFieldAsString(index)) +
"'");
421 int index = poFeature.GetDefnRef()->GetFieldIndex(
"nolanes");
422 if (index >= 0 && poFeature.IsFieldSet(index)) {
423 return (
int) poFeature.GetFieldAsInteger(index);
425 index = poFeature.GetDefnRef()->GetFieldIndex(
"NOLANES");
426 if (index >= 0 && poFeature.IsFieldSet(index)) {
427 return (
int) poFeature.GetFieldAsInteger(index);
429 index = poFeature.GetDefnRef()->GetFieldIndex(
"rnol");
430 if (index >= 0 && poFeature.IsFieldSet(index)) {
431 return (
int) poFeature.GetFieldAsInteger(index);
433 index = poFeature.GetDefnRef()->GetFieldIndex(
"LANE_CAT");
434 if (index >= 0 && poFeature.IsFieldSet(index)) {
435 std::string def = poFeature.GetFieldAsString(index);
443 NIImporter_ArcView::getPriority(OGRFeature& poFeature,
const std::string& ) {
449 int index = poFeature.GetDefnRef()->GetFieldIndex(
"priority");
450 if (index >= 0 && poFeature.IsFieldSet(index)) {
451 return poFeature.GetFieldAsInteger(index);
453 index = poFeature.GetDefnRef()->GetFieldIndex(
"PRIORITY");
454 if (index >= 0 && poFeature.IsFieldSet(index)) {
455 return poFeature.GetFieldAsInteger(index);
458 index = poFeature.GetDefnRef()->GetFieldIndex(
"FUNC_CLASS");
459 if (index >= 0 && poFeature.IsFieldSet(index)) {
460 return poFeature.GetFieldAsInteger(index);
466 NIImporter_ArcView::checkSpread(
NBEdge* e) {
475 NIImporter_ArcView::getStringEntry(OGRFeature* poFeature,
const std::string& optionName,
const char* defaultName,
bool prune, std::string& into) {
476 std::string v(defaultName);
480 if (poFeature->GetFieldIndex(v.c_str()) < 0) {
488 into = poFeature->GetFieldAsString((
char*)v.c_str());
495 std::vector<std::string>
496 NIImporter_ArcView::getFieldNames(OGRFeature* poFeature)
const {
497 std::vector<std::string> fields;
498 for (
int i = 0; i < poFeature->GetFieldCount(); i++) {
499 fields.push_back(poFeature->GetFieldDefnRef(i)->GetNameRef());
505 NIImporter_ArcView::addParams(
NBEdge* edge, OGRFeature* poFeature,
const std::vector<std::string>& params)
const {
506 for (
const std::string& p : params) {
507 int index = poFeature->GetDefnRef()->GetFieldIndex(p.c_str());
508 if (index >= 0 && poFeature->IsFieldSet(index)) {
509 edge->
setParameter(p, poFeature->GetFieldAsString(index));