Zürich departures

git-svn-id: https://public-transport-enabler.googlecode.com/svn/trunk@592 0924bc21-9374-b0fa-ee44-9ff1593b38f0
This commit is contained in:
andreas.schildbach@gmail.com 2011-05-03 14:01:41 +00:00
parent 82413fa953
commit 58cddabbd6
5 changed files with 296 additions and 11 deletions

View file

@ -273,7 +273,7 @@ public abstract class AbstractHafasProvider implements NetworkProvider
Pattern.DOTALL);
private static final Pattern P_XML_QUERY_DEPARTURES_FINE = Pattern.compile("" //
+ "fpTime\\s*=\"(\\d{1,2}:\\d{2})\"\\s*" // time
+ "fpDate\\s*=\"(\\d{2}\\.\\d{2}\\.\\d{2})\"\\s*" // date
+ "fpDate\\s*=\"(\\d{2}\\.\\d{2}\\.\\d{2}|\\d{4}-\\d{2}-\\d{2})\"\\s*" // date
+ "delay\\s*=\"(?:-|k\\.A\\.?|cancel|\\+?\\s*(\\d+))\"\\s*" // delay
+ "(?:e_delay\\s*=\"\\d+\"\\s*)?" // (???)
+ "(?:newpl\\s*=\"([^\"]*)\"\\s*)?" //
@ -283,6 +283,7 @@ public abstract class AbstractHafasProvider implements NetworkProvider
+ "prod\\s*=\"([^\"]*)\"\\s*" // line
+ "(?:class\\s*=\"[^\"]*\"\\s*)?" // (???)
+ "(?:dir\\s*=\"[^\"]*\"\\s*)?" // (destination)
+ "(?:capacity\\s*=\"[^\"]*\"\\s*)?" // (???)
+ "(?:depStation\\s*=\"(.*?)\"\\s*)?" //
+ "(?:delayReason\\s*=\"([^\"]*)\"\\s*)?" // message
+ "(?:is_reachable\\s*=\"([^\"]*)\"\\s*)?" // (???)
@ -329,7 +330,11 @@ public abstract class AbstractHafasProvider implements NetworkProvider
final Calendar plannedTime = new GregorianCalendar(timeZone());
plannedTime.clear();
ParserUtils.parseEuropeanTime(plannedTime, mFine.group(1));
ParserUtils.parseGermanDate(plannedTime, mFine.group(2));
final String dateStr = mFine.group(2);
if (dateStr.length() == 8)
ParserUtils.parseGermanDate(plannedTime, dateStr);
else
ParserUtils.parseIsoDate(plannedTime, dateStr);
final Calendar predictedTime;
if (mFine.group(3) != null)
@ -815,13 +820,13 @@ public abstract class AbstractHafasProvider implements NetworkProvider
throw new UnsupportedOperationException();
}
private static final Pattern P_XML_NEARBY_STATIONS_COARSE = Pattern.compile("\\G<St\\s*(.*?)/?>(?:\n|\\z)", Pattern.DOTALL);
private static final Pattern P_XML_NEARBY_STATIONS_COARSE = Pattern.compile("\\G<\\s*St\\s*(.*?)/?>(?:\n|\\z)", Pattern.DOTALL);
private static final Pattern P_XML_NEARBY_STATIONS_FINE = Pattern.compile("" //
+ "evaId=\"(\\d+)\"\\s*" // id
+ "name=\"([^\"]+)\".*?" // name
+ "x=\"(\\d+)\"\\s*" // x
+ "y=\"(\\d+)\"\\s*" // y
);
+ "(?:x=\"(\\d+)\"\\s*)?" // x
+ "(?:y=\"(\\d+)\"\\s*)?" // y
, Pattern.DOTALL);
private static final Pattern P_XML_NEARBY_STATIONS_MESSAGES = Pattern.compile("<Err code=\"([^\"]*)\" text=\"([^\"]*)\"");
protected final NearbyStationsResult xmlNearbyStations(final String uri) throws IOException
@ -855,9 +860,18 @@ public abstract class AbstractHafasProvider implements NetworkProvider
final String parsedName = ParserUtils.resolveEntities(mFine.group(2)).trim();
final int parsedLon = Integer.parseInt(mFine.group(3));
final int parsedLat = Integer.parseInt(mFine.group(4));
final int parsedLon;
final int parsedLat;
if (mFine.group(3) != null && mFine.group(4) != null)
{
parsedLon = Integer.parseInt(mFine.group(3));
parsedLat = Integer.parseInt(mFine.group(4));
}
else
{
parsedLon = 0;
parsedLat = 0;
}
stations.add(new Location(LocationType.STATION, parsedId, parsedLat, parsedLon, null, parsedName));
}
@ -931,7 +945,7 @@ public abstract class AbstractHafasProvider implements NetworkProvider
return new NearbyStationsResult(stations.subList(0, maxStations));
}
protected static final Pattern P_NORMALIZE_LINE = Pattern.compile("([A-Za-zÄÖÜäöüßáàâéèêíìîóòôúùûØ/-]+)[\\s-]*(.*)");
protected static final Pattern P_NORMALIZE_LINE = Pattern.compile("([A-Za-zßÄÅäáàâåéèêíìîÖöóòôÜüúùûØ/-]+)[\\s-]*(.*)");
protected String normalizeLine(final String type, final String line)
{
@ -1048,6 +1062,8 @@ public abstract class AbstractHafasProvider implements NetworkProvider
return 'B';
if (ucType.equals("RUF")) // Rufbus
return 'B';
if ("RFT".equals(ucType)) // Ruftaxi
return 'B';
if (ucType.equals("SEV")) // Schienen-Ersatz-Verkehr
return 'B';
if (ucType.equals("BUSSEV")) // Schienen-Ersatz-Verkehr

View file

@ -29,7 +29,7 @@ public enum NetworkId
OEBB, VOR, LINZ, SVV, VVV, IVB, STV,
// Switzerland
SBB, BVB, VBL,
SBB, BVB, VBL, ZVV,
// Belgium
SNCB,

View file

@ -0,0 +1,189 @@
/*
* Copyright 2010, 2011 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 <http://www.gnu.org/licenses/>.
*/
package de.schildbach.pte;
import java.io.IOException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import de.schildbach.pte.dto.Location;
import de.schildbach.pte.dto.NearbyStationsResult;
import de.schildbach.pte.dto.QueryDeparturesResult;
import de.schildbach.pte.util.ParserUtils;
/**
* @author Andreas Schildbach
*/
public class ZvvProvider extends AbstractHafasProvider
{
public static final NetworkId NETWORK_ID = NetworkId.ZVV;
private static final String API_BASE = "http://onlinev2.fahrplan.zvv.ch/bin/"; // http://online.fahrplan.zvv.ch/bin/
public ZvvProvider()
{
super(null, null);
}
public NetworkId id()
{
return NETWORK_ID;
}
public boolean hasCapabilities(Capability... capabilities)
{
for (final Capability capability : capabilities)
if (capability == Capability.DEPARTURES)
return true;
return false;
}
private static final String AUTOCOMPLETE_URI = API_BASE
+ "ajax-getstop.exe/dny?start=1&tpl=suggest2json&REQ0JourneyStopsS0A=255&REQ0JourneyStopsS0B=5&REQ0JourneyStopsB=12&getstop=1&noSession=yes&REQ0JourneyStopsS0G=%s?&js=true&";
private static final String ENCODING = "ISO-8859-1";
@Override
public List<Location> autocompleteStations(final CharSequence constraint) throws IOException
{
final String uri = String.format(AUTOCOMPLETE_URI, ParserUtils.urlEncode(constraint.toString(), ENCODING));
return ajaxGetStops(uri);
}
@Override
protected String nearbyStationUri(String stationId)
{
throw new UnsupportedOperationException();
}
@Override
public NearbyStationsResult nearbyStations(final String stationId, final int lat, final int lon, final int maxDistance, final int maxStations)
throws IOException
{
final StringBuilder uri = new StringBuilder(API_BASE);
uri.append("stboard.exe/dn");
uri.append("?productsFilter=1111111111");
uri.append("&boardType=dep");
uri.append("&input=").append(ParserUtils.urlEncode(stationId));
uri.append("&sTI=1&start=yes&hcount=0");
uri.append("&L=vs_java3");
// &inputTripelId=A%3d1%40O%3dCopenhagen%20Airport%40X%3d12646941%40Y%3d55629753%40U%3d86%40L%3d900000011%40B%3d1
return xmlNearbyStations(uri.toString());
}
private static final Pattern P_NORMALIZE_LINE_AND_TYPE = Pattern.compile("([^#]*)#(.*)");
@Override
protected String normalizeLine(final String line)
{
final Matcher m = P_NORMALIZE_LINE_AND_TYPE.matcher(line);
if (m.matches())
{
final String number = m.group(1).replaceAll("\\s+", " ");
final String type = m.group(2);
final char normalizedType = normalizeType(type);
if (normalizedType != 0)
return normalizedType + number;
throw new IllegalStateException("cannot normalize type " + type + " number " + number + " line " + line);
}
throw new IllegalStateException("cannot normalize line " + line);
}
private static final Pattern P_NORMALIZE_LINE_S = Pattern.compile("S\\d+");
@Override
protected char normalizeType(final String type)
{
final String ucType = type.toUpperCase();
// E-Bus: Bus, Tram oder Zug?
// TX: Ruftaxi?
if ("ICN".equals(ucType))
return 'I';
if ("D".equals(ucType))
return 'R';
if ("EXT".equals(ucType))
return 'R';
if ("S-BAHN".equals(ucType))
return 'S';
if (P_NORMALIZE_LINE_S.matcher(type).matches())
return 'S';
if ("T".equals(ucType))
return 'T';
if ("TRM".equals(ucType))
return 'T';
if ("TRM-NF".equals(ucType)) // Niederflur
return 'T';
if ("BUS-NF".equals(ucType)) // Niederflur
return 'B';
if ("TRO".equals(ucType))
return 'B';
if ("SCHIFF".equals(ucType))
return 'F';
if ("D-SCHIFF".equals(ucType))
return 'F';
if ("FAE".equals(ucType))
return 'F';
if ("BERGBAHN".equals(ucType))
return 'C';
if ("SEILBAHN".equals(ucType))
return 'C';
if ("FUN".equals(ucType)) // Standseilbahn
return 'C';
if ("GB".equals(ucType)) // Gondelbahn
return 'C';
if ("LB".equals(ucType)) // Luftseilbahn
return 'C';
if ("UNB".equals(ucType))
return '?';
final char t = normalizeCommonTypes(ucType);
if (t != 0)
return t;
return 0;
}
public QueryDeparturesResult queryDepartures(final String stationId, final int maxDepartures, final boolean equivs) throws IOException
{
final StringBuilder uri = new StringBuilder();
uri.append(API_BASE).append("stboard.exe/dn");
uri.append("?productsFilter=1111111111");
uri.append("&boardType=dep");
uri.append("&maxJourneys=50"); // ignore maxDepartures because result contains other stations
uri.append("&start=yes");
uri.append("&L=vs_java3");
uri.append("&input=").append(stationId);
return xmlQueryDepartures(uri.toString(), Integer.parseInt(stationId));
}
}

View file

@ -263,6 +263,19 @@ public final class ParserUtils
return builder.toString();
}
private static final Pattern P_ISO_DATE = Pattern.compile("(\\d{4})-(\\d{2})-(\\d{2})");
public static final void parseIsoDate(final Calendar calendar, final CharSequence str)
{
final Matcher m = P_ISO_DATE.matcher(str);
if (!m.matches())
throw new RuntimeException("cannot parse: '" + str + "'");
calendar.set(Calendar.YEAR, Integer.parseInt(m.group(3)));
calendar.set(Calendar.MONTH, Integer.parseInt(m.group(2)) - 1);
calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(m.group(3)));
}
private static final Pattern P_GERMAN_DATE = Pattern.compile("(\\d{2})[\\./](\\d{2})[\\./](\\d{2,4})");
public static final void parseGermanDate(final Calendar calendar, final CharSequence str)

