Search in sources :

Example 6 with CommandLineParameters

use of org.opentripplanner.standalone.CommandLineParameters in project OpenTripPlanner by opentripplanner.

the class RoutersTest method getRouterInfoReturnsFirstAndLastValidDateForGraph.

@Test
public void getRouterInfoReturnsFirstAndLastValidDateForGraph() {
    final CalendarServiceData calendarService = new CalendarServiceData();
    final List<ServiceDate> serviceDates = new ArrayList<ServiceDate>() {

        {
            add(new ServiceDate(2015, 10, 1));
            add(new ServiceDate(2015, 11, 1));
        }
    };
    calendarService.putServiceDatesForServiceId(new AgencyAndId("NA", "1"), serviceDates);
    final Graph graph = new Graph();
    graph.updateTransitFeedValidity(calendarService);
    graph.expandToInclude(0, 100);
    OTPServer otpServer = new OTPServer(new CommandLineParameters(), new GraphService());
    otpServer.getGraphService().registerGraph("A", new MemoryGraphSource("A", graph));
    Routers routerApi = new Routers();
    routerApi.otpServer = otpServer;
    RouterInfo info = routerApi.getGraphId("A");
    assertNotNull(info.transitServiceStarts);
    assertNotNull(info.transitServiceEnds);
    assertTrue(info.transitServiceStarts < info.transitServiceEnds);
}
Also used : CalendarServiceData(org.onebusaway.gtfs.model.calendar.CalendarServiceData) GraphService(org.opentripplanner.routing.services.GraphService) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) CommandLineParameters(org.opentripplanner.standalone.CommandLineParameters) Graph(org.opentripplanner.routing.graph.Graph) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) OTPServer(org.opentripplanner.standalone.OTPServer) MemoryGraphSource(org.opentripplanner.routing.impl.MemoryGraphSource) RouterInfo(org.opentripplanner.api.model.RouterInfo) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 7 with CommandLineParameters

use of org.opentripplanner.standalone.CommandLineParameters in project OpenTripPlanner by opentripplanner.

the class OtpsEntryPoint method fromArgs.

/**
 * Create an OTP scripting entry point using the same command-line parameters as used by the OTP
 * application. This is useful when using scripting from an outside environment, using OTP as a
 * library. For example in jython the following script 'myscript.py':
 *
 * <pre>
 *   #!/usr/bin/jython
 *   from org.opentripplanner.scripting.api import OtpsEntryPoint
 *   otp = OtpsEntryPoint.fromArgs([ "-g", "." ])
 *   ...
 * </pre>
 *
 * One can then run jython with:
 *
 * <pre>
 *   CLASSPATH=otp.jar jython myscript.py
 * </pre>
 *
 * @param args Command-line arguments, as used by the application.
 * @return The "otp" scripting facade bean.
 * @throws Exception
 */
public static OtpsEntryPoint fromArgs(String[] args) throws Exception {
    if (args == null)
        args = new String[0];
    CommandLineParameters params = new CommandLineParameters();
    @SuppressWarnings("unused") JCommander jc = new JCommander(params, args);
    // Force analyst
    params.analyst = true;
    params.infer();
    OTPMain otpMain = new OTPMain(params);
    if (// TODO Enable this
    params.build != null)
        throw new IllegalArgumentException("Building from script is not yet supported.");
    if (// This would be weird
    params.scriptFile != null)
        throw new IllegalArgumentException("Scripting from script is not supported.");
    if (// Useless
    params.visualize)
        throw new IllegalArgumentException("Vizualizer from script is not supported.");
    if (// Why not?
    params.server)
        throw new IllegalArgumentException("Server from script is not supported.");
    otpMain.run();
    return new OtpsEntryPoint(otpMain.otpServer);
}
Also used : CommandLineParameters(org.opentripplanner.standalone.CommandLineParameters) JCommander(com.beust.jcommander.JCommander) OTPMain(org.opentripplanner.standalone.OTPMain)

