Skip to content

Commit

Permalink
BART: New provider for the Bay Area Rapid Transit (San Francisco, USA).
Browse files Browse the repository at this point in the history
  • Loading branch information
schildbach committed Oct 17, 2019
1 parent a465ded commit b8d1d5b
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/de/schildbach/pte/AbstractHafasProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ protected final Set<Product> intToProducts(int value) {

protected static final Pattern P_SPLIT_NAME_FIRST_COMMA = Pattern.compile("([^,]*), (.*)");
protected static final Pattern P_SPLIT_NAME_LAST_COMMA = Pattern.compile("(.*), ([^,]*)");
protected static final Pattern P_SPLIT_NAME_NEXT_TO_LAST_COMMA = Pattern.compile("(.*), ([^,]*, [^,]*)");
protected static final Pattern P_SPLIT_NAME_PAREN = Pattern.compile("(.*) \\((.{3,}?)\\)");

protected String[] splitStationName(final String name) {
Expand Down
72 changes: 72 additions & 0 deletions src/de/schildbach/pte/BartProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package de.schildbach.pte;

import de.schildbach.pte.dto.Product;
import okhttp3.HttpUrl;

import java.util.regex.Matcher;

/**
* Provider implementation for the Bay Area Rapid Transit (San Francisco, USA).
*
* @author Andreas Schildbach
*/
public class BartProvider extends AbstractHafasClientInterfaceProvider {
private static final HttpUrl API_BASE = HttpUrl.parse("https://planner.bart.gov/bin/");
private static final Product[] PRODUCTS_MAP = { null, null, Product.CABLECAR, Product.REGIONAL_TRAIN, null,
Product.BUS, Product.FERRY, Product.SUBURBAN_TRAIN, Product.TRAM };
private static final String DEFAULT_API_CLIENT = "{\"id\":\"BART\",\"type\":\"WEB\"}";

public BartProvider(final String apiAuthorization) {
this(DEFAULT_API_CLIENT, apiAuthorization);
}

public BartProvider(final String apiClient, final String apiAuthorization) {
super(NetworkId.BART, API_BASE, PRODUCTS_MAP);
setTimeZone("America/Los_Angeles");
setApiVersion("1.18");
setApiClient(apiClient);
setApiAuthorization(apiAuthorization);
httpClient.setTrustAllCertificates(true);
}

@Override
protected String[] splitStationName(final String name) {
final Matcher m = P_SPLIT_NAME_LAST_COMMA.matcher(name);
if (m.matches())
return new String[] { m.group(2), m.group(1) };
return super.splitStationName(name);
}

@Override
protected String[] splitPOI(final String name) {
final Matcher m = P_SPLIT_NAME_LAST_COMMA.matcher(name);
if (m.matches())
return new String[] { m.group(2), m.group(1) };
return super.splitPOI(name);
}

@Override
protected String[] splitAddress(final String address) {
final Matcher m = P_SPLIT_NAME_NEXT_TO_LAST_COMMA.matcher(address);
if (m.matches())
return new String[] { m.group(2), m.group(1) };
return super.splitAddress(address);
}
}
2 changes: 1 addition & 1 deletion src/de/schildbach/pte/NetworkId.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public enum NetworkId {
DUB,

// United States
RTACHICAGO, OREGON, MASSACHUSETTS, CMTA,
BART, RTACHICAGO, OREGON, MASSACHUSETTS, CMTA,

// Canada
ONTARIO, QUEBEC, BRITISHCOLUMBIA,
Expand Down
110 changes: 110 additions & 0 deletions test/de/schildbach/pte/live/BartProviderLiveTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package de.schildbach.pte.live;

import de.schildbach.pte.BartProvider;
import de.schildbach.pte.dto.Location;
import de.schildbach.pte.dto.LocationType;
import de.schildbach.pte.dto.NearbyLocationsResult;
import de.schildbach.pte.dto.Point;
import de.schildbach.pte.dto.QueryDeparturesResult;
import de.schildbach.pte.dto.QueryTripsResult;
import de.schildbach.pte.dto.SuggestLocationsResult;
import org.junit.Test;

import java.util.Date;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
* @author Andreas Schildbach
*/
public class BartProviderLiveTest extends AbstractProviderLiveTest {
public BartProviderLiveTest() {
super(new BartProvider(secretProperty("bart.api_authorization")));
}

@Test
public void nearbyStationsByCoordinate() throws Exception {
final NearbyLocationsResult result = queryNearbyStations(Location.coord(Point.fromDouble(37.7928550, -122.3968986)));
print(result);
}

@Test
public void queryDepartures() throws Exception {
final QueryDeparturesResult result = queryDepartures("100013295", false);
print(result);
}

@Test
public void queryDeparturesInvalidStation() throws Exception {
final QueryDeparturesResult result = queryDepartures("999999", false);
assertEquals(QueryDeparturesResult.Status.INVALID_STATION, result.status);
}

@Test
public void suggestLocations() throws Exception {
final SuggestLocationsResult result = suggestLocations("Airport");
print(result);
}

@Test
public void suggestLocationsIdentified() throws Exception {
final SuggestLocationsResult result = suggestLocations("Embarcadero BART Station, San Francisco");
print(result);
}

@Test
public void shortTrip() throws Exception {
final Location from = new Location(LocationType.STATION, "100028458", null, "Airport Plaza, Concord");
final Location to = new Location(LocationType.STATION, "100013295", null, "Embarcadero BART Station, San Francisco");
final QueryTripsResult result = queryTrips(from, null, to, new Date(), true, null);
print(result);
assertEquals(QueryTripsResult.Status.OK, result.status);
assertTrue(result.trips.size() > 0);
final QueryTripsResult laterResult = queryMoreTrips(result.context, true);
print(laterResult);
final QueryTripsResult later2Result = queryMoreTrips(laterResult.context, true);
print(later2Result);
final QueryTripsResult later3Result = queryMoreTrips(later2Result.context, true);
print(later3Result);
final QueryTripsResult later4Result = queryMoreTrips(later3Result.context, true);
print(later4Result);
final QueryTripsResult later5Result = queryMoreTrips(later4Result.context, true);
print(later5Result);
final QueryTripsResult later6Result = queryMoreTrips(later5Result.context, true);
print(later6Result);
final QueryTripsResult earlierResult = queryMoreTrips(result.context, false);
print(earlierResult);
final QueryTripsResult earlier2Result = queryMoreTrips(earlierResult.context, false);
print(earlier2Result);
final QueryTripsResult earlier3Result = queryMoreTrips(earlier2Result.context, false);
print(earlier3Result);
final QueryTripsResult earlier4Result = queryMoreTrips(earlier3Result.context, false);
print(earlier4Result);
}

@Test
public void tripBetweenCoordinates() throws Exception {
final Location from = Location.coord(Point.fromDouble(37.7927820, -122.3969430)); // Embarcadero BART Station, San Francisco
final Location to = Location.coord(Point.fromDouble(37.9793260, -122.0541840)); // Airport Plaza, Concord
final QueryTripsResult result = queryTrips(from, null, to, new Date(), true, null);
print(result);
}
}
1 change: 1 addition & 0 deletions test/de/schildbach/pte/live/secrets.properties.template
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ sncb.api_authorization =
dsb.api_authorization =
se.api_authorization =
lu.api_authorization =
bart.api_authorization =

0 comments on commit b8d1d5b

Please sign in to comment.