View file

@ -0,0 +1,67 @@
/*
* Copyright 2010, 2011 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 <http://www.gnu.org/licenses/>.
*/
package de.schildbach.pte.live;
import java.util.List;
import org.junit.Test;
import de.schildbach.pte.ZvvProvider;
import de.schildbach.pte.dto.Location;
import de.schildbach.pte.dto.NearbyStationsResult;
import de.schildbach.pte.dto.QueryDeparturesResult;
/**
* @author Andreas Schildbach
*/
public class ZvvProviderLiveTest
{
private final ZvvProvider provider = new ZvvProvider();
@Test
public void autocomplete() throws Exception
{
final List<Location> autocompletes = provider.autocompleteStations("Flughafen");
list(autocompletes);
}
private void list(final List<Location> autocompletes)
{
System.out.print(autocompletes.size() + " ");
for (final Location autocomplete : autocompletes)
System.out.print(autocomplete.toDebugString() + " ");
System.out.println();
}
@Test
public void nearbyStation() throws Exception
{
final NearbyStationsResult result = provider.nearbyStations("183400", 0, 0, 0, 0);
System.out.println(result.stations.size() + " " + result.stations);
}
@Test
public void queryDepartures() throws Exception
{
final QueryDeparturesResult result = provider.queryDepartures("183400", 0, false);
System.out.println(result.stationDepartures);
}
}