Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update DrtStopsWriter.java #62

Merged
merged 8 commits into from
Sep 8, 2023
Merged
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 72 additions & 34 deletions src/main/java/org/matsim/run/prepare/DrtStopsWriter.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
package org.matsim.run.prepare;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.locationtech.jts.geom.Geometry;
import org.matsim.api.core.v01.Coord;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.network.Link;
import org.matsim.api.core.v01.network.Network;
import org.matsim.application.options.ShpOptions;
import org.matsim.core.network.NetworkUtils;
import org.matsim.core.utils.collections.Tuple;
import org.matsim.core.utils.geometry.CoordUtils;
import org.matsim.core.utils.geometry.geotools.MGC;
import org.matsim.core.utils.io.IOUtils;
import org.matsim.core.utils.io.MatsimXmlWriter;
import org.matsim.core.utils.io.UncheckedIOException;
import org.matsim.run.RunKelheimScenario;
import org.opengis.feature.simple.SimpleFeature;

import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -66,13 +73,19 @@ public void write() throws UncheckedIOException, IOException {
this.writeDoctype("transitSchedule", "http://www.matsim.org/files/dtd/transitSchedule_v1.dtd");
this.writeStartTag("transitSchedule", null);
this.writeStartTag("transitStops", null);
this.writeTransitStops(network);
this.writeTransitStopsAndVizFiles(network);
this.writeEndTag("transitStops");
this.writeEndTag("transitSchedule");
this.close();
}

private void writeTransitStops(Network network) throws IOException {
/**
* additionally to writing the stops xml file, also writes a csv file that contains the same information as well as a network file that contains only
* the links assigned to stops (for visualisation).
* @param network to retrieve link id's from
* @throws IOException if some file can't be opened or written
*/
private void writeTransitStopsAndVizFiles(Network network) throws IOException {
// Write csv file for adjusted stop location
FileWriter csvWriter = new FileWriter(outputFolder + "/"
+ mode + "-stops-locations.csv");
Expand All @@ -89,39 +102,64 @@ private void writeTransitStops(Network network) throws IOException {
log.info("Start processing the network. This may take some time...");
URL data = new URL("https://svn.vsp.tu-berlin.de/" +
"repos/public-svn/matsim/scenarios/countries/de/kelheim/original-data/" +
"KEXI_Haltestellen_Liste_Kelheim_utm32n.csv");

BufferedReader csvReader = new BufferedReader(new InputStreamReader(data.openStream()));
csvReader.readLine();
String stopEntry = csvReader.readLine();
while (stopEntry != null) {

String[] stopData = stopEntry.split(";");
// write stop
Coord coord = new Coord(Double.parseDouble(stopData[2]), Double.parseDouble(stopData[3]));

if (serviceArea == null || MGC.coord2Point(coord).within(serviceArea)) {
List<Tuple<String, String>> attributes = new ArrayList<Tuple<String, String>>(5);
attributes.add(createTuple("id", stopData[0]));
attributes.add(createTuple("x", stopData[2]));
attributes.add(createTuple("y", stopData[3]));
Link link = getStopLink(coord, network);
attributes.add(createTuple("linkRefId", link.getId().toString()));
this.writeStartTag("stopFacility", attributes, true);

csvWriter.append(stopData[0]);
csvWriter.append(",");
csvWriter.append(link.getId().toString());
csvWriter.append(",");
csvWriter.append(Double.toString(link.getToNode().getCoord().getX()));
csvWriter.append(",");
csvWriter.append(Double.toString(link.getToNode().getCoord().getY()));
csvWriter.append("\n");
"KEXI_Haltestellen_Liste_Kelheim_utm32n_withLinkIds.csv");
Set<Id<Link>> allLinks = new HashSet<>();

try (CSVParser parser = new CSVParser(IOUtils.getBufferedReader(data),
CSVFormat.DEFAULT.withDelimiter(';').withFirstRecordAsHeader())) {
for (CSVRecord row : parser) {
Coord coord = new Coord(Double.parseDouble(row.get("x")), Double.parseDouble(row.get("y")));
if (serviceArea == null || MGC.coord2Point(coord).within(serviceArea)) {
List<Tuple<String, String>> attributes = new ArrayList<>(5);
attributes.add(createTuple("id", row.get("Haltestellen-Nr.")));
attributes.add(createTuple("x", row.get("x")));
attributes.add(createTuple("y", row.get("y")));
Link link = null;
// If link is already determined by hand in the raw data, then use that link directly.
if (row.get("linkId_v" + RunKelheimScenario.VERSION)!=null){
link = network.getLinks().get(Id.createLinkId(row.get("linkId_v" + RunKelheimScenario.VERSION)));
} else {
link = getStopLink(coord, network);
}
allLinks.add(link.getId());
attributes.add(createTuple("linkRefId", link.getId().toString()));

//write into stops xml file
this.writeStartTag("stopFacility", attributes, true);

//write into csv file for viz
csvWriter.append(row.get("Haltestellen-Nr."));
csvWriter.append(",");
csvWriter.append(link.getId().toString());
csvWriter.append(",");
csvWriter.append(Double.toString(link.getToNode().getCoord().getX()));
csvWriter.append(",");
csvWriter.append(Double.toString(link.getToNode().getCoord().getY()));
csvWriter.append("\n");
}
}

stopEntry = csvReader.readLine();
}

csvWriter.close();

//write filtered network file (for viz)
writeFilteredNetwork(network, allLinks);
}

private void writeFilteredNetwork(Network network, Set<Id<Link>> allLinks) {
//remove all links but the ones in the set
network.getLinks().keySet()
.forEach(linkId -> {
if (!allLinks.contains(linkId)) {
network.removeLink(linkId);
}
});
//remove 'empty' nodes
network.getNodes().values().stream()
.filter(node -> node.getInLinks().size() == 0 && node.getOutLinks().size() == 0)
.forEach(node -> network.removeNode(node.getId()));

NetworkUtils.writeNetwork(network, outputFolder + "/" + mode + "-stops-links.xml.gz");
}

private Link getStopLink(Coord coord, Network network) {
Expand Down