Skip to content

Commit

Permalink
Add test provider and builder
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardehrenfried committed Jan 28, 2025
1 parent cc633fa commit 3e028ef
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
import org.opentripplanner.gtfs.graphbuilder.GtfsModule;
import org.opentripplanner.netex.NetexModule;
import org.opentripplanner.netex.configure.NetexConfigure;
import org.opentripplanner.osm.OsmProvider;
import org.opentripplanner.osm.DefaultOsmProvider;
import org.opentripplanner.osm.OsmProvider;
import org.opentripplanner.routing.api.request.preference.WalkPreferences;
import org.opentripplanner.routing.graph.Graph;
import org.opentripplanner.service.osminfo.OsmInfoGraphBuildRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import org.opentripplanner.graph_builder.issue.api.DataImportIssueStore;
import org.opentripplanner.graph_builder.model.GraphBuilderModule;
import org.opentripplanner.graph_builder.module.osm.parameters.OsmProcessingParameters;
import org.opentripplanner.osm.OsmProvider;
import org.opentripplanner.osm.DefaultOsmProvider;
import org.opentripplanner.osm.OsmProvider;
import org.opentripplanner.osm.model.OsmLevel;
import org.opentripplanner.osm.model.OsmNode;
import org.opentripplanner.osm.model.OsmWay;
Expand Down Expand Up @@ -453,7 +453,8 @@ private void buildBasicGraph() {
}

private Optional<Platform> getPlatform(OsmWay way) {
if (way.isBoardingLocation()) {
var references = way.getMultiTagValues(params.boardingAreaRefTags());
if (way.isBoardingLocation() && !references.isEmpty()) {
var nodeRefs = way.getNodeRefs();
var size = nodeRefs.size();
var nodes = new Coordinate[size];
Expand All @@ -465,8 +466,6 @@ private Optional<Platform> getPlatform(OsmWay way) {

var geometry = geometryFactory.createLineString(nodes);

var references = way.getMultiTagValues(params.boardingAreaRefTags());

return Optional.of(
new Platform(
params.edgeNamer().getNameForWay(way, "platform " + way.getId()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ public void testBuildGraphDetailed() {
}
}


@Test
public void testBuildAreaWithoutVisibility() {
testBuildingAreas(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package org.opentripplanner.graph_builder.module.osm.moduletests;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.List;
import java.util.Set;
import org.junit.jupiter.api.Test;
import org.opentripplanner.graph_builder.module.osm.OsmModule;
import org.opentripplanner.graph_builder.module.osm.moduletests._support.TestOsmProvider;
import org.opentripplanner.osm.model.OsmWay;
import org.opentripplanner.osm.wayproperty.specifier.WayTestData;
import org.opentripplanner.routing.graph.Graph;
import org.opentripplanner.service.osminfo.internal.DefaultOsmInfoGraphBuildRepository;
import org.opentripplanner.service.vehicleparking.internal.DefaultVehicleParkingRepository;
import org.opentripplanner.transit.model.framework.Deduplicator;

public class BoardingLocationTest {

/**
* There is a one-way road which is also marked as a platform in Sky Campus which crashed OSM.
*/
@Test
void oneWayPlatform() {
var way = WayTestData.platform();
way.addTag("access", "no");
way.addTag("motor_vehicle", "permissive");
way.addTag("oneway", "yes");
var provider = TestOsmProvider.of().addWay(way).build();

var graph = new Graph(new Deduplicator());
var osmInfoRepository = new DefaultOsmInfoGraphBuildRepository();
var osmModule = OsmModule
.of(provider, graph, osmInfoRepository, new DefaultVehicleParkingRepository())
.withBoardingAreaRefTags(Set.of("ref"))
.build();

osmModule.buildGraph();
var edges = List.copyOf(graph.getEdges());
assertThat(edges).hasSize(1);

var platform = osmInfoRepository.findPlatform(edges.getFirst());

assertTrue(platform.isPresent());
assertEquals(Set.of("123"), platform.get().references());
}

@Test
void skipPlatformsWithoutReferences() {
var way = new OsmWay();
way.addTag("public_transport", "platform");
var provider = TestOsmProvider.of().addWay(way).build();

var graph = new Graph(new Deduplicator());
var osmInfoRepository = new DefaultOsmInfoGraphBuildRepository();
var osmModule = OsmModule
.of(provider, graph, osmInfoRepository, new DefaultVehicleParkingRepository())
.withBoardingAreaRefTags(Set.of("ref"))
.build();

osmModule.buildGraph();
var edges = List.copyOf(graph.getEdges());
assertThat(edges).hasSize(2);

var platform = osmInfoRepository.findPlatform(edges.getFirst());
assertTrue(platform.isEmpty());
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package org.opentripplanner.graph_builder.module.osm.moduletests._support;

import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;
import org.opentripplanner._support.time.ZoneIds;
import org.opentripplanner.graph_builder.module.osm.OsmDatabase;
import org.opentripplanner.osm.OsmProvider;
import org.opentripplanner.osm.model.OsmNode;
import org.opentripplanner.osm.model.OsmWay;
import org.opentripplanner.osm.tagmapping.OsmTagMapper;
import org.opentripplanner.osm.wayproperty.WayPropertySet;

public class TestOsmProvider implements OsmProvider {

private final List<OsmWay> ways;
private final List<OsmNode> nodes;

public TestOsmProvider(List<OsmWay> ways, List<OsmNode> nodes) {
this.ways = ways.stream().peek(w -> w.setOsmProvider(this)).toList();
this.nodes = List.copyOf(nodes);
}

public static Builder of() {
return new Builder();
}

@Override
public void readOsm(OsmDatabase osmdb) {
ways.forEach(osmdb::addWay);

osmdb.doneSecondPhaseWays();

nodes.forEach(osmdb::addNode);
}

@Override
public OsmTagMapper getOsmTagMapper() {
return new OsmTagMapper();
}

@Override
public void checkInputs() {}

@Override
public WayPropertySet getWayPropertySet() {
return new WayPropertySet();
}

@Override
public ZoneId getZoneId() {
return ZoneIds.LONDON;
}

public static class Builder {

private final List<OsmWay> ways = new ArrayList<>();
private final List<OsmNode> nodes = new ArrayList<>();

public TestOsmProvider build() {
return new TestOsmProvider(ways, nodes);
}

/**
* Add a way and create nodes for the from and to coordinates.
*/
public Builder addWay(OsmWay way) {
var from = new OsmNode(1, 1);
from.setId(1);
var to = new OsmNode(1.1, 1.1);
to.setId(2);
way.getNodeRefs().add(from.getId());
way.getNodeRefs().add(to.getId());

ways.add(way);
nodes.addAll(List.of(from, to));
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,11 @@ public static OsmWithTags parkAndRide() {
way.addTag("capacity", "10");
return way;
}

public static OsmWay platform() {
var way = new OsmWay();
way.addTag("public_transport", "platform");
way.addTag("ref", "123");
return way;
}
}

0 comments on commit 3e028ef

Please sign in to comment.