Skip to content

Commit

Permalink
add toll area as map
Browse files Browse the repository at this point in the history
  • Loading branch information
simei94 committed Mar 21, 2024
1 parent d34980b commit f5d7cdf
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,32 @@
import org.matsim.application.options.CsvOptions;
import org.matsim.application.options.InputOptions;
import org.matsim.application.options.OutputOptions;
import org.matsim.application.options.ShpOptions;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.utils.gis.ShapeFileWriter;
import org.matsim.core.utils.io.IOUtils;
import org.matsim.prepare.MexicoCityUtils;
import org.matsim.simwrapper.SimWrapperConfigGroup;
import picocli.CommandLine;
import tech.tablesaw.aggregate.AggregateFunctions;
import tech.tablesaw.api.*;
import tech.tablesaw.io.csv.CsvReadOptions;
import tech.tablesaw.joining.DataFrameJoiner;
import tech.tablesaw.selection.Selection;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Path;
import java.util.*;

import static tech.tablesaw.aggregate.AggregateFunctions.count;

@CommandLine.Command(name = "roadPricing", description = "Calculates various road pricing related metrics.")
@CommandSpec(
requires = {"personMoneyEvents.tsv", "persons.csv"},
produces = {"roadPricing_income_groups.csv", "roadPricing_tolled_agents.csv", "roadPricing_daytime_groups.csv", "roadPricing_tolled_agents_home_locations.csv"}
requires = {"personMoneyEvents.tsv", "persons.csv", "config.xml"},
produces = {"roadPricing_income_groups.csv", "roadPricing_tolled_agents.csv", "roadPricing_daytime_groups.csv", "roadPricing_tolled_agents_home_locations.csv", "roadPricing_area.shp"}
)
public class RoadPricingAnalysis implements MATSimAppCommand {

Expand Down Expand Up @@ -99,9 +107,30 @@ public Integer call() throws Exception {
Table homes = joined.retainColumns(person, "home_x", "home_y");
homes.write().csv(output.getPath("roadPricing_tolled_agents_home_locations.csv").toFile());

// write road pricing shp such that simwrapper can access it
Config config = ConfigUtils.loadConfig(input.getPath("config.xml"));

writeTollAreaShpFile(config);

return 0;
}

private void writeTollAreaShpFile(Config config) throws IOException {
SimWrapperConfigGroup sw = ConfigUtils.addOrGetModule(config, SimWrapperConfigGroup.class);

ShpOptions shp = new ShpOptions(Path.of(sw.defaultParams().getValue(MexicoCityUtils.ROAD_PRICING_AREA)), null, null);

ShapeFileWriter.writeGeometries(shp.readFeatures(), output.getPath("roadPricing_area.shp").toString());

// We cannot use the same output option for 2 different files, so the string has to be manipulated
String prj = output.getPath("roadPricing_area.shp").toString().replace(".shp", ".prj");

// .prj file needs to be simplified to make it readable for simwrapper
try (BufferedWriter writer = IOUtils.getBufferedWriter(prj)) {
writer.write(MexicoCityUtils.CRS);
}
}

private void writeHourlyDistr(Table joined) {

joined.addColumns(IntColumn.create("hour"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public List<Dashboard> getDashboards(Config config, SimWrapper simWrapper) {

TripDashboard trips = new TripDashboard("mode_share_ref.csv", null, null);

return List.of(trips, new RoadPricingDashboard(roadPricingAreaPath));
return List.of(trips, new RoadPricingDashboard());
}

/**
Expand Down
8 changes: 2 additions & 6 deletions src/main/java/org/matsim/dashboard/RoadPricingDashboard.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,9 @@
* Shows information about an optional road pricing policy case.
*/
public class RoadPricingDashboard implements Dashboard {
String roadPricingAreaPath;
String share = "share";

RoadPricingDashboard(String roadPricingAreaPath) {
this.roadPricingAreaPath = roadPricingAreaPath;
}
public RoadPricingDashboard() {}
@Override
public void configure(Header header, Layout layout) {
header.title = "Road Pricing";
Expand Down Expand Up @@ -70,8 +67,7 @@ public void configure(Header header, Layout layout) {
viz.zoom = data.context().mapZoomLevel;
viz.height = 7.5;
viz.width = 2.0;
// TODO: parsing of roadPricingAreaPath does not work yet
viz.setShape(roadPricingAreaPath);
viz.setShape(data.compute(RoadPricingAnalysis.class, "roadPricing_area.shp"));
});
}
}
2 changes: 2 additions & 0 deletions src/main/java/org/matsim/prepare/MexicoCityUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public final class MexicoCityUtils {
// -> avg of 2017: 1€=21.344 MXN
public static final Double PESO_EURO = 21.344;

public static final String ROAD_PRICING_AREA = "roadPricingAreaShp";

//do not instantiate
private MexicoCityUtils() {
}
Expand Down
16 changes: 4 additions & 12 deletions src/main/java/org/matsim/run/RunMexicoCityScenario.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,12 @@
import org.matsim.core.replanning.annealing.ReplanningAnnealerConfigGroup;
import org.matsim.core.router.AnalysisMainModeIdentifier;
import org.matsim.core.scoring.functions.ScoringParametersForPerson;
import org.matsim.dashboard.MexicoCityDashboardProvider;
import org.matsim.prepare.*;
import org.matsim.prepare.network.CreateMexicoCityNetworkFromSumo;
import org.matsim.prepare.network.PrepareNetwork;
import org.matsim.prepare.opt.RunCountOptimization;
import org.matsim.prepare.opt.SelectPlansFromIndex;
import org.matsim.prepare.population.*;
import org.matsim.simwrapper.DashboardProvider;
import org.matsim.simwrapper.SimWrapperConfigGroup;
import org.matsim.simwrapper.SimWrapperModule;
import org.matsim.vehicles.VehicleType;
Expand All @@ -50,9 +48,7 @@
import javax.annotation.Nullable;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.*;

@CommandLine.Command(header = ":: Open Mexico-City Scenario ::", version = RunMexicoCityScenario.VERSION, mixinStandardHelpOptions = true)
@MATSimApplication.Prepare({
Expand Down Expand Up @@ -183,6 +179,9 @@ protected Config prepareConfig(Config config) {

if (MexicoCityUtils.isDefined(RoadPricingOptions.roadPricingAreaPath)) {
ConfigUtils.addOrGetModule(config, RoadPricingConfigGroup.class).setTollLinksFile(null);

// this is needed to display the toll area on the road pricing dashboard
sw.defaultParams().set(MexicoCityUtils.ROAD_PRICING_AREA, RoadPricingOptions.roadPricingAreaPath.toString());
}

return config;
Expand Down Expand Up @@ -278,13 +277,6 @@ public void install() {
if (MexicoCityUtils.isDefined(RoadPricingOptions.roadPricingAreaPath)) {
install(new RoadPricingModule());

MexicoCityDashboardProvider provider = new MexicoCityDashboardProvider();
provider.setRoadPricingAreaPath(RoadPricingOptions.roadPricingAreaPath.toString());

bind(DashboardProvider.class).toInstance(provider);



// use own RoadPricingControlerListener, which throws person money events by multiplying the toll (factor) by the agent's income
if (RoadPricingOptions.roadPricingType.equals(RoadPricingOptions.RoadPricingType.RELATIVE_TO_INCOME)) {
if (!MexicoCityUtils.isDefined(incomeAreaPath)) {
Expand Down

0 comments on commit f5d7cdf

Please sign in to comment.