diff --git a/onebusaway-gtfs-transformer/src/main/java/org/onebusaway/gtfs_transformer/updates/RemoveNonRevenueStopsStrategy.java b/onebusaway-gtfs-transformer/src/main/java/org/onebusaway/gtfs_transformer/updates/RemoveNonRevenueStopsStrategy.java index 04280aa13..5bad6f15e 100644 --- a/onebusaway-gtfs-transformer/src/main/java/org/onebusaway/gtfs_transformer/updates/RemoveNonRevenueStopsStrategy.java +++ b/onebusaway-gtfs-transformer/src/main/java/org/onebusaway/gtfs_transformer/updates/RemoveNonRevenueStopsStrategy.java @@ -54,7 +54,7 @@ public void run(TransformContext context, GtfsMutableRelationalDao dao) { } } - _log.info("removed=" + removedStopTimeCount + " total=" + totalStopTimeCount); + _log.info("removed " + removedStopTimeCount + " of " + totalStopTimeCount + " StopTimes"); UpdateLibrary.clearDaoCache(dao); } diff --git a/onebusaway-gtfs-transformer/src/main/java/org/onebusaway/gtfs_transformer/updates/RemoveUnusedStopsStrategy.java b/onebusaway-gtfs-transformer/src/main/java/org/onebusaway/gtfs_transformer/updates/RemoveUnusedStopsStrategy.java new file mode 100644 index 000000000..b69a1cc5b --- /dev/null +++ b/onebusaway-gtfs-transformer/src/main/java/org/onebusaway/gtfs_transformer/updates/RemoveUnusedStopsStrategy.java @@ -0,0 +1,76 @@ +/** + * Copyright (C) 2017 Tony Laidig + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onebusaway.gtfs_transformer.updates; + +import java.util.Collection; +import java.util.List; + +import java.util.HashSet; +import java.util.Set; + +import org.onebusaway.gtfs.model.Stop; +import org.onebusaway.gtfs.model.StopTime; +import org.onebusaway.gtfs.services.GtfsMutableRelationalDao; +import org.onebusaway.gtfs_transformer.services.GtfsTransformStrategy; +import org.onebusaway.gtfs_transformer.services.TransformContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Remove Stops that are not present in StopTimes + * @author laidig + * + */ + +public class RemoveUnusedStopsStrategy implements GtfsTransformStrategy { + private static Logger _log = LoggerFactory.getLogger(RemoveNonRevenueStopsStrategy.class); + + @Override + public void run(TransformContext context, GtfsMutableRelationalDao dao) { + + int removedStopsCount = 0; + + HashSet usedStops = new HashSet<>(); + HashSet stopsToRemove = new HashSet<>(); + + for (StopTime stopTime : dao.getAllStopTimes()) { + usedStops.add(stopTime.getStop()); + } + + Collection stops = dao.getAllStops(); + + for (Stop stop : stops){ + if (!usedStops.contains(stop)){ + // modifying the DAO at this point risks ConcurrentModificationException + stopsToRemove.add(stop); + removedStopsCount++; + } + } + + for (Stop stopToRemove : stopsToRemove ){ + dao.removeEntity(stopToRemove); + } + + _log.info("removed=" + removedStopsCount + " of " + stops.size() + " stops"); + + UpdateLibrary.clearDaoCache(dao); + } + + private boolean isNonRevenue (StopTime s){ + return (s.getDropOffType() ==1 && s.getPickupType() ==1); + } +} diff --git a/onebusaway-gtfs-transformer/src/test/java/org/onebusaway/gtfs_transformer/updates/RemoveUnusedStopsStrategyTest.java b/onebusaway-gtfs-transformer/src/test/java/org/onebusaway/gtfs_transformer/updates/RemoveUnusedStopsStrategyTest.java new file mode 100644 index 000000000..d087a8c8e --- /dev/null +++ b/onebusaway-gtfs-transformer/src/test/java/org/onebusaway/gtfs_transformer/updates/RemoveUnusedStopsStrategyTest.java @@ -0,0 +1,64 @@ +/** + * Copyright (C) 2017 Tony Laidig + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onebusaway.gtfs_transformer.updates; + +import static org.junit.Assert.*; + +import java.io.IOException; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; +import org.onebusaway.gtfs.model.StopTime; +import org.onebusaway.gtfs.model.Trip; +import org.onebusaway.gtfs.services.GtfsMutableRelationalDao; +import org.onebusaway.gtfs.services.MockGtfs; +import org.onebusaway.gtfs_transformer.services.TransformContext; + +public class RemoveUnusedStopsStrategyTest { + + + private MockGtfs _gtfs; + + @Before + public void before() throws IOException { + _gtfs = MockGtfs.create(); + } + @Test + public void test() throws IOException { + RemoveUnusedStopsStrategy _strategy = new RemoveUnusedStopsStrategy(); + _gtfs.putAgencies(1); + _gtfs.putStops(8); + _gtfs.putRoutes(1); + _gtfs.putCalendars(1, "start_date=20120903", "end_date=20121016", + "mask=1111100"); + _gtfs.putTrips(1, "r0", "sid0"); + _gtfs.putLines("stop_times.txt", + "trip_id,stop_id,stop_sequence,arrival_time,departure_time,drop_off_type,pickup_type", + "t0,s0,0,01:00:00,01:05:00,1,1", + "t0,s1,1,01:30:00,01:30:00,0,0", + "t0,s2,2,02:30:00,02:30:00,1,1", + "t0,s0,3,03:00:00,03:00:00,0,0", + "t0,s2,4,03:30:00,03:30:00,1,1"); + + GtfsMutableRelationalDao dao = _gtfs.read(); + _strategy.run(new TransformContext(), dao); + + assertEquals(3, dao.getAllStops().size()); + + } + +} diff --git a/src/site/apt/onebusaway-gtfs-transformer-cli.apt.vm b/src/site/apt/onebusaway-gtfs-transformer-cli.apt.vm index 75ea053b0..c55718810 100644 --- a/src/site/apt/onebusaway-gtfs-transformer-cli.apt.vm +++ b/src/site/apt/onebusaway-gtfs-transformer-cli.apt.vm @@ -395,6 +395,16 @@ such that the resulting schedule is semantically the same. {"op":"remove_non_revenue_stops_excluding_terminals"}} +---+ +* Remove unused stops + + Some feeds contain Stops that are not present in Stop Times. + + To remove them, apply the following transform: + ++---+ +{"op":"remove_unused_stops"} ++---+ + * Arbitrary Transform We also allow you to specify arbitrary transformations as well. Here, you specify your transformation class and we will