Skip to content

Instantly share code, notes, and snippets.

@aquiles2k
Created July 15, 2017 16:56
Show Gist options
  • Select an option

  • Save aquiles2k/1c2ac0422ae19f273c142e5ac74434f9 to your computer and use it in GitHub Desktop.

Select an option

Save aquiles2k/1c2ac0422ae19f273c142e5ac74434f9 to your computer and use it in GitHub Desktop.
int idealDistance(QPointF p_start, QPointF p_end) {
DBThreadRef dbThread = OSMScoutQt::GetInstance().GetDBThread();
int distance = 0;
dbThread->RunSynchronousJob([dbThread, p_start, p_end, &distance](const std::list<DBInstanceRef>& databases) {
if (databases.size() > 0) {
DBInstanceRef db = databases.front();
osmscout::TypeConfigRef typeConfig = db->database->GetTypeConfig();
osmscout::FastestPathRoutingProfile routingProfile(typeConfig);
std::map<std::string,double> speedMap;
speedMap["highway_motorway"] = 110.0;
speedMap["highway_motorway_trunk"] = 100.0;
speedMap["highway_motorway_primary"] = 70.0;
speedMap["highway_motorway_link"] = 60.0;
speedMap["highway_motorway_junction"] = 60.0;
speedMap["highway_trunk"] = 100.0;
speedMap["highway_trunk_link"] = 60.0;
speedMap["highway_primary"] = 70.0;
speedMap["highway_primary_link"] = 60.0;
speedMap["highway_secondary"] = 60.0;
speedMap["highway_secondary_link"] = 50.0;
speedMap["highway_tertiary"] = 55.0;
speedMap["highway_tertiary_link"] = 55.0;
speedMap["highway_unclassified"] = 50.0;
speedMap["highway_road"] = 50.0;
speedMap["highway_residential"] = 40.0;
speedMap["highway_roundabout"] = 40.0;
speedMap["highway_living_street"] = 10.0;
speedMap["highway_service"] = 30.0;
routingProfile.ParametrizeForCar(*typeConfig, speedMap, 160.0);
osmscout::RouterParameter routerParameter;
db->AssureRouter(osmscout::vehicleCar, routerParameter);
osmscout::RoutePosition startPosition =
db->router->GetClosestRoutableNode(osmscout::GeoCoord(p_start.x(), p_start.y()), routingProfile, 1000);
if (!startPosition.IsValid()) {
qDebug() << "cannot find a routing node close to the start location";
distance = 0;
}
osmscout::RoutePosition targetPosition =
db->router->GetClosestRoutableNode(osmscout::GeoCoord(p_end.x(), p_end.y()), routingProfile, 1000);
if (!targetPosition.IsValid()) {
qDebug() << "cannot find a routing node close to the target location";
distance = 0;
}
osmscout::RoutingParameter parameter;
osmscout::RoutingResult result = db->router->CalculateRoute(routingProfile, startPosition, targetPosition, parameter);
if (!result.Success()) {
qDebug() << "there was an error while calculating the route!";
distance = 0;
}
qDebug() << "route calculated";
osmscout::RouteDescription description;
db->router->TransformRouteDataToRouteDescription(result.GetRoute(), description);
std::list<osmscout::RoutePostprocessor::PostprocessorRef> postprocessors;
postprocessors.push_back(std::make_shared<osmscout::RoutePostprocessor::DistanceAndTimePostprocessor>());
osmscout::RoutePostprocessor postprocessor;
if (!postprocessor.PostprocessRouteDescription(
description, routingProfile, *(db->database), postprocessors)) {
qDebug() << "Error during route postprocessing";
}
if (description.Nodes().size() > 0) {
qDebug() << "idealDistance(" << p_start << "," << p_end << ") returns"
<< (int) (description.Nodes().back().GetDistance() * 1000);
distance = (int) (description.Nodes().back().GetDistance() * 1000);
} else {
distance = 0;
}
} else {
qDebug() << "no database found for ideal distance calculation, returning 0";
distance = 0;
}
}
);
return distance;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment