Skip to content

Commit

Permalink
Added emmisions plot support
Browse files Browse the repository at this point in the history
  • Loading branch information
frievoe97 committed Jan 3, 2024
1 parent e3546fd commit d3f13ba
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@
)
@CommandSpec(requireRunDirectory = true,
produces = {
"emissions_total.csv", "emissions_grid_per_day.xyt.csv", "emissions_per_link.csv",
"emissions_total.csv", "emissions_grid_per_day.csv", "emissions_per_link.csv",
"emissions_per_link_per_m.csv",
"emissions_grid_per_hour.xyt.csv",
"emissions_grid_per_hour.csv",
"emissions_vehicle_info.csv",
}
)
Expand Down Expand Up @@ -253,7 +253,7 @@ private void writeRaster(Network network, Config config, EmissionsOnLinkEventHan

Raster raster = rasterMap.values().stream().findFirst().orElseThrow();

try (CSVPrinter printer = new CSVPrinter(Files.newBufferedWriter(output.getPath("emissions_grid_per_day.xyt.csv")),
try (CSVPrinter printer = new CSVPrinter(Files.newBufferedWriter(output.getPath("emissions_grid_per_day.csv")),
CSVFormat.DEFAULT.builder().setCommentMarker('#').build())) {

String crs = ProjectionUtils.getCRS(network);
Expand All @@ -263,7 +263,7 @@ private void writeRaster(Network network, Config config, EmissionsOnLinkEventHan
crs = config.global().getCoordinateSystem();

// print coordinate system
printer.printComment(crs);
// printer.printComment(crs);

// print header
printer.print("time");
Expand All @@ -280,8 +280,8 @@ private void writeRaster(Network network, Config config, EmissionsOnLinkEventHan
Coord coord = raster.getCoordForIndex(xi, yi);
double value = rasterMap.get(Pollutant.CO2_TOTAL).getValueByIndex(xi, yi);

if (value == 0)
continue;
// if (value == 0)
// continue;

printer.print(0.0);
printer.print(coord.getX());
Expand Down Expand Up @@ -309,7 +309,7 @@ private void writeTimeDependentRaster(Network network, Config config, EmissionsO

Raster raster = firstBin.values().stream().findFirst().orElseThrow();

try (CSVPrinter printer = new CSVPrinter(IOUtils.getBufferedWriter(output.getPath("emissions_grid_per_hour.xyt.csv").toString()),
try (CSVPrinter printer = new CSVPrinter(IOUtils.getBufferedWriter(output.getPath("emissions_grid_per_hour.csv").toString()),
CSVFormat.DEFAULT.builder().setCommentMarker('#').build())) {

String crs = ProjectionUtils.getCRS(network);
Expand Down Expand Up @@ -337,8 +337,8 @@ private void writeTimeDependentRaster(Network network, Config config, EmissionsO
Coord coord = raster.getCoordForIndex(xi, yi);
double value = timeBin.getValue().get(Pollutant.CO2_TOTAL).getValueByIndex(xi, yi);

if (value == 0)
continue;
// if (value == 0)
// continue;

printer.print(timeBin.getStartTime());
printer.print(coord.getX());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
import org.matsim.simwrapper.Dashboard;
import org.matsim.simwrapper.Header;
import org.matsim.simwrapper.Layout;
import org.matsim.simwrapper.viz.GridMap;
import org.matsim.simwrapper.viz.Links;
import org.matsim.simwrapper.viz.Table;
import org.matsim.simwrapper.viz.XYTime;

/**
* Shows emission in the scenario.
*/
public class EmissionsDashboard implements Dashboard {
public class EmissionsDashboard implements Dashboard {
@Override
public void configure(Header header, Layout layout) {

Expand Down Expand Up @@ -49,11 +49,29 @@ public void configure(Header header, Layout layout) {
});

layout.row("second")
.el(XYTime.class, (viz, data) -> {
.el(GridMap.class, (viz, data) -> {
viz.title = "CO₂ Emissions";
viz.description = "per day";
viz.height = 12.;
viz.file = data.compute(AirPollutionAnalysis.class, "emissions_grid_per_day.xyt.csv");
viz.cellSize = 100;
viz.opacity = 0.5;
viz.maxHeight = 100;
viz.projection = "EPSG:25832";
viz.setColorRamp("RdYlGn", 10, false);
viz.file = data.compute(AirPollutionAnalysis.class, "emissions_grid_per_day.csv");
});

layout.row("third")
.el(GridMap.class, (viz, data) -> {
viz.title = "CO₂ Emissions";
viz.description = "per hour";
viz.height = 12.;
viz.cellSize = 100;
viz.opacity = 0.5;
viz.maxHeight = 100;
viz.projection = "EPSG:25832";
viz.setColorRamp("RdYlGn", 10, false);
viz.file = data.compute(AirPollutionAnalysis.class, "emissions_grid_per_hour.csv");
});


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void configure(Header header, Layout layout) {
viz.display.lineColor.dataset = "traffic";
viz.display.lineColor.columnName = "simulated_traffic_volume";
viz.display.lineColor.join = "link_id";
viz.display.lineColor.setColorRamp(Plotly.ColorScheme.RdYlBu, 5, true);
viz.display.lineColor.setColorRamp(ColorScheme.RdYlBu, 5, true);

viz.display.lineWidth.dataset = "traffic";
viz.display.lineWidth.columnName = "simulated_traffic_volume";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.matsim.simwrapper.Dashboard;
import org.matsim.simwrapper.Header;
import org.matsim.simwrapper.Layout;
import org.matsim.simwrapper.viz.ColorScheme;
import org.matsim.simwrapper.viz.MapPlot;
import org.matsim.simwrapper.viz.Plotly;
import tech.tablesaw.plotly.components.Axis;
Expand Down Expand Up @@ -111,7 +112,7 @@ else if (i == labels.size() - 1)
viz.addTrace(BarTrace.builder(Plotly.OBJ_INPUT, Plotly.INPUT).orientation(BarTrace.Orientation.HORIZONTAL).build(), ds.mapping()
.x("n")
.y("source")
.name("quality", Plotly.ColorScheme.RdYlBu)
.name("quality", ColorScheme.RdYlBu)
);
}).el(Plotly.class, (viz, data) -> {

Expand All @@ -128,7 +129,7 @@ else if (i == labels.size() - 1)
viz.addTrace(BarTrace.builder(Plotly.OBJ_INPUT, Plotly.INPUT).build(), ds.mapping()
.x("road_type")
.y("share")
.name("quality", Plotly.ColorScheme.RdYlBu)
.name("quality", ColorScheme.RdYlBu)
);

});
Expand Down Expand Up @@ -195,7 +196,7 @@ else if (i == labels.size() - 1)
viz.display.lineColor.dataset = "counts";
viz.display.lineColor.columnName = "quality";
viz.display.lineColor.join = "link_id";
viz.display.lineColor.setColorRamp(Plotly.ColorScheme.RdYlBu, labels.size(), false);
viz.display.lineColor.setColorRamp(ColorScheme.RdYlBu, labels.size(), false);

// 8px
viz.display.lineWidth.dataset = "@8";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.matsim.simwrapper.Dashboard;
import org.matsim.simwrapper.Header;
import org.matsim.simwrapper.Layout;
import org.matsim.simwrapper.viz.ColorScheme;
import org.matsim.simwrapper.viz.MapPlot;
import org.matsim.simwrapper.viz.Plotly;
import org.matsim.simwrapper.viz.Table;
Expand Down Expand Up @@ -53,7 +54,7 @@ public void configure(Header header, Layout layout) {
viz.addTrace(ScatterTrace.builder(Plotly.INPUT, Plotly.INPUT).mode(ScatterTrace.Mode.LINE).build(), ds.mapping()
.x("hour")
.y("congestion_index")
.name("road_type", Plotly.ColorScheme.Spectral)
.name("road_type", ColorScheme.Spectral)
);
})
.el(Table.class, ((viz, data) -> {
Expand Down Expand Up @@ -82,7 +83,7 @@ public void configure(Header header, Layout layout) {
viz.display.lineColor.dataset = "traffic";
viz.display.lineColor.columnName = "avg_speed";
viz.display.lineColor.join = "link_id";
viz.display.lineColor.setColorRamp(Plotly.ColorScheme.RdYlBu, 5, false);
viz.display.lineColor.setColorRamp(ColorScheme.RdYlBu, 5, false);

viz.display.lineWidth.dataset = "traffic";
viz.display.lineWidth.columnName = "simulated_traffic_volume";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.matsim.simwrapper.Dashboard;
import org.matsim.simwrapper.Header;
import org.matsim.simwrapper.Layout;
import org.matsim.simwrapper.viz.ColorScheme;
import org.matsim.simwrapper.viz.Plotly;
import org.matsim.simwrapper.viz.Table;
import tech.tablesaw.plotly.components.Axis;
Expand Down Expand Up @@ -95,7 +96,7 @@ public void configure(Header header, Layout layout) {
first.el(Plotly.class, (viz, data) -> {

viz.title = "Trip distance distribution";
viz.colorRamp = Plotly.ColorScheme.Viridis;
viz.colorRamp = ColorScheme.Viridis;

viz.addTrace(BarTrace.builder(Plotly.OBJ_INPUT, Plotly.INPUT).name("Simulated").build(),
viz.addDataset(data.compute(TripAnalysis.class, "mode_share.csv", args))
Expand Down Expand Up @@ -199,7 +200,7 @@ public void configure(Header header, Layout layout) {

viz.addTrace(BarTrace.builder(Plotly.OBJ_INPUT, Plotly.INPUT).build(),
viz.addDataset(data.compute(TripAnalysis.class, "trip_purposes_by_hour.csv")).mapping()
.name("purpose", Plotly.ColorScheme.Spectral)
.name("purpose", ColorScheme.Spectral)
.x("h")
.y("departure")
);
Expand All @@ -218,7 +219,7 @@ public void configure(Header header, Layout layout) {

viz.addTrace(BarTrace.builder(Plotly.OBJ_INPUT, Plotly.INPUT).build(),
viz.addDataset(data.compute(TripAnalysis.class, "trip_purposes_by_hour.csv")).mapping()
.name("purpose", Plotly.ColorScheme.Spectral)
.name("purpose", ColorScheme.Spectral)
.x("h")
.y("arrival")
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.matsim.simwrapper.viz;

/**
* Utility class that holds some, but not all available color schemes.
* See {@link <a href="https://github.com/d3/d3-scale-chromatic/tree/main">here</a>}
*/
public final class ColorScheme {

public static final String Accent = "Accent";
public static final String Dark2 = "Dark2";
public static final String Paired = "Paired";
public static final String Pastel1 = "Pastel1";
public static final String Pastel2 = "Pastel2";
public static final String Set1 = "Set1";
public static final String Set2 = "Set2";
public static final String Set3 = "Set3";
public static final String Tableau10 = "Tableau10";
public static final String RdGy = "RdGy";
public static final String RdYlBu = "RdYlBu";
public static final String RdBu = "RdBu";
public static final String PiYG = "PiYG";
public static final String RdYlGn = "RdYlGn";
public static final String Spectral = "Spectral";
public static final String Turbo = "Turbo";
public static final String CubehelixDefault = "CubehelixDefault";
public static final String Viridis = "Viridis";
public static final String Inferne = "Inferne";
public static final String Cividis = "Cividis";
public static final String Rainbow = "Rainbow";


private ColorScheme() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.matsim.simwrapper.viz;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.Map;

/**
* The Tile plug-in creates an overview of important key figures.
*/
public class GridMap extends Viz {

/**
* The filepath containing the data.
*/
@JsonProperty(required = true)
public String file;

/**
* The projection containing the projection e.g. EPSG:25832.
*/
@JsonProperty(required = true)
public String projection;

/**
* The cellSize containing the cellSize in meters.
*/
@JsonProperty(required = false)
public Integer cellSize;

/**
* The opacity defines the opacity of the grids (between 0 and 1);
*/
@JsonProperty(required = false)
public Double opacity;

/**
* The maxHeight defines the maximum height of the "towers".
*/
@JsonProperty(required = false)
public Integer maxHeight;

private Map<String, Object> colorRamp;

public GridMap() {
super("gridmap");
}

/**
* Set the color ramp name.
*/
public GridMap setColorRamp(String ramp) {
colorRamp = Map.of("ramp", ramp);
return this;
}

/**
* Sets the full color ramps settings.
*/
public GridMap setColorRamp(String ramp, int steps, boolean reverse) {
colorRamp = Map.of("ramp", ramp, "reverse", reverse, "steps", steps);
return this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -541,37 +541,4 @@ else if (columns.containsKey(ColumnType.Y))
}
}

/**
* Utility class that holds some, but not all available color schemes.
* See {@link <a href="https://github.com/d3/d3-scale-chromatic/tree/main">here</a>}
*/
public static final class ColorScheme {

public static final String Accent = "Accent";
public static final String Dark2 = "Dark2";
public static final String Paired = "Paired";
public static final String Pastel1 = "Pastel1";
public static final String Pastel2 = "Pastel2";
public static final String Set1 = "Set1";
public static final String Set2 = "Set2";
public static final String Set3 = "Set3";
public static final String Tableau10 = "Tableau10";
public static final String RdGy = "RdGy";
public static final String RdYlBu = "RdYlBu";
public static final String RdBu = "RdBu";
public static final String PiYG = "PiYG";
public static final String RdYlGn = "RdYlGn";
public static final String Spectral = "Spectral";
public static final String Turbo = "Turbo";
public static final String CubehelixDefault = "CubehelixDefault";
public static final String Viridis = "Viridis";
public static final String Inferne = "Inferne";
public static final String Cividis = "Cividis";
public static final String Rainbow = "Rainbow";


private ColorScheme() {
}
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.matsim.simwrapper.viz;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonProperty;

/**
* The Tile plug-in creates an overview of important key figures.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void generate() {

Assertions.assertThat(out)
.isDirectoryContaining("glob:**emissions_total.csv")
.isDirectoryContaining("glob:**emissions_grid_per_day.xyt.csv");
.isDirectoryContaining("glob:**emissions_grid_per_day.csv");

}

Expand Down

0 comments on commit d3f13ba

Please sign in to comment.