Search in sources :

Example 21 with DefaultStreetVertexIndexFactory

use of org.opentripplanner.routing.impl.DefaultStreetVertexIndexFactory in project OpenTripPlanner by opentripplanner.

the class ClusterGraphBuilder method getGraph.

/**
 * Return the graph for the given unique identifier for graph builder inputs on S3.
 * If this is the same as the last graph built, just return the pre-built graph.
 * If not, build the graph from the inputs, fetching them from S3 to the local cache as needed.
 */
public synchronized Graph getGraph(String graphId) {
    LOG.info("Finding a graph for ID {}", graphId);
    if (graphId.equals(currGraphId)) {
        LOG.info("GraphID has not changed. Reusing the last graph that was built.");
        return currGraph;
    }
    // The location of the inputs that will be used to build this graph
    File graphDataDirectory = new File(GRAPH_CACHE_DIR, graphId);
    // If we don't have a local copy of the inputs, fetch graph data as a ZIP from S3 and unzip it
    if (!graphDataDirectory.exists() || graphDataDirectory.list().length == 0) {
        LOG.info("Downloading graph input files.");
        graphDataDirectory.mkdirs();
        S3Object graphDataZipObject = s3.getObject(graphBucket, graphId + ".zip");
        ZipInputStream zis = new ZipInputStream(graphDataZipObject.getObjectContent());
        try {
            ZipEntry entry;
            while ((entry = zis.getNextEntry()) != null) {
                File entryDestination = new File(graphDataDirectory, entry.getName());
                // Are both these mkdirs calls necessary?
                entryDestination.getParentFile().mkdirs();
                if (entry.isDirectory())
                    entryDestination.mkdirs();
                else {
                    OutputStream entryFileOut = new FileOutputStream(entryDestination);
                    IOUtils.copy(zis, entryFileOut);
                    entryFileOut.close();
                }
            }
            zis.close();
        } catch (Exception e) {
            // TODO delete graph cache dir which is probably corrupted
            LOG.info("Error retrieving graph files", e);
        }
    } else {
        LOG.info("Graph input files were found locally. Using these files from the cache.");
    }
    // Now we have a local copy of these graph inputs. Make a graph out of them.
    CommandLineParameters params = new CommandLineParameters();
    params.build = new File(GRAPH_CACHE_DIR, graphId);
    params.inMemory = true;
    GraphBuilder graphBuilder = GraphBuilder.forDirectory(params, params.build);
    graphBuilder.run();
    Graph graph = graphBuilder.getGraph();
    graph.routerId = graphId;
    graph.index(new DefaultStreetVertexIndexFactory());
    graph.index.clusterStopsAsNeeded();
    this.currGraphId = graphId;
    this.currGraph = graph;
    return graph;
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) CommandLineParameters(org.opentripplanner.standalone.CommandLineParameters) Graph(org.opentripplanner.routing.graph.Graph) ZipEntry(java.util.zip.ZipEntry) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) GraphBuilder(org.opentripplanner.graph_builder.GraphBuilder) S3Object(com.amazonaws.services.s3.model.S3Object) DefaultStreetVertexIndexFactory(org.opentripplanner.routing.impl.DefaultStreetVertexIndexFactory) File(java.io.File)

Aggregations

DefaultStreetVertexIndexFactory (org.opentripplanner.routing.impl.DefaultStreetVertexIndexFactory)21 Graph (org.opentripplanner.routing.graph.Graph)19 Test (org.junit.Test)11 FakeGraph (org.opentripplanner.graph_builder.module.FakeGraph)10 LocalDate (org.joda.time.LocalDate)6 File (java.io.File)5 QualifiedModeSet (org.opentripplanner.api.parameter.QualifiedModeSet)5 GraphBuilder (org.opentripplanner.graph_builder.GraphBuilder)5 ProfileRequest (org.opentripplanner.profile.ProfileRequest)5 RepeatedRaptorProfileRouter (org.opentripplanner.profile.RepeatedRaptorProfileRouter)5 TraverseModeSet (org.opentripplanner.routing.core.TraverseModeSet)5 TIntIntMap (gnu.trove.map.TIntIntMap)4 CommandLineParameters (org.opentripplanner.standalone.CommandLineParameters)4 GtfsBundle (org.opentripplanner.graph_builder.model.GtfsBundle)3 AStar (org.opentripplanner.routing.algorithm.AStar)3 RoutingRequest (org.opentripplanner.routing.core.RoutingRequest)3 Router (org.opentripplanner.standalone.Router)3 TripUpdate (com.google.transit.realtime.GtfsRealtime.TripUpdate)2 LineString (com.vividsolutions.jts.geom.LineString)2 TIntIntIterator (gnu.trove.iterator.TIntIntIterator)2