From 1957734ab3ff2085c70c8ca82de59548bfb6cdee Mon Sep 17 00:00:00 2001 From: rakow Date: Wed, 9 Oct 2024 17:04:15 +0200 Subject: [PATCH] write edge attributes from sumo into the features csv (#3508) --- .../matsim/contrib/sumo/SumoNetworkConverter.java | 5 ++++- .../contrib/sumo/SumoNetworkFeatureExtractor.java | 4 ++++ .../matsim/contrib/sumo/SumoNetworkHandler.java | 15 +++++++++++++++ .../contrib/sumo/SumoNetworkConverterTest.java | 14 +++++++++++++- 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/contribs/sumo/src/main/java/org/matsim/contrib/sumo/SumoNetworkConverter.java b/contribs/sumo/src/main/java/org/matsim/contrib/sumo/SumoNetworkConverter.java index 378703db1eb..b563e1325d4 100644 --- a/contribs/sumo/src/main/java/org/matsim/contrib/sumo/SumoNetworkConverter.java +++ b/contribs/sumo/src/main/java/org/matsim/contrib/sumo/SumoNetworkConverter.java @@ -165,7 +165,10 @@ public void writeFeatures(SumoNetworkHandler handler, String output) { SumoNetworkFeatureExtractor props = new SumoNetworkFeatureExtractor(handler); try (CSVPrinter out = new CSVPrinter(IOUtils.getBufferedWriter(output), CSVFormat.DEFAULT)) { - out.printRecord(props.getHeader()); + List header = new ArrayList<>(props.getHeader()); + header.addAll(handler.attributes); + + out.printRecord(header); props.print(out); } catch (IOException e) { diff --git a/contribs/sumo/src/main/java/org/matsim/contrib/sumo/SumoNetworkFeatureExtractor.java b/contribs/sumo/src/main/java/org/matsim/contrib/sumo/SumoNetworkFeatureExtractor.java index ee8c9a45b87..91445ca7f33 100644 --- a/contribs/sumo/src/main/java/org/matsim/contrib/sumo/SumoNetworkFeatureExtractor.java +++ b/contribs/sumo/src/main/java/org/matsim/contrib/sumo/SumoNetworkFeatureExtractor.java @@ -274,6 +274,10 @@ public void print(CSVPrinter out, String linkId, SumoNetworkHandler.Edge edge) t out.print(numConnections.getInt('r')); out.print(numConnections.getInt('s')); + for (String attribute : handler.attributes) { + out.print(edge.attributes.getOrDefault(attribute, "")); + } + out.println(); } diff --git a/contribs/sumo/src/main/java/org/matsim/contrib/sumo/SumoNetworkHandler.java b/contribs/sumo/src/main/java/org/matsim/contrib/sumo/SumoNetworkHandler.java index 65de2df3a94..367be8f1827 100644 --- a/contribs/sumo/src/main/java/org/matsim/contrib/sumo/SumoNetworkHandler.java +++ b/contribs/sumo/src/main/java/org/matsim/contrib/sumo/SumoNetworkHandler.java @@ -47,6 +47,11 @@ public class SumoNetworkHandler extends DefaultHandler { */ final Map types = new HashMap<>(); + /** + * Attribute names that have been observed during parsing. + */ + Set attributes = new LinkedHashSet<>(); + /** * Stores current parsed edge. */ @@ -257,6 +262,14 @@ public void startElement(String uri, String localName, String qName, Attributes case "origTo": tmpEdge.origTo = value; break; + // Redundant attribute, that does not need to be stored + case "highway": + break; + default: + String attribute = attributes.getValue("key").intern(); + this.attributes.add(attribute); + tmpEdge.attributes.put(attribute, value); + break; } break; @@ -345,6 +358,8 @@ static final class Edge { @Nullable String origTo; + final Map attributes = new HashMap<>(); + public Edge(String id, String from, String to, String type, int priority, String name, String[] shape) { this.id = id; this.from = from; diff --git a/contribs/sumo/src/test/java/org/matsim/contrib/sumo/SumoNetworkConverterTest.java b/contribs/sumo/src/test/java/org/matsim/contrib/sumo/SumoNetworkConverterTest.java index 2bb89ec4caa..7a13bf4a440 100644 --- a/contribs/sumo/src/test/java/org/matsim/contrib/sumo/SumoNetworkConverterTest.java +++ b/contribs/sumo/src/test/java/org/matsim/contrib/sumo/SumoNetworkConverterTest.java @@ -1,6 +1,9 @@ package org.matsim.contrib.sumo; import com.google.common.io.Resources; +import org.apache.commons.csv.CSVFormat; +import org.apache.commons.csv.CSVParser; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.matsim.api.core.v01.Id; import org.matsim.api.core.v01.TransportMode; @@ -8,6 +11,8 @@ import org.matsim.api.core.v01.network.Network; import org.matsim.core.network.NetworkUtils; +import java.io.File; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; @@ -42,9 +47,16 @@ void convert() throws Exception { assert Files.exists(geometry) : "Geometries must exist"; - Path fts = Path.of(output.toString().replace(".xml", "-ft.csv")); + String csv = output.toString().replace(".xml", "-ft.csv"); + Path fts = Path.of(csv); assert Files.exists(fts) : "Features must exists"; + CSVParser parser = CSVParser.parse(new File(csv), StandardCharsets.UTF_8, CSVFormat.DEFAULT.builder().setHeader().setHeader().build()); + + List header = parser.getHeaderNames(); + Assertions.assertEquals("linkId", header.get(0)); + Assertions.assertEquals("highway_type", header.get(1)); + } }