Search in sources :

Example 1 with MemoryGraphSource

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

the class Routers method postGraphOverWire.

/**
 * Deserialize a graph sent with the HTTP request as POST data, associating it with the given
 * routerId.
 */
@RolesAllowed({ "ROUTERS" })
@POST
@Path("{routerId}")
@Produces({ MediaType.TEXT_PLAIN })
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
public Response postGraphOverWire(@PathParam("routerId") String routerId, @QueryParam("preEvict") @DefaultValue("true") boolean preEvict, @QueryParam("loadLevel") @DefaultValue("FULL") LoadLevel level, InputStream is) {
    if (preEvict) {
        LOG.debug("pre-evicting graph");
        otpServer.getGraphService().evictRouter(routerId);
    }
    LOG.debug("deserializing graph from POST data stream...");
    Graph graph;
    try {
        graph = Graph.load(is, level);
        GraphService graphService = otpServer.getGraphService();
        graphService.registerGraph(routerId, new MemoryGraphSource(routerId, graph));
        return Response.status(Status.CREATED).entity(graph.toString() + "\n").build();
    } catch (Exception e) {
        return Response.status(Status.BAD_REQUEST).entity(e.toString() + "\n").build();
    }
}
Also used : GraphService(org.opentripplanner.routing.services.GraphService) Graph(org.opentripplanner.routing.graph.Graph) MemoryGraphSource(org.opentripplanner.routing.impl.MemoryGraphSource) 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) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes)

Example 2 with MemoryGraphSource

use of org.opentripplanner.routing.impl.MemoryGraphSource 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 3 with MemoryGraphSource

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

the class TestIntermediatePlaces method setUp.

@BeforeClass
public static void setUp() {
    try {
        Graph graph = FakeGraph.buildGraphNoTransit();
        FakeGraph.addPerpendicularRoutes(graph);
        FakeGraph.link(graph);
        graph.index(new DefaultStreetVertexIndexFactory());
        OTPServer otpServer = new OTPServer(new CommandLineParameters(), new GraphService());
        otpServer.getGraphService().registerGraph("A", new MemoryGraphSource("A", graph));
        Router router = otpServer.getGraphService().getRouter("A");
        TestIntermediatePlaces.graphPathFinder = new GraphPathFinder(router);
        timeZone = graph.getTimeZone();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        assert false : "Could not build graph: " + e.getMessage();
    } catch (Exception e) {
        e.printStackTrace();
        assert false : "Could not add transit data: " + e.getMessage();
    }
}
Also used : GraphService(org.opentripplanner.routing.services.GraphService) CommandLineParameters(org.opentripplanner.standalone.CommandLineParameters) FakeGraph(org.opentripplanner.graph_builder.module.FakeGraph) Graph(org.opentripplanner.routing.graph.Graph) OTPServer(org.opentripplanner.standalone.OTPServer) MemoryGraphSource(org.opentripplanner.routing.impl.MemoryGraphSource) Router(org.opentripplanner.standalone.Router) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DefaultStreetVertexIndexFactory(org.opentripplanner.routing.impl.DefaultStreetVertexIndexFactory) GraphPathFinder(org.opentripplanner.routing.impl.GraphPathFinder) UnsupportedEncodingException(java.io.UnsupportedEncodingException) BeforeClass(org.junit.BeforeClass)

Example 4 with MemoryGraphSource

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

the class TestOpenStreetMapGraphBuilder method testBuildingAreas.

/**
 * This reads test file with area
 * and tests if it can be routed if visibility is used and if it isn't
 *
 * Routing needs to be successful in both options since without visibility calculation
 * area rings are used.
 * @param skipVisibility if true visibility calculations are skipped
 * @throws UnsupportedEncodingException
 */
