Search in sources :

Example 1 with WorldPath

use of im.tny.segvault.subway.WorldPath in project underlx by underlx.

the class StationLobbyFragment method trySetupMap.

private void trySetupMap(final Station station, final int[] lobbyColors, final int preselExit) {
    if (googleMap == null || !mapLayoutReady) {
        return;
    }
    if (!station.getFeatures().train) {
        googleMap.setMapStyle(MapStyleOptions.loadRawResourceStyle(getContext(), R.raw.no_train_stations_map_style));
    }
    markers.clear();
    LatLngBounds.Builder builder = new LatLngBounds.Builder();
    int curLobbyColorIdx = 0;
    Marker focusMarker = null;
    for (Lobby lobby : station.getLobbies()) {
        for (Lobby.Exit exit : lobby.getExits()) {
            LatLng pos = new LatLng(exit.worldCoord[0], exit.worldCoord[1]);
            builder.include(pos);
            Marker marker = googleMap.addMarker(new MarkerOptions().position(pos).title(String.format(getString(R.string.frag_station_lobby_name), lobby.getName())).snippet(exit.getExitsString()).icon(Util.createMapMarker(getContext(), Util.getDrawableResourceIdForExitType(exit.type, lobby.isAlwaysClosed()), lobbyColors[curLobbyColorIdx])).alpha(lobby.isAlwaysClosed() ? 0.5f : 1));
            if (exit.id == preselExit) {
                marker.showInfoWindow();
                focusMarker = marker;
            }
            markers.put(exit, marker);
        }
        curLobbyColorIdx = (curLobbyColorIdx + 1) % lobbyColors.length;
    }
    for (Line line : station.getNetwork().getLines()) {
        for (WorldPath path : line.getPaths()) {
            PolylineOptions options = new PolylineOptions().width(5).color(line.getColor()).geodesic(true);
            for (float[] point : path.getPath()) {
                options.add(new LatLng(point[0], point[1]));
            }
            googleMap.addPolyline(options);
        }
    }
    LatLngBounds bounds = builder.build();
    // offset from edges of the map in pixels
    int padding = 100;
    CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
    googleMap.moveCamera(cu);
    if (focusMarker != null) {
        googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(CameraPosition.fromLatLngZoom(focusMarker.getPosition(), googleMap.getCameraPosition().zoom)));
    }
}
Also used : MarkerOptions(com.google.android.gms.maps.model.MarkerOptions) LatLngBounds(com.google.android.gms.maps.model.LatLngBounds) Marker(com.google.android.gms.maps.model.Marker) WorldPath(im.tny.segvault.subway.WorldPath) PolylineOptions(com.google.android.gms.maps.model.PolylineOptions) Line(im.tny.segvault.subway.Line) Lobby(im.tny.segvault.subway.Lobby) LatLng(com.google.android.gms.maps.model.LatLng) CameraUpdate(com.google.android.gms.maps.CameraUpdate)

Example 2 with WorldPath

use of im.tny.segvault.subway.WorldPath in project underlx by underlx.

the class StationPOIFragment method trySetupMap.

