Skip to content

Commit

Permalink
Merge pull request #3210 from matsim-org/someRailsimAdjustments
Browse files Browse the repository at this point in the history
Some railsim adjustments
  • Loading branch information
ikaddoura authored Apr 22, 2024
2 parents 060ea5e + c493226 commit 4914256
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,39 @@ class OsmNetworkParser {
Map<Long, ProcessedOsmWay> ways;
Map<Long, ProcessedOsmNode> nodes;
Map<Long, List<ProcessedOsmWay>> nodeReferences;

private final String wayType;

/**
* The default constructor for roads (OSM highway tag)
*
* @param transformation
* @param linkProperties
* @param linkFilter
* @param executor
*/
OsmNetworkParser(CoordinateTransformation transformation, Map<String, LinkProperties> linkProperties, BiPredicate<Coord, Integer> linkFilter, ExecutorService executor) {
this.transformation = transformation;
this.linkProperties = linkProperties;
this.linkFilter = linkFilter;
this.executor = executor;
this.wayType = OsmTags.HIGHWAY;
}

/**
* A more flexible constructor which allows to pass a different way type, e.g. railway
*
* @param transformation
* @param linkProperties
* @param linkFilter
* @param executor
* @param wayType
*/
OsmNetworkParser(CoordinateTransformation transformation, Map<String, LinkProperties> linkProperties, BiPredicate<Coord, Integer> linkFilter, ExecutorService executor, String wayType) {
this.transformation = transformation;
this.linkProperties = linkProperties;
this.linkFilter = linkFilter;
this.executor = executor;
this.wayType = wayType;
}

public Map<Long, ProcessedOsmWay> getWays() {
Expand Down Expand Up @@ -106,7 +133,7 @@ void handleWay(OsmWay osmWay) {
Map<String, String> tags = OsmModelUtil.getTagsAsMap(osmWay);

if (isStreetOfInterest(tags)) {
LinkProperties linkProperty = linkProperties.get(tags.get(OsmTags.HIGHWAY));
LinkProperties linkProperty = linkProperties.get(tags.get(wayType));
ProcessedOsmWay processedWay = ProcessedOsmWay.create(osmWay, tags, linkProperty);
ways.put(osmWay.getId(), processedWay);

Expand All @@ -125,7 +152,7 @@ void handleWay(OsmWay osmWay) {
}

private boolean isStreetOfInterest(Map<String, String> tags) {
return tags.containsKey(OsmTags.HIGHWAY) && linkProperties.containsKey(tags.get(OsmTags.HIGHWAY));
return tags.containsKey(wayType) && linkProperties.containsKey(tags.get(wayType));
}

private boolean isEndNodeOfReferencingLink(OsmNode node, ProcessedOsmWay processedOsmWay) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package org.matsim.contrib.osm.networkReader;

import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.Executors;
import java.util.function.BiPredicate;
import java.util.function.Predicate;

import org.matsim.api.core.v01.Coord;
import org.matsim.api.core.v01.network.Link;
import org.matsim.core.network.NetworkUtils;

public final class OsmRailwayReader extends SupersonicOsmNetworkReader {

private static final double FALLBACK_MAX_SPEED = 100.;

public OsmRailwayReader(OsmNetworkParser parser,
Predicate<Long> preserveNodeWithId,
BiPredicate<Coord, Integer> includeLinkAtCoordWithHierarchy,
AfterLinkCreated afterLinkCreated, double freeSpeedFactor, double adjustCapacityLength, boolean storeOriginalGeometry) {

super(parser, preserveNodeWithId, includeLinkAtCoordWithHierarchy, (link, tags, direction) -> handleLink(link, tags, direction, afterLinkCreated), freeSpeedFactor, adjustCapacityLength, storeOriginalGeometry);
}

private static void handleLink(Link link, Map<String, String> tags, SupersonicOsmNetworkReader.Direction direction, AfterLinkCreated outfacingCallback) {

String railwayType = tags.get(OsmTags.RAILWAY);
link.getAttributes().putAttribute("osm_way_type", "railway");
link.getAttributes().putAttribute(NetworkUtils.TYPE, railwayType);

setAttributes(link, tags);
setAllowedModes(link, tags);
setSpeed(link, tags);

outfacingCallback.accept(link, tags, direction);
}

private static void setSpeed(Link link, Map<String, String> tags) {

}

private static void setAttributes(Link link, Map<String, String> tags) {

if (tags.containsKey(OsmTags.USAGE)) {
link.getAttributes().putAttribute(OsmTags.USAGE, tags.get(OsmTags.USAGE));
}

if (tags.containsKey(OsmTags.GAUGE)) {
link.getAttributes().putAttribute(OsmTags.GAUGE, tags.get(OsmTags.GAUGE));
}

if (tags.containsKey(OsmTags.ELECTRIFIED)) {
link.getAttributes().putAttribute(OsmTags.ELECTRIFIED, tags.get(OsmTags.ELECTRIFIED));
}

if (tags.containsKey(OsmTags.MAXSPEED)) {
link.getAttributes().putAttribute(OsmTags.MAXSPEED, tags.get(OsmTags.MAXSPEED));
} else {
link.getAttributes().putAttribute(OsmTags.MAXSPEED, FALLBACK_MAX_SPEED);
}

if (tags.containsKey(OsmTags.ETCS)) {
link.getAttributes().putAttribute(OsmTags.ETCS, tags.get(OsmTags.ETCS));
}

if (tags.containsKey(OsmTags.OPERATOR)) {
link.getAttributes().putAttribute(OsmTags.OPERATOR, tags.get(OsmTags.OPERATOR));
}

}

private static void setAllowedModes(Link link, Map<String, String> tags) {
Set<String> allowedModes = new HashSet<>();
allowedModes.add(tags.get(OsmTags.RAILWAY));
link.setAllowedModes(allowedModes);
}

@Override
Collection<Link> createLinks(WaySegment segment) {
Collection<Link> links = super.createLinks(segment);
return links;
}

public static class Builder extends AbstractBuilder<OsmRailwayReader> {

public Builder() {
ConcurrentMap<String, LinkProperties> linkProperties = new ConcurrentHashMap<>();
linkProperties.put(OsmTags.RAIL, new LinkProperties(1, 1, 30., 1000., false));
linkProperties.put(OsmTags.NARROW_GAUGE, new LinkProperties(1, 1, 30., 1000., false));
linkProperties.put(OsmTags.LIGHT_RAIL, new LinkProperties(1, 1, 30., 1000., false));
linkProperties.put(OsmTags.MONORAIL, new LinkProperties(1, 1, 30., 1000., false));
linkProperties.put(OsmTags.FUNICULAR, new LinkProperties(1, 1, 30., 1000., false));
linkProperties.put(OsmTags.SUBWAY, new LinkProperties(1, 1, 30., 1000., false));
linkProperties.put(OsmTags.TRAM, new LinkProperties(1, 1, 30., 1000., false));

setLinkProperties(linkProperties);
}

@Override
OsmRailwayReader createInstance() {
OsmNetworkParser parser = new OsmNetworkParser(coordinateTransformation, linkProperties, includeLinkAtCoordWithHierarchy, Executors.newWorkStealingPool(), OsmTags.RAILWAY);
return new OsmRailwayReader(parser, preserveNodeWithId, includeLinkAtCoordWithHierarchy, afterLinkCreated, freeSpeedFactor, adjustCapacityLength, storeOriginalGeometry);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,20 @@ public class OsmTags {
public static final String CROSSING = "crossing";
public static final String TYPE = "type";
public static final String RESTRICTION = "restriction";

public static final String RAILWAY = "railway";
public static final String RAIL = "rail";
public static final String NARROW_GAUGE = "narrow_gauge";
public static final String TRAM = "tram";
public static final String FUNICULAR = "funicular";
public static final String SUBWAY = "subway";
public static final String LIGHT_RAIL = "light_rail";
public static final String MONORAIL = "monorail";
public static final String USAGE = "usage";
public static final String GAUGE = "gauge";
public static final String ELECTRIFIED = "electrified";
public static final String ETCS = "etcs";
public static final String OPERATOR = "operator";


}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@

package ch.sbb.matsim.contrib.railsim;

import ch.sbb.matsim.contrib.railsim.qsimengine.RailsimQSimModule;
import org.matsim.api.core.v01.Scenario;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.controler.Controler;
import org.matsim.core.controler.OutputDirectoryHierarchy;
import org.matsim.core.scenario.ScenarioUtils;

import ch.sbb.matsim.contrib.railsim.qsimengine.RailsimQSimModule;

/**
* Example script that shows how to use railsim included in this contrib.
*/
Expand All @@ -37,12 +38,13 @@ private RunRailsimExample() {

public static void main(String[] args) {

if (args.length == 0) {
System.err.println("Path to config is required as first argument.");
System.exit(2);
String configFilename;
if (args.length != 0) {
configFilename = args[0];
} else {
configFilename = "test/input/ch/sbb/matsim/contrib/railsim/integration/microOlten/config.xml";
}

String configFilename = args[0];
Config config = ConfigUtils.loadConfig(configFilename);
config.controller().setOverwriteFileSetting(OutputDirectoryHierarchy.OverwriteFileSetting.deleteDirectoryIfExists);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,14 @@
import org.matsim.api.core.v01.TransportMode;
import org.matsim.api.core.v01.events.Event;
import org.matsim.api.core.v01.network.Link;
import org.matsim.api.core.v01.population.Activity;
import org.matsim.api.core.v01.population.Person;
import org.matsim.api.core.v01.population.Plan;
import org.matsim.api.core.v01.population.PlanElement;
import org.matsim.core.api.experimental.events.VehicleArrivesAtFacilityEvent;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.config.groups.ScoringConfigGroup;
import org.matsim.core.controler.AbstractModule;
import org.matsim.core.controler.Controler;
import org.matsim.core.scenario.ScenarioUtils;
Expand All @@ -51,6 +56,7 @@
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;
Expand Down Expand Up @@ -367,8 +373,25 @@ void testScenarioKelheim() {
type.setMaximumVelocity(30);
type.setLength(100);
}

SnzActivities.addScoringParams(config);

// simplify the activity types, e.g. home_3600 -> home
Set<String> activityTypes = new HashSet<>();
for (Person person : scenario.getPopulation().getPersons().values()) {
for (Plan plan : person.getPlans()) {
for (PlanElement pE : plan.getPlanElements()) {
if(pE instanceof Activity) {
Activity act = (Activity) pE;
String baseType = act.getType().split("_")[0];
act.setType(baseType);
activityTypes.add(baseType);
}
}
}
}

for (String type : activityTypes) {
config.scoring().addActivityParams(new ScoringConfigGroup.ActivityParams(type).setTypicalDuration(1234.));
}

Controler controler = new Controler(scenario);
controler.addOverridingModule(new RailsimModule());
Expand Down

This file was deleted.

0 comments on commit 4914256

Please sign in to comment.