From 16376d89472d527e768a72e8ec90f39dd0034586 Mon Sep 17 00:00:00 2001 From: "andreas.schildbach" Date: Wed, 1 Sep 2010 18:52:25 +0000 Subject: [PATCH] nearby stations git-svn-id: https://public-transport-enabler.googlecode.com/svn/trunk@122 0924bc21-9374-b0fa-ee44-9ff1593b38f0 --- src/de/schildbach/pte/MvvProvider.java | 40 +++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/src/de/schildbach/pte/MvvProvider.java b/src/de/schildbach/pte/MvvProvider.java index a3b86cd9..9fb5104a 100644 --- a/src/de/schildbach/pte/MvvProvider.java +++ b/src/de/schildbach/pte/MvvProvider.java @@ -76,9 +76,47 @@ public class MvvProvider implements NetworkProvider return names; } + private static final String NEARBY_URI = "http://efa.mvv-muenchen.de/ultralite/XML_DM_REQUEST" + + "?mode=direct&coordOutputFormat=WGS84&mergeDep=1&useAllStops=1&name_dm=%2.6f:%2.6f:WGS84&type_dm=coord&itOptionsActive=1&ptOptionsActive=1&useProxFootSearch=1&excludedMeans=checkbox"; + private static final Pattern P_NEARBY_COARSE = Pattern.compile("(.*?)"); + private static final Pattern P_NEARBY_FINE = Pattern.compile(".*?(.*?).*?.*?(.*?).*?.*?(\\d+),(\\d+).*?"); + public List nearbyStations(final double lat, final double lon, final int maxDistance, final int maxStations) throws IOException { - throw new UnsupportedOperationException(); + final String uri = String.format(NEARBY_URI, lon, lat); + final CharSequence page = ParserUtils.scrape(uri); + + final List stations = new ArrayList(); + + final Matcher mNearbyCoarse = P_NEARBY_COARSE.matcher(page); + while (mNearbyCoarse.find()) + { + final Matcher mNearbyFine = P_NEARBY_FINE.matcher(mNearbyCoarse.group(1)); + if (mNearbyFine.matches()) + { + final String sName = mNearbyFine.group(1).trim(); + final int sId = Integer.parseInt(mNearbyFine.group(2)); + final double sLon = latLonToDouble(Integer.parseInt(mNearbyFine.group(3))); + final double sLat = latLonToDouble(Integer.parseInt(mNearbyFine.group(4))); + + final Station station = new Station(sId, sName, sLat, sLon, 0, null, null); + stations.add(station); + } + else + { + throw new IllegalArgumentException("cannot parse '" + mNearbyCoarse.group(1) + "' on " + uri); + } + } + + if (maxStations == 0 || maxStations >= stations.size()) + return stations; + else + return stations.subList(0, maxStations); + } + + private static double latLonToDouble(int value) + { + return (double) value / 1000000; } public StationLocationResult stationLocation(final String stationId) throws IOException