Skip to content

Commit

Permalink
move network preparation to separate class, add to Makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
simei94 committed Aug 22, 2024
1 parent afe958e commit 353a236
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 30 deletions.
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,15 @@ input/$V/$N-$V-network.xml.gz: input/sumo.net.xml
$(sc) prepare network-from-sumo $< --output $@ --free-speed-factor 0.75
$(sc) prepare clean-network $@ --output $@ --modes car --modes bike

# add freight modes as allowed modes
# add hbefa attributes as link attributes
input/$V/$N-$V-network-freight-hbefa.xml.gz: input/$V/$N-$V-network.xml.gz
$(sc) prepare network\
--network $<\
--output $@

#add pt to network from german wide gtfs, but only for area of shp file
input/$V/$N-$V-network-with-pt.xml.gz: input/$V/$N-$V-network.xml.gz
input/$V/$N-$V-network-with-pt.xml.gz: input/$V/$N-$V-network-freight-hbefa.xml.gz
$(sc) prepare transit-from-gtfs --network $<\
--output=input/$V\
--name $N-$V --date "2023-01-11" --target-crs $(CRS) \
Expand Down
36 changes: 7 additions & 29 deletions src/main/java/org/matsim/run/LausitzScenario.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.google.inject.name.Names;
import org.matsim.api.core.v01.Scenario;
import org.matsim.api.core.v01.TransportMode;
import org.matsim.api.core.v01.network.Link;
import org.matsim.application.MATSimApplication;
import org.matsim.application.analysis.CheckPopulation;
import org.matsim.application.analysis.traffic.LinkStats;
Expand All @@ -17,9 +16,7 @@
import org.matsim.application.prepare.network.CreateNetworkFromSumo;
import org.matsim.application.prepare.population.*;
import org.matsim.application.prepare.pt.CreateTransitScheduleFromGtfs;
import org.matsim.contrib.emissions.HbefaRoadTypeMapping;
import org.matsim.contrib.emissions.HbefaVehicleCategory;
import org.matsim.contrib.emissions.OsmHbefaMapping;
import org.matsim.contrib.emissions.utils.EmissionsConfigGroup;
import org.matsim.contrib.vsp.scenario.SnzActivities;
import org.matsim.core.config.Config;
Expand All @@ -29,12 +26,12 @@
import org.matsim.core.config.groups.ScoringConfigGroup;
import org.matsim.core.controler.AbstractModule;
import org.matsim.core.controler.Controler;
import org.matsim.core.network.NetworkUtils;
import org.matsim.core.replanning.strategies.DefaultPlanStrategiesModule;
import org.matsim.core.router.costcalculators.TravelDisutilityFactory;
import org.matsim.core.router.util.TravelTime;
import org.matsim.core.scoring.functions.ScoringParametersForPerson;
import org.matsim.run.analysis.CommuterAnalysis;
import org.matsim.run.prepare.PrepareNetwork;
import org.matsim.run.prepare.PreparePopulation;
import org.matsim.simwrapper.SimWrapperConfigGroup;
import org.matsim.simwrapper.SimWrapperModule;
Expand All @@ -59,7 +56,7 @@
CreateNetworkFromSumo.class, CreateTransitScheduleFromGtfs.class, TrajectoryToPlans.class, GenerateShortDistanceTrips.class,
MergePopulations.class, ExtractRelevantFreightTrips.class, DownSamplePopulation.class, ExtractHomeCoordinates.class, CleanNetwork.class,
CreateLandUseShp.class, ResolveGridCoordinates.class, FixSubtourModes.class, AdjustActivityToLinkDistances.class, XYToLinks.class,
SplitActivityTypesDuration.class, CreateCountsFromBAStData.class, PreparePopulation.class, CleanPopulation.class
SplitActivityTypesDuration.class, CreateCountsFromBAStData.class, PreparePopulation.class, CleanPopulation.class, PrepareNetwork.class
})
@MATSimApplication.Analysis({
LinkStats.class, CheckPopulation.class, CommuterAnalysis.class,
Expand Down Expand Up @@ -174,32 +171,13 @@ protected Config prepareConfig(Config config) {
@Override
protected void prepareScenario(Scenario scenario) {

for (Link link : scenario.getNetwork().getLinks().values()) {
Set<String> modes = link.getAllowedModes();

// allow freight traffic together with cars
if (modes.contains(TransportMode.car)) {
Set<String> newModes = Sets.newHashSet(modes);
newModes.add(FREIGHT);
newModes.add(TransportMode.truck);

link.setAllowedModes(newModes);
}
}
// add freight and truck as allowed modes together with car
PrepareNetwork.prepareFreightNetwork(scenario.getNetwork());

if (emissions == EmissionAnalysisHandling.PERFORM_EMISSIONS_ANALYSIS) {
// do not use VspHbefaRoadTypeMapping() as it results in almost every road to mapped to "highway"!
HbefaRoadTypeMapping roadTypeMapping = OsmHbefaMapping.build();
// the type attribute in our network has the prefix "highway" for all links but pt links.
// we need to delete that because OsmHbefaMapping does not handle that.
for (Link link : scenario.getNetwork().getLinks().values()) {
//pt links can be disregarded
if (!link.getAllowedModes().contains("pt")) {
NetworkUtils.setType(link, NetworkUtils.getType(link).replaceFirst("highway.", ""));
}
}
roadTypeMapping.addHbefaMappings(scenario.getNetwork());

// prepare hbefa link attributes + make link.getType() handable for OsmHbefaMapping
PrepareNetwork.prepareEmissionsAttributes(scenario.getNetwork());
// prepare vehicle types for emission analysis
prepareVehicleTypesForEmissionAnalysis(scenario);
}
}
Expand Down
93 changes: 93 additions & 0 deletions src/main/java/org/matsim/run/prepare/PrepareNetwork.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package org.matsim.run.prepare;

import com.google.common.collect.Sets;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
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.application.MATSimAppCommand;
import org.matsim.contrib.emissions.HbefaRoadTypeMapping;
import org.matsim.contrib.emissions.OsmHbefaMapping;
import org.matsim.core.network.NetworkUtils;
import org.matsim.core.network.algorithms.MultimodalNetworkCleaner;
import picocli.CommandLine;

import java.util.Set;

@CommandLine.Command(
name = "network",
description = "Prepare network / link attributes."
)
public class PrepareNetwork implements MATSimAppCommand {

private static final Logger log = LogManager.getLogger(PrepareNetwork.class);

private static final String FREIGHT = "freight";

@CommandLine.Option(names = "--network", description = "Path to network file", required = true)
private String networkFile;

@CommandLine.Option(names = "--output", description = "Output path of the prepared network", required = true)
private String outputPath;

public static void main(String[] args) {
new PrepareNetwork().execute(args);
}

@Override
public Integer call() throws Exception {

Network network = NetworkUtils.readNetwork(networkFile);

prepareFreightNetwork(network);
prepareEmissionsAttributes(network);

NetworkUtils.writeNetwork(network, outputPath);

return 0;
}

/**
* prepare link attributes for freight and truck as allowed modes together with car.
*/
public static void prepareFreightNetwork(Network network) {
int linkCount = 0;

for (Link link : network.getLinks().values()) {
Set<String> modes = link.getAllowedModes();

// allow freight traffic together with cars
if (modes.contains(TransportMode.car)) {
Set<String> newModes = Sets.newHashSet(modes);

newModes.add(FREIGHT);
newModes.add(TransportMode.truck);

link.setAllowedModes(newModes);
linkCount++;
}
}

log.info("For {} links {} and {} has been added as an allowed mode.", linkCount, FREIGHT, TransportMode.truck);

new MultimodalNetworkCleaner(network).run(Set.of(FREIGHT, TransportMode.truck));
}

/**
* add hbefa link attributes.
*/
public static void prepareEmissionsAttributes(Network network) {
// do not use VspHbefaRoadTypeMapping() as it results in almost every road to mapped to "highway"!
HbefaRoadTypeMapping roadTypeMapping = OsmHbefaMapping.build();
// the type attribute in our network has the prefix "highway" for all links but pt links.
// we need to delete that because OsmHbefaMapping does not handle that.
for (Link link : network.getLinks().values()) {
//pt links can be disregarded
if (!link.getAllowedModes().contains("pt")) {
NetworkUtils.setType(link, NetworkUtils.getType(link).replaceFirst("highway.", ""));
}
}
roadTypeMapping.addHbefaMappings(network);
}
}

0 comments on commit 353a236

Please sign in to comment.