Skip to content

Commit

Permalink
write edge attributes from sumo into the features csv (#3508)
Browse files Browse the repository at this point in the history
  • Loading branch information
rakow authored Oct 9, 2024
1 parent fcdf909 commit 1957734
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> header = new ArrayList<>(props.getHeader());
header.addAll(handler.attributes);

out.printRecord(header);
props.print(out);

} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public class SumoNetworkHandler extends DefaultHandler {
*/
final Map<String, Type> types = new HashMap<>();

/**
* Attribute names that have been observed during parsing.
*/
Set<String> attributes = new LinkedHashSet<>();

/**
* Stores current parsed edge.
*/
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -345,6 +358,8 @@ static final class Edge {
@Nullable
String origTo;

final Map<String, String> 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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
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;
import org.matsim.api.core.v01.network.Link;
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;
Expand Down Expand Up @@ -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<String> header = parser.getHeaderNames();
Assertions.assertEquals("linkId", header.get(0));
Assertions.assertEquals("highway_type", header.get(1));

}
}

0 comments on commit 1957734

Please sign in to comment.