Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor the OSRM/BirdDistance management #256

Merged
merged 4 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion connection_scan_algorithm/include/calculator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,15 @@ namespace TrRouting
class TransitData;
class ConnectionSet;
class Point;
class GeoFilter;

class Calculator {

public:

std::map<std::string, int> benchmarking;

Calculator(const TransitData &_transitData);
Calculator(const TransitData &_transitData, GeoFilter &_geofilter);

void reset(CommonParameters &parameters, std::optional<std::reference_wrapper<const Point>> origin, std::optional<std::reference_wrapper<const Point>> destination, bool resetAccessPaths = true, bool resetFilters = true);
// TODO This function supports both allNodes and simple calculation, which
Expand Down Expand Up @@ -101,6 +102,8 @@ namespace TrRouting

//TODO set it mutable so it can be changed/reset?
const TransitData &transitData;
//TODO Should it be const?
GeoFilter &geoFilter;

int departureTimeSeconds;
int arrivalTimeSeconds;
Expand Down
1 change: 1 addition & 0 deletions connection_scan_algorithm/include/program_options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace TrRouting
int port;
bool debug;
bool cacheAllConnectionSets;
bool useEuclideanDistance;
std::string algorithm;
std::string dataFetcherShortname;
std::string osrmWalkingPort;
Expand Down
4 changes: 3 additions & 1 deletion connection_scan_algorithm/src/initializations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@
#include "person.hpp"
#include "connection_cache.hpp"
#include "transit_data.hpp"
#include "geofilter.hpp"

namespace TrRouting
{

Calculator::Calculator(const TransitData &_transitData) :
Calculator::Calculator(const TransitData &_transitData, GeoFilter &_geoFilter) :
algorithmCalculationTime(CalculationTime()),
odTripGlob(std::nullopt),
transitData(_transitData),
geoFilter(_geoFilter),
departureTimeSeconds(0),
arrivalTimeSeconds(-1),
minAccessTravelTime(0),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ namespace TrRouting
maxTotalWalkingTravelTimeSeconds = 60*60; // not used right now
maxOnlyWalkingAccessTravelTimeRatio = 1.5; // prefer walking only if it is faster than transit and total only walking travel time <= maxAccessWalkingTravelTimeSeconds * this ratio
transferPenaltySeconds = 0; // not used right now
accessMode = "walking";
egressMode = "walking";
noResultSecondMode = "driving";
tryNextModeIfRoutingFails = false;
noResultNextAccessTimeSecondsIncrement = 5*60;
Expand Down
7 changes: 7 additions & 0 deletions connection_scan_algorithm/src/program_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ namespace TrRouting {
("cachePath", boost::program_options::value<std::string>()->default_value("cache"), "cache path");
options.add_options()
("cacheAllConnectionSets", boost::program_options::value<bool>() ->default_value(false), "cache all connections set instead of the ones from the last used scenario");
options.add_options()
("useEuclideanDistance", boost::program_options::value<bool>() ->default_value(false), "Use euclidean distance calculation instead of OSRM for access and egress calculations");
options.add_options()
("osrmPort,osrmWalkPort,osrmWalkingPort", boost::program_options::value<std::string>()->default_value("5000"), "osrm walking port");
options.add_options()
Expand All @@ -43,6 +45,7 @@ namespace TrRouting {
dataFetcherShortname = "cache";
cachePath = "cache";
cacheAllConnectionSets = false;
useEuclideanDistance = false;
osrmWalkingPort = "5000";
osrmCyclingPort = "8000";
osrmDrivingPort = "7000";
Expand Down Expand Up @@ -78,6 +81,10 @@ namespace TrRouting {
{
cacheAllConnectionSets = variablesMap["cacheAllConnectionSets"].as<bool>();
}
if(variablesMap.count("useEuclideanDistance") == 1)
{
useEuclideanDistance = variablesMap["useEuclideanDistance"].as<bool>();
}

if(variablesMap.count("osrmWalkPort") == 1)
{
Expand Down
8 changes: 4 additions & 4 deletions connection_scan_algorithm/src/resets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include "calculator.hpp"
#include "parameters.hpp"
#include "trip.hpp"
#include "osrm_fetcher.hpp"
#include "toolbox.hpp" //MAX_INT
#include "od_trip.hpp"
#include "routing_result.hpp"
Expand All @@ -13,6 +12,7 @@
#include "node.hpp"
#include "transit_data.hpp"
#include "connection_set.hpp"
#include "geofilter.hpp"

namespace TrRouting
{
Expand Down Expand Up @@ -199,9 +199,9 @@ namespace TrRouting
}
else
{
spdlog::debug(" fetching nodes with osrm with mode {}", params.accessMode);
spdlog::debug(" fetching nodes with osrm");

accessFootpaths = OsrmFetcher::getAccessibleNodesFootpathsFromPoint(origin, transitData.getNodes(), params.accessMode, parameters.getMaxAccessWalkingTravelTimeSeconds(), params.walkingSpeedMetersPerSecond);
accessFootpaths = geoFilter.getAccessibleNodesFootpathsFromPoint(origin, transitData.getNodes(), parameters.getMaxAccessWalkingTravelTimeSeconds(), params.walkingSpeedMetersPerSecond);
if (accessFootpaths.size() == 0) {
accessFootpathOk = false;
}
Expand Down Expand Up @@ -240,7 +240,7 @@ namespace TrRouting
}
else
{
egressFootpaths = OsrmFetcher::getAccessibleNodesFootpathsFromPoint(destination, transitData.getNodes(), params.accessMode, parameters.getMaxEgressWalkingTravelTimeSeconds(), params.walkingSpeedMetersPerSecond);
egressFootpaths = geoFilter.getAccessibleNodesFootpathsFromPoint(destination, transitData.getNodes(), parameters.getMaxEgressWalkingTravelTimeSeconds(), params.walkingSpeedMetersPerSecond);
if (egressFootpaths.size() == 0) {
egressFootpathOk = false;
}
Expand Down
24 changes: 14 additions & 10 deletions connection_scan_algorithm/src/transit_routing_http_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
#include "result_to_v2_summary.hpp"
#include "result_to_v2_accessibility.hpp"
#include "routing_result.hpp"
#include "osrm_fetcher.hpp"
#include "transit_data.hpp"
#include "osrmgeofilter.hpp"
#include "euclideangeofilter.hpp"

using namespace TrRouting;

Expand Down Expand Up @@ -108,13 +109,6 @@ int main(int argc, char** argv) {
// setup program options:
spdlog::info("Starting transit routing on port {} for the data: {}", programOptions.port, programOptions.cachePath);

OsrmFetcher::osrmWalkingPort = programOptions.osrmWalkingPort;
OsrmFetcher::osrmCyclingPort = programOptions.osrmCyclingPort;
OsrmFetcher::osrmDrivingPort = programOptions.osrmDrivingPort;
OsrmFetcher::osrmWalkingHost = programOptions.osrmWalkingHost;
OsrmFetcher::osrmCyclingHost = programOptions.osrmCyclingHost;
OsrmFetcher::osrmDrivingHost = programOptions.osrmDrivingHost;

if (programOptions.debug) {
spdlog::set_level(spdlog::level::debug);
}
Expand All @@ -132,8 +126,18 @@ int main(int argc, char** argv) {
//TODO We wanted to handle error in the constructor, but later part of this code expect a dataStatus
// leaving as a todo
DataStatus dataStatus = transitData.getDataStatus();

Calculator calculator(transitData);

// Selection which geofilter to use. OSRM is the default one. Euclidean mostly used for debugging and testing
GeoFilter *geoFilter = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pointer never gets deleted. I guess it doesn't matter since it's constructed once in the main?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it should be cleaned, but it does not really matter as you said since it lives for the whole duration of the program.

if (programOptions.useEuclideanDistance) {
geoFilter = new EuclideanGeoFilter();
spdlog::info("Using Euclidean distance for access/egress node time/distance");
} else {
geoFilter = new OsrmGeoFilter("walking", programOptions.osrmWalkingHost, programOptions.osrmWalkingPort);
spdlog::info("Using OSRM for access/egress node time/distance");
}

Calculator calculator(transitData, *geoFilter);
//TODO, should this be in the constructor?
calculator.initializeCalculationData();
spdlog::info("preparing server...");
Expand Down
23 changes: 23 additions & 0 deletions include/euclideangeofilter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef TR_EUCLIDEAN_GEO_FILTER
#define TR_EUCLIDEAN_GEO_FILTER

#include "geofilter.hpp"
#include <string>

namespace TrRouting
{
/* Filter nodes using Euclidean distance */
class EuclideanGeoFilter : public GeoFilter
{
public:
EuclideanGeoFilter();

virtual std::vector<NodeTimeDistance> getAccessibleNodesFootpathsFromPoint(const Point &point,
const std::map<boost::uuids::uuid, Node> &nodes,
int maxWalkingTravelTime,
float walkingSpeedMetersPerSecond,
bool reversed = false);
};
}

#endif // TR_EUCLIDEAN_GEO_FILTER
34 changes: 34 additions & 0 deletions include/geofilter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef TR_GEO_FILTER
#define TR_GEO_FILTER

#include <vector>
#include <map> //For node map types, TODO have a typedef somewhere for the nodes array
#include <boost/uuid/uuid.hpp>
#include <tuple>

namespace TrRouting
{
class Point;
class Node;
class NodeTimeDistance;

/* Base class to implement filter based in geography */
class GeoFilter
{
public:
virtual std::vector<NodeTimeDistance> getAccessibleNodesFootpathsFromPoint(const Point &point,
const std::map<boost::uuids::uuid, Node> &nodes,
int maxWalkingTravelTime,
float walkingSpeedMetersPerSecond,
bool reversed = false) = 0;
protected:
// Common utility functions
static std::tuple<float, float> calculateLengthOfOneDegree(const Point &point);
static float calculateMaxDistanceSquared(int maxWalkingTravelTime, float walkingSpeed);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eventually todo, don't refer to walking, it's just a travel time and speed

//TODO WHy one point * and other &
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed good question, why? It was like that before?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that was just a copy paste from the previous code.

static float calculateNodeDistanceSquared(const Point *node, const Point &point, const std::tuple<float, float> &lengthOfOneDegree);

};
}

#endif // TR_GEO_FILTER
47 changes: 0 additions & 47 deletions include/osrm_fetcher.hpp

This file was deleted.

28 changes: 28 additions & 0 deletions include/osrmgeofilter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef TR_OSRM_GEO_FILTER
#define TR_OSRM_GEO_FILTER

#include "geofilter.hpp"
#include <string>

namespace TrRouting
{
/* Filter nodes using OSRM */
class OsrmGeoFilter : public GeoFilter
{
public:
OsrmGeoFilter(const std::string &mode, const std::string &host, const std::string & port);

virtual std::vector<NodeTimeDistance> getAccessibleNodesFootpathsFromPoint(const Point &point,
const std::map<boost::uuids::uuid, Node> &nodes,
int maxWalkingTravelTime,
float walkingSpeedMetersPerSecond,
bool reversed = false);
protected:
std::string mode;
std::string host;
std::string port; //Could be an int, but it's used as a string every where. Keep a string remove conversions

};
}

#endif // TR_OSRM_GEO_FILTER
2 changes: 0 additions & 2 deletions include/parameters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,6 @@ namespace TrRouting
std::optional<boost::uuids::uuid> startingNodeUuid;
std::optional<boost::uuids::uuid> endingNodeUuid;

std::string accessMode;
std::string egressMode;
bool tryNextModeIfRoutingFails;
std::string noResultSecondMode;
int noResultNextAccessTimeSecondsIncrement;
Expand Down
4 changes: 3 additions & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ lines_cache_fetcher.cpp \
modes_initialization.cpp \
nodes_cache_fetcher.cpp \
od_trips_cache_fetcher.cpp \
osrm_fetcher.cpp \
geofilter.cpp \
euclideangeofilter.cpp \
osrmgeofilter.cpp \
paths_cache_fetcher.cpp \
persons_cache_fetcher.cpp \
scenarios_cache_fetcher.cpp \
Expand Down
41 changes: 41 additions & 0 deletions src/euclideangeofilter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "euclideangeofilter.hpp"
#include "point.hpp"
#include "node.hpp"
#include "spdlog/spdlog.h"

namespace TrRouting
{

EuclideanGeoFilter::EuclideanGeoFilter() {}


std::vector<NodeTimeDistance> EuclideanGeoFilter::getAccessibleNodesFootpathsFromPoint(const Point &point,
const std::map<boost::uuids::uuid, Node> &nodes,
int maxWalkingTravelTime,
float walkingSpeedMetersPerSecond,
bool /*Unused reversed*/) {
std::vector<NodeTimeDistance> accessibleNodesFootpaths;

auto lengthOfOneDegree = calculateLengthOfOneDegree(point);
float maxDistanceMetersSquared = calculateMaxDistanceSquared(maxWalkingTravelTime, walkingSpeedMetersPerSecond);
float distanceMetersSquared;

spdlog::debug("use of bird distance ");

for (auto &&[uuid,node] : nodes)
{
distanceMetersSquared = calculateNodeDistanceSquared(node.point.get(), point, lengthOfOneDegree);

if (distanceMetersSquared <= maxDistanceMetersSquared)
{
int distanceMeters = sqrt(distanceMetersSquared);
int travelTimeSeconds = distanceMeters / walkingSpeedMetersPerSecond;
accessibleNodesFootpaths.push_back(NodeTimeDistance(node, travelTimeSeconds, distanceMeters));
}
}

spdlog::debug("fetched footpaths using bird distance ({} footpaths found)", accessibleNodesFootpaths.size());

return accessibleNodesFootpaths;
}
}
Loading