Example 8 with CommandLineParameters

use of org.opentripplanner.standalone.CommandLineParameters in project OpenTripPlanner by opentripplanner.

the class RoutersTest method testRouters.

@Test
public void testRouters() {
    OTPServer otpServer = new OTPServer(new CommandLineParameters(), new GraphService());
    otpServer.getGraphService().registerGraph("", new MemoryGraphSource(null, new Graph()));
    otpServer.getGraphService().registerGraph("A", new MemoryGraphSource("", new Graph()));
    otpServer.getGraphService().getRouter("A").graph.addVertex(new ExitVertex(null, "A", 0, 0, 0));
    otpServer.getGraphService().getRouter("A").graph.addVertex(new ExitVertex(null, "B", 0, 1, 0));
    otpServer.getGraphService().getRouter("A").graph.addVertex(new ExitVertex(null, "C", 1, 1, 0));
    // this needs to be added since convex hull isn't lazy loaded anymore
    otpServer.getGraphService().getRouter("A").graph.calculateConvexHull();
    otpServer.getGraphService().getRouter("").graph.calculateConvexHull();
    // this needs to be added since it is otherwise calculated during OSM/Transit loading
    // which doesn't happen in this test
    otpServer.getGraphService().getRouter("A").graph.calculateEnvelope();
    otpServer.getGraphService().getRouter("").graph.calculateEnvelope();
    Routers routerApi = new Routers();
    routerApi.otpServer = otpServer;
    RouterList routers = routerApi.getRouterIds();
    assertEquals(2, routers.routerInfo.size());
    RouterInfo router0 = routers.routerInfo.get(0);
    RouterInfo router1 = routers.routerInfo.get(1);
    RouterInfo otherRouter;
    RouterInfo defaultRouter;
    if (router0.routerId.equals("")) {
        defaultRouter = router0;
        otherRouter = router1;
    } else {
        defaultRouter = router1;
        otherRouter = router0;
    }
    assertEquals("", defaultRouter.routerId);
    assertEquals("A", otherRouter.routerId);
    assertTrue(otherRouter.polygon.getArea() > 0);
}
Also used : GraphService(org.opentripplanner.routing.services.GraphService) ExitVertex(org.opentripplanner.routing.vertextype.ExitVertex) CommandLineParameters(org.opentripplanner.standalone.CommandLineParameters) Graph(org.opentripplanner.routing.graph.Graph) OTPServer(org.opentripplanner.standalone.OTPServer) MemoryGraphSource(org.opentripplanner.routing.impl.MemoryGraphSource) RouterList(org.opentripplanner.api.model.RouterList) RouterInfo(org.opentripplanner.api.model.RouterInfo) Test(org.junit.Test)

Example 9 with CommandLineParameters

use of org.opentripplanner.standalone.CommandLineParameters 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

CommandLineParameters (org.opentripplanner.standalone.CommandLineParameters)9 Graph (org.opentripplanner.routing.graph.Graph)8 DefaultStreetVertexIndexFactory (org.opentripplanner.routing.impl.DefaultStreetVertexIndexFactory)5 MemoryGraphSource (org.opentripplanner.routing.impl.MemoryGraphSource)5 GraphService (org.opentripplanner.routing.services.GraphService)5 GraphBuilder (org.opentripplanner.graph_builder.GraphBuilder)4 OTPServer (org.opentripplanner.standalone.OTPServer)4 File (java.io.File)3 Router (org.opentripplanner.standalone.Router)3 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 ZipEntry (java.util.zip.ZipEntry)2 ZipInputStream (java.util.zip.ZipInputStream)2 Test (org.junit.Test)2 RouterInfo (org.opentripplanner.api.model.RouterInfo)2 GraphPathFinder (org.opentripplanner.routing.impl.GraphPathFinder)2 S3Object (com.amazonaws.services.s3.model.S3Object)1 JCommander (com.beust.jcommander.JCommander)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ArrayList (java.util.ArrayList)1