Skip to content

Commit

Permalink
Update emissions codes to handle unknown Osm highway tag values (#277)
Browse files Browse the repository at this point in the history
Co-authored-by: diallitoz@gmail.com
  • Loading branch information
Nitnelav authored Nov 13, 2024
1 parent b338bf8 commit a0edb7f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ included in the (note yet determined) next version number.

**Development version**

- Improve Emissions tools in order to handle unknown Osm highway tag values when mapping HBEFA road types
- add configurable policies for IDF
- Introduce `travelTimeRecordingInterval` config option that decouples travel time writing from general analysis
- Add eqasim_activities.csv for analysis
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.matsim.api.core.v01.network.Link;
import org.matsim.api.core.v01.network.Network;
import org.matsim.contrib.emissions.EmissionModule;
import org.matsim.contrib.emissions.OsmHbefaMapping;
import org.matsim.contrib.emissions.utils.EmissionsConfigGroup;
import org.matsim.core.api.experimental.events.EventsManager;
import org.matsim.core.config.CommandLine;
Expand Down Expand Up @@ -60,7 +59,10 @@ public static void main(String[] args) throws CommandLine.ConfigurationException
Scenario scenario = ScenarioUtils.createScenario(config);
ScenarioUtils.loadScenario(scenario);

OsmHbefaMapping osmHbefaMapping = OsmHbefaMapping.build();
// the default hbefa type is URB/Acess/30 but can be changed like this
// SafeOsmHbefaMapping.defaultType = "URB/Local/50";
SafeOsmHbefaMapping osmHbefaMapping = new SafeOsmHbefaMapping();

Network network = scenario.getNetwork();
// if the network is from pt2matsim it might not have "type" but "osm:way:highway" attribute instead
for (Link link: network.getLinks().values()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.eqasim.core.components.emissions;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.matsim.api.core.v01.network.Link;
import org.matsim.contrib.emissions.HbefaRoadTypeMapping;
import org.matsim.contrib.emissions.OsmHbefaMapping;
import org.matsim.core.network.NetworkUtils;

// This class is designed to wrap the matsim-libs emissions contrib OsmHbefaMapping by providing a default hbefa type for unknown osm keys (instead of throwing a RuntimeException
public class SafeOsmHbefaMapping extends HbefaRoadTypeMapping {

private final static OsmHbefaMapping osmHbefaMapping = OsmHbefaMapping.build();
private final static Logger log = LogManager.getLogger(SafeOsmHbefaMapping.class);
public static String defaultType = "URB/Access/30";

@Override
public String determineHbefaType(Link link) {
String result;
try {
result = osmHbefaMapping.determineHbefaType(link);
} catch (RuntimeException runtimeException) {
String type = (String) link.getAttributes().getAttribute(NetworkUtils.TYPE);
log.warn("'" + type + "' not in hbefa map; setting to " + defaultType);
result = defaultType;
}
return result;
}

}
Binary file modified core/src/main/resources/melun/network.xml.gz
Binary file not shown.
17 changes: 12 additions & 5 deletions core/src/test/java/org/eqasim/TestEmissions.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.zip.GZIPInputStream;

import org.apache.commons.io.FileUtils;
import org.eqasim.core.components.emissions.RunComputeEmissionsEvents;
import org.eqasim.core.components.emissions.RunExportEmissionsNetwork;
import org.eqasim.core.components.emissions.SafeOsmHbefaMapping;
import org.eqasim.core.simulation.EqasimConfigurator;
import org.eqasim.core.simulation.analysis.EqasimAnalysisModule;
import org.eqasim.core.simulation.mode_choice.AbstractEqasimExtension;
Expand Down Expand Up @@ -143,8 +140,16 @@ private void runModifyNetwork() {
Scenario scenario = ScenarioUtils.loadScenario(config);
Network network = scenario.getNetwork();
for (Link link : network.getLinks().values()) {

// this forces the OSM Mapping code to use URB/Local/50 as it the only thing we
// have in the sample HBEFA.
// We intentionally leave the 'pedestrian' links here to test the SafeOsmHbefaMapping class
String linkType = NetworkUtils.getType(link);
if (linkType != null) {
if (!linkType.equals("pedestrian")) {
continue;
}
}
NetworkUtils.setType(link, "tertiary");
link.getAttributes().putAttribute(NetworkUtils.ALLOWED_SPEED, 50 / 3.6);
}
Expand All @@ -159,6 +164,8 @@ private void runMelunEmissions() throws CommandLine.ConfigurationException, IOEx
Assert.assertEquals(3412, (long) counts.getOrDefault("bike", 0L));
Assert.assertEquals(2108, (long) counts.get("pt"));

SafeOsmHbefaMapping.defaultType = "URB/Loca/50";

RunComputeEmissionsEvents.main(new String[] { "--config-path", "melun_test/input/config.xml",
"--hbefa-cold-avg", "sample_41_EFA_ColdStart_vehcat_2020average.csv", "--hbefa-hot-avg",
"sample_41_EFA_HOT_vehcat_2020average.csv", "--hbefa-cold-detailed",
Expand Down

0 comments on commit a0edb7f

Please sign in to comment.