use of com.graphhopper.GraphHopper in project graphhopper by graphhopper.
the class ChangeGraphServlet method doPost.
@Override
protected void doPost(HttpServletRequest httpReq, HttpServletResponse httpRes) throws ServletException, IOException {
String infoStr = httpReq.getRemoteAddr() + " " + httpReq.getPathInfo() + " " + httpReq.getQueryString();
float took = -1;
StopWatch sw = new StopWatch().start();
try {
JsonFeatureCollection collection = ghJson.fromJson(new InputStreamReader(httpReq.getInputStream(), Helper.UTF_CS), JsonFeatureCollection.class);
// TODO put changeGraph on GraphHopperAPI interface and remove cast (or some other solution)
if (!(graphHopper instanceof GraphHopper)) {
throw new IllegalStateException("Graph change API not supported with public transit.");
}
// TODO make asynchronous!
ChangeGraphResponse rsp = ((GraphHopper) graphHopper).changeGraph(collection.getFeatures());
ObjectNode resObject = objectMapper.createObjectNode();
resObject.put("updates", rsp.getUpdateCount());
// prepare the consumer to get some changes not immediately when returning after POST
resObject.put("scheduled_updates", 0);
httpRes.setHeader("X-GH-Took", "" + Math.round(took * 1000));
writeJson(httpReq, httpRes, resObject);
took = sw.stop().getSeconds();
logger.info(infoStr + " " + took);
} catch (IllegalArgumentException ex) {
took = sw.stop().getSeconds();
logger.warn(infoStr + " " + took + ", " + ex.getMessage());
writeError(httpRes, 400, "Wrong arguments for endpoint /change, " + infoStr);
} catch (Exception ex) {
took = sw.stop().getSeconds();
logger.error(infoStr + " " + took, ex);
writeError(httpRes, 500, "Error at endpoint /change, " + infoStr);
}
}
use of com.graphhopper.GraphHopper in project graphhopper by graphhopper.
the class RoutingAlgorithmIT method testPerformance.
@Test
public void testPerformance() throws IOException {
int N = 10;
int noJvmWarming = N / 4;
Random rand = new Random(0);
final EncodingManager eManager = new EncodingManager("car");
final GraphHopperStorage graph = new GraphBuilder(eManager).create();
String bigFile = "10000EWD.txt.gz";
new PrincetonReader(graph).setStream(new GZIPInputStream(PrincetonReader.class.getResourceAsStream(bigFile))).read();
GraphHopper hopper = new GraphHopper() {
{
setCHEnabled(false);
setEncodingManager(eManager);
loadGraph(graph);
}
@Override
protected LocationIndex createLocationIndex(Directory dir) {
return new LocationIndexTree(graph, dir);
}
};
Collection<AlgoHelperEntry> prepares = createAlgos(hopper, new HintsMap().setWeighting("shortest").setVehicle("car"), TraversalMode.NODE_BASED);
for (AlgoHelperEntry entry : prepares) {
StopWatch sw = new StopWatch();
for (int i = 0; i < N; i++) {
int node1 = Math.abs(rand.nextInt(graph.getNodes()));
int node2 = Math.abs(rand.nextInt(graph.getNodes()));
RoutingAlgorithm d = entry.createRoutingFactory().createAlgo(graph, entry.getAlgorithmOptions());
if (i >= noJvmWarming)
sw.start();
Path p = d.calcPath(node1, node2);
// avoid jvm optimization => call p.distance
if (i >= noJvmWarming && p.getDistance() > -1)
sw.stop();
// System.out.println("#" + i + " " + name + ":" + sw.getSeconds() + " " + p.nodes());
}
float perRun = sw.stop().getSeconds() / ((float) (N - noJvmWarming));
System.out.println("# " + getClass().getSimpleName() + " " + entry + ":" + sw.stop().getSeconds() + ", per run:" + perRun);
assertTrue("speed to low!? " + perRun + " per run", perRun < 0.08);
}
}
use of com.graphhopper.GraphHopper in project graphhopper by graphhopper.
the class GraphHopperLandmarksIT method testLandmarkDisconnect.
@Test
public void testLandmarkDisconnect() throws Exception {
// if one algorithm is disabled then the following chain is executed: CH -> LM -> flexible
// disconnected for landmarks
JsonNode json = query("point=55.99022,29.129734&point=56.007787,29.208355&ch.disable=true", 400);
JsonNode errorJson = json.get("message");
assertTrue(errorJson.toString(), errorJson.toString().contains("Different subnetworks"));
// without landmarks it should work
GraphHopper hopper = getInstance(GraphHopper.class);
hopper.getLMFactoryDecorator().setDisablingAllowed(true);
json = query("point=55.99022,29.129734&point=56.007787,29.208355&ch.disable=true&lm.disable=true", 200);
JsonNode path = json.get("paths").get(0);
double distance = path.get("distance").asDouble();
assertEquals("distance wasn't correct:" + distance, 5790, distance, 100);
hopper.getLMFactoryDecorator().setDisablingAllowed(false);
}
use of com.graphhopper.GraphHopper in project massim by agentcontest.
the class GraphHopperManager method init.
/**
* Creates a new GraphHopper for the given map name.
* @param newMapName the name of the map to load
*/
public static void init(String newMapName) {
if (newMapName.equals(mapName))
return;
mapName = newMapName;
hopper = new GraphHopper().forDesktop();
hopper.setOSMFile("osm" + File.separator + mapName + ".osm.pbf");
// CH does not work with shortest weighting (at the moment)
hopper.setCHEnabled(false);
// where to store GH files?
hopper.setGraphHopperLocation("graphs" + File.separator + mapName);
hopper.setEncodingManager(new EncodingManager("car"));
// this may take a few minutes
hopper.importOrLoad();
}
use of com.graphhopper.GraphHopper in project graphhopper by graphhopper.
the class MiniGraphUI method main.
public static void main(String[] strs) {
PMap args = PMap.read(strs);
args.putObject("datareader.file", args.getString("datareader.file", "core/files/monaco.osm.gz"));
args.putObject("graph.location", args.getString("graph.location", "tools/target/mini-graph-ui-gh"));
args.putObject("graph.flag_encoders", args.getString("graph.flag_encoders", "car"));
GraphHopperConfig ghConfig = new GraphHopperConfig(args);
ghConfig.setProfiles(Arrays.asList(new Profile("profile").setVehicle("car").setWeighting("fastest")));
ghConfig.setCHProfiles(Arrays.asList(new CHProfile("profile")));
ghConfig.setLMProfiles(Arrays.asList(new LMProfile("profile")));
GraphHopper hopper = new GraphHopper().init(ghConfig).importOrLoad();
boolean debug = args.getBool("minigraphui.debug", false);
boolean useCH = args.getBool("minigraphui.useCH", false);
new MiniGraphUI(hopper, debug, useCH).visualize();
}
Aggregations