Migrate Copenhagen to auto-complete and query for nearby stations via getstops endpoint.

This commit is contained in:
Andreas Schildbach 2014-07-07 12:43:03 +02:00
parent b265dd03cb
commit 7ddac5813d
2 changed files with 4 additions and 91 deletions

View file

@ -417,73 +417,6 @@ public abstract class AbstractHafasProvider extends AbstractNetworkProvider
}
}
protected final List<Location> xmlLocationList(final String uri) throws IOException
{
Reader reader = null;
try
{
reader = new InputStreamReader(ParserUtils.scrapeInputStream(uri), UTF_8);
final XmlPullParserFactory factory = XmlPullParserFactory.newInstance(System.getProperty(XmlPullParserFactory.PROPERTY_NAME), null);
final XmlPullParser pp = factory.newPullParser();
pp.setInput(reader);
final List<Location> results = new ArrayList<Location>();
pp.require(XmlPullParser.START_DOCUMENT, null, null);
pp.next();
XmlPullUtil.enter(pp, "LocationList");
while (XmlPullUtil.test(pp, "StopLocation") || XmlPullUtil.test(pp, "CoordLocation"))
{
final String name = ParserUtils.resolveEntities(XmlPullUtil.attr(pp, "name"));
final int lon = XmlPullUtil.intAttr(pp, "x");
final int lat = XmlPullUtil.intAttr(pp, "y");
if (XmlPullUtil.test(pp, "StopLocation"))
{
final String id = XmlPullUtil.attr(pp, "id");
final String[] placeAndName = splitPlaceAndName(name);
results.add(new Location(LocationType.STATION, id, lat, lon, placeAndName[0], placeAndName[1]));
}
else
{
final String type = XmlPullUtil.attr(pp, "type");
if ("POI".equals(type))
results.add(new Location(LocationType.POI, null, lat, lon, null, name));
else if ("ADR".equals(type))
results.add(new Location(LocationType.ADDRESS, null, lat, lon, null, name));
else
throw new IllegalStateException("unknown type " + type + " on " + uri);
}
if (pp.isEmptyElementTag())
{
XmlPullUtil.next(pp);
}
else
{
XmlPullUtil.enter(pp);
XmlPullUtil.exit(pp);
}
}
XmlPullUtil.exit(pp, "LocationList");
return results;
}
catch (final XmlPullParserException x)
{
throw new RuntimeException(x);
}
finally
{
if (reader != null)
reader.close();
}
}
private static final Pattern P_XML_MLC_REQ_ID = Pattern.compile(".*?@L=0*(\\d+)@.*?");
private static final Pattern P_XML_MLC_REQ_LONLAT = Pattern.compile(".*?@X=(-?\\d+)@Y=(-?\\d+)@.*?");

View file

@ -19,15 +19,12 @@ package de.schildbach.pte;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import de.schildbach.pte.dto.Location;
import de.schildbach.pte.dto.LocationType;
import de.schildbach.pte.dto.NearbyStationsResult;
import de.schildbach.pte.dto.Product;
import de.schildbach.pte.dto.QueryDeparturesResult;
import de.schildbach.pte.util.ParserUtils;
/**
* @author Andreas Schildbach
@ -42,7 +39,7 @@ public class DsbProvider extends AbstractHafasProvider
public DsbProvider()
{
super(API_BASE + "stboard.exe/mn", null, API_BASE + "query.exe/dn", 11);
super(API_BASE + "stboard.exe/mn", API_BASE + "ajax-getstop.exe/mn", API_BASE + "query.exe/dn", 11);
}
public NetworkId id()
@ -135,21 +132,14 @@ public class DsbProvider extends AbstractHafasProvider
}
}
private static final String NEARBY_STATIONS_BY_COORDINATE_URI = "http://xmlopen.rejseplanen.dk/bin/rest.exe/stopsNearby?coordX=%d&coordY=%d";
public NearbyStationsResult queryNearbyStations(final Location location, final int maxDistance, final int maxStations) throws IOException
{
if (location.hasLocation())
{
final StringBuilder uri = new StringBuilder(String.format(Locale.ENGLISH, NEARBY_STATIONS_BY_COORDINATE_URI, location.lon, location.lat));
if (maxStations != 0)
uri.append("&maxNumber=").append(maxStations);
if (maxDistance != 0)
uri.append("&maxRadius=").append(maxDistance);
final StringBuilder uri = new StringBuilder(queryEndpoint);
uri.append(jsonNearbyStationsParameters(location, maxDistance, maxStations));
final List<Location> locations = xmlLocationList(uri.toString());
return new NearbyStationsResult(null, locations);
return jsonNearbyStations(uri.toString());
}
else if (location.type == LocationType.STATION && location.hasId())
{
@ -172,16 +162,6 @@ public class DsbProvider extends AbstractHafasProvider
return xmlQueryDepartures(uri.toString(), stationId);
}
private static final String AUTOCOMPLETE_URI = "http://xmlopen.rejseplanen.dk/bin/rest.exe/location.name?input=%s";
@Override
public List<Location> autocompleteStations(final CharSequence constraint) throws IOException
{
final String uri = String.format(Locale.ENGLISH, AUTOCOMPLETE_URI, ParserUtils.urlEncode(constraint.toString(), ISO_8859_1));
return xmlLocationList(uri);
}
@Override
public Collection<Product> defaultProducts()
{