Skip to content

Commit

Permalink
Merge pull request #5505 from HSLdevcom/co2
Browse files Browse the repository at this point in the history
Emissions module: Emissions.txt is not a requirement
  • Loading branch information
optionsome authored Nov 14, 2023
2 parents 5ceb712 + b7a1bd5 commit 5c4ab21
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.opentripplanner.ext.emissions;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import org.junit.jupiter.api.Test;
Expand All @@ -13,6 +14,8 @@ public class Co2EmissionsDataReaderTest {
private static final File CO2_GTFS_ZIP = RES.file("emissions-test-gtfs.zip");
private static final File CO2_GTFS = RES.file("emissions-test-gtfs/");
private static final File INVALID_CO2_GTFS = RES.file("emissions-invalid-test-gtfs/");
private static final File CO2_MISSING_GTFS_ZIP = RES.file("emissions-missing-test-gtfs.zip");
private static final File CO2_MISSING_GTFS = RES.file("emissions-missing-test-gtfs");

private Co2EmissionsDataReader co2EmissionsDataReader = new Co2EmissionsDataReader(
new DefaultDataImportIssueStore()
Expand All @@ -35,4 +38,16 @@ void testInvalidCo2EmissionsDataReading() {
var emissions = co2EmissionsDataReader.readGtfs(INVALID_CO2_GTFS);
assertEquals(0, emissions.size());
}

@Test
void testMissingCo2EmissionsZipDataReading() {
var emissions = co2EmissionsDataReader.readGtfsZip(CO2_MISSING_GTFS_ZIP);
assertTrue(emissions.isEmpty());
}

@Test
void testMissingCo2EmissionsDataReading() {
var emissions = co2EmissionsDataReader.readGtfs(CO2_MISSING_GTFS);
assertTrue(emissions.isEmpty());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package org.opentripplanner.ext.emissions;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.opentripplanner.datastore.api.FileType.GTFS;
import static org.opentripplanner.standalone.config.framework.json.JsonSupport.jsonNodeFromResource;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import java.io.File;
import java.util.Optional;
import org.junit.jupiter.api.Test;
import org.opentripplanner.datastore.api.DataSource;
import org.opentripplanner.datastore.api.FileType;
import org.opentripplanner.datastore.file.FileDataSource;
import org.opentripplanner.framework.application.OtpFileNames;
import org.opentripplanner.graph_builder.ConfiguredDataSource;
import org.opentripplanner.gtfs.graphbuilder.GtfsFeedParameters;
import org.opentripplanner.standalone.config.BuildConfig;
import org.opentripplanner.test.support.ResourceLoader;
import org.opentripplanner.transit.model.framework.FeedScopedId;

public class EmissionsModuleTest {

private final ResourceLoader RES = ResourceLoader.of(EmissionsModuleTest.class);
private final File CO2_GTFS_ZIP = RES.file("emissions-test-gtfs.zip");
private final File CO2_GTFS = RES.file("emissions-test-gtfs/");
private final String CONFIG_PATH = "standalone/config/" + OtpFileNames.BUILD_CONFIG_FILENAME;
private final BuildConfig buildConfig = new BuildConfig(
jsonNodeFromResource(CONFIG_PATH),
CONFIG_PATH,
true
);
private final Multimap<FileType, DataSource> inputData = ArrayListMultimap.create();

@Test
void testMultipleGtfsDataReading() {
inputData.put(GTFS, new FileDataSource(CO2_GTFS_ZIP, GTFS));
inputData.put(GTFS, new FileDataSource(CO2_GTFS, GTFS));
Iterable<ConfiguredDataSource<GtfsFeedParameters>> configuredDataSource = getGtfsConfiguredDatasource();
EmissionsDataModel emissionsDataModel = new EmissionsDataModel();
EmissionsModule emissionsModule = new EmissionsModule(
configuredDataSource,
buildConfig,
emissionsDataModel,
null
);
emissionsModule.buildGraph();
assertEquals(
Optional.of(0.006),
emissionsDataModel.getCO2EmissionsById(new FeedScopedId("emissionstest", "1001"))
);
assertEquals(
Optional.of(0.041),
emissionsDataModel.getCO2EmissionsById(new FeedScopedId("emissionstest1", "1002"))
);
}

private Iterable<ConfiguredDataSource<GtfsFeedParameters>> getGtfsConfiguredDatasource() {
return inputData
.get(GTFS)
.stream()
.map(it -> new ConfiguredDataSource<>(it, getGtfsFeedConfig(it)))
.toList();
}

private GtfsFeedParameters getGtfsFeedConfig(DataSource dataSource) {
return buildConfig.transitFeeds
.gtfsFeeds()
.stream()
.findFirst()
.orElse(buildConfig.gtfsDefaults.copyOf().withSource(dataSource.uri()).build());
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
feed_publisher_name,feed_publisher_url,feed_lang,feed_start_date,feed_end_date,feed_version,feed_id
emissionstest,http://www.emissionstest.fi/,fi,20230623,20230806,2023-06-23 22:42:09,emissionstest
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
feed_publisher_name,feed_publisher_url,feed_lang,feed_start_date,feed_end_date,feed_version,feed_id
emissionstest,http://www.emissionstest.fi/,fi,20230623,20230806,2023-06-23 22:42:09,emissionstest
emissionstest,http://www.emissionstest.fi/,fi,20230623,20230806,2023-06-23 22:42:09,emissionstest1
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.opentripplanner.framework.lang.Sandbox;
import org.opentripplanner.graph_builder.issue.api.DataImportIssueStore;
Expand Down Expand Up @@ -38,20 +39,23 @@ public Co2EmissionsDataReader(DataImportIssueStore issueStore) {
*/
public Map<FeedScopedId, Double> readGtfs(File directory) {
String feedId = "";
Map<FeedScopedId, Double> emissionsData = new HashMap<>();
try (InputStream feedInfoStream = new FileInputStream(directory + "/feed_info.txt")) {
feedId = readFeedId(feedInfoStream);
} catch (IOException e) {
issueStore.add("InvalidEmissionData", "Reading feed_info.txt failed.");
LOG.error("InvalidEmissionData: reading feed_info.txt failed.", e);
}
try (InputStream stream = new FileInputStream(directory + "/emissions.txt")) {
emissionsData = readEmissions(stream, feedId);
} catch (IOException e) {
issueStore.add("InvalidEmissionData", "Reading emissions.txt failed.");
LOG.error("InvalidEmissionData: reading emissions.txt failed.", e);
File feedFile = new File(directory + "/feed_info.txt");
File emissionsFile = new File(directory + "/emissions.txt");
if (feedFile.exists() && emissionsFile.exists()) {
try (InputStream feedInfoStream = new FileInputStream(feedFile)) {
feedId = readFeedId(feedInfoStream);
} catch (IOException e) {
issueStore.add("InvalidEmissionData", "Reading feed_info.txt failed.");
LOG.error("InvalidEmissionData: reading feed_info.txt failed.", e);
}
try (InputStream stream = new FileInputStream(emissionsFile)) {
return readEmissions(stream, feedId);
} catch (IOException e) {
issueStore.add("InvalidEmissionData", "Reading emissions.txt failed.");
LOG.error("InvalidEmissionData: reading emissions.txt failed.", e);
}
}
return emissionsData;
return Map.of();
}

/**
Expand All @@ -60,18 +64,21 @@ public Map<FeedScopedId, Double> readGtfs(File directory) {
* @return emissions data
*/
public Map<FeedScopedId, Double> readGtfsZip(File file) {
try {
ZipFile zipFile = new ZipFile(file, ZipFile.OPEN_READ);
String feedId = readFeedId(zipFile.getInputStream(zipFile.getEntry("feed_info.txt")));
InputStream stream = zipFile.getInputStream(zipFile.getEntry("emissions.txt"));
Map<FeedScopedId, Double> emissionsData = readEmissions(stream, feedId);
zipFile.close();
return emissionsData;
try (ZipFile zipFile = new ZipFile(file, ZipFile.OPEN_READ)) {
ZipEntry feedInfo = zipFile.getEntry("feed_info.txt");
ZipEntry emissions = zipFile.getEntry("emissions.txt");
if (emissions != null && feedInfo != null) {
String feedId = readFeedId(zipFile.getInputStream(feedInfo));
InputStream stream = zipFile.getInputStream(emissions);
Map<FeedScopedId, Double> emissionsData = readEmissions(stream, feedId);
zipFile.close();
return emissionsData;
}
} catch (IOException e) {
issueStore.add("InvalidEmissionData", "Reading emissions data failed.");
LOG.error("InvalidEmissionData: Reading emissions data failed.", e);
}
return null;
return Map.of();
}

private Map<FeedScopedId, Double> readEmissions(InputStream stream, String feedId)
Expand Down
22 changes: 11 additions & 11 deletions src/ext/java/org/opentripplanner/ext/emissions/EmissionsModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.HashMap;
import java.util.Map;
import org.opentripplanner.graph_builder.ConfiguredDataSource;
import org.opentripplanner.graph_builder.GraphBuilderDataSources;
import org.opentripplanner.graph_builder.issue.api.DataImportIssueStore;
import org.opentripplanner.graph_builder.model.GraphBuilderModule;
import org.opentripplanner.gtfs.graphbuilder.GtfsFeedParameters;
Expand All @@ -22,15 +21,14 @@
public class EmissionsModule implements GraphBuilderModule {

private static final Logger LOG = LoggerFactory.getLogger(EmissionsModule.class);
private BuildConfig config;
private EmissionsDataModel emissionsDataModel;
private GraphBuilderDataSources dataSources;
private Map<FeedScopedId, Double> emissionsData = new HashMap<>();
private final BuildConfig config;
private final EmissionsDataModel emissionsDataModel;
private final Iterable<ConfiguredDataSource<GtfsFeedParameters>> dataSources;
private final DataImportIssueStore issueStore;

@Inject
public EmissionsModule(
GraphBuilderDataSources dataSources,
Iterable<ConfiguredDataSource<GtfsFeedParameters>> dataSources,
BuildConfig config,
EmissionsDataModel emissionsDataModel,
DataImportIssueStore issueStore
Expand All @@ -48,15 +46,17 @@ public void buildGraph() {
double carAvgCo2PerKm = config.emissions.getCarAvgCo2PerKm();
double carAvgOccupancy = config.emissions.getCarAvgOccupancy();
double carAvgEmissionsPerMeter = carAvgCo2PerKm / 1000 / carAvgOccupancy;

for (ConfiguredDataSource<GtfsFeedParameters> gtfsData : dataSources.getGtfsConfiguredDatasource()) {
Map<FeedScopedId, Double> emissionsData = new HashMap<>();
for (ConfiguredDataSource<GtfsFeedParameters> gtfsData : dataSources) {
Map<FeedScopedId, Double> co2Emissions;
if (gtfsData.dataSource().name().contains(".zip")) {
emissionsData = co2EmissionsDataReader.readGtfsZip(new File(gtfsData.dataSource().uri()));
co2Emissions = co2EmissionsDataReader.readGtfsZip(new File(gtfsData.dataSource().uri()));
} else {
emissionsData = co2EmissionsDataReader.readGtfs(new File(gtfsData.dataSource().uri()));
co2Emissions = co2EmissionsDataReader.readGtfs(new File(gtfsData.dataSource().uri()));
}
emissionsData.putAll(co2Emissions);
}
this.emissionsDataModel.setCo2Emissions(this.emissionsData);
this.emissionsDataModel.setCo2Emissions(emissionsData);
this.emissionsDataModel.setCarAvgCo2PerMeter(carAvgEmissionsPerMeter);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,12 @@ static EmissionsModule provideEmissionsModule(
@Nullable EmissionsDataModel emissionsDataModel,
DataImportIssueStore issueStore
) {
return new EmissionsModule(dataSources, config, emissionsDataModel, issueStore);
return new EmissionsModule(
dataSources.getGtfsConfiguredDatasource(),
config,
emissionsDataModel,
issueStore
);
}

@Provides
Expand Down

0 comments on commit 5c4ab21

Please sign in to comment.