-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from munterfi/feature/default-converter
Feature/default converter
- Loading branch information
Showing
17 changed files
with
421 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,4 +35,7 @@ build/ | |
.vscode/ | ||
|
||
### Mac OS ### | ||
.DS_Store | ||
.DS_Store | ||
|
||
### Integrations Tests ### | ||
integration-test/output/ |
70 changes: 70 additions & 0 deletions
70
src/main/java/ch/sbb/pfi/netzgrafikeditor/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package ch.sbb.pfi.netzgrafikeditor; | ||
|
||
import ch.sbb.pfi.netzgrafikeditor.converter.ConverterSink; | ||
import ch.sbb.pfi.netzgrafikeditor.converter.NetworkGraphicSource; | ||
import ch.sbb.pfi.netzgrafikeditor.converter.NetzgrafikConverter; | ||
import ch.sbb.pfi.netzgrafikeditor.converter.io.matsim.TransitScheduleXmlWriter; | ||
import ch.sbb.pfi.netzgrafikeditor.converter.io.netzgrafik.JsonFileReader; | ||
import ch.sbb.pfi.netzgrafikeditor.converter.matsim.MatsimSupplyBuilder; | ||
import ch.sbb.pfi.netzgrafikeditor.converter.supply.SupplyBuilder; | ||
import ch.sbb.pfi.netzgrafikeditor.converter.supply.impl.NoInfrastructureRepository; | ||
import ch.sbb.pfi.netzgrafikeditor.converter.supply.impl.NoRollingStockRepository; | ||
import ch.sbb.pfi.netzgrafikeditor.converter.supply.impl.NoVehicleCircuitsPlanner; | ||
import org.matsim.api.core.v01.Scenario; | ||
import org.matsim.core.config.ConfigUtils; | ||
import org.matsim.core.scenario.ScenarioUtils; | ||
import org.springframework.boot.CommandLineRunner; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
@SpringBootApplication | ||
public class Application implements CommandLineRunner { | ||
|
||
private Path outputPath; | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(Application.class, args); | ||
} | ||
|
||
@Override | ||
public void run(String... args) throws Exception { | ||
if (args.length < 2) { | ||
System.err.println("Please provide the path to the netzgrafik JSON file and the output directory."); | ||
System.exit(1); | ||
} | ||
|
||
String jsonFilePath = args[0]; | ||
String outputDirPath = args[1]; | ||
outputPath = Paths.get(outputDirPath); | ||
|
||
convertJsonToMatsimSchedule(Paths.get(jsonFilePath)); | ||
} | ||
|
||
private void convertJsonToMatsimSchedule(Path jsonFilePath) { | ||
try { | ||
Scenario scenario = ScenarioUtils.createScenario(ConfigUtils.createConfig()); | ||
NetworkGraphicSource source = new JsonFileReader(jsonFilePath); | ||
SupplyBuilder builder = new MatsimSupplyBuilder(scenario, new NoInfrastructureRepository(source.load()), | ||
new NoRollingStockRepository(), new NoVehicleCircuitsPlanner()); | ||
|
||
String baseFilename = jsonFilePath.getFileName().toString(); | ||
String filenameWithoutExtension = jsonFilePath.getFileName() | ||
.toString() | ||
.substring(0, baseFilename.lastIndexOf('.')); | ||
ConverterSink sink = new TransitScheduleXmlWriter(scenario, outputPath, filenameWithoutExtension + "."); | ||
|
||
NetzgrafikConverter converter = new NetzgrafikConverter(source, builder, sink); | ||
converter.run(); | ||
|
||
System.out.println("MATSim schedule has been written to: " + outputPath); | ||
System.exit(0); | ||
} catch (IOException e) { | ||
System.err.println("Error during conversion: " + e.getMessage()); | ||
System.exit(1); | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/ch/sbb/pfi/netzgrafikeditor/converter/ConverterSink.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package ch.sbb.pfi.netzgrafikeditor.converter; | ||
|
||
import java.io.IOException; | ||
|
||
public interface ConverterSink { | ||
|
||
void save() throws IOException; | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/ch/sbb/pfi/netzgrafikeditor/converter/NetworkGraphicSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package ch.sbb.pfi.netzgrafikeditor.converter; | ||
|
||
import ch.sbb.pfi.netzgrafikeditor.converter.model.NetworkGraphic; | ||
|
||
import java.io.IOException; | ||
|
||
public interface NetworkGraphicSource { | ||
|
||
NetworkGraphic load() throws IOException; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/main/java/ch/sbb/pfi/netzgrafikeditor/converter/io/matsim/TransitScheduleXmlWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package ch.sbb.pfi.netzgrafikeditor.converter.io.matsim; | ||
|
||
import ch.sbb.pfi.netzgrafikeditor.converter.ConverterSink; | ||
import lombok.RequiredArgsConstructor; | ||
import org.matsim.api.core.v01.Scenario; | ||
import org.matsim.core.config.ConfigWriter; | ||
import org.matsim.core.network.io.NetworkWriter; | ||
import org.matsim.pt.transitSchedule.api.TransitScheduleWriter; | ||
import org.matsim.vehicles.MatsimVehicleWriter; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
@RequiredArgsConstructor | ||
public class TransitScheduleXmlWriter implements ConverterSink { | ||
|
||
private static final String CONFIG_FILE = "config.xml"; | ||
private static final String NETWORK_FILE = "network.xml.gz"; | ||
private static final String TRANSIT_SCHEDULE_FILE = "transitSchedule.xml.gz"; | ||
private static final String TRANSIT_VEHICLE_FILE = "transitVehicles.xml.gz"; | ||
|
||
private final Scenario scenario; | ||
private final Path directory; | ||
private final String prefix; | ||
|
||
@Override | ||
public void save() throws IOException { | ||
Files.createDirectories(directory); | ||
|
||
writeFile(CONFIG_FILE, new ConfigWriter(scenario.getConfig())::write); | ||
writeFile(NETWORK_FILE, new NetworkWriter(scenario.getNetwork())::write); | ||
writeFile(TRANSIT_SCHEDULE_FILE, new TransitScheduleWriter(scenario.getTransitSchedule())::writeFile); | ||
writeFile(TRANSIT_VEHICLE_FILE, new MatsimVehicleWriter(scenario.getTransitVehicles())::writeFile); | ||
} | ||
|
||
private void writeFile(String fileName, FileWriterAction writerAction) throws IOException { | ||
Path filePath = directory.resolve(prefix + fileName); | ||
writerAction.write(filePath.toString()); | ||
} | ||
|
||
@FunctionalInterface | ||
private interface FileWriterAction { | ||
void write(String filePath) throws IOException; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/java/ch/sbb/pfi/netzgrafikeditor/converter/io/netzgrafik/JsonFileReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package ch.sbb.pfi.netzgrafikeditor.converter.io.netzgrafik; | ||
|
||
import ch.sbb.pfi.netzgrafikeditor.converter.NetworkGraphicSource; | ||
import ch.sbb.pfi.netzgrafikeditor.converter.model.NetworkGraphic; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
|
||
@RequiredArgsConstructor | ||
public class JsonFileReader implements NetworkGraphicSource { | ||
|
||
private final Path filePath; | ||
private volatile NetworkGraphic networkGraphic; // ensure visibility across threads | ||
|
||
@Override | ||
public NetworkGraphic load() throws IOException { | ||
|
||
// first check (without locking) | ||
if (networkGraphic == null) { | ||
synchronized (this) { | ||
// second check (with locking) | ||
if (networkGraphic == null) { | ||
networkGraphic = new JsonDeserializer().read(filePath); | ||
} | ||
} | ||
} | ||
|
||
return networkGraphic; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...in/java/ch/sbb/pfi/netzgrafikeditor/converter/supply/impl/NoInfrastructureRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package ch.sbb.pfi.netzgrafikeditor.converter.supply.impl; | ||
|
||
import ch.sbb.pfi.netzgrafikeditor.converter.model.NetworkGraphic; | ||
import ch.sbb.pfi.netzgrafikeditor.converter.model.Node; | ||
import ch.sbb.pfi.netzgrafikeditor.converter.supply.Coordinate; | ||
import ch.sbb.pfi.netzgrafikeditor.converter.supply.InfrastructureRepository; | ||
import ch.sbb.pfi.netzgrafikeditor.converter.supply.StopFacilityInfo; | ||
import ch.sbb.pfi.netzgrafikeditor.converter.supply.TrackSegmentInfo; | ||
import ch.sbb.pfi.netzgrafikeditor.converter.supply.TransitLineInfo; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class NoInfrastructureRepository implements InfrastructureRepository { | ||
|
||
private final Map<String, Node> nodes = new HashMap<>(); | ||
|
||
public NoInfrastructureRepository(NetworkGraphic networkGraphic) { | ||
networkGraphic.getNodes().forEach(node -> nodes.put(node.getBetriebspunktName(), node)); | ||
} | ||
|
||
@Override | ||
public StopFacilityInfo getStopFacility(String stopId) { | ||
Node node = nodes.get(stopId); | ||
return new StopFacilityInfo(stopId, new Coordinate(-node.getPositionY(), node.getPositionX())); | ||
} | ||
|
||
@Override | ||
public List<TrackSegmentInfo> getTrack(StopFacilityInfo fromStop, StopFacilityInfo toStop, TransitLineInfo transitLineInfo) { | ||
Node fromNode = nodes.get(fromStop.getId()); | ||
Node toNode = nodes.get(toStop.getId()); | ||
|
||
Coordinate fromCoord = new Coordinate(fromNode.getPositionX(), fromNode.getPositionY()); | ||
Coordinate toCoord = new Coordinate(toNode.getPositionX(), toNode.getPositionY()); | ||
double distance = euclideanDistance(fromCoord, toCoord); | ||
|
||
return List.of( | ||
new TrackSegmentInfo(String.format("%s-%s", fromStop.getId(), toStop.getId()), fromCoord, toCoord, | ||
distance)); | ||
} | ||
|
||
private double euclideanDistance(Coordinate from, Coordinate to) { | ||
// coordinates represent screen positions (not geographical); Euclidean formula is valid in this case | ||
double deltaX = to.getLongitude() - from.getLongitude(); | ||
double deltaY = to.getLatitude() - from.getLatitude(); | ||
|
||
return Math.sqrt(deltaX * deltaX + deltaY * deltaY); | ||
} | ||
} |
Oops, something went wrong.