use of im.tny.segvault.subway.Connection in project underlx by underlx.
the class TripFragment method populatePathView.
public static void populatePathView(final Context context, final LayoutInflater inflater, final Path path, ViewGroup root, boolean showInfoIcons) {
root.removeAllViews();
List<Connection> el = path.getEdgeList();
final int stepviewPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16, context.getResources().getDisplayMetrics());
if (el.size() == 0) {
View stepview = inflater.inflate(R.layout.path_station, root, false);
FrameLayout prevLineStripeLayout = (FrameLayout) stepview.findViewById(R.id.prev_line_stripe_layout);
FrameLayout nextLineStripeLayout = (FrameLayout) stepview.findViewById(R.id.next_line_stripe_layout);
prevLineStripeLayout.setVisibility(View.GONE);
nextLineStripeLayout.setVisibility(View.GONE);
TextView timeView = (TextView) stepview.findViewById(R.id.time_view);
if (path.getManualEntry(0)) {
timeView.setVisibility(View.INVISIBLE);
} else {
timeView.setText(DateUtils.formatDateTime(context, path.getEntryTime(0).getTime(), DateUtils.FORMAT_SHOW_TIME));
}
final Station station = path.getStartVertex().getStation();
stepview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, StationActivity.class);
intent.putExtra(StationActivity.EXTRA_STATION_ID, station.getId());
intent.putExtra(StationActivity.EXTRA_NETWORK_ID, station.getNetwork().getId());
context.startActivity(intent);
}
});
RouteFragment.populateStationView(context, station, stepview, showInfoIcons, false);
stepview.setPadding(stepviewPadding, 0, stepviewPadding, 0);
root.addView(stepview);
return;
}
boolean isFirst = true;
Connection c = el.get(0);
int i = 0;
for (; i < el.size(); i++) {
c = el.get(i);
if (i == 0 && c instanceof Transfer) {
// starting with a line change? ignore
continue;
}
View stepview = inflater.inflate(R.layout.path_station, root, false);
FrameLayout prevLineStripeLayout = (FrameLayout) stepview.findViewById(R.id.prev_line_stripe_layout);
FrameLayout nextLineStripeLayout = (FrameLayout) stepview.findViewById(R.id.next_line_stripe_layout);
if (isFirst) {
Line line = c.getSource().getLine();
int lineColor = line.getColor();
prevLineStripeLayout.setVisibility(View.GONE);
nextLineStripeLayout.setBackgroundColor(lineColor);
TextView timeView = (TextView) stepview.findViewById(R.id.time_view);
if (path.getManualEntry(i)) {
timeView.setVisibility(View.INVISIBLE);
} else {
timeView.setText(DateUtils.formatDateTime(context, path.getEntryTime(i).getTime(), DateUtils.FORMAT_SHOW_TIME));
}
isFirst = false;
} else {
Line targetLine = c.getTarget().getLine();
int prevLineColor = c.getSource().getLine().getColor();
int nextLineColor = targetLine.getColor();
prevLineStripeLayout.setBackgroundColor(prevLineColor);
nextLineStripeLayout.setBackgroundColor(nextLineColor);
TextView timeView = (TextView) stepview.findViewById(R.id.time_view);
// start of the path was extended with the first step getting replaced by a transfer
if (path.getManualEntry(i) && !(c instanceof Transfer && !path.getManualEntry(i + 1))) {
timeView.setVisibility(View.INVISIBLE);
} else {
timeView.setText(DateUtils.formatDateTime(context, path.getEntryTime(i).getTime(), DateUtils.FORMAT_SHOW_TIME));
}
}
final Station station = c.getSource().getStation();
stepview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, StationActivity.class);
intent.putExtra(StationActivity.EXTRA_STATION_ID, station.getId());
intent.putExtra(StationActivity.EXTRA_NETWORK_ID, station.getNetwork().getId());
context.startActivity(intent);
}
});
RouteFragment.populateStationView(context, station, stepview, showInfoIcons, false);
ImageView crossView = (ImageView) stepview.findViewById(R.id.station_cross_image);
if (c.getSource().getStation().isAlwaysClosed()) {
crossView.setVisibility(View.VISIBLE);
}
stepview.setPadding(stepviewPadding, 0, stepviewPadding, 0);
root.addView(stepview);
if (c instanceof Transfer && i != el.size() - 1) {
c = el.get(++i);
}
}
View stepview = inflater.inflate(R.layout.path_station, root, false);
int lineColor = c.getSource().getLine().getColor();
FrameLayout prevLineStripeLayout = (FrameLayout) stepview.findViewById(R.id.prev_line_stripe_layout);
FrameLayout nextLineStripeLayout = (FrameLayout) stepview.findViewById(R.id.next_line_stripe_layout);
prevLineStripeLayout.setBackgroundColor(lineColor);
nextLineStripeLayout.setVisibility(View.GONE);
TextView timeView = (TextView) stepview.findViewById(R.id.time_view);
if (path.getManualEntry(i)) {
timeView.setVisibility(View.INVISIBLE);
} else {
timeView.setText(DateUtils.formatDateTime(context, path.getEntryTime(i).getTime(), DateUtils.FORMAT_SHOW_TIME));
}
ImageView crossView = (ImageView) stepview.findViewById(R.id.station_cross_image);
if (c.getTarget().getStation().isAlwaysClosed()) {
crossView.setVisibility(View.VISIBLE);
}
final Station station = c.getTarget().getStation();
stepview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, StationActivity.class);
intent.putExtra(StationActivity.EXTRA_STATION_ID, station.getId());
intent.putExtra(StationActivity.EXTRA_NETWORK_ID, station.getNetwork().getId());
context.startActivity(intent);
}
});
RouteFragment.populateStationView(context, station, stepview, showInfoIcons, false);
stepview.setPadding(stepviewPadding, 0, stepviewPadding, 0);
root.addView(stepview);
}
use of im.tny.segvault.subway.Connection in project underlx by underlx.
the class TopologyCache method loadNetwork.
public static Network loadNetwork(Context context, String id, String apiEndpoint) throws CacheException {
String filename = "net-" + id;
Topology t = null;
try {
FileInputStream fis = context.openFileInput(filename);
ObjectInputStream is = new ObjectInputStream(fis);
t = (Topology) is.readObject();
is.close();
fis.close();
} catch (FileNotFoundException e) {
throw new CacheException(e).addInfo("File " + filename + " not found");
} catch (IOException e) {
throw new CacheException(e).addInfo("IO exception");
} catch (ClassNotFoundException e) {
throw new CacheException(e).addInfo("Class not found");
} catch (Exception e) {
e.printStackTrace();
throw new CacheException(e).addInfo("Unforeseen exception");
}
Network net = new Network(t.network.id, t.network.mainLocale, t.network.names, t.network.typCars, t.network.holidays, t.network.timezone, t.network.newsURL);
for (API.POI poi : t.pois) {
net.addPOI(new POI(poi.id, poi.type, poi.worldCoord, poi.webURL, poi.mainLocale, poi.names));
}
for (String lineid : t.network.lines) {
API.Line l = t.lines.get(lineid);
Line line = new Line(net, l.mainLocale, l.names, new HashSet<Stop>(), l.id, l.typCars, l.order);
line.setColor(Color.parseColor("#" + l.color));
boolean isFirstStationInLine = true;
for (String sid : l.stations) {
API.Station s = t.stations.get(sid);
Station station = net.getStation(s.id);
if (station == null) {
Map<String, String> triviaURLs = new HashMap<>();
for (Map.Entry<String, String> entry : s.triviaURLs.entrySet()) {
triviaURLs.put(entry.getKey(), apiEndpoint + entry.getValue());
}
Map<String, Map<String, String>> connURLs = new HashMap<>();
for (Map.Entry<String, Map<String, String>> entry : s.connURLs.entrySet()) {
Map<String, String> urls = new HashMap<>();
for (Map.Entry<String, String> localeEntry : entry.getValue().entrySet()) {
urls.put(localeEntry.getKey(), apiEndpoint + localeEntry.getValue());
}
connURLs.put(entry.getKey(), urls);
}
station = new Station(net, s.id, s.name, s.altNames, new Station.Features(s.features.lift, s.features.bus, s.features.boat, s.features.train, s.features.airport), triviaURLs);
station.setConnectionURLs(connURLs);
// Lobbies
for (String lid : s.lobbies) {
API.Lobby alobby = t.lobbies.get(lid);
Lobby lobby = new Lobby(alobby.id, alobby.name);
for (API.Exit aexit : alobby.exits) {
Lobby.Exit exit = new Lobby.Exit(aexit.id, aexit.worldCoord, aexit.streets, aexit.type);
lobby.addExit(exit);
}
for (API.Schedule asched : alobby.schedule) {
Schedule sched = new Schedule(asched.holiday, asched.day, asched.open, asched.openTime * 1000, asched.duration * 1000);
lobby.addSchedule(sched);
}
station.addLobby(lobby);
}
// POIs
for (String poiId : s.pois) {
POI poi = net.getPOI(poiId);
if (poi != null) {
station.addPOI(poi);
}
}
}
Stop stop = new Stop(station, line);
line.addVertex(stop);
station.addVertex(stop);
if (isFirstStationInLine) {
line.setFirstStop(stop);
isFirstStationInLine = false;
}
// WiFi APs
for (API.WiFiAP w : s.wiFiAPs) {
// take line affinity into account
if (w.line.equals(line.getId())) {
WiFiLocator.addBSSIDforStop(stop, new BSSID(w.bssid));
}
}
}
for (API.Schedule asched : l.schedule) {
Schedule sched = new Schedule(asched.holiday, asched.day, asched.open, asched.openTime * 1000, asched.duration * 1000);
line.addSchedule(sched);
}
for (API.WorldPath apath : l.worldPaths) {
WorldPath path = new WorldPath(apath.id, apath.path);
line.addPath(path);
}
net.addLine(line);
}
// Connections are within stations in the same line
for (API.Connection c : t.connections) {
Set<Stop> sFrom = net.getStation(c.from).vertexSet();
Set<Stop> sTo = net.getStation(c.to).vertexSet();
Stop from = null, to = null;
for (Stop s : sFrom) {
for (Stop s2 : sTo) {
if (s.getLine().getId().equals(s2.getLine().getId())) {
from = s;
to = s2;
}
}
}
if (from != null && to != null) {
Connection newConnection = net.addEdge(from, to);
from.getLine().addEdge(from, to);
newConnection.setTimes(new Connection.Times(c.typWaitS, c.typStopS, c.typS));
newConnection.setWorldLength(c.worldLength);
net.setEdgeWeight(newConnection, c.typS);
}
}
for (API.Transfer tr : t.transfers) {
Transfer newTransfer = new Transfer();
// find stations with the right IDs for each line
Station station = net.getStation(tr.station);
for (Stop from : station.vertexSet()) {
for (Stop to : station.vertexSet()) {
if (from.getLine().getId().equals(tr.from) && to.getLine().getId().equals(tr.to)) {
net.addEdge(from, to, newTransfer);
net.setEdgeWeight(newTransfer, tr.typS);
newTransfer.setTimes(new Connection.Times(0, 0, tr.typS));
}
}
}
}
for (API.Schedule asched : t.network.schedule) {
Schedule sched = new Schedule(asched.holiday, asched.day, asched.open, asched.openTime * 1000, asched.duration * 1000);
net.addSchedule(sched);
}
net.setDatasetAuthors(t.info.authors);
net.setDatasetVersion(t.info.version);
return net;
}
use of im.tny.segvault.subway.Connection in project underlx by underlx.
the class Path method manualExtendEnd.
public void manualExtendEnd(Stop vertex) {
// start by reverting to the path with the end without user-made extensions
int edgesToRemove = 0;
while (manualEntry.get(manualEntry.size() - 1)) {
manualEntry.remove(manualEntry.size() - 1);
times.remove(times.size() - 1);
edgesToRemove++;
}
for (int i = 0; i < edgesToRemove; i++) {
edgeList.remove(edgeList.size() - 1);
}
if (edgeList.size() > 0) {
endVertex = edgeList.get(edgeList.size() - 1).getTarget();
}
// now go from the program-made end
Date time = times.get(times.size() - 1).second;
List<Connection> cs = Route.getShortestPath(graph, endVertex, vertex, new LeastHopsWeighter()).getEdgeList();
int size = cs.size();
for (int i = 0; i < size; i++) {
// the same line)
if (i == size - 1 && cs.get(i) instanceof Transfer) {
this.endVertex = cs.get(i).getSource();
return;
}
times.add(new Pair<Date, Date>(time, time));
manualEntry.add(true);
edgeList.add(cs.get(i));
}
this.endVertex = vertex;
for (OnPathChangedListener l : listeners) {
l.onPathChanged(this);
}
}
use of im.tny.segvault.subway.Connection in project underlx by underlx.
the class Route method getNextStep.
public Step getNextStep(Path currentPath) {
if (currentPath == null) {
return get(0);
}
List<Connection> cur = currentPath.getEdgeList();
List<Connection> tar = path.getEdgeList();
if (cur.size() == 0) {
return get(0);
}
// iterate over the target path until the last edge of the current path is found
Connection lastCur = cur.get(cur.size() - 1);
int tarIdx;
for (tarIdx = 0; tarIdx < tar.size(); tarIdx++) {
if (tar.get(tarIdx) == lastCur) {
break;
}
}
if (tarIdx >= tar.size()) {
// so at the moment the user is not following the instructions
if (get(0) instanceof EnterStep) {
// if user is already on a path on the right direction, do not tell him to "catch a train" he is already in
Stop direction = null;
if (path.getGraph() instanceof Network) {
direction = ((Network) path.getGraph()).getDirectionForConnection(lastCur);
} else if (path.getGraph() instanceof Line) {
direction = ((Line) path.getGraph()).getDirectionForConnection(lastCur);
}
if (direction != null && ((EnterStep) get(0)).getDirection() == direction.getStation()) {
return get(1);
}
}
return get(0);
}
// find next step
for (; tarIdx < tar.size(); tarIdx++) {
if (pathIndexToStep.get(tarIdx) != null) {
return pathIndexToStep.get(tarIdx);
}
}
return null;
}
use of im.tny.segvault.subway.Connection in project underlx by underlx.
the class MainService method getLikelyNextExit.
public Stop getLikelyNextExit(List<Connection> path, double threshold) {
if (path.size() == 0) {
return null;
}
// get the line for the latest connection
Connection last = path.get(path.size() - 1);
Line line = null;
for (Line l : getAllLines()) {
if (l.edgeSet().contains(last)) {
line = l;
break;
}
}
if (line == null) {
return null;
}
Set<Stop> alreadyVisited = new HashSet<>();
for (Connection c : path) {
alreadyVisited.add(c.getSource());
alreadyVisited.add(c.getTarget());
}
// get all the stops till the end of the line, after the given connection
// (or in the case of circular lines, all stops of the line)
Stop maxStop = null;
double max = 0;
Set<Stop> stops = new HashSet<>();
while (stops.add(last.getSource())) {
Stop curStop = last.getTarget();
if (!alreadyVisited.contains(curStop)) {
double r = getLeaveTrainFactorForStop(curStop);
if (maxStop == null || r > max) {
maxStop = curStop;
max = r;
}
}
if (line.outDegreeOf(curStop) == 1) {
break;
}
for (Connection outedge : line.outgoingEdgesOf(curStop)) {
if (!stops.contains(outedge.getTarget())) {
last = outedge;
break;
}
}
}
if (max < threshold) {
// most relevant station is not relevant enough
return null;
}
return maxStop;
}
Aggregations