BART: New provider for the Bay Area Rapid Transit (San Francisco, USA).

This commit is contained in:
Andreas Schildbach 2019-10-16 10:36:16 +02:00
parent a465ded2b9
commit b8d1d5b6d7
5 changed files with 185 additions and 1 deletions

View file

@ -123,6 +123,7 @@ public abstract class AbstractHafasProvider extends AbstractNetworkProvider {
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) {

View file

@ -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);
}
}

View file

@ -76,7 +76,7 @@ public enum NetworkId {
DUB,
// United States
RTACHICAGO, OREGON, MASSACHUSETTS, CMTA,
BART, RTACHICAGO, OREGON, MASSACHUSETTS, CMTA,
// Canada
ONTARIO, QUEBEC, BRITISHCOLUMBIA,

View file

@ -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);
}
}

View file

@ -30,3 +30,4 @@ sncb.api_authorization =
dsb.api_authorization =
se.api_authorization =
lu.api_authorization =
bart.api_authorization =