generated from matsim-scenarios/matsim-scenario-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move network preparation to separate class, add to Makefile
- Loading branch information
Showing
3 changed files
with
107 additions
and
30 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
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,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); | ||
} | ||
} |