private void trySetupMap(final Station station, final List<POI> pois, final int[] lobbyColors, final String lang) {
    if (googleMap == null || !mapLayoutReady) {
        return;
    }
    if (!station.getFeatures().train) {
        googleMap.setMapStyle(MapStyleOptions.loadRawResourceStyle(getContext(), R.raw.no_train_stations_map_style));
    }
    LatLngBounds.Builder builder = new LatLngBounds.Builder();
    int curLobbyColorIdx = 0;
    for (Lobby lobby : station.getLobbies()) {
        if (lobby.isAlwaysClosed()) {
            continue;
        }
        for (Lobby.Exit exit : lobby.getExits()) {
            LatLng pos = new LatLng(exit.worldCoord[0], exit.worldCoord[1]);
            builder.include(pos);
            googleMap.addMarker(new MarkerOptions().position(pos).title(String.format(getString(R.string.frag_station_lobby_name), lobby.getName())).snippet(exit.getExitsString()).icon(Util.createMapMarker(getContext(), Util.getDrawableResourceIdForExitType(exit.type, lobby.isAlwaysClosed()), lobbyColors[curLobbyColorIdx])).alpha(lobby.isAlwaysClosed() ? 0.5f : 1));
        }
        curLobbyColorIdx = (curLobbyColorIdx + 1) % lobbyColors.length;
    }
    markers.clear();
    for (POI poi : pois) {
        LatLng pos = new LatLng(poi.getWorldCoord()[0], poi.getWorldCoord()[1]);
        builder.include(pos);
        Marker marker = googleMap.addMarker(new MarkerOptions().position(pos).icon(getMarkerIcon(Util.getColorForPOIType(poi.getType(), getContext()))).title(poi.getNames(lang)[0]));
        markers.put(poi, marker);
    }
    for (Line line : station.getNetwork().getLines()) {
        for (WorldPath path : line.getPaths()) {
            PolylineOptions options = new PolylineOptions().width(5).color(line.getColor()).geodesic(true);
            for (float[] point : path.getPath()) {
                options.add(new LatLng(point[0], point[1]));
            }
            googleMap.addPolyline(options);
        }
    }
    googleMap.setOnInfoWindowClickListener(this);
    LatLngBounds bounds = builder.build();
    // offset from edges of the map in pixels
    int padding = 64;
    CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
    googleMap.moveCamera(cu);
}
Also used : MarkerOptions(com.google.android.gms.maps.model.MarkerOptions) LatLngBounds(com.google.android.gms.maps.model.LatLngBounds) POI(im.tny.segvault.subway.POI) Marker(com.google.android.gms.maps.model.Marker) WorldPath(im.tny.segvault.subway.WorldPath) PolylineOptions(com.google.android.gms.maps.model.PolylineOptions) Line(im.tny.segvault.subway.Line) Lobby(im.tny.segvault.subway.Lobby) LatLng(com.google.android.gms.maps.model.LatLng) CameraUpdate(com.google.android.gms.maps.CameraUpdate)

Example 3 with WorldPath

use of im.tny.segvault.subway.WorldPath 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;
}
Also used : CacheException(im.tny.segvault.disturbances.exception.CacheException) Stop(im.tny.segvault.subway.Stop) HashMap(java.util.HashMap) FileNotFoundException(java.io.FileNotFoundException) Lobby(im.tny.segvault.subway.Lobby) Network(im.tny.segvault.subway.Network) Connection(im.tny.segvault.subway.Connection) IOException(java.io.IOException) POI(im.tny.segvault.subway.POI) WorldPath(im.tny.segvault.subway.WorldPath) FileInputStream(java.io.FileInputStream) CacheException(im.tny.segvault.disturbances.exception.CacheException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Line(im.tny.segvault.subway.Line) Station(im.tny.segvault.subway.Station) BSSID(im.tny.segvault.s2ls.wifi.BSSID) Schedule(im.tny.segvault.subway.Schedule) Transfer(im.tny.segvault.subway.Transfer) HashMap(java.util.HashMap) Map(java.util.Map) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

Line (im.tny.segvault.subway.Line)3 Lobby (im.tny.segvault.subway.Lobby)3 WorldPath (im.tny.segvault.subway.WorldPath)3 CameraUpdate (com.google.android.gms.maps.CameraUpdate)2 LatLng (com.google.android.gms.maps.model.LatLng)2 LatLngBounds (com.google.android.gms.maps.model.LatLngBounds)2 Marker (com.google.android.gms.maps.model.Marker)2 MarkerOptions (com.google.android.gms.maps.model.MarkerOptions)2 PolylineOptions (com.google.android.gms.maps.model.PolylineOptions)2 POI (im.tny.segvault.subway.POI)2 CacheException (im.tny.segvault.disturbances.exception.CacheException)1 BSSID (im.tny.segvault.s2ls.wifi.BSSID)1 Connection (im.tny.segvault.subway.Connection)1 Network (im.tny.segvault.subway.Network)1 Schedule (im.tny.segvault.subway.Schedule)1 Station (im.tny.segvault.subway.Station)1 Stop (im.tny.segvault.subway.Stop)1 Transfer (im.tny.segvault.subway.Transfer)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1