Search in sources :

Example 1 with GraphBuilder

use of org.opentripplanner.graph_builder.GraphBuilder in project OpenTripPlanner by opentripplanner.

the class RepeatedRaptorComparison method buildGraph.

private static Graph buildGraph(File directory) {
    CommandLineParameters params = new CommandLineParameters();
    params.build = directory;
    params.inMemory = true;
    GraphBuilder graphBuilder = GraphBuilder.forDirectory(params, params.build);
    graphBuilder.run();
    Graph graph = graphBuilder.getGraph();
    graph.routerId = "GRAPH";
    graph.index(new DefaultStreetVertexIndexFactory());
    graph.index.clusterStopsAsNeeded();
    return graph;
}
Also used : CommandLineParameters(org.opentripplanner.standalone.CommandLineParameters) Graph(org.opentripplanner.routing.graph.Graph) GraphBuilder(org.opentripplanner.graph_builder.GraphBuilder) DefaultStreetVertexIndexFactory(org.opentripplanner.routing.impl.DefaultStreetVertexIndexFactory)

Example 2 with GraphBuilder

use of org.opentripplanner.graph_builder.GraphBuilder in project OpenTripPlanner by opentripplanner.

the class ClusterGraphService method getRouter.

@Override
public synchronized Router getRouter(String graphId) {
    GRAPH_DIR.mkdirs();
    if (!graphMap.containsKey(graphId)) {
        try {
            if (!bucketCached(graphId)) {
                if (!workOffline) {
                    downloadGraphSourceFiles(graphId, GRAPH_DIR);
                }
            }
        } catch (IOException e) {
            LOG.error("exception finding graph {}", graphId, e);
        }
        CommandLineParameters params = new CommandLineParameters();
        params.build = new File(GRAPH_DIR, graphId);
        params.inMemory = true;
        GraphBuilder gbt = GraphBuilder.forDirectory(params, params.build);
        gbt.run();
        Graph g = gbt.getGraph();
        g.routerId = graphId;
        g.index(new DefaultStreetVertexIndexFactory());
        g.index.clusterStopsAsNeeded();
        Router r = new Router(graphId, g);
        return r;
    } else {
        return graphMap.get(graphId);
    }
}
Also used : CommandLineParameters(org.opentripplanner.standalone.CommandLineParameters) Graph(org.opentripplanner.routing.graph.Graph) Router(org.opentripplanner.standalone.Router) GraphBuilder(org.opentripplanner.graph_builder.GraphBuilder) DefaultStreetVertexIndexFactory(org.opentripplanner.routing.impl.DefaultStreetVertexIndexFactory) ZipFile(java.util.zip.ZipFile)

Example 3 with GraphBuilder

use of org.opentripplanner.graph_builder.GraphBuilder in project OpenTripPlanner by opentripplanner.

the class Routers method buildGraphOverWire.

/**
 * Build a graph from data in the ZIP file posted over the wire, associating it with the given router ID.
 * This method will be selected when the Content-Type is application/zip.
 */
@RolesAllowed({ "ROUTERS" })
@POST
@Path("{routerId}")
@Consumes({ "application/zip" })
@Produces({ MediaType.TEXT_PLAIN })
public Response buildGraphOverWire(@PathParam("routerId") String routerId, @QueryParam("preEvict") @DefaultValue("true") boolean preEvict, InputStream input) {
    if (preEvict) {
        LOG.debug("Pre-evicting graph with routerId {} before building new graph", routerId);
        otpServer.getGraphService().evictRouter(routerId);
    }
    // get a temporary directory, using Google Guava
    File tempDir = Files.createTempDir();
    // extract the zip file to the temp dir
    ZipInputStream zis = new ZipInputStream(input);
    try {
        for (ZipEntry entry = zis.getNextEntry(); entry != null; entry = zis.getNextEntry()) {
            if (entry.isDirectory())
                // we only support flat ZIP files
                return Response.status(Response.Status.BAD_REQUEST).entity("ZIP files containing directories are not supported").build();
            File file = new File(tempDir, entry.getName());
            if (!file.getParentFile().equals(tempDir))
                return Response.status(Response.Status.BAD_REQUEST).entity("ZIP files containing directories are not supported").build();
            OutputStream os = new FileOutputStream(file);
            ByteStreams.copy(zis, os);
            os.close();
        }
    } catch (Exception ex) {
        return Response.status(Response.Status.BAD_REQUEST).entity("Could not extract zip file: " + ex.getMessage()).build();
    }
    // set up the build, using default parameters
    // this is basically simulating calling otp -b on the command line
    CommandLineParameters params = otpServer.params.clone();
    params.build = tempDir;
    params.inMemory = true;
    GraphBuilder graphBuilder = GraphBuilder.forDirectory(params, tempDir);
    graphBuilder.run();
    // so we'll crash long before we get here . . .
    for (File file : tempDir.listFiles()) {
        file.delete();
    }
    tempDir.delete();
    Graph graph = graphBuilder.getGraph();
    graph.index(new DefaultStreetVertexIndexFactory());
    GraphService graphService = otpServer.getGraphService();
    graphService.registerGraph(routerId, new MemoryGraphSource(routerId, graph));
    return Response.status(Status.CREATED).entity(graph.toString() + "\n").build();
}
Also used : GraphService(org.opentripplanner.routing.services.GraphService) ZipInputStream(java.util.zip.ZipInputStream) CommandLineParameters(org.opentripplanner.standalone.CommandLineParameters) Graph(org.opentripplanner.routing.graph.Graph) MemoryGraphSource(org.opentripplanner.routing.impl.MemoryGraphSource) ZipEntry(java.util.zip.ZipEntry) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) GraphBuilder(org.opentripplanner.graph_builder.GraphBuilder) DefaultStreetVertexIndexFactory(org.opentripplanner.routing.impl.DefaultStreetVertexIndexFactory) File(java.io.File) GraphNotFoundException(org.opentripplanner.routing.error.GraphNotFoundException) WebApplicationException(javax.ws.rs.WebApplicationException) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 4 with GraphBuilder

use of org.opentripplanner.graph_builder.GraphBuilder in project OpenTripPlanner by opentripplanner.

the class OTPMain method run.

/**
 * Making OTPMain a concrete class and placing this logic an instance method instead of embedding it in the static
 * main method makes it possible to build graphs from web services or scripts, not just from the command line.
 */
public void run() {
    // TODO do params.infer() here to ensure coherency?
    /* Create the top-level objects that represent the OTP server. */
    makeGraphService();
    otpServer = new OTPServer(params, graphService);
    /* Start graph builder if requested */
    if (params.build != null) {
        // TODO multiple directories
        GraphBuilder graphBuilder = GraphBuilder.forDirectory(params, params.build);
        if (graphBuilder != null) {
            graphBuilder.run();
            /* If requested, hand off the graph to the server as the default graph using an in-memory GraphSource. */
            if (params.inMemory || params.preFlight) {
                Graph graph = graphBuilder.getGraph();
                graph.index(new DefaultStreetVertexIndexFactory());
                // FIXME set true router IDs
                graphService.registerGraph("", new MemoryGraphSource("", graph));
            }
        } else {
            LOG.error("An error occurred while building the graph. Exiting.");
            System.exit(-1);
        }
    }
    // FIXME eventually router IDs will be present even when just building a graph.
    if ((params.routerIds != null && params.routerIds.size() > 0) || params.autoScan) {
        /* Auto-register pre-existing graph on disk, with optional auto-scan. */
        GraphScanner graphScanner = new GraphScanner(graphService, params.graphDirectory, params.autoScan);
        graphScanner.basePath = params.graphDirectory;
        if (params.routerIds != null && params.routerIds.size() > 0) {
            graphScanner.defaultRouterId = params.routerIds.get(0);
        }
        graphScanner.autoRegister = params.routerIds;
        graphScanner.startup();
    }
    /* Start visualizer if requested */
    if (params.visualize) {
        Router defaultRouter = graphService.getRouter();
        defaultRouter.graphVisualizer = new GraphVisualizer(defaultRouter);
        defaultRouter.graphVisualizer.run();
        // avoid timeouts due to search animation
        defaultRouter.timeouts = new double[] { 60 };
    }
    /* Start script if requested */
    if (params.scriptFile != null) {
        try {
            OTPScript otpScript = new BSFOTPScript(otpServer, params.scriptFile);
            if (otpScript != null) {
                Object retval = otpScript.run();
                if (retval != null) {
                    LOG.warn("Your script returned something, no idea what to do with it: {}", retval);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    /* Start web server if requested */
    if (params.server) {
        GrizzlyServer grizzlyServer = new GrizzlyServer(params, otpServer);
        while (true) {
            // Loop to restart server on uncaught fatal exceptions.
            try {
                grizzlyServer.run();
                return;
            } catch (Throwable throwable) {
                LOG.error("An uncaught {} occurred inside OTP. Restarting server.", throwable.getClass().getSimpleName(), throwable);
            }
        }
    }
}
Also used : GraphVisualizer(org.opentripplanner.visualizer.GraphVisualizer) BSFOTPScript(org.opentripplanner.scripting.impl.BSFOTPScript) BSFOTPScript(org.opentripplanner.scripting.impl.BSFOTPScript) OTPScript(org.opentripplanner.scripting.impl.OTPScript) GraphScanner(org.opentripplanner.routing.impl.GraphScanner) DefaultStreetVertexIndexFactory(org.opentripplanner.routing.impl.DefaultStreetVertexIndexFactory) ParameterException(com.beust.jcommander.ParameterException) FileNotFoundException(java.io.FileNotFoundException) Graph(org.opentripplanner.routing.graph.Graph) MemoryGraphSource(org.opentripplanner.routing.impl.MemoryGraphSource) GraphBuilder(org.opentripplanner.graph_builder.GraphBuilder)

Example 5 with GraphBuilder

use of org.opentripplanner.graph_builder.GraphBuilder 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

GraphBuilder (org.opentripplanner.graph_builder.GraphBuilder)5 Graph (org.opentripplanner.routing.graph.Graph)5 DefaultStreetVertexIndexFactory (org.opentripplanner.routing.impl.DefaultStreetVertexIndexFactory)5 CommandLineParameters (org.opentripplanner.standalone.CommandLineParameters)4 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 ZipEntry (java.util.zip.ZipEntry)2 ZipInputStream (java.util.zip.ZipInputStream)2 MemoryGraphSource (org.opentripplanner.routing.impl.MemoryGraphSource)2 S3Object (com.amazonaws.services.s3.model.S3Object)1 ParameterException (com.beust.jcommander.ParameterException)1 FileNotFoundException (java.io.FileNotFoundException)1 ZipFile (java.util.zip.ZipFile)1 RolesAllowed (javax.annotation.security.RolesAllowed)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 WebApplicationException (javax.ws.rs.WebApplicationException)1