private void testBuildingAreas(boolean skipVisibility) throws UnsupportedEncodingException {
    Graph gg = new Graph();
    OpenStreetMapModule loader = new OpenStreetMapModule();
    loader.skipVisibility = skipVisibility;
    loader.setDefaultWayPropertySetSource(new DefaultWayPropertySetSource());
    FileBasedOpenStreetMapProviderImpl provider = new FileBasedOpenStreetMapProviderImpl();
    File file = new File(URLDecoder.decode(getClass().getResource("usf_area.osm.gz").getFile(), "UTF-8"));
    provider.setPath(file);
    loader.setProvider(provider);
    loader.buildGraph(gg, extra);
    new StreetVertexIndexServiceImpl(gg);
    OTPServer otpServer = new OTPServer(new CommandLineParameters(), new GraphService());
    otpServer.getGraphService().registerGraph("A", new MemoryGraphSource("A", gg));
    Router a = otpServer.getGraphService().getRouter("A");
    RoutingRequest request = new RoutingRequest("WALK");
    // This are vertices that can be connected only over edges on area (with correct permissions)
    // It tests if it is possible to route over area without visibility calculations
    Vertex bottomV = gg.getVertex("osm:node:580290955");
    Vertex topV = gg.getVertex("osm:node:559271124");
    request.setRoutingContext(a.graph, bottomV, topV);
    GraphPathFinder graphPathFinder = new GraphPathFinder(a);
    List<GraphPath> pathList = graphPathFinder.graphPathFinderEntryPoint(request);
    assertNotNull(pathList);
    assertFalse(pathList.isEmpty());
    for (GraphPath path : pathList) {
        assertFalse(path.states.isEmpty());
    }
}
Also used : Vertex(org.opentripplanner.routing.graph.Vertex) IntersectionVertex(org.opentripplanner.routing.vertextype.IntersectionVertex) CommandLineParameters(org.opentripplanner.standalone.CommandLineParameters) FileBasedOpenStreetMapProviderImpl(org.opentripplanner.openstreetmap.impl.FileBasedOpenStreetMapProviderImpl) GraphPath(org.opentripplanner.routing.spt.GraphPath) Router(org.opentripplanner.standalone.Router) GraphService(org.opentripplanner.routing.services.GraphService) Graph(org.opentripplanner.routing.graph.Graph) OTPServer(org.opentripplanner.standalone.OTPServer) MemoryGraphSource(org.opentripplanner.routing.impl.MemoryGraphSource) RoutingRequest(org.opentripplanner.routing.core.RoutingRequest) File(java.io.File) GraphPathFinder(org.opentripplanner.routing.impl.GraphPathFinder) StreetVertexIndexServiceImpl(org.opentripplanner.routing.impl.StreetVertexIndexServiceImpl)

Example 5 with MemoryGraphSource

use of org.opentripplanner.routing.impl.MemoryGraphSource 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)

Aggregations

Graph (org.opentripplanner.routing.graph.Graph)7 MemoryGraphSource (org.opentripplanner.routing.impl.MemoryGraphSource)7 GraphService (org.opentripplanner.routing.services.GraphService)6 CommandLineParameters (org.opentripplanner.standalone.CommandLineParameters)5 OTPServer (org.opentripplanner.standalone.OTPServer)4 DefaultStreetVertexIndexFactory (org.opentripplanner.routing.impl.DefaultStreetVertexIndexFactory)3 File (java.io.File)2 RolesAllowed (javax.annotation.security.RolesAllowed)2 Consumes (javax.ws.rs.Consumes)2 POST (javax.ws.rs.POST)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 Test (org.junit.Test)2 RouterInfo (org.opentripplanner.api.model.RouterInfo)2 GraphBuilder (org.opentripplanner.graph_builder.GraphBuilder)2 GraphNotFoundException (org.opentripplanner.routing.error.GraphNotFoundException)2 GraphPathFinder (org.opentripplanner.routing.impl.GraphPathFinder)2 Router (org.opentripplanner.standalone.Router)2 ParameterException (com.beust.jcommander.ParameterException)1