mirror of
https://gitlab.com/oeffi/public-transport-enabler.git
synced 2025-07-07 15:28:49 +00:00
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:
parent
bb548b6adb
commit
dfb68c5e03
3 changed files with 77 additions and 9 deletions
|
@ -991,6 +991,38 @@ public abstract class AbstractHafasProvider implements NetworkProvider
|
|||
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_FINE_COORDS = Pattern
|
||||
.compile("REQMapRoute0\\.Location0\\.X=(-?\\d+)&(?:amp;)?REQMapRoute0\\.Location0\\.Y=(-?\\d+)&");
|
||||
|
|
|
@ -54,6 +54,20 @@ public class ZvvProvider extends AbstractHafasProvider
|
|||
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
|
||||
{
|
||||
return xmlMLcReq(constraint);
|
||||
|
@ -70,16 +84,30 @@ public class ZvvProvider extends AbstractHafasProvider
|
|||
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
|
||||
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 xmlNearbyStations(uri.toString());
|
||||
return jsonNearbyStations(uri.toString());
|
||||
}
|
||||
else
|
||||
{
|
||||
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");
|
||||
|
||||
return xmlNearbyStations(uri.toString());
|
||||
}
|
||||
}
|
||||
|
||||
private static final Pattern P_NORMALIZE_LINE_AND_TYPE = Pattern.compile("([^#]*)#(.*)");
|
||||
|
|
|
@ -55,13 +55,21 @@ public class ZvvProviderLiveTest
|
|||
}
|
||||
|
||||
@Test
|
||||
public void nearbyStation() throws Exception
|
||||
public void nearbyStationsByStation() throws Exception
|
||||
{
|
||||
final NearbyStationsResult result = provider.nearbyStations("183400", 0, 0, 0, 0);
|
||||
|
||||
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
|
||||
public void queryDepartures() throws Exception
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue