Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strategy for removing unused stops #85

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Stop> usedStops = new HashSet<>();
HashSet<Stop> stopsToRemove = new HashSet<>();

for (StopTime stopTime : dao.getAllStopTimes()) {
usedStops.add(stopTime.getStop());
}

Collection<Stop> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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());

}

}
10 changes: 10 additions & 0 deletions src/site/apt/onebusaway-gtfs-transformer-cli.apt.vm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down