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;
}
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);
}
}
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();
}
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);
}
}
}
}
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;
}
Aggregations