use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.
the class NearestServlet method doGet.
@Override
public void doGet(HttpServletRequest httpReq, HttpServletResponse httpRes) throws ServletException, IOException {
String pointStr = getParam(httpReq, "point", null);
boolean enabledElevation = getBooleanParam(httpReq, "elevation", false);
ObjectNode result = objectMapper.createObjectNode();
if (pointStr != null && !pointStr.equalsIgnoreCase("")) {
GHPoint place = GHPoint.parse(pointStr);
QueryResult qr = index.findClosest(place.lat, place.lon, EdgeFilter.ALL_EDGES);
if (!qr.isValid()) {
result.put("error", "Nearest point cannot be found!");
} else {
GHPoint3D snappedPoint = qr.getSnappedPoint();
result.put("type", "Point");
ArrayNode coord = result.putArray("coordinates");
coord.add(snappedPoint.lon);
coord.add(snappedPoint.lat);
if (hasElevation && enabledElevation)
coord.add(snappedPoint.ele);
// Distance from input to snapped point in meters
result.put("distance", calc.calcDist(place.lat, place.lon, snappedPoint.lat, snappedPoint.lon));
}
} else {
result.put("error", "No lat/lon specified!");
}
writeJson(httpReq, httpRes, result);
}
use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.
the class GraphHopperWeb method route.
@Override
public GHResponse route(GHRequest request) {
try {
String places = "";
for (GHPoint p : request.getPoints()) {
places += "point=" + p.lat + "," + p.lon + "&";
}
boolean tmpInstructions = request.getHints().getBool("instructions", instructions);
boolean tmpCalcPoints = request.getHints().getBool("calc_points", calcPoints);
boolean tmpTurnDescription = request.getHints().getBool("turn_description", turnDescription);
if (tmpInstructions && !tmpCalcPoints)
throw new IllegalStateException("Cannot calculate instructions without points (only points without instructions). " + "Use calc_points=false and instructions=false to disable point and instruction calculation");
boolean tmpElevation = request.getHints().getBool("elevation", elevation);
String url = routeServiceUrl + "?" + places + "&type=json" + "&instructions=" + tmpInstructions + "&points_encoded=true" + "&calc_points=" + tmpCalcPoints + "&algorithm=" + request.getAlgorithm() + "&locale=" + request.getLocale().toString() + "&elevation=" + tmpElevation;
if (!request.getVehicle().isEmpty())
url += "&vehicle=" + request.getVehicle();
if (!key.isEmpty())
url += "&key=" + key;
for (Entry<String, String> entry : request.getHints().toMap().entrySet()) {
String urlKey = entry.getKey();
String urlValue = entry.getValue();
// use lower case conversion for check only!
if (ignoreSet.contains(urlKey.toLowerCase()))
continue;
if (urlValue != null && !urlValue.isEmpty())
url += "&" + WebHelper.encodeURL(urlKey) + "=" + WebHelper.encodeURL(urlValue);
}
String str = downloader.downloadAsString(url, true);
JSONObject json = new JSONObject(str);
GHResponse res = new GHResponse();
res.addErrors(readErrors(json));
if (res.hasErrors())
return res;
JSONArray paths = json.getJSONArray("paths");
for (int index = 0; index < paths.length(); index++) {
JSONObject path = paths.getJSONObject(index);
PathWrapper altRsp = createPathWrapper(path, tmpCalcPoints, tmpInstructions, tmpElevation, tmpTurnDescription);
res.add(altRsp);
}
return res;
} catch (Exception ex) {
throw new RuntimeException("Problem while fetching path " + request.getPoints() + ": " + ex.getMessage(), ex);
}
}
use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.
the class GraphHopperIT method testMonacoNonChMaxWaypointDistance.
@Test
public void testMonacoNonChMaxWaypointDistance() {
GHPoint from = new GHPoint(43.741069, 7.426854);
GHPoint to = new GHPoint(43.727697, 7.419199);
GHRequest req = new GHRequest().addPoint(from).addPoint(to).setVehicle(vehicle).setWeighting("fastest");
// Fail since points are too far
hopper.setNonChMaxWaypointDistance(1000);
GHResponse rsp = hopper.route(req);
assertTrue(rsp.hasErrors());
// Suceed since points are not far anymore
hopper.setNonChMaxWaypointDistance(Integer.MAX_VALUE);
rsp = hopper.route(req);
assertFalse(rsp.hasErrors());
}
use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.
the class GraphHopperIT method checkMultiVehiclesWithCH.
private void checkMultiVehiclesWithCH(GraphHopper tmpHopper) {
String str = tmpHopper.getEncodingManager().toString();
GHResponse rsp = tmpHopper.route(new GHRequest(43.73005, 7.415707, 43.741522, 7.42826).setVehicle("car"));
PathWrapper arsp = rsp.getBest();
assertFalse("car routing for " + str + " should not have errors:" + rsp.getErrors(), rsp.hasErrors());
assertEquals(207, arsp.getTime() / 1000f, 1);
assertEquals(2838, arsp.getDistance(), 1);
rsp = tmpHopper.route(new GHRequest(43.73005, 7.415707, 43.741522, 7.42826).setVehicle("bike"));
arsp = rsp.getBest();
assertFalse("bike routing for " + str + " should not have errors:" + rsp.getErrors(), rsp.hasErrors());
assertEquals(494, arsp.getTime() / 1000f, 1);
assertEquals(2192, arsp.getDistance(), 1);
rsp = tmpHopper.route(new GHRequest(43.73005, 7.415707, 43.741522, 7.42826).setVehicle("foot"));
assertTrue("only bike and car were imported. foot request should fail", rsp.hasErrors());
GHRequest req = new GHRequest().addPoint(new GHPoint(43.741069, 7.426854), 0.).addPoint(new GHPoint(43.744445, 7.429483), 190.).setVehicle("bike").setWeighting("fastest");
rsp = hopper.route(req);
assertTrue("heading not allowed for CH enabled graph", rsp.hasErrors());
}
use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.
the class GraphHopperIT method testMonacoNonChMaxWaypointDistanceMultiplePoints.
@Test
public void testMonacoNonChMaxWaypointDistanceMultiplePoints() {
GHPoint from = new GHPoint(43.741069, 7.426854);
GHPoint via = new GHPoint(43.744445, 7.429483);
GHPoint to = new GHPoint(43.727697, 7.419199);
GHRequest req = new GHRequest().addPoint(from).addPoint(via).addPoint(to).setVehicle(vehicle).setWeighting("fastest");
// Fail since points are too far
hopper.setNonChMaxWaypointDistance(1000);
GHResponse rsp = hopper.route(req);
assertTrue(rsp.hasErrors());
PointDistanceExceededException exception = (PointDistanceExceededException) rsp.getErrors().get(0);
assertEquals(2, exception.getDetails().get("to"));
// Suceed since points are not far anymore
hopper.setNonChMaxWaypointDistance(Integer.MAX_VALUE);
rsp = hopper.route(req);
assertFalse(rsp.hasErrors());
}
Aggregations