nearby stations by coordinate for Zürich

git-svn-id: https://public-transport-enabler.googlecode.com/svn/trunk@609 0924bc21-9374-b0fa-ee44-9ff1593b38f0
This commit is contained in:
andreas.schildbach@gmail.com 2011-05-07 11:08:41 +00:00
parent bb548b6adb
commit dfb68c5e03
3 changed files with 77 additions and 9 deletions

View file

@ -991,6 +991,38 @@ public abstract class AbstractHafasProvider implements NetworkProvider
return new NearbyStationsResult(stations); return new NearbyStationsResult(stations);
} }
protected NearbyStationsResult jsonNearbyStations(final String uri) throws IOException
{
final CharSequence page = ParserUtils.scrape(uri);
final List<Location> stations = new ArrayList<Location>();
try
{
final JSONObject head = new JSONObject(page.toString());
final JSONArray aStops = head.getJSONArray("stops");
for (int i = 0; i < aStops.length(); i++)
{
final JSONObject stop = aStops.optJSONObject(i);
final int id = stop.getInt("extId");
final String name = ParserUtils.resolveEntities(stop.getString("name"));
final int lat = stop.optInt("y");
final int lon = stop.optInt("x");
final String[] nameAndPlace = splitNameAndPlace(name);
stations.add(new Location(LocationType.STATION, id, lat, lon, nameAndPlace[0], nameAndPlace[1]));
}
}
catch (final JSONException x)
{
x.printStackTrace();
throw new RuntimeException("cannot parse: '" + page + "' on " + uri, x);
}
return new NearbyStationsResult(stations);
}
private final static Pattern P_NEARBY_COARSE = Pattern.compile("<tr class=\"(zebra[^\"]*)\">(.*?)</tr>", Pattern.DOTALL); private final static Pattern P_NEARBY_COARSE = Pattern.compile("<tr class=\"(zebra[^\"]*)\">(.*?)</tr>", Pattern.DOTALL);
private final static Pattern P_NEARBY_FINE_COORDS = Pattern private final static Pattern P_NEARBY_FINE_COORDS = Pattern
.compile("REQMapRoute0\\.Location0\\.X=(-?\\d+)&(?:amp;)?REQMapRoute0\\.Location0\\.Y=(-?\\d+)&"); .compile("REQMapRoute0\\.Location0\\.X=(-?\\d+)&(?:amp;)?REQMapRoute0\\.Location0\\.Y=(-?\\d+)&");

View file

@ -54,6 +54,20 @@ public class ZvvProvider extends AbstractHafasProvider
return false; return false;
} }
private static final String[] PLACES = { "Zürich" };
@Override
protected String[] splitNameAndPlace(final String name)
{
for (final String place : PLACES)
{
if (name.startsWith(place + ", "))
return new String[] { place, name.substring(place.length() + 2) };
}
return super.splitNameAndPlace(name);
}
public List<Location> autocompleteStations(final CharSequence constraint) throws IOException public List<Location> autocompleteStations(final CharSequence constraint) throws IOException
{ {
return xmlMLcReq(constraint); return xmlMLcReq(constraint);
@ -70,6 +84,21 @@ public class ZvvProvider extends AbstractHafasProvider
throws IOException throws IOException
{ {
final StringBuilder uri = new StringBuilder(API_BASE); final StringBuilder uri = new StringBuilder(API_BASE);
if (lat != 0 || lon != 0)
{
uri.append("query.exe/dny");
uri.append("?performLocating=2&tpl=stop2json");
uri.append("&look_maxno=").append(maxStations != 0 ? maxStations : 150);
uri.append("&look_maxdist=").append(maxDistance != 0 ? maxDistance : 5000);
uri.append("&look_stopclass=1023");
uri.append("&look_x=").append(lon);
uri.append("&look_y=").append(lat);
return jsonNearbyStations(uri.toString());
}
else
{
uri.append("stboard.exe/dn"); uri.append("stboard.exe/dn");
uri.append("?productsFilter=1111111111"); uri.append("?productsFilter=1111111111");
uri.append("&boardType=dep"); uri.append("&boardType=dep");
@ -77,10 +106,9 @@ public class ZvvProvider extends AbstractHafasProvider
uri.append("&sTI=1&start=yes&hcount=0"); uri.append("&sTI=1&start=yes&hcount=0");
uri.append("&L=vs_java3"); 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()); return xmlNearbyStations(uri.toString());
} }
}
private static final Pattern P_NORMALIZE_LINE_AND_TYPE = Pattern.compile("([^#]*)#(.*)"); private static final Pattern P_NORMALIZE_LINE_AND_TYPE = Pattern.compile("([^#]*)#(.*)");

View file

@ -55,13 +55,21 @@ public class ZvvProviderLiveTest
} }
@Test @Test
public void nearbyStation() throws Exception public void nearbyStationsByStation() throws Exception
{ {
final NearbyStationsResult result = provider.nearbyStations("183400", 0, 0, 0, 0); final NearbyStationsResult result = provider.nearbyStations("183400", 0, 0, 0, 0);
System.out.println(result.stations.size() + " " + result.stations); System.out.println(result.stations.size() + " " + result.stations);
} }
@Test
public void nearbyStationsByCoordinate() throws Exception
{
final NearbyStationsResult result = provider.nearbyStations(null, 47378968, 8540534, 0, 0);
System.out.println(result.stations.size() + " " + result.stations);
}
@Test @Test
public void queryDepartures() throws Exception public void queryDepartures() throws Exception